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