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