00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef _WIN32
00036 #include <sys/socket.h>
00037 #include <arpa/inet.h>
00038 #include <netinet/in.h>
00039 #include <netdb.h>
00040 #include <sys/un.h>
00041 #include <unistd.h>
00042 #include <sys/time.h>
00043 #include <netdb.h>
00044 #include <pthread.h>
00045 #include "config.h"
00046 #else
00047 #include <winsock.h>
00048 #include <windows.h>
00049 #include <time.h>
00050 #include "winconfig.h"
00051 #endif
00052
00053 #include <stdlib.h>
00054 #include "include/acc.h"
00055 #include "include/connection.h"
00056 #include "include/data_structures.h"
00057 #include "include/macros.h"
00058 #include "include/mc_error.h"
00059 #include "include/mc_platform.h"
00060 #include "include/message.h"
00061 #include "include/mtp_http.h"
00062 #include "include/xml_parser.h"
00063 #include "include/fipa_acl_envelope.h"
00064
00065 #define BACKLOG 200
00066
00067 acc_p
00068 acc_Initialize(struct mc_platform_s* mc_platform)
00069 {
00070 acc_p acc;
00071 acc = (acc_p)malloc(sizeof(acc_t));
00072 acc->mc_platform = mc_platform;
00073
00074 acc->waiting = 0;
00075 acc->waiting_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00076 MUTEX_INIT(acc->waiting_lock);
00077 acc->waiting_cond = (COND_T*)malloc(sizeof(COND_T));
00078 COND_INIT(acc->waiting_cond);
00079
00080
00081 MUTEX_INIT(&acc->conn_thread_lock);
00082 COND_INIT(&acc->conn_thread_cond);
00083 acc->num_conn_threads = 0;
00084
00085
00086 MUTEX_INIT(&acc->msg_thread_lock);
00087 COND_INIT(&acc->msg_thread_cond);
00088 acc->num_msg_threads = 0;
00089 return acc;
00090 }
00091
00092 int
00093 acc_Destroy(acc_p acc)
00094 {
00095 if(acc == NULL) {
00096 return MC_SUCCESS;
00097 }
00098 free(acc);
00099 acc = NULL;
00100 return MC_SUCCESS;
00101 }
00102
00103 #ifndef _WIN32
00104 void*
00105 acc_MessageHandlerThread(void* arg)
00106 #else
00107 DWORD WINAPI
00108 acc_MessageHandlerThread(LPVOID arg)
00109 #endif
00110 {
00111 mc_platform_p mc_platform = (mc_platform_p)arg;
00112 message_p message;
00113 agent_p agent;
00114 int mobile_agent_counter = 1;
00115 char* tmpstr;
00116 char* origname;
00117 int i;
00118
00119 while(1)
00120 {
00121 MUTEX_LOCK(mc_platform->message_queue->lock);
00122 MUTEX_LOCK(mc_platform->quit_lock);
00123 while(mc_platform->message_queue->size == 0 && !mc_platform->quit) {
00124 MUTEX_UNLOCK(mc_platform->quit_lock);
00125 COND_WAIT(
00126 mc_platform->message_queue->cond,
00127 mc_platform->message_queue->lock );
00128 MUTEX_LOCK(mc_platform->quit_lock);
00129 }
00130 if (mc_platform->message_queue->size == 0 && mc_platform->quit)
00131 {
00132 MUTEX_LOCK(&mc_platform->acc->msg_thread_lock);
00133 while(mc_platform->acc->num_msg_threads > 0) {
00134 COND_WAIT(
00135 &mc_platform->acc->msg_thread_cond,
00136 &mc_platform->acc->msg_thread_lock
00137 );
00138 }
00139 MUTEX_UNLOCK(&mc_platform->acc->msg_thread_lock);
00140 MUTEX_UNLOCK(mc_platform->quit_lock);
00141 MUTEX_UNLOCK(mc_platform->message_queue->lock);
00142 THREAD_EXIT();
00143 }
00144
00145 MUTEX_UNLOCK(mc_platform->quit_lock);
00146 MUTEX_UNLOCK(mc_platform->message_queue->lock);
00147 message = message_queue_Pop(mc_platform->message_queue);
00148 if (message == NULL) {
00149 printf("POP ERROR\n");
00150 continue;
00151 }
00152
00153 MUTEX_LOCK(mc_platform->MC_signal_lock);
00154 mc_platform->MC_signal = MC_RECV_MESSAGE;
00155 COND_BROADCAST(mc_platform->MC_signal_cond);
00156 MUTEX_UNLOCK(mc_platform->MC_signal_lock);
00157 MUTEX_LOCK(mc_platform->giant_lock);
00158 while(mc_platform->giant == 0) {
00159 COND_WAIT (
00160 mc_platform->giant_cond,
00161 mc_platform->giant_lock);
00162 }
00163 MUTEX_UNLOCK(mc_platform->giant_lock);
00164
00165
00166 if(message->to_address == NULL) {
00167
00168
00169 switch(message->message_type) {
00170 case MOBILE_AGENT:
00171 agent = agent_Initialize(
00172 mc_platform,
00173 message,
00174 mobile_agent_counter);
00175 if (agent != NULL) {
00176
00177 i = 1;
00178 if(agent_queue_SearchName(mc_platform->agent_queue, agent->name)) {
00179 origname = agent->name;
00180 while(agent_queue_SearchName(mc_platform->agent_queue, agent->name)) {
00181
00182
00183 tmpstr = (char*)malloc(sizeof(char) * strlen(origname) + 7);
00184 sprintf(tmpstr, "%s_%04d", origname, i);
00185 agent->name = tmpstr;
00186 i++;
00187 }
00188 fprintf(stderr, "Warning: Agent '%s' has been renamed to '%s'.\n",
00189 origname, agent->name);
00190 free(origname);
00191 }
00192 mobile_agent_counter++;
00193 agent_queue_Add(
00194 mc_platform->agent_queue,
00195 agent);
00196 } else {
00197 fprintf(stderr, "agent_Initialize() failed. %s:%d\n", __FILE__, __LINE__);
00198 }
00199 message_Destroy(message);
00200
00201 MUTEX_LOCK(mc_platform->MC_signal_lock);
00202 mc_platform->MC_signal = MC_RECV_AGENT;
00203 COND_BROADCAST(mc_platform->MC_signal_cond);
00204 MUTEX_UNLOCK(mc_platform->MC_signal_lock);
00205 MUTEX_LOCK(mc_platform->giant_lock);
00206 while(mc_platform->giant == 0) {
00207 COND_WAIT(mc_platform->giant_cond,
00208 mc_platform->giant_lock);
00209 }
00210 MUTEX_UNLOCK(mc_platform->giant_lock);
00211
00212 MUTEX_LOCK(mc_platform->ams->runflag_lock);
00213 mc_platform->ams->run = 1;
00214 COND_BROADCAST(mc_platform->ams->runflag_cond);
00215 MUTEX_UNLOCK(mc_platform->ams->runflag_lock);
00216 break;
00217 case FIPA_ACL:
00218
00219
00220 break;
00221 case RETURN_MSG:
00222
00223 agent = agent_Initialize(
00224 mc_platform,
00225 message,
00226 mobile_agent_counter);
00227 if (agent != NULL) {
00228 MUTEX_LOCK(agent->lock);
00229 agent->datastate->persistent = 1;
00230 agent->agent_status = MC_AGENT_NEUTRAL;
00231 MUTEX_UNLOCK(agent->lock);
00232 mobile_agent_counter++;
00233 agent_queue_Add(
00234 mc_platform->agent_queue,
00235 agent);
00236 }
00237 message_Destroy(message);
00238
00239 MUTEX_LOCK(mc_platform->MC_signal_lock);
00240 mc_platform->MC_signal = MC_RECV_RETURN;
00241 COND_BROADCAST(mc_platform->MC_signal_cond);
00242 MUTEX_UNLOCK(mc_platform->MC_signal_lock);
00243 MUTEX_LOCK(mc_platform->giant_lock);
00244 while(mc_platform->giant == 0) {
00245 COND_WAIT(
00246 mc_platform->giant_cond,
00247 mc_platform->giant_lock);
00248 }
00249 MUTEX_UNLOCK(mc_platform->giant_lock);
00250
00251 MUTEX_LOCK(mc_platform->ams->runflag_lock);
00252 mc_platform->ams->run = 1;
00253 COND_BROADCAST(mc_platform->ams->runflag_cond);
00254 MUTEX_UNLOCK(mc_platform->ams->runflag_lock);
00255 break;
00256 case RELAY:
00257 case REQUEST:
00258 case SUBSCRIBE:
00259 case CANCEL:
00260 case N_UNDRSTD:
00261 case QUER_IF:
00262 case QUER_REF:
00263 case AGENT_UPDATE:
00264 fprintf(stderr, "FIXME: Message type %d not processable.%s:%d\n",
00265 message->message_type, __FILE__, __LINE__ );
00266 message_Destroy(message);
00267 break;
00268 default:
00269 fprintf(stderr, "Unknown message type:%d %s:%d\n",
00270 message->message_type, __FILE__, __LINE__);
00271 message_Destroy(message);
00272 }
00273 } else {
00274 message_Send
00275 (
00276 mc_platform, message, mc_platform -> private_key
00277 );
00278 }
00279 }
00280 THREAD_EXIT();
00281 }
00282
00283
00284 #define CONN_THREADS 40
00285 #ifndef _WIN32
00286 void*
00287 acc_Thread(void* arg)
00288 #else
00289 DWORD WINAPI
00290 acc_Thread( LPVOID arg )
00291 #endif
00292 {
00293 connection_p connection;
00294 mc_platform_p mc_platform = (mc_platform_p)arg;
00295
00296 connection_thread_arg_t* connection_thread_arg;
00297
00298 acc_t* acc = mc_platform->acc;
00299
00300 #ifndef _WIN32
00301 pthread_attr_t attr;
00302 pthread_attr_init(&attr);
00303 #else
00304 int stack_size = 0;
00305 #endif
00306 THREAD_T conn_thread;
00307
00308
00309
00310 while(1) {
00311 connection = NULL;
00312 MUTEX_LOCK(mc_platform->connection_queue->lock);
00313 MUTEX_LOCK(mc_platform->quit_lock);
00314 while (
00315 (mc_platform->connection_queue->size == 0 ) && !mc_platform->quit) {
00316 MUTEX_UNLOCK(mc_platform->quit_lock);
00317 COND_WAIT(
00318 mc_platform->connection_queue->cond,
00319 mc_platform->connection_queue->lock
00320 );
00321 MUTEX_LOCK(mc_platform->quit_lock);
00322 }
00323 if
00324 (
00325 mc_platform->connection_queue->size == 0 &&
00326 mc_platform->quit
00327 )
00328 {
00329 MUTEX_LOCK(&mc_platform->acc->conn_thread_lock);
00330 while(mc_platform->acc->num_conn_threads > 0) {
00331 COND_WAIT(
00332 &mc_platform->acc->conn_thread_cond,
00333 &mc_platform->acc->conn_thread_lock
00334 );
00335 }
00336 MUTEX_UNLOCK(&mc_platform->acc->conn_thread_lock);
00337 MUTEX_UNLOCK(mc_platform->quit_lock);
00338 MUTEX_UNLOCK(mc_platform->connection_queue->lock);
00339 THREAD_EXIT();
00340 }
00341 MUTEX_UNLOCK(mc_platform->quit_lock);
00342 MUTEX_UNLOCK(mc_platform->connection_queue->lock);
00343
00344 MUTEX_LOCK(mc_platform->MC_signal_lock);
00345 mc_platform->MC_signal = MC_RECV_CONNECTION;
00346 COND_BROADCAST(mc_platform->MC_signal_cond);
00347 MUTEX_UNLOCK(mc_platform->MC_signal_lock);
00348
00349
00350 MUTEX_LOCK(mc_platform->giant_lock);
00351 while (mc_platform->giant == 0) {
00352 COND_WAIT(
00353 mc_platform->giant_cond,
00354 mc_platform->giant_lock
00355 );
00356 }
00357 MUTEX_UNLOCK(mc_platform->giant_lock);
00358
00359
00360 connection = connection_queue_Pop(mc_platform->connection_queue);
00361 connection_thread_arg = (connection_thread_arg_t*)malloc(sizeof(connection_thread_arg_t));
00362 connection_thread_arg->mc_platform = mc_platform;
00363 connection_thread_arg->connection = connection;
00364 MUTEX_LOCK(&acc->conn_thread_lock);
00365 while(acc->num_conn_threads > CONN_THREADS) {
00366 COND_WAIT(&acc->conn_thread_cond, &acc->conn_thread_lock);
00367 }
00368 acc->num_conn_threads++;
00369 MUTEX_UNLOCK(&acc->conn_thread_lock);
00370 THREAD_CREATE(&conn_thread, acc_connection_Thread, connection_thread_arg);
00371 THREAD_DETACH(conn_thread);
00372 }
00373 }
00374
00375
00376 #define CONNECT_THREAD_EXIT() \
00377 free(arg); \
00378 MUTEX_LOCK(&acc->conn_thread_lock); \
00379 acc->num_conn_threads--; \
00380 COND_SIGNAL(&acc->conn_thread_cond); \
00381 MUTEX_UNLOCK(&acc->conn_thread_lock); \
00382 THREAD_EXIT();
00383
00384 #ifndef _WIN32
00385 void*
00386 acc_connection_Thread(void* arg)
00387 #else
00388 DWORD WINAPI
00389 acc_connection_Thread( LPVOID arg )
00390 #endif
00391 {
00392 mtp_http_p mtp_http;
00393 acc_t* acc;
00394 mc_platform_t* mc_platform;
00395 connection_t* connection;
00396 message_p message;
00397 fipa_acl_envelope_p fipa_envelope;
00398 int err;
00399 int i, j;
00400 agent_t* agent;
00401 fipa_message_string_p fipa_message_string;
00402 fipa_acl_message_p fipa_message;
00403
00404 mc_platform = ((connection_thread_arg_t*)arg)->mc_platform;
00405 acc = ((connection_thread_arg_t*)arg)->mc_platform->acc;
00406 connection = ((connection_thread_arg_t*)arg)->connection;
00407 mtp_http = mtp_http_New();
00408 if ( mtp_http_InitializeFromConnection(mtp_http, connection, mc_platform->private_key ) )
00409 {
00410 fprintf(stderr, "mtp_http_InitializeFromConnection failed. %s:%d\n", __FILE__, __LINE__);
00411 connection_Destroy(connection);
00412 mtp_http_Destroy(mtp_http);
00413 CONNECT_THREAD_EXIT();
00414 }
00415
00416 switch(mtp_http->http_performative)
00417 {
00418 case HTTP_POST:
00419 case HTTP_PUT:
00420
00421
00422 if(
00423 !strcmp(mtp_http->target, "/ams") ||
00424 !strcmp( strrchr(mtp_http->target, (int)'/'), "/ams" )
00425 ) {
00426 message = message_New();
00427
00428 message->message_body = (char*)malloc
00429 (
00430 sizeof(char) *
00431 (strlen((char*)mtp_http->content->data)+1)
00432 );
00433 strcpy(message->message_body, (char*)mtp_http->content->data);
00434 message->xml_root = mxmlLoadString
00435 (
00436 NULL,
00437 message->message_body,
00438 MXML_NO_CALLBACK
00439 );
00440 if (message->xml_root == NULL) {
00441 fprintf(stderr, "xml loadstring error. %s:%d\n", __FILE__, __LINE__);
00442 }
00443 if(message_xml_parse(message)) {
00444 fprintf(stderr, "Error parsing message. %s:%d\n",
00445 __FILE__,__LINE__);
00446 message_Destroy(message);
00447 mtp_http_Destroy(mtp_http);
00448 CONNECT_THREAD_EXIT();
00449 }
00450 mtp_http_Destroy(mtp_http);
00451 break;
00452 } else if
00453 (
00454 !strcmp(mtp_http->target, "/acc") ||
00455 !strcmp( strrchr(mtp_http->target, (int)'/'), "/acc")
00456 ) {
00457
00458
00459 if (mtp_http->message_parts != 2) {
00460 fprintf(stderr, "Error parsing message. %s:%d\n",
00461 __FILE__,__LINE__);
00462 mtp_http_Destroy(mtp_http);
00463 CONNECT_THREAD_EXIT();
00464 }
00465
00466 fipa_envelope = fipa_acl_envelope_New();
00467 err = fipa_envelope_Parse(fipa_envelope, (char*)mtp_http->content[0].data);
00468 if (err) {
00469 fprintf(stderr, "Error parsing message. %s:%d\n",
00470 __FILE__, __LINE__);
00471 fipa_acl_envelope_Destroy(fipa_envelope);
00472 mtp_http_Destroy(mtp_http);
00473 CONNECT_THREAD_EXIT();
00474 }
00475
00476
00477 for(i = 0; i < fipa_envelope->num_params; i++) {
00478 char* portstr;
00479 for(j = 0; j < fipa_envelope->params[i]->to->num; j++) {
00480 agent = agent_queue_SearchName(
00481 mc_platform->agent_queue,
00482 fipa_envelope->params[i]->to->fipa_agent_identifiers[j]->name
00483 );
00484 if (agent != NULL) {
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495 portstr = fipa_envelope->params[i]->to->fipa_agent_identifiers[j]->addresses->urls[0]->str;
00496 portstr = strstr(portstr, ":")+1;
00497 portstr = strstr(portstr, ":")+1;
00498
00499 if(agent->mc_platform->port != atoi(portstr)) continue;
00500
00501
00502 fipa_message_string = fipa_message_string_New();
00503 fipa_message_string->message = strdup((char*)mtp_http->content[1].data);
00504 fipa_message_string->parse = fipa_message_string->message;
00505 fipa_message = fipa_acl_message_New();
00506 err = fipa_acl_Parse(fipa_message, fipa_message_string);
00507 if (err) {
00508 fipa_message_string_Destroy(fipa_message_string);
00509 fipa_acl_message_Destroy(fipa_message);
00510 fipa_acl_envelope_Destroy(fipa_envelope);
00511 mtp_http_Destroy(mtp_http);
00512 CONNECT_THREAD_EXIT();
00513 }
00514 agent_mailbox_Post( agent->mailbox, fipa_message);
00515 fipa_message_string_Destroy(fipa_message_string);
00516 }
00517 }
00518 }
00519 fipa_acl_envelope_Destroy(fipa_envelope);
00520 mtp_http_Destroy(mtp_http);
00521 CONNECT_THREAD_EXIT();
00522 }
00523 else {
00524
00525 fprintf(stderr, "Unsupported. %s:%d\n", __FILE__, __LINE__);
00526 mtp_http_Destroy(mtp_http);
00527 }
00528 default:
00529 fprintf(stderr, "unsupported http performative. %s:%d\n",
00530 __FILE__, __LINE__);
00531 }
00532
00533
00534 connection_Destroy(connection);
00535 switch(message->message_type) {
00536 case RELAY:
00537 case REQUEST:
00538 case SUBSCRIBE:
00539 case CANCEL:
00540 case N_UNDRSTD:
00541 case MOBILE_AGENT:
00542 case QUER_IF:
00543 case QUER_REF:
00544 case AGENT_UPDATE:
00545 case RETURN_MSG:
00546 case FIPA_ACL:
00547 message_queue_Add(mc_platform->message_queue, message);
00548 break;
00549 default:
00550 fprintf(stderr, "Unknown message type:%d. Rejecting message.%s:%d\n",
00551 message->message_type,
00552 __FILE__, __LINE__ );
00553 free(message);
00554 break;
00555 }
00556 CONNECT_THREAD_EXIT();
00557 }
00558 #undef CONNECT_THREAD_EXIT
00559
00560
00561 void
00562 acc_Start(mc_platform_p mc_platform)
00563 {
00564 acc_p acc = mc_platform->acc;
00565 #ifndef _WIN32
00566 pthread_attr_t attr;
00567 pthread_attr_init(&attr);
00568 if(mc_platform->stack_size[MC_THREAD_ACC] != -1) {
00569 pthread_attr_setstacksize
00570 (
00571 &attr,
00572 mc_platform->stack_size[MC_THREAD_ACC]
00573 );
00574 }
00575 #else
00576 int stack_size;
00577 if (mc_platform->stack_size[MC_THREAD_ACC] < 1) {
00578
00579 stack_size = mc_platform->stack_size[MC_THREAD_ACC]+1;
00580 } else {
00581 stack_size = mc_platform->stack_size[MC_THREAD_ACC];
00582 }
00583 #endif
00584 THREAD_CREATE
00585 (
00586 &acc->thread,
00587 acc_Thread,
00588 mc_platform
00589 );
00590 THREAD_CREATE
00591 (
00592 &acc->message_handler_thread,
00593 acc_MessageHandlerThread,
00594 mc_platform
00595 );
00596 THREAD_CREATE
00597 (
00598 &acc->listen_thread,
00599 listen_Thread,
00600 mc_platform
00601 );
00602 THREAD_CREATE
00603 (
00604 &acc->udplisten_thread,
00605 udplisten_Thread,
00606 mc_platform
00607 );
00608 }
00609
00610 int
00611 auth_conn_rece_key(int sockfd, char *peer_name, int *nonce, unsigned char *aes_key, char *privkey, char* known_host_filename){
00612 int ret = -1;
00613
00614 char privatekey[1210];
00615 char peer_pubkey[300];
00616 char plaintext[135];
00617
00618
00619 memset(privatekey, '\0', 1210);
00620
00621 memset(plaintext, '\0', 135);
00622 memset(aes_key, '\0', 35);
00623
00624 strcpy(privatekey, privkey);
00625
00626
00627
00628 if (read_known_host_file(peer_pubkey, peer_name, known_host_filename) == -1 ){
00629 printf("Server: %s 's Public key not found in known host file\n",peer_name);
00630
00631 }else{
00632 if ( (ret=reply_migration_process(sockfd, nonce, peer_pubkey, privatekey, aes_key)) != 1){
00633 if (ret == -1)
00634 printf("Server: Connected peer is not authenticated \n");
00635 if (ret == -2)
00636 printf("Server: Unable to get authentication from other peer \n");
00637 }else{
00638 if(ret == 2)
00639 ret = 2;
00640 else
00641 ret = 1;
00642 }
00643 }
00644 return ret;
00645 }
00646
00647
00648 #ifndef _WIN32
00649 void*
00650 listen_Thread(void* arg)
00651 #else
00652 DWORD WINAPI
00653 listen_Thread( LPVOID arg )
00654 #endif
00655 {
00656 #ifndef _WIN32
00657 int connectionsockfd;
00658 int sockfd;
00659 struct sockaddr_in sktin;
00660 struct sockaddr_in peer_addr;
00661 #else
00662 SOCKET connectionsockfd;
00663 SOCKET sockfd;
00664 struct sockaddr_in sktin;
00665 struct sockaddr_in peer_addr;
00666 struct sockaddr_in name_addr;
00667
00668 struct hostent *remoteHost;
00669 struct in_addr addr;
00670
00671 #endif
00672
00673 char peer_name[45];
00674 connection_p connection;
00675 u_long connection_number;
00676 int connectionlen;
00677 mc_platform_p mc_platform = (mc_platform_p)arg;
00678
00679
00680 connection_number = 0;
00681
00682 connectionlen = sizeof(struct sockaddr_in);
00683
00684
00685 sockfd = socket(PF_INET, SOCK_STREAM, 0);
00686 if (sockfd < 0) {
00687 SOCKET_ERROR();
00688 }
00689 mc_platform->sockfd = sockfd;
00690 sktin.sin_family = AF_INET;
00691 sktin.sin_port = htons(mc_platform->port);
00692 sktin.sin_addr.s_addr = INADDR_ANY;
00693 memset(sktin.sin_zero, '\0', sizeof sktin.sin_zero);
00694 if (bind(sockfd, (struct sockaddr *)&sktin, sizeof(struct sockaddr))
00695 == -1) {
00696 fprintf(stderr, "bind() error. %s:%d\n",
00697 __FILE__, __LINE__ );
00698 exit(1);
00699 }
00700 listen(sockfd, BACKLOG);
00701
00702
00703 while(1)
00704 {
00705
00706 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00707 mc_platform->acc->waiting = 1;
00708 COND_BROADCAST(mc_platform->acc->waiting_cond);
00709 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00710 #ifndef _WIN32
00711 if((connectionsockfd = accept(sockfd,
00712 (struct sockaddr *)&peer_addr,
00713 (socklen_t *)&connectionlen)) < 0)
00714 #else
00715 if((connectionsockfd = accept(sockfd,
00716 (struct sockaddr *)&peer_addr,
00717 (int*)&connectionlen)) == INVALID_SOCKET)
00718 #endif
00719 {
00720 fprintf(stderr, "ListenThread: accept error \n");
00721 #ifdef _WIN32
00722 printf("Error number: %d\n", WSAGetLastError() );
00723 #endif
00724 continue;
00725 }
00726 else
00727 {
00728
00729 #ifndef _WIN32
00730 getnameinfo((const struct sockaddr*)&peer_addr, sizeof(peer_addr),
00731 peer_name, sizeof(peer_name), NULL, 0, 0);
00732 #else
00733 addr.s_addr = inet_addr( inet_ntoa(peer_addr.sin_addr) );
00734 if (addr.s_addr == INADDR_NONE)
00735 printf("The IPv4 address entered must be a legal address\n");
00736 else
00737 remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
00738
00739 memset(peer_name, '\0', 45 );
00740 strcpy(peer_name, remoteHost->h_name);
00741
00742 #endif
00743
00744
00745 #ifdef NEW_SECURITY
00746
00747 ret = auth_conn_rece_key(connectionsockfd, peer_name, &nonce, aes_key, mc_platform->private_key, mc_platform->agency->known_host_filename);
00748 if( ret == 2 || ret == 1){
00749
00750
00751 #endif
00752
00753 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00754 mc_platform->acc->waiting = 0;
00755 COND_BROADCAST(mc_platform->acc->waiting_cond);
00756 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00757
00758
00759 connection = connection_New();
00760 connection->connect_id = rand();
00761 connection->remote_hostname = NULL;
00762 connection->addr = peer_addr;
00763 connection->serverfd = sockfd;
00764 connection->clientfd = connectionsockfd;
00765 #ifdef NEW_SECURITY
00766 connection->nonce = nonce;
00767 connection->AES_key = aes_key;
00768 #endif
00769
00770
00771 connection_queue_Add(mc_platform->connection_queue, connection);
00772 #ifdef NEW_SECURITY
00773 }else{
00774 printf("Unable to authenticate %s \n", peer_name);
00775 }
00776 #endif
00777 }
00778 }
00779
00780
00781 THREAD_EXIT();
00782 }
00783
00784 #define BUFLEN 512
00785 #define UDPPORT 8866
00786 #ifndef _WIN32
00787 void*
00788 udplisten_Thread(void* arg)
00789 #else
00790 DWORD WINAPI
00791 udplisten_Thread( LPVOID arg )
00792 #endif
00793 {
00794 mc_platform_p mc_platform = (mc_platform_p)arg;
00795 struct sockaddr_in si_me, si_remote;
00796 int s, slen = sizeof(si_remote);
00797 char buf[BUFLEN];
00798 char mc_req_string[] = "Mobile-C Agency Information Request";
00799
00800 if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 ) {
00801 perror("Socket error.");
00802 exit(1);
00803 }
00804
00805 memset((char*) &si_me, 0, sizeof(si_me));
00806 si_me.sin_family = AF_INET;
00807 si_me.sin_port = htons(UDPPORT);
00808 si_me.sin_addr.s_addr = htons(INADDR_ANY);
00809
00810 if(bind(s, (const struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
00811
00812 return NULL;
00813 }
00814 while(1) {
00815 if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_remote, &slen)==-1){
00816 perror("recvfrom");
00817 continue;
00818 }
00819
00820
00821
00822
00823
00824
00825 if(strncmp(buf, mc_req_string, strlen(mc_req_string)) == 0) {
00826 sprintf(buf, "Mobile-C Version %s\n%s:%d\n",
00827 PACKAGE_VERSION, mc_platform->hostname, mc_platform->port);
00828 if(sendto(s, buf, strlen(buf)+1, 0, (const struct sockaddr*)&si_remote, slen) == -1) {
00829 perror("sendto");
00830 }
00831 } else {
00832 sprintf(buf, "Message not understood.");
00833 if(sendto(s, buf, strlen(buf)+1, 0, (const struct sockaddr*)&si_remote, slen) == -1) {
00834 perror("sendto");
00835 }
00836 }
00837 }
00838 }
00839 #undef BUFLEN
00840 #undef UDPPORT