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 #include <string.h>
00036 #ifndef _WIN32
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <arpa/inet.h>
00041 #include <unistd.h>
00042 #include <netdb.h>
00043 #include <errno.h>
00044 #include "config.h"
00045 #if HAVE_LIBBLUETOOTH
00046 #include <bluetooth/bluetooth.h>
00047 #include <bluetooth/rfcomm.h>
00048 #endif
00049 #else
00050 #include <winsock2.h>
00051 #include <Ws2bth.h>
00052 #include "winconfig.h"
00053 #endif
00054 #include <mxml.h>
00055 #include "include/libmc.h"
00056 #include "include/agent.h"
00057 #include "include/mc_platform.h"
00058 #include "include/message.h"
00059 #include "include/mtp_http.h"
00060 #include "include/xml_compose.h"
00061 #include "include/xml_helper.h"
00062 #include "include/xml_parser.h"
00063 #include "include/macros.h"
00064
00065 #include "security/asm_node.h"
00066
00067 #define SOCKET_INPUT_SIZE 4096
00068
00069 message_p
00070 message_New(void)
00071 {
00072 message_p message;
00073 message = (message_p)malloc(sizeof(message_t));
00074 CHECK_NULL(message, exit(0););
00075 message->addr = NULL;
00076 message->connect_id = 0;
00077 message->message_id = 0;
00078 message->isHTTP = 0;
00079 message->message_type = (enum message_type_e)0;
00080 message->http_type = (enum http_performative_e)0;
00081 message->xml_root = NULL;
00082 message->xml_payload = NULL;
00083 message->message_body = NULL;
00084 message->update_name = NULL;
00085 message->update_num = 0;
00086 message->from_address = NULL;
00087 message->to_address = NULL;
00088 message->target = NULL;
00089 message->agent_xml_flag = 0;
00090 return message;
00091 }
00092
00093 message_p
00094 message_Copy(message_p src)
00095 {
00096 fprintf(stderr, "FIXME: message_Copy() is not implemented yet. %s:%d\n",
00097 __FILE__, __LINE__);
00098 return NULL;
00099 }
00100
00101 int
00102 message_InitializeFromAgent(
00103 mc_platform_p mc_platform,
00104 message_p message,
00105 agent_p agent)
00106 {
00107 struct hostent* host;
00108
00109 char* buf;
00110 char* destination_host;
00111 char* destination_port_str;
00112 #ifndef _WIN32
00113 char* save_ptr;
00114 #endif
00115 int destination_port;
00116
00117 message->message_id = rand();
00118 message->message_type = MOBILE_AGENT;
00119
00120 message->xml_root = agent_xml_compose(agent);
00121
00122
00123 CHECK_NULL(message->xml_root, exit(0););
00124 message->message_body = mxmlSaveAllocString(
00125 message->xml_root,
00126 MXML_NO_CALLBACK );
00127
00128 message->update_name = NULL;
00129
00130 message->from_address =
00131 (char*)malloc(sizeof(char) * (strlen(mc_platform->hostname) + 10));
00132 sprintf(
00133 message->from_address,
00134 "%s:%d",
00135 mc_platform->hostname,
00136 mc_platform->port );
00137 if (
00138 agent->datastate->task_progress >=
00139 agent->datastate->number_of_tasks
00140 )
00141 {
00142 message->to_address =
00143 (char*)malloc
00144 (
00145 sizeof(char) *
00146 (
00147 strlen(agent->home) + 1
00148 )
00149 );
00150 CHECK_NULL(message->to_address, exit(0););
00151 strcpy
00152 (
00153 message->to_address,
00154 agent->home
00155 );
00156 } else {
00157 message->to_address =
00158 (char*) malloc
00159 (
00160 sizeof(char) *
00161 (
00162 strlen
00163 (
00164 agent->datastate->tasks[ agent->datastate->task_progress ]
00165 ->server_name
00166 )
00167 +1
00168 )
00169 );
00170 CHECK_NULL( message->to_address, mc_platform->err = MC_ERR_MEMORY; return MC_ERR_MEMORY;);
00171 strcpy(
00172 message->to_address,
00173 agent->datastate->tasks[ agent->datastate->task_progress ]->server_name
00174 );
00175 }
00176 message->agent_xml_flag = 0;
00177 message->target = strdup("ams");
00178 message->sending_agent_name = strdup(agent->name);
00179
00180 if(mc_platform->bluetooth == 0) {
00181 buf = (char*)malloc
00182 (
00183 sizeof(char) *
00184 (strlen(message->to_address)+1)
00185 );
00186 CHECK_NULL(buf, exit(0););
00187 strcpy(buf, message->to_address);
00188 destination_host = strtok_r(buf, ":", &save_ptr);
00189 destination_port_str = strtok_r(NULL, ":", &save_ptr);
00190 destination_port = atoi(destination_port_str);
00191 message->addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
00192 if ((host = gethostbyname(destination_host))) {
00193 memcpy(&(message->addr->sin_addr), host->h_addr, host->h_length);
00194 message->addr->sin_port = htons(destination_port);
00195 } else {
00196 WARN("Host not found.");
00197 }
00198 free(buf);
00199 }
00200 return MC_SUCCESS;
00201 }
00202
00203 int
00204 message_InitializeFromConnection(
00205 mc_platform_p mc_platform,
00206 message_p message,
00207 connection_p connection)
00208 {
00209 int i = 1;
00210 int n;
00211 char *message_string;
00212 char *buffer;
00213
00214 message->addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
00215 CHECK_NULL(message->addr, exit(0););
00216 *(message->addr) = connection->addr;
00217
00218 message->connect_id = connection->connect_id;
00219
00220 message->message_id = rand();
00221
00222 message->to_address = NULL;
00223 message->from_address = NULL;
00224 message->target = NULL;
00225
00226 buffer = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00227 CHECK_NULL(buffer, exit(0););
00228 message_string = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00229 CHECK_NULL(message_string, exit(0););
00230 message_string[0] = '\0';
00231 buffer[0] = '\0';
00232
00233
00234 while(1) {
00235 #ifndef _WIN32
00236
00237
00238
00239
00240
00241
00242 n = recv(connection->clientfd, (void*)buffer, (size_t) sizeof(char)*SOCKET_INPUT_SIZE, MSG_WAITALL);
00243 #else
00244 n = recvfrom(connection->clientfd,
00245 buffer,
00246 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00247 0,
00248 (struct sockaddr *) 0,
00249 0);
00250 #endif
00251 if (n < 0) {
00252 free(buffer);
00253 SOCKET_ERROR();
00254 return MC_ERR_CONNECT;
00255 }
00256 else if (n == 0) {
00257 free(buffer);
00258 break;
00259 } else {
00260 buffer[n] = '\0';
00261 i++;
00262 strcat(message_string, buffer);
00263 message_string = (char*)realloc
00264 (
00265 message_string,
00266 sizeof(char) * (SOCKET_INPUT_SIZE+1) * i
00267 );
00268 CHECK_NULL(message_string, exit(0););
00269 buffer[0] = '\0';
00270 }
00271 }
00272 message->message_body = (char*)malloc
00273 (
00274 sizeof(char) *
00275 (strlen(message_string) + 1)
00276 );
00277 CHECK_NULL(message->message_body, exit(0););
00278 strcpy(message->message_body, message_string);
00279 free(message_string);
00280 message->xml_root = mxmlLoadString
00281 (
00282 NULL,
00283 message->message_body,
00284 MXML_NO_CALLBACK
00285 );
00286 if (message_xml_parse(message)) {
00287 fprintf(stderr, "Error parsing message at %s:%d.\n",
00288 __FILE__, __LINE__);
00289 message_Destroy(message);
00290 return MC_ERR_PARSE;
00291 }
00292 return MC_SUCCESS;
00293 }
00294
00295 int http_to_hostport(const char* http_str, char** host, int* port, char** target)
00296 {
00297
00298
00299
00300
00301 const char* tmp;
00302 if(strncmp(http_str, "http://", 7)) {
00303 return MC_ERR_PARSE;
00304 }
00305 http_str += 7;
00306 tmp = strchr(http_str, (int)':');
00307 if (tmp == NULL) return MC_ERR_PARSE;
00308
00309
00310 *host = (char*)malloc(sizeof(char) *
00311 (tmp - http_str + 1) );
00312 strncpy(*host, http_str, tmp - http_str);
00313 (*host)[tmp-http_str] = '\0';
00314
00315
00316 tmp++;
00317 sscanf(tmp, "%d", port);
00318
00319
00320 tmp = strchr(tmp, (int)'/');
00321 tmp++;
00322 *target = (char*)malloc(sizeof(char) *
00323 (strlen(tmp)+1) );
00324 strcpy(*target, tmp);
00325
00326 return 0;
00327 }
00328
00329 int
00330 message_InitializeFromString(
00331 mc_platform_p mc_platform,
00332 message_p message,
00333 const char* string,
00334 const char* destination_host,
00335 int destination_port,
00336 const char* target)
00337 {
00338 char* destination;
00339 struct hostent* host;
00340
00341 message->connect_id = 0;
00342 message->message_id = rand();
00343
00344 message->message_type = MOBILE_AGENT;
00345
00346 message->xml_root = NULL;
00347
00348 message->message_body =
00349 (char*)malloc( sizeof(char) * (strlen(string)+1));
00350 CHECK_NULL(message->message_body,
00351 mc_platform->err = MC_ERR_MEMORY;
00352 return MC_ERR_MEMORY; );
00353 strcpy(message->message_body, string);
00354
00355 message->update_name = NULL;
00356
00357 if(destination_host != NULL) {
00358 destination = (char*)malloc(sizeof(char)*(strlen(destination_host) + 10));
00359 CHECK_NULL(destination,
00360 mc_platform->err = MC_ERR_MEMORY;
00361 return MC_ERR_MEMORY; );
00362 sprintf(destination, "%s:%d",
00363 destination_host,
00364 destination_port
00365 );
00366 message->to_address = destination;
00367 } else {
00368 message->to_address = NULL;
00369 }
00370
00371
00372
00373 message->from_address = (char*)malloc(
00374 sizeof(char) * (strlen(mc_platform->hostname)+10));
00375 sprintf(message->from_address,
00376 "%s:%d",
00377 mc_platform->hostname,
00378 mc_platform->port );
00379 message->target = (char*)malloc(sizeof(char) *
00380 (strlen(target)+1));
00381 strcpy(message->target, target);
00382
00383
00384 message->addr = (struct sockaddr_in*)malloc(sizeof(struct sockaddr_in));
00385 if (destination_host != NULL && strlen(destination_host)!= 0) {
00386 if((host = gethostbyname(destination_host)))
00387 {
00388 memcpy(&(message->addr->sin_addr), host->h_addr, host->h_length);
00389 message->addr->sin_port = htons(destination_port);
00390 } else {
00391 fprintf(stderr, "Warning: Host not found: %s:%d %s:%d",
00392 destination_host, destination_port, __FILE__, __LINE__ );
00393 }
00394 }
00395 message->xml_root = mxmlLoadString
00396 (
00397 NULL,
00398 message->message_body,
00399 MXML_NO_CALLBACK
00400 );
00401 if (message_xml_parse(message)) {
00402 fprintf(stderr, "Error parsing message at %s:%d.\n",
00403 __FILE__, __LINE__);
00404 return MC_ERR_PARSE;
00405 }
00406
00407 return MC_SUCCESS;
00408 }
00409
00410 int
00411 message_Destroy(message_p message)
00412 {
00413 if (message == NULL) {
00414 return MC_SUCCESS;
00415 }
00416
00417
00418 if(message->xml_root != NULL && message->agent_xml_flag == 0) {
00419 mxmlDelete(message->xml_root);
00420 }
00421
00422 if(message->addr) {
00423 free(message->addr);
00424 message->addr = NULL;
00425 }
00426 if(message->message_body != NULL) {
00427 free(message->message_body);
00428 message->message_body = NULL;
00429 }
00430 if(message->update_name != NULL) {
00431 free(message->update_name);
00432 }
00433 if(message->from_address != NULL) {
00434 free(message->from_address);
00435 }
00436 if(message->to_address != NULL) {
00437 free(message->to_address);
00438 }
00439 if(message->target != NULL) {
00440 free(message->target);
00441 }
00442
00443 free(message);
00444 message = NULL;
00445 return MC_SUCCESS;
00446 }
00447
00448
00449 int
00450 auth_rece_send_msg(int sockfd, char *hostname, char *message, char *privkey, char *known_host_filename){
00451
00452 int ret=-1;
00453 unsigned char passphrase[35], aes_key[35];
00454 int nonce, mode;
00455 char privatekey[1215];
00456 char peer_pubkey[300];
00457 char plaintext[135];
00458 FILE *fd;
00459
00460
00461 char *infile;
00462 char *outfile;
00463 memset(passphrase , '\0', 35);
00464 memset(privatekey , '\0', 1215);
00465 memset(plaintext , '\0', 135);
00466 memset(aes_key, '\0', 35);
00467
00468
00469
00470
00471 #ifndef _WIN32
00472 int fd1;
00473 infile = (char *)malloc(sizeof(char)*20);
00474 strcpy(infile, "msgXXXXXX");
00475 fd1 = mkstemp(infile);
00476 if (fd1 == -1) {
00477 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
00478 infile, __FILE__, __LINE__ );
00479 exit(EXIT_FAILURE);
00480 }
00481 close(fd1);
00482
00483 outfile = (char *)malloc(sizeof(char)*20);
00484 strcpy(outfile, "en-msgXXXXXX");
00485 fd1 = mkstemp(outfile);
00486 if (fd1 == -1) {
00487 fprintf(stderr, "Could not create temporary file:%s. %s:%d\n",
00488 outfile, __FILE__, __LINE__ );
00489 exit(EXIT_FAILURE);
00490 }
00491 close(fd1);
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 #else
00503 infile = _tempnam(".", "msg");
00504 outfile = _tempnam(".", "en-msg");
00505 #endif
00506
00507
00508
00509
00510
00511
00512 strcpy(privatekey, privkey);
00513
00514
00515
00516
00517 if (read_known_host_file(peer_pubkey, hostname, known_host_filename) == -1 ){
00518 printf("Client: %s 's Public key not found in know host file\n", hostname);
00519 if( remove(infile) ) printf("message.c : remove error 4");
00520 if( remove(outfile) ) printf("message.c : remove error 5");
00521 #ifndef _WIN32
00522
00523 #else
00524
00525 #endif
00526 }else{
00527 if ((ret=initiate_migration_process(sockfd, &nonce, peer_pubkey, privatekey, aes_key)) != 1){
00528 if (ret == -1)
00529 printf("Client: Connected peer is not authenticated \n");
00530 if (ret == -2)
00531 printf("Client: Unable to get authentication from oither peer \n");
00532 }else{
00533
00534
00535
00536
00537 if( (fd = fopen(infile,"w")) == NULL){
00538 printf("Unable to write message in a file \n");
00539 if( remove(infile) ) printf("message.c : remove error 1");
00540 }else{
00541
00542 fwrite (message , 1 , strlen(message) , fd );
00543 fclose(fd);
00544
00545 mode = 0;
00546 if (aes_en_de(mode, infile, outfile, aes_key, &nonce, 0) != 1){
00547 printf("Client: AES Encryption Failed \n");
00548 if( remove(infile) ) printf("message.c : remove error 2");
00549 if( remove(outfile) ) printf("message.c : remove error 3");
00550 }else{
00551 if( remove(infile) ) printf("message.c : remove error 4");
00552 if (send_AES_en_MA(sockfd, &nonce, outfile, peer_pubkey) != 1 ){
00553 printf("Client: Error while sending mobile agent \n");
00554 if( remove(outfile) ) printf("message.c : remove error 5");
00555 }else{
00556 if( remove(outfile) ) printf("message.c : remove error 6");
00557
00558 ret = 2;
00559 }
00560 }
00561 }
00562 }
00563 }
00564
00565
00566
00567
00568 return ret;
00569 }
00570
00571 #define MSG_THREADS 40
00572 int message_Send(mc_platform_t* mc_platform, message_p message, char *privatekey)
00573 {
00574 THREAD_T msg_thread;
00575 message_send_arg_t* arg;
00576 #ifndef _WIN32
00577 pthread_attr_t attr;
00578 pthread_attr_init(&attr);
00579 #else
00580 int stack_size = 0;
00581 #endif
00582
00583 arg = (message_send_arg_t*)malloc(sizeof(message_send_arg_t));
00584 arg->mc_platform = mc_platform;
00585 arg->message = message;
00586 arg->privatekey = privatekey;
00587
00588 MUTEX_LOCK(&mc_platform->acc->msg_thread_lock);
00589 while(mc_platform->acc->num_msg_threads >= MSG_THREADS) {
00590 COND_WAIT(&mc_platform->acc->msg_thread_cond, &mc_platform->acc->msg_thread_lock);
00591 }
00592 mc_platform->acc->num_msg_threads++;
00593 MUTEX_UNLOCK(&mc_platform->acc->msg_thread_lock);
00594
00595 THREAD_CREATE(&msg_thread, message_send_Thread, arg);
00596 THREAD_DETACH(msg_thread);
00597 return 0;
00598 }
00599
00600 #ifdef _WIN32
00601 typedef struct bdaddr_s {
00602 UINT8 b[6];
00603 } bdaddr_t;
00604
00605 void baswap(bdaddr_t *dst, const bdaddr_t *src)
00606 {
00607 register unsigned char *d = (unsigned char *) dst;
00608 register const unsigned char *s = (const unsigned char *) src;
00609 register int i;
00610
00611 for (i = 0; i < 6; i++)
00612 d[i] = s[5-i];
00613 }
00614
00615 int str2ba(const char *str, bdaddr_t *ba)
00616 {
00617 UINT8 b[6];
00618 const char *ptr = str;
00619 int i;
00620
00621 for (i = 0; i < 6; i++) {
00622 b[i] = (UINT8) strtol(ptr, NULL, 16);
00623 if (i != 5 && !(ptr = strchr(ptr, ':')))
00624 ptr = ":00:00:00:00:00";
00625 ptr++;
00626 }
00627
00628 baswap(ba, (bdaddr_t *) b);
00629
00630 return 0;
00631 }
00632 #endif
00633
00634 #define MSG_THREAD_EXIT() \
00635 free(arg); \
00636 message_Destroy(message); \
00637 MUTEX_LOCK(&mc_platform->acc->msg_thread_lock); \
00638 mc_platform->acc->num_msg_threads--; \
00639 COND_SIGNAL(&mc_platform->acc->msg_thread_cond); \
00640 MUTEX_UNLOCK(&mc_platform->acc->msg_thread_lock); \
00641 THREAD_EXIT()
00642
00643 #ifndef _WIN32
00644 void*
00645 message_send_Thread(void* arg)
00646 #else
00647 DWORD WINAPI
00648 message_send_Thread( LPVOID arg )
00649 #endif
00650 {
00651 mc_platform_t* mc_platform;
00652 message_p message;
00653 char* privatekey;
00654 char *buffer;
00655 mtp_http_t* mtp_http;
00656 int n;
00657 #ifndef _WIN32
00658 int skt;
00659 struct sockaddr_in sktin;
00660 #if HAVE_LIBBLUETOOTH
00661 struct sockaddr_rc btsktin;
00662 #endif
00663 struct addrinfo* myaddrinfo;
00664 struct addrinfo hint_addrinfo;
00665 #else
00666 SOCKET skt;
00667 SOCKADDR_IN sktin;
00668 SOCKADDR_BTH btsktin;
00669 struct hostent host;
00670 struct hostent* host_result;
00671 #endif
00672 char *buf;
00673 char *hostname;
00674 #ifndef _WIN32
00675 char *saveptr;
00676 #endif
00677 int port;
00678
00679 int num_tries = 0;
00680 const int max_num_tries = 100;
00681 int errnum;
00682
00683 dynstring_t* message_string;
00684 #ifdef NEW_SECURITY
00685 int ret;
00686 #endif
00687
00688 mc_platform = ((message_send_arg_t*)arg)->mc_platform;
00689 message = ((message_send_arg_t*)arg)->message;
00690 privatekey = ((message_send_arg_t*)arg)->privatekey;
00691
00692
00693 if (
00694 mtp_http_ComposeMessage(
00695 message
00696 )
00697 )
00698 {
00699 fprintf(stderr, "Compose Message Error. %s:%d\n", __FILE__, __LINE__);
00700 MSG_THREAD_EXIT();
00701 }
00702
00703 if(mc_platform->bluetooth) {
00704 #if HAVE_LIBBLUETOOTH
00705 #ifndef _WIN32
00706 if((skt = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
00707 {
00708 fprintf(stderr, "Error - can't create socket\n");
00709 MSG_THREAD_EXIT();
00710 }
00711 btsktin.rc_family = AF_BLUETOOTH;
00712 buf = (char*)malloc(sizeof(char)*(strlen(message->to_address)+1));
00713 strcpy(buf, message->to_address);
00714 hostname = strtok_r(buf, " ", &saveptr);
00715 sscanf( strtok_r(NULL, " ", &saveptr), "%d", &port );
00716 btsktin.rc_channel = (uint8_t) port;
00717 str2ba( hostname, &btsktin.rc_bdaddr );
00718 #else
00719 if((skt = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM)) < 0)
00720 {
00721 fprintf(stderr, "Error - can't create socket\n");
00722 MSG_THREAD_EXIT();
00723 }
00724 btsktin.addressFamily = AF_BTH;
00725 buf = (char*)malloc(sizeof(char)*(strlen(message->to_address)+1));
00726 strcpy(buf, message->to_address);
00727 hostname = strtok_r(buf, " ", &saveptr);
00728 sscanf( strtok_r(NULL, " ", &saveptr), "%d", &port );
00729 btsktin.port = port;
00730 str2ba( hostname, (bdaddr_t*)&btsktin.btAddr );
00731 #endif
00732 #endif
00733 } else {
00734
00735 buf = (char*)malloc(sizeof(char)*(strlen(message->to_address)+1));
00736 strcpy(buf, message->to_address);
00737 hostname = strtok_r(buf, ":", &saveptr);
00738 sscanf( strtok_r(NULL, ":", &saveptr), "%d", &port );
00739
00740 if((skt = socket(PF_INET, SOCK_STREAM, 0)) < 0)
00741 {
00742 fprintf(stderr, "Error - can't create socket\n");
00743 MSG_THREAD_EXIT();
00744 }
00745
00746 memset(&sktin, 0, sizeof(sktin));
00747 sktin.sin_family = PF_INET;
00748 sktin.sin_port = htons(port);
00749 }
00750
00751 if (mc_platform->bluetooth == 0) {
00752 #ifndef _WIN32
00753
00754 memset(&hint_addrinfo, 0, sizeof(hint_addrinfo));
00755 hint_addrinfo.ai_family = AF_INET;
00756 if( (errnum = getaddrinfo(hostname, NULL, &hint_addrinfo, &myaddrinfo)) == 0 )
00757 #else
00758 if(host_result = gethostbyname(hostname))
00759 #endif
00760 {
00761 #ifdef _WIN32
00762 host = *host_result;
00763 memcpy(&sktin.sin_addr, host.h_addr, host.h_length);
00764 #else
00765 ((struct sockaddr_in*)myaddrinfo->ai_addr)->sin_port = htons(port);
00766 #endif
00767 }
00768 else if((sktin.sin_addr.s_addr = inet_addr(hostname)) < 0)
00769 {
00770 fprintf(stderr, "Error - can't get host entry for %s\n", hostname);
00771 free(buf);
00772 MSG_THREAD_EXIT();
00773 }
00774 }
00775
00776 #ifndef _WIN32
00777 if (mc_platform->bluetooth) {
00778 #if HAVE_LIBBLUETOOTH
00779 errnum = connect(skt, (struct sockaddr *)&btsktin, sizeof(btsktin));
00780 #endif
00781 } else {
00782 errnum = connect(skt, myaddrinfo->ai_addr, sizeof(struct sockaddr));
00783 }
00784 #else
00785 if (mc_platform->bluetooth) {
00786 #if HAVE_LIBBLUETOOTH
00787 errnum = connect(skt, (struct sockaddr *) &btsktin, sizeof(btsktin));
00788 #endif
00789 } else {
00790 errnum = connect(skt, (struct sockaddr *) &sktin, sizeof(sktin));
00791 }
00792 #endif
00793 num_tries = 0;
00794 while(
00795 (num_tries < max_num_tries) && (errnum < 0)
00796 ) {
00797 printf("ERROR Socket Connect failed... errno:%s\n", strerror(errno));
00798 #ifndef _WIN32
00799 usleep(100000);
00800 #else
00801 Sleep(100);
00802 #endif
00803 num_tries++;
00804 #ifndef _WIN32
00805 if (mc_platform->bluetooth) {
00806 #if HAVE_LIBBLUETOOTH
00807 errnum = connect(skt, (struct sockaddr *)&btsktin, sizeof(btsktin));
00808 #endif
00809 } else {
00810 errnum = connect(skt, myaddrinfo->ai_addr, sizeof(struct sockaddr));
00811 }
00812 #else
00813 if (mc_platform->bluetooth) {
00814 #if HAVE_LIBBLUETOOTH
00815 errnum = connect(skt, (struct sockaddr *) &btsktin, sizeof(btsktin));
00816 #endif
00817 } else {
00818 errnum = connect(skt, (struct sockaddr *) &sktin, sizeof(sktin));
00819 }
00820 #endif
00821 }
00822 if (num_tries == max_num_tries) {
00823 fprintf(stderr, "Error - can't connect to %s:%d\n",
00824 hostname,
00825 port
00826 );
00827 free(buf);
00828 MSG_THREAD_EXIT();
00829 }
00830
00831 #ifdef NEW_SECURITY
00832
00833 ret = auth_rece_send_msg(skt, hostname, message->message_body, privatekey, mc_platform->agency->known_host_filename);
00834 if( ret == 1 ){
00835 printf("Successfull Authenticate and but send of MA is failed \n");
00836 #ifndef _WIN32
00837 if(close(skt) < 0) {
00838 SOCKET_ERROR();
00839 }
00840 #else
00841 closesocket(skt);
00842 #endif
00843 free(buf);
00844 fprintf(stderr, "Security Error. %s:%d\n", __FILE__, __LINE__);
00845 MSG_THREAD_EXIT();
00846 }else if(ret != 2){
00847 #ifndef _WIN32
00848 if(close(skt) < 0) {
00849 SOCKET_ERROR();
00850 }
00851 #else
00852 closesocket(skt);
00853 #endif
00854 free(buf);
00855 fprintf(stderr, "Security Error. %s:%d\n", __FILE__, __LINE__);
00856 MSG_THREAD_EXIT();
00857 }else if(ret == 2)
00858
00859 #else
00860 if(send(skt, message->message_body, strlen(message->message_body), 0) < 0) {
00861 perror("send");
00862 printf("cannot write to socket %s:%d\n", __FILE__, __LINE__);
00863 }
00864 else
00865 #endif
00866 {
00867
00868 buffer = (char*) malloc(sizeof(char) * (SOCKET_INPUT_SIZE + 1));
00869 CHECK_NULL(buffer, exit(0););
00870 mtp_http = mtp_http_New();
00871 message_string = dynstring_New();
00872 while(1) {
00873 #ifndef _WIN32
00874
00875
00876
00877
00878
00879
00880
00881 n = recv(skt, (void*)buffer, (size_t) sizeof(char)*SOCKET_INPUT_SIZE, MSG_WAITALL);
00882 #else
00883 n = recvfrom(skt,
00884 buffer,
00885 (size_t) sizeof(char)*SOCKET_INPUT_SIZE,
00886 0,
00887 (struct sockaddr *) 0,
00888 0);
00889 #endif
00890 if (n<0) {
00891
00892 SOCKET_ERROR();
00893 free(buffer);
00894 MSG_THREAD_EXIT();
00895 }
00896 else if (n==0) {
00897 break;
00898 }
00899 else
00900 {
00901 dynstring_Append(message_string, buffer);
00902 }
00903 }
00904 if( mtp_http_Parse(mtp_http, message_string->message) ) {
00905 fprintf(stderr, "http parsing error: Response expected. %s:%d\n",
00906 __FILE__, __LINE__);
00907 fprintf(stderr, "Received message was:\n%s\n", message_string->message);
00908 }
00909 if (mtp_http->response_code != 200) {
00910 fprintf(stderr, "Warning: remote http server responded: %d %s\n",
00911 mtp_http->response_code, mtp_http->response_string );
00912 }
00913
00914 mtp_http_Destroy(mtp_http);
00915 }
00916
00917 #ifndef _WIN32
00918 if(close(skt) <0) {
00919 SOCKET_ERROR();
00920 }
00921 #else
00922 closesocket(skt);
00923 #endif
00924 free(buf);
00925 free(buffer);
00926 dynstring_Destroy(message_string);
00927 MSG_THREAD_EXIT();
00928 }
00929
00930 #undef MSG_THREAD_EXIT