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