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 int err;
00296 int i, j;
00297
00298 connection_thread_arg_t* connection_thread_arg;
00299
00300 acc_t* acc = mc_platform->acc;
00301
00302 #ifndef _WIN32
00303 pthread_attr_t attr;
00304 pthread_attr_init(&attr);
00305 #else
00306 int stack_size = 0;
00307 #endif
00308 THREAD_T conn_thread;
00309
00310
00311
00312 while(1) {
00313 connection = NULL;
00314 MUTEX_LOCK(mc_platform->connection_queue->lock);
00315 MUTEX_LOCK(mc_platform->quit_lock);
00316 while (
00317 (mc_platform->connection_queue->size == 0 ) && !mc_platform->quit) {
00318 MUTEX_UNLOCK(mc_platform->quit_lock);
00319 COND_WAIT(
00320 mc_platform->connection_queue->cond,
00321 mc_platform->connection_queue->lock
00322 );
00323 MUTEX_LOCK(mc_platform->quit_lock);
00324 }
00325 if
00326 (
00327 mc_platform->connection_queue->size == 0 &&
00328 mc_platform->quit
00329 )
00330 {
00331 MUTEX_LOCK(&mc_platform->acc->conn_thread_lock);
00332 while(mc_platform->acc->num_conn_threads > 0) {
00333 COND_WAIT(
00334 &mc_platform->acc->conn_thread_cond,
00335 &mc_platform->acc->conn_thread_lock
00336 );
00337 }
00338 MUTEX_UNLOCK(&mc_platform->acc->conn_thread_lock);
00339 MUTEX_UNLOCK(mc_platform->quit_lock);
00340 MUTEX_UNLOCK(mc_platform->connection_queue->lock);
00341 THREAD_EXIT();
00342 }
00343 MUTEX_UNLOCK(mc_platform->quit_lock);
00344 MUTEX_UNLOCK(mc_platform->connection_queue->lock);
00345
00346 MUTEX_LOCK(mc_platform->MC_signal_lock);
00347 mc_platform->MC_signal = MC_RECV_CONNECTION;
00348 COND_BROADCAST(mc_platform->MC_signal_cond);
00349 MUTEX_UNLOCK(mc_platform->MC_signal_lock);
00350
00351
00352 MUTEX_LOCK(mc_platform->giant_lock);
00353 while (mc_platform->giant == 0) {
00354 COND_WAIT(
00355 mc_platform->giant_cond,
00356 mc_platform->giant_lock
00357 );
00358 }
00359 MUTEX_UNLOCK(mc_platform->giant_lock);
00360
00361
00362 connection = connection_queue_Pop(mc_platform->connection_queue);
00363 connection_thread_arg = (connection_thread_arg_t*)malloc(sizeof(connection_thread_arg_t));
00364 connection_thread_arg->mc_platform = mc_platform;
00365 connection_thread_arg->connection = connection;
00366 MUTEX_LOCK(&acc->conn_thread_lock);
00367 while(acc->num_conn_threads > CONN_THREADS) {
00368 COND_WAIT(&acc->conn_thread_cond, &acc->conn_thread_lock);
00369 }
00370 acc->num_conn_threads++;
00371 MUTEX_UNLOCK(&acc->conn_thread_lock);
00372 THREAD_CREATE(&conn_thread, acc_connection_Thread, connection_thread_arg);
00373 THREAD_DETACH(conn_thread);
00374 }
00375 }
00376
00377
00378 #define CONNECT_THREAD_EXIT() \
00379 free(arg); \
00380 MUTEX_LOCK(&acc->conn_thread_lock); \
00381 acc->num_conn_threads--; \
00382 COND_SIGNAL(&acc->conn_thread_cond); \
00383 MUTEX_UNLOCK(&acc->conn_thread_lock); \
00384 THREAD_EXIT();
00385
00386 #ifndef _WIN32
00387 void*
00388 acc_connection_Thread(void* arg)
00389 #else
00390 DWORD WINAPI
00391 acc_connection_Thread( LPVOID arg )
00392 #endif
00393 {
00394 mtp_http_p mtp_http;
00395 acc_t* acc;
00396 mc_platform_t* mc_platform;
00397 connection_t* connection;
00398 message_p message;
00399 fipa_acl_envelope_p fipa_envelope;
00400 int err;
00401 int i, j;
00402 agent_t* agent;
00403 fipa_message_string_p fipa_message_string;
00404 fipa_acl_message_p fipa_message;
00405
00406 mc_platform = ((connection_thread_arg_t*)arg)->mc_platform;
00407 acc = ((connection_thread_arg_t*)arg)->mc_platform->acc;
00408 connection = ((connection_thread_arg_t*)arg)->connection;
00409 mtp_http = mtp_http_New();
00410 if ( mtp_http_InitializeFromConnection(mtp_http, connection, mc_platform->private_key ) )
00411 {
00412 fprintf(stderr, "mtp_http_InitializeFromConnection failed. %s:%d\n", __FILE__, __LINE__);
00413 connection_Destroy(connection);
00414 mtp_http_Destroy(mtp_http);
00415 CONNECT_THREAD_EXIT();
00416 }
00417
00418 switch(mtp_http->http_performative)
00419 {
00420 case HTTP_POST:
00421 case HTTP_PUT:
00422
00423
00424 if(
00425 !strcmp(mtp_http->target, "/ams") ||
00426 !strcmp( strrchr(mtp_http->target, (int)'/'), "/ams" )
00427 ) {
00428 message = message_New();
00429
00430 message->message_body = (char*)malloc
00431 (
00432 sizeof(char) *
00433 (strlen((char*)mtp_http->content->data)+1)
00434 );
00435 strcpy(message->message_body, (char*)mtp_http->content->data);
00436 message->xml_root = mxmlLoadString
00437 (
00438 NULL,
00439 message->message_body,
00440 MXML_NO_CALLBACK
00441 );
00442 if (message->xml_root == NULL) {
00443 fprintf(stderr, "xml loadstring error. %s:%d\n", __FILE__, __LINE__);
00444 }
00445 if(message_xml_parse(message)) {
00446 fprintf(stderr, "Error parsing message. %s:%d\n",
00447 __FILE__,__LINE__);
00448 message_Destroy(message);
00449 mtp_http_Destroy(mtp_http);
00450 CONNECT_THREAD_EXIT();
00451 }
00452 mtp_http_Destroy(mtp_http);
00453 break;
00454 } else if
00455 (
00456 !strcmp(mtp_http->target, "/acc") ||
00457 !strcmp( strrchr(mtp_http->target, (int)'/'), "/acc")
00458 ) {
00459
00460
00461 if (mtp_http->message_parts != 2) {
00462 fprintf(stderr, "Error parsing message. %s:%d\n",
00463 __FILE__,__LINE__);
00464 mtp_http_Destroy(mtp_http);
00465 CONNECT_THREAD_EXIT();
00466 }
00467
00468 fipa_envelope = fipa_acl_envelope_New();
00469 err = fipa_envelope_Parse(fipa_envelope, (char*)mtp_http->content[0].data);
00470 if (err) {
00471 fprintf(stderr, "Error parsing message. %s:%d\n",
00472 __FILE__, __LINE__);
00473 fipa_acl_envelope_Destroy(fipa_envelope);
00474 mtp_http_Destroy(mtp_http);
00475 CONNECT_THREAD_EXIT();
00476 }
00477
00478
00479 for(i = 0; i < fipa_envelope->num_params; i++) {
00480 char* portstr;
00481 for(j = 0; j < fipa_envelope->params[i]->to->num; j++) {
00482 agent = agent_queue_SearchName(
00483 mc_platform->agent_queue,
00484 fipa_envelope->params[i]->to->fipa_agent_identifiers[j]->name
00485 );
00486 if (agent != NULL) {
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 portstr = fipa_envelope->params[i]->to->fipa_agent_identifiers[j]->addresses->urls[0]->str;
00498 portstr = strstr(portstr, ":")+1;
00499 portstr = strstr(portstr, ":")+1;
00500
00501 if(agent->mc_platform->port != atoi(portstr)) continue;
00502
00503
00504 fipa_message_string = fipa_message_string_New();
00505 fipa_message_string->message = strdup((char*)mtp_http->content[1].data);
00506 fipa_message_string->parse = fipa_message_string->message;
00507 fipa_message = fipa_acl_message_New();
00508 err = fipa_acl_Parse(fipa_message, fipa_message_string);
00509 if (err) {
00510 fipa_message_string_Destroy(fipa_message_string);
00511 fipa_acl_message_Destroy(fipa_message);
00512 fipa_acl_envelope_Destroy(fipa_envelope);
00513 mtp_http_Destroy(mtp_http);
00514 CONNECT_THREAD_EXIT();
00515 }
00516 agent_mailbox_Post( agent->mailbox, fipa_message);
00517 fipa_message_string_Destroy(fipa_message_string);
00518 }
00519 }
00520 }
00521 fipa_acl_envelope_Destroy(fipa_envelope);
00522 mtp_http_Destroy(mtp_http);
00523 CONNECT_THREAD_EXIT();
00524 }
00525 else {
00526
00527 fprintf(stderr, "Unsupported. %s:%d\n", __FILE__, __LINE__);
00528 mtp_http_Destroy(mtp_http);
00529 }
00530 default:
00531 fprintf(stderr, "unsupported http performative. %s:%d\n",
00532 __FILE__, __LINE__);
00533 }
00534
00535
00536 connection_Destroy(connection);
00537 switch(message->message_type) {
00538 case RELAY:
00539 case REQUEST:
00540 case SUBSCRIBE:
00541 case CANCEL:
00542 case N_UNDRSTD:
00543 case MOBILE_AGENT:
00544 case QUER_IF:
00545 case QUER_REF:
00546 case AGENT_UPDATE:
00547 case RETURN_MSG:
00548 case FIPA_ACL:
00549 message_queue_Add(mc_platform->message_queue, message);
00550 break;
00551 default:
00552 fprintf(stderr, "Unknown message type:%d. Rejecting message.%s:%d\n",
00553 message->message_type,
00554 __FILE__, __LINE__ );
00555 free(message);
00556 break;
00557 }
00558 CONNECT_THREAD_EXIT();
00559 }
00560 #undef CONNECT_THREAD_EXIT
00561
00562
00563 void
00564 acc_Start(mc_platform_p mc_platform)
00565 {
00566 acc_p acc = mc_platform->acc;
00567 #ifndef _WIN32
00568 pthread_attr_t attr;
00569 pthread_attr_init(&attr);
00570 if(mc_platform->stack_size[MC_THREAD_ACC] != -1) {
00571 pthread_attr_setstacksize
00572 (
00573 &attr,
00574 mc_platform->stack_size[MC_THREAD_ACC]
00575 );
00576 }
00577 #else
00578 int stack_size;
00579 if (mc_platform->stack_size[MC_THREAD_ACC] < 1) {
00580
00581 stack_size = mc_platform->stack_size[MC_THREAD_ACC]+1;
00582 } else {
00583 stack_size = mc_platform->stack_size[MC_THREAD_ACC];
00584 }
00585 #endif
00586 THREAD_CREATE
00587 (
00588 &acc->thread,
00589 acc_Thread,
00590 mc_platform
00591 );
00592 THREAD_CREATE
00593 (
00594 &acc->message_handler_thread,
00595 acc_MessageHandlerThread,
00596 mc_platform
00597 );
00598 THREAD_CREATE
00599 (
00600 &acc->listen_thread,
00601 listen_Thread,
00602 mc_platform
00603 );
00604 THREAD_CREATE
00605 (
00606 &acc->udplisten_thread,
00607 udplisten_Thread,
00608 mc_platform
00609 );
00610 }
00611
00612 int
00613 auth_conn_rece_key(int sockfd, char *peer_name, int *nonce, unsigned char *aes_key, char *privkey, char* known_host_filename){
00614 int ret = -1;
00615
00616 char privatekey[1210];
00617 char peer_pubkey[300];
00618 char plaintext[135];
00619
00620
00621 memset(privatekey, '\0', 1210);
00622
00623 memset(plaintext, '\0', 135);
00624 memset(aes_key, '\0', 35);
00625
00626 strcpy(privatekey, privkey);
00627
00628
00629
00630 if (read_known_host_file(peer_pubkey, peer_name, known_host_filename) == -1 ){
00631 printf("Server: %s 's Public key not found in known host file\n",peer_name);
00632
00633 }else{
00634 if ( (ret=reply_migration_process(sockfd, nonce, peer_pubkey, privatekey, aes_key)) != 1){
00635 if (ret == -1)
00636 printf("Server: Connected peer is not authenticated \n");
00637 if (ret == -2)
00638 printf("Server: Unable to get authentication from other peer \n");
00639 }else{
00640 if(ret == 2)
00641 ret = 2;
00642 else
00643 ret = 1;
00644 }
00645 }
00646 return ret;
00647 }
00648
00649
00650 #ifndef _WIN32
00651 void*
00652 listen_Thread(void* arg)
00653 #else
00654 DWORD WINAPI
00655 listen_Thread( LPVOID arg )
00656 #endif
00657 {
00658 #ifndef _WIN32
00659 int connectionsockfd;
00660 int sockfd;
00661 struct sockaddr_in sktin;
00662 struct sockaddr_in peer_addr;
00663 #else
00664 SOCKET connectionsockfd;
00665 SOCKET sockfd;
00666 struct sockaddr_in sktin;
00667 struct sockaddr_in peer_addr;
00668 struct sockaddr_in name_addr;
00669
00670 struct hostent *remoteHost;
00671 struct in_addr addr;
00672
00673 #endif
00674
00675 unsigned char aes_key[35];
00676 int nonce, ret;
00677 char peer_name[45];
00678 connection_p connection;
00679 u_long connection_number;
00680 int connectionlen;
00681 mc_platform_p mc_platform = (mc_platform_p)arg;
00682
00683
00684 connection_number = 0;
00685
00686 connectionlen = sizeof(struct sockaddr_in);
00687
00688
00689 sockfd = socket(PF_INET, SOCK_STREAM, 0);
00690 if (sockfd < 0) {
00691 SOCKET_ERROR();
00692 }
00693 mc_platform->sockfd = sockfd;
00694 sktin.sin_family = AF_INET;
00695 sktin.sin_port = htons(mc_platform->port);
00696 sktin.sin_addr.s_addr = INADDR_ANY;
00697 memset(sktin.sin_zero, '\0', sizeof sktin.sin_zero);
00698 if (bind(sockfd, (struct sockaddr *)&sktin, sizeof(struct sockaddr))
00699 == -1) {
00700 fprintf(stderr, "bind() error. %s:%d\n",
00701 __FILE__, __LINE__ );
00702 exit(1);
00703 }
00704 listen(sockfd, BACKLOG);
00705
00706
00707 while(1)
00708 {
00709
00710 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00711 mc_platform->acc->waiting = 1;
00712 COND_BROADCAST(mc_platform->acc->waiting_cond);
00713 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00714 #ifndef _WIN32
00715 if((connectionsockfd = accept(sockfd,
00716 (struct sockaddr *)&peer_addr,
00717 (socklen_t *)&connectionlen)) < 0)
00718 #else
00719 if((connectionsockfd = accept(sockfd,
00720 (struct sockaddr *)&peer_addr,
00721 (int*)&connectionlen)) == INVALID_SOCKET)
00722 #endif
00723 {
00724 fprintf(stderr, "ListenThread: accept error \n");
00725 #ifdef _WIN32
00726 printf("Error number: %d\n", WSAGetLastError() );
00727 #endif
00728 continue;
00729 }
00730 else
00731 {
00732
00733 #ifndef _WIN32
00734 getnameinfo(&peer_addr, sizeof(peer_addr),
00735 peer_name, sizeof(peer_name), NULL, 0, 0);
00736 #else
00737 addr.s_addr = inet_addr( inet_ntoa(peer_addr.sin_addr) );
00738 if (addr.s_addr == INADDR_NONE)
00739 printf("The IPv4 address entered must be a legal address\n");
00740 else
00741 remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
00742
00743 memset(peer_name, '\0', 45 );
00744 strcpy(peer_name, remoteHost->h_name);
00745
00746 #endif
00747
00748
00749 #ifdef NEW_SECURITY
00750
00751 ret = auth_conn_rece_key(connectionsockfd, peer_name, &nonce, aes_key, mc_platform->private_key, mc_platform->agency->known_host_filename);
00752 if( ret == 2 || ret == 1){
00753
00754
00755 #endif
00756
00757 MUTEX_LOCK(mc_platform->acc->waiting_lock);
00758 mc_platform->acc->waiting = 0;
00759 COND_BROADCAST(mc_platform->acc->waiting_cond);
00760 MUTEX_UNLOCK(mc_platform->acc->waiting_lock);
00761
00762
00763 connection = connection_New();
00764 connection->connect_id = rand();
00765 connection->remote_hostname = NULL;
00766 connection->addr = peer_addr;
00767 connection->serverfd = sockfd;
00768 connection->clientfd = connectionsockfd;
00769 #ifdef NEW_SECURITY
00770 connection->nonce = nonce;
00771 connection->AES_key = aes_key;
00772 #endif
00773
00774
00775 connection_queue_Add(mc_platform->connection_queue, connection);
00776 #ifdef NEW_SECURITY
00777 }else{
00778 printf("Unable to authenticate %s \n", peer_name);
00779 }
00780 #endif
00781 }
00782 }
00783
00784
00785 THREAD_EXIT();
00786 }
00787
00788 #define BUFLEN 512
00789 #define UDPPORT 8866
00790 #ifndef _WIN32
00791 void*
00792 udplisten_Thread(void* arg)
00793 #else
00794 DWORD WINAPI
00795 udplisten_Thread( LPVOID arg )
00796 #endif
00797 {
00798 mc_platform_p mc_platform = (mc_platform_p)arg;
00799 struct sockaddr_in si_me, si_remote;
00800 int s, i, slen = sizeof(si_remote);
00801 char buf[BUFLEN];
00802 char mc_req_string[] = "Mobile-C Agency Information Request";
00803
00804 if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 ) {
00805 perror("Socket error.");
00806 exit(1);
00807 }
00808
00809 memset((char*) &si_me, 0, sizeof(si_me));
00810 si_me.sin_family = AF_INET;
00811 si_me.sin_port = htons(UDPPORT);
00812 si_me.sin_addr.s_addr = htons(INADDR_ANY);
00813
00814 if(bind(s, (const struct sockaddr*)&si_me, sizeof(si_me)) == -1) {
00815
00816 return NULL;
00817 }
00818 while(1) {
00819 if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_remote, &slen)==-1){
00820 perror("recvfrom");
00821 continue;
00822 }
00823
00824
00825
00826
00827
00828
00829 if(strncmp(buf, mc_req_string, strlen(mc_req_string)) == 0) {
00830 sprintf(buf, "Mobile-C Version %s\n%s:%d\n",
00831 PACKAGE_VERSION, mc_platform->hostname, mc_platform->port);
00832 if(sendto(s, buf, strlen(buf)+1, 0, (const struct sockaddr*)&si_remote, slen) == -1) {
00833 perror("sendto");
00834 }
00835 } else {
00836 sprintf(buf, "Message not understood.");
00837 if(sendto(s, buf, strlen(buf)+1, 0, (const struct sockaddr*)&si_remote, slen) == -1) {
00838 perror("sendto");
00839 }
00840 }
00841 }
00842 }
00843 #undef BUFLEN
00844 #undef UDPPORT