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 #ifdef NEW_SECURITY
00674 int ret;
00675 int nonce;
00676 unsigned char aes_key[36];
00677 #endif
00678
00679 char peer_name[45];
00680 connection_p connection;
00681 u_long connection_number;
00682 int connectionlen;
00683 mc_platform_p mc_platform = (mc_platform_p)arg;
00684
00685
00686 connection_number = 0;
00687
00688 connectionlen = sizeof(struct sockaddr_in);
00689
00690
00691 sockfd = socket(PF_INET, SOCK_STREAM, 0);
00692 if (sockfd < 0) {
00693 SOCKET_ERROR();
00694 }
00695 mc_platform->sockfd = sockfd;
00696 sktin.sin_family = AF_INET;
00697 sktin.sin_port = htons(mc_platform->port);
00698 sktin.sin_addr.s_addr = INADDR_ANY;
00699 memset(sktin.sin_zero, '\0', sizeof sktin.sin_zero);
00700 if (bind(sockfd, (struct sockaddr *)&sktin, sizeof(struct sockaddr))
00701 == -1) {
00702 fprintf(stderr, "bind() error. %s:%d\n",
00703 __FILE__, __LINE__ );
00704 exit(1);
00705 }
00706 listen(sockfd, BACKLOG);
00707
00708
00709 while(1)
00710 {
00711
00712 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00713 mc_platform->acc->waiting = 1;
00714 COND_BROADCAST(mc_platform->acc->waiting_cond);
00715 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00716 #ifndef _WIN32
00717 if((connectionsockfd = accept(sockfd,
00718 (struct sockaddr *)&peer_addr,
00719 (socklen_t *)&connectionlen)) < 0)
00720 #else
00721 if((connectionsockfd = accept(sockfd,
00722 (struct sockaddr *)&peer_addr,
00723 (int*)&connectionlen)) == INVALID_SOCKET)
00724 #endif
00725 {
00726 fprintf(stderr, "ListenThread: accept error \n");
00727 #ifdef _WIN32
00728 printf("Error number: %d\n", WSAGetLastError() );
00729 #endif
00730 continue;
00731 }
00732 else
00733 {
00734
00735 #ifndef _WIN32
00736 getnameinfo((const struct sockaddr*)&peer_addr, sizeof(peer_addr),
00737 peer_name, sizeof(peer_name), NULL, 0, 0);
00738 #else
00739 addr.s_addr = inet_addr( inet_ntoa(peer_addr.sin_addr) );
00740 if (addr.s_addr == INADDR_NONE)
00741 printf("The IPv4 address entered must be a legal address\n");
00742 else
00743 remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
00744
00745 memset(peer_name, '\0', 45 );
00746 strcpy(peer_name, remoteHost->h_name);
00747
00748 #endif
00749
00750
00751 #ifdef NEW_SECURITY
00752
00753 ret = auth_conn_rece_key(connectionsockfd, peer_name, &nonce, aes_key, mc_platform->private_key, mc_platform->agency->known_host_filename);
00754 if( ret == 2 || ret == 1){
00755
00756
00757 #endif
00758
00759 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00760 mc_platform->acc->waiting = 0;
00761 COND_BROADCAST(mc_platform->acc->waiting_cond);
00762 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00763
00764
00765 connection = connection_New();
00766 connection->connect_id = rand();
00767 connection->remote_hostname = NULL;
00768 connection->addr = peer_addr;
00769 connection->serverfd = sockfd;
00770 connection->clientfd = connectionsockfd;
00771 #ifdef NEW_SECURITY
00772 connection->nonce = nonce;
00773 connection->AES_key = aes_key;
00774 #endif
00775
00776
00777 connection_queue_Add(mc_platform->connection_queue, connection);
00778 #ifdef NEW_SECURITY
00779 }else{
00780 printf("Unable to authenticate %s \n", peer_name);
00781 }
00782 #endif
00783 }
00784 }
00785
00786
00787 THREAD_EXIT();
00788 }
00789
00790 #define BUFLEN 512
00791 #define UDPPORT 8866
00792 #ifndef _WIN32
00793 void*
00794 udplisten_Thread(void* arg)
00795 #else
00796 DWORD WINAPI
00797 udplisten_Thread( LPVOID arg )
00798 #endif
00799 {
00800 mc_platform_p mc_platform = (mc_platform_p)arg;
00801 struct sockaddr_in si_me, si_remote;
00802 int s, slen = sizeof(si_remote);
00803 char buf[BUFLEN];
00804 char mc_req_string[] = "Mobile-C Agency Information Request";
00805
00806 if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 ) {
00807 perror("Socket error.");
00808 exit(1);
00809 }
00810
00811 memset((char*) &si_me, 0, sizeof(si_me));
00812 si_me.sin_family = AF_INET;
00813 si_me.sin_port = htons(UDPPORT);
00814 si_me.sin_addr.s_addr = htons(INADDR_ANY);
00815
00816 if(bind(s, (const struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
00817
00818 return NULL;
00819 }
00820 while(1) {
00821 if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_remote, &slen)==-1){
00822 perror("recvfrom");
00823 continue;
00824 }
00825
00826
00827
00828
00829
00830
00831 if(strncmp(buf, mc_req_string, strlen(mc_req_string)) == 0) {
00832 sprintf(buf, "Mobile-C Version %s\n%s:%d\n",
00833 PACKAGE_VERSION, mc_platform->hostname, mc_platform->port);
00834 if(sendto(s, buf, strlen(buf)+1, 0, (const struct sockaddr*)&si_remote, slen) == -1) {
00835 perror("sendto");
00836 }
00837 } else {
00838 sprintf(buf, "Message not understood.");
00839 if(sendto(s, buf, strlen(buf)+1, 0, (const struct sockaddr*)&si_remote, slen) == -1) {
00840 perror("sendto");
00841 }
00842 }
00843 }
00844 }
00845 #undef BUFLEN
00846 #undef UDPPORT