00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "OpenSdkThread.h"
00023
00024 #include <OPENR/OSyslog.h>
00025 #include <ant.h>
00026 #include <EndpointTypes.h>
00027 #include <TCPEndpointMsg.h>
00028 #include <UDPEndpointMsg.h>
00029 #include <DNSEndpointMsg.h>
00030
00031
00032
00033 #undef IPPROTO_IP
00034 #undef IPPROTO_ICMP
00035 #undef IPPROTO_IGMP
00036 #undef IPPROTO_GGP
00037 #undef IPPROTO_TCP
00038 #undef IPPROTO_EGP
00039 #undef IPPROTO_PUP
00040 #undef IPPROTO_UDP
00041 #undef IPPROTO_IDP
00042 #undef IPPROTO_TP
00043 #undef IPPROTO_EON
00044 #undef IPPROTO_ENCAP
00045 #undef IPPROTO_RAW
00046 #undef IPPROTO_MAX
00047 #undef IPPORT_RESERVED
00048 #undef IPPORT_USERRESERVED
00049
00050 #include <sys/types.h>
00051 #include <sys/socket.h>
00052 #include <netinet/in.h>
00053 #include <netinet/tcp.h>
00054 #include <arpa/inet.h>
00055 #include <netdb.h>
00056 #include <unistd.h>
00057 #include <errno.h>
00058
00059 #include <pthread.h>
00060
00061 #include <map>
00062 #include "OpenSdkEndpoint.h"
00063 #include "OpenSdkEndpointInfo.h"
00064 #include <opensdkAPI.h>
00065 #include "antSharedBufferManager.h"
00066
00067 using namespace std;
00068
00069 typedef std::map<int, OpenSdkEndpointInfo*> EndpointInfoMap;
00070
00071 static EndpointInfoMap endpointMap = EndpointInfoMap();
00072 static int nextEndpointId = 0;
00073 static pthread_mutex_t mapMut = PTHREAD_MUTEX_INITIALIZER;
00074
00075
00076 struct portsStruct {
00077 int socket;
00078 pthread_mutex_t mutex;
00079
00080 static inline portsStruct getNew(int s) {
00081 portsStruct x;
00082 x.socket = s;
00083 pthread_mutex_init(&x.mutex, NULL);
00084 return x;
00085 }
00086 };
00087 static map<short, portsStruct> PortsMap;
00088 static pthread_mutex_t PortsMutex = PTHREAD_MUTEX_INITIALIZER;
00089
00090
00091
00092 static TCPEndpointError TCP_error(void);
00093 static TCPEndpointError TCP_state_error(OpenSdkEndpointInfo::State state);
00094 static UDPEndpointError UDP_error(void);
00095 static UDPEndpointError UDP_state_error(OpenSdkEndpointInfo::State state);
00096
00097 static void* tcpListenThreadFunc(void *arg);
00098 static void* tcpReceiveThreadFunc(void *arg);
00099 static void* tcpSendThreadFunc(void *arg);
00100 static void* tcpConnectThreadFunc(void *arg);
00101 static void* tcpCloseThreadFunc(void *arg);
00102
00103 static void* udpReceiveThreadFunc(void *arg);
00104 static void* udpSendThreadFunc(void *arg);
00105 static void* udpCloseThreadFunc(void *arg);
00106
00107 static void* dnsGetHostByNameThreadFunc(void *arg);
00108
00109
00110 antStackRef::antStackRef(char* name)
00111 {
00112 if (strcmp(name, "IPStack")) {
00113 OSYSLOG1((osyslogERROR, "antStackRef::antStackRef(): protocol stack not supported: '%s'", name));
00114 }
00115 }
00116
00117 antStackRef::~antStackRef()
00118 {
00119
00120 }
00121
00122
00123 antError antEnvMsg::Reply()
00124 {
00125
00126 return ANT_FAIL;
00127 }
00128
00129
00130 antError antEnvMsg::Send(antStackRef _target, OID& _sender, int32 _selector, int _msgSize)
00131 {
00132 OpenSdkEndpoint *endpoint = (OpenSdkEndpoint*)(&(this->dstModule));
00133
00134 pthread_mutex_lock(&mapMut);
00135 OpenSdkEndpointInfo *osei = endpointMap[(int)endpoint->endpointId];
00136 osei->entry.Set(_sender, _selector);
00137 pthread_mutex_unlock(&mapMut);
00138
00139 switch (endpoint->protocol) {
00140
00141 case EndpointType_TCP:
00142
00143 switch(this->dstOperation) {
00144
00145 case TCPEndpoint_Operation_Bind:
00146 {
00147
00148 return ANT_FAIL;
00149 }
00150 break;
00151
00152
00153 case TCPEndpoint_Operation_Listen:
00154 {
00155 TCPEndpointListenMsg *listenMsg = (TCPEndpointListenMsg*)this;
00156 struct sockaddr_in listeningAddress;
00157 int sret;
00158
00159 if (osei->state != OpenSdkEndpointInfo::NEW && osei->state != OpenSdkEndpointInfo::CLOSED) {
00160 listenMsg->error = TCP_state_error(osei->state);
00161 return ANT_FAIL;
00162 }
00163
00164
00165 pthread_mutex_lock(&PortsMutex);
00166 if (PortsMap.find(listenMsg->lPort) != PortsMap.end()) {
00167 osei->server_socket = PortsMap[listenMsg->lPort].socket;
00168 goto tcp_listen_skip;
00169 }
00170
00171 osei->server_socket = socket(PF_INET, SOCK_STREAM, 0);
00172
00173 if (osei->server_socket < 0) {
00174 listenMsg->error = TCP_error();
00175 pthread_mutex_unlock(&PortsMutex);
00176 return ANT_FAIL;
00177 }
00178
00179 memset(&listeningAddress, 0, sizeof(listeningAddress));
00180 listeningAddress.sin_family = AF_INET;
00181 listeningAddress.sin_addr.s_addr = listenMsg->lAddress.Address();
00182 listeningAddress.sin_port = htons(listenMsg->lPort);
00183
00184 sret = bind(osei->server_socket, (const struct sockaddr *)&listeningAddress, sizeof(listeningAddress));
00185 if (sret != 0) {
00186 listenMsg->error = TCP_error();
00187 pthread_mutex_unlock(&PortsMutex);
00188 return ANT_FAIL;
00189 }
00190
00191 sret = listen(osei->server_socket, 42);
00192 if (sret != 0) {
00193 listenMsg->error = TCP_error();
00194 pthread_mutex_unlock(&PortsMutex);
00195 return ANT_FAIL;
00196 }
00197
00198
00199 PortsMap[listenMsg->lPort] = portsStruct::getNew(osei->server_socket);
00200 tcp_listen_skip:
00201 osei->listen_mutex = &PortsMap[listenMsg->lPort].mutex;
00202 pthread_mutex_unlock(&PortsMutex);
00203
00204 osei->msg = new TCPEndpointListenMsg(*listenMsg);
00205 osei->state = OpenSdkEndpointInfo::LISTENING;
00206
00207 if (OpenSdkThread::runFunction(tcpListenThreadFunc, osei) == 0) {
00208 listenMsg->error = TCP_SUCCESS;
00209 return ANT_SUCCESS;
00210
00211 } else {
00212 listenMsg->error = TCP_FAIL;
00213 return ANT_FAIL;
00214 }
00215 }
00216 break;
00217
00218
00219 case TCPEndpoint_Operation_Connect:
00220 {
00221 TCPEndpointConnectMsg *connectMsg = (TCPEndpointConnectMsg*)this;
00222
00223 if (osei->state != OpenSdkEndpointInfo::NEW && osei->state != OpenSdkEndpointInfo::CLOSED) {
00224 connectMsg->error = TCP_state_error(osei->state);
00225 return ANT_FAIL;
00226 }
00227
00228 osei->socket = socket(PF_INET, SOCK_STREAM, 0);
00229 if (osei->socket < 0) {
00230 connectMsg->error = TCP_error();
00231 return ANT_FAIL;
00232 }
00233
00234 osei->ip = connectMsg->fAddress;
00235 osei->port = connectMsg->fPort;
00236 osei->state = OpenSdkEndpointInfo::CONNECTING;
00237 osei->msg = new TCPEndpointConnectMsg(*connectMsg);
00238
00239 if (OpenSdkThread::runFunction(tcpConnectThreadFunc, osei) == 0) {
00240 connectMsg->error = TCP_SUCCESS;
00241 return ANT_SUCCESS;
00242
00243 } else {
00244 connectMsg->error = TCP_FAIL;
00245 return ANT_FAIL;
00246 }
00247 }
00248 break;
00249
00250
00251 case TCPEndpoint_Operation_Send:
00252 {
00253 TCPEndpointSendMsg *sendMsg = (TCPEndpointSendMsg*)this;
00254
00255 if (osei->state != OpenSdkEndpointInfo::ACTIVE) {
00256 sendMsg->error = TCP_state_error(osei->state);
00257 return ANT_FAIL;
00258 }
00259
00260 osei->buffer = sendMsg->buffer;
00261 osei->size = sendMsg->size;
00262 osei->msg = new TCPEndpointSendMsg(*sendMsg);
00263
00264 if (OpenSdkThread::runFunction(tcpSendThreadFunc, osei) == 0) {
00265 sendMsg->error = TCP_SUCCESS;
00266 return ANT_SUCCESS;
00267
00268 } else {
00269 sendMsg->error = TCP_FAIL;
00270 return ANT_FAIL;
00271 }
00272 }
00273 break;
00274
00275
00276 case TCPEndpoint_Operation_Receive:
00277 {
00278 TCPEndpointReceiveMsg *receiveMsg = (TCPEndpointReceiveMsg*)this;
00279
00280 if (osei->state != OpenSdkEndpointInfo::ACTIVE) {
00281 receiveMsg->error = TCP_state_error(osei->state);
00282 return ANT_FAIL;
00283 }
00284
00285 osei->buffer = receiveMsg->buffer;
00286 osei->size = receiveMsg->sizeMax;
00287 osei->msg = new TCPEndpointReceiveMsg(*receiveMsg);
00288
00289 if (OpenSdkThread::runFunction(tcpReceiveThreadFunc, osei) == 0) {
00290 receiveMsg->error = TCP_SUCCESS;
00291 return ANT_SUCCESS;
00292
00293 } else {
00294 receiveMsg->error = TCP_FAIL;
00295 return ANT_FAIL;
00296 }
00297 }
00298 break;
00299
00300
00301 case TCPEndpoint_Operation_Close:
00302 {
00303 TCPEndpointCloseMsg *closeMsg = (TCPEndpointCloseMsg*)this;
00304
00305 if (osei->state != OpenSdkEndpointInfo::ACTIVE) {
00306 closeMsg->error = TCP_state_error(osei->state);
00307 return ANT_FAIL;
00308 }
00309
00310 osei->msg = new TCPEndpointCloseMsg(*closeMsg);
00311
00312 if (OpenSdkThread::runFunction(tcpCloseThreadFunc, osei) == 0) {
00313 closeMsg->error = TCP_SUCCESS;
00314 return ANT_SUCCESS;
00315
00316 } else {
00317 closeMsg->error = TCP_FAIL;
00318 return ANT_FAIL;
00319 }
00320 }
00321 break;
00322
00323
00324 case TCPEndpoint_Operation_SetBlockingMode:
00325 {
00326
00327 return ANT_FAIL;
00328 }
00329 break;
00330
00331
00332 case TCPEndpoint_Operation_ReadyForRead:
00333 {
00334
00335 return ANT_FAIL;
00336 }
00337 break;
00338
00339
00340 case TCPEndpoint_Operation_ReadyForWrite:
00341 {
00342
00343 return ANT_FAIL;
00344 }
00345 break;
00346
00347
00348 case TCPEndpoint_Operation_BacklogListen:
00349 {
00350
00351 return ANT_FAIL;
00352 }
00353 break;
00354
00355
00356 case TCPEndpoint_Operation_Accept:
00357 {
00358
00359 return ANT_FAIL;
00360 }
00361 break;
00362
00363
00364 case TCPEndpoint_Operation_BacklogQueues:
00365 {
00366
00367 return ANT_FAIL;
00368 }
00369 break;
00370
00371
00372 case TCPEndpoint_Operation_Address:
00373 {
00374
00375 return ANT_FAIL;
00376 }
00377 break;
00378
00379
00380 default:
00381 OSYSLOG1((osyslogERROR, "antEnvMsg::Send: Invalid TCP message type (%d)", this->dstOperation));
00382 break;
00383
00384 }
00385 break;
00386
00387 case EndpointType_UDP:
00388
00389 switch(this->dstOperation) {
00390
00391 case UDPEndpoint_Operation_Send:
00392 {
00393 UDPEndpointSendMsg *sendMsg = (UDPEndpointSendMsg*)this;
00394
00395 if (osei->state != OpenSdkEndpointInfo::ACTIVE) {
00396 sendMsg->error = UDP_state_error(osei->state);
00397 return ANT_FAIL;
00398 }
00399
00400 osei = new OpenSdkEndpointInfo(*osei);
00401
00402 osei->buffer = sendMsg->buffer;
00403 osei->size = sendMsg->size;
00404 osei->msg = new UDPEndpointSendMsg(*sendMsg);
00405
00406 if (OpenSdkThread::runFunction(udpSendThreadFunc, osei) == 0) {
00407 sendMsg->error = UDP_SUCCESS;
00408 return ANT_SUCCESS;
00409
00410 } else {
00411 sendMsg->error = UDP_FAIL;
00412 return ANT_FAIL;
00413 }
00414 }
00415 break;
00416
00417 case UDPEndpoint_Operation_Receive:
00418 {
00419 UDPEndpointReceiveMsg *receiveMsg = (UDPEndpointReceiveMsg*)this;
00420
00421 if (osei->state != OpenSdkEndpointInfo::ACTIVE) {
00422 receiveMsg->error = UDP_state_error(osei->state);
00423 return ANT_FAIL;
00424 }
00425
00426 osei = new OpenSdkEndpointInfo(*osei);
00427
00428 osei->buffer = receiveMsg->buffer;
00429 osei->size = receiveMsg->size;
00430 osei->msg = new UDPEndpointReceiveMsg(*receiveMsg);
00431
00432 if (OpenSdkThread::runFunction(udpReceiveThreadFunc, osei) == 0) {
00433 receiveMsg->error = UDP_SUCCESS;
00434 return ANT_SUCCESS;
00435
00436 } else {
00437 receiveMsg->error = UDP_FAIL;
00438 return ANT_FAIL;
00439 }
00440 }
00441 break;
00442
00443 case UDPEndpoint_Operation_Close:
00444 {
00445 UDPEndpointCloseMsg *closeMsg = (UDPEndpointCloseMsg*)this;
00446
00447 if (osei->state != OpenSdkEndpointInfo::ACTIVE) {
00448 closeMsg->error = UDP_state_error(osei->state);
00449 return ANT_FAIL;
00450 }
00451
00452 osei = new OpenSdkEndpointInfo(*osei);
00453 osei->msg = new UDPEndpointCloseMsg(*closeMsg);
00454
00455 if (OpenSdkThread::runFunction(udpCloseThreadFunc, osei) == 0) {
00456 closeMsg->error = UDP_SUCCESS;
00457 return ANT_SUCCESS;
00458
00459 } else {
00460 closeMsg->error = UDP_FAIL;
00461 return ANT_FAIL;
00462 }
00463 }
00464 break;
00465
00466 default:
00467 OSYSLOG1((osyslogERROR, "antEnvMsg::Send: Invalid UDP message type (%d)", this->dstOperation));
00468 break;
00469 }
00470 break;
00471
00472 case EndpointType_DNS:
00473
00474 switch(this->dstOperation) {
00475 case DNSEndpoint_Operation_GetHostByName:
00476 {
00477 DNSEndpointGetHostByNameMsg *msg = (DNSEndpointGetHostByNameMsg*)this;
00478
00479 osei->msg = new DNSEndpointGetHostByNameMsg(*msg);
00480 osei->hostname = strdup(msg->name);
00481
00482 if (OpenSdkThread::runFunction(dnsGetHostByNameThreadFunc, osei) == 0) {
00483 msg->error = DNS_SUCCESS;
00484 return ANT_SUCCESS;
00485
00486 } else {
00487 msg->error = DNS_FAIL;
00488 return ANT_FAIL;
00489 }
00490 }
00491 break;
00492
00493 case DNSEndpoint_Operation_Close:
00494 {
00495
00496 }
00497 break;
00498
00499 default:
00500 OSYSLOG1((osyslogERROR, "antEnvMsg::Send: Invalid DNS message type (%d)", this->dstOperation));
00501 }
00502 break;
00503
00504 default:
00505 OSYSLOG1((osyslogERROR, "antEnvMsg::Send: Invalid protocol (%d)", endpoint->protocol));
00506 break;
00507
00508 }
00509
00510 return ANT_FAIL;
00511 }
00512
00513
00514 antError antEnvMsg::Call(antStackRef _dest, int _msgSize)
00515 {
00516 switch (this->dstSelector) {
00517 case Entry_antEnv_External:
00518
00519 switch (this->dstOperation) {
00520
00521 case antEnv_Operation_CreateEndpoint:
00522 {
00523 antEnvCreateEndpointMsg *cEndPointMsg = (antEnvCreateEndpointMsg*)this;
00524
00525 pthread_mutex_lock(&mapMut);
00526 int endpointId = nextEndpointId++;
00527 OpenSdkEndpointInfo *osei = new OpenSdkEndpointInfo();
00528 endpointMap[endpointId] = osei;
00529 pthread_mutex_unlock(&mapMut);
00530
00531 antModuleRef endpoint((void*)endpointId, cEndPointMsg->protocol);
00532
00533 cEndPointMsg->moduleRef = endpoint;
00534 cEndPointMsg->error = ANT_SUCCESS;
00535 return ANT_SUCCESS;
00536
00537 }
00538 break;
00539
00540
00541 case antEnv_Operation_CreateSharedBuffer:
00542 {
00543 antEnvCreateSharedBufferMsg *cSharedBufferMsg = (antEnvCreateSharedBufferMsg*)this;
00544
00545 void *bufferPtr = malloc(cSharedBufferMsg->size);
00546 if (bufferPtr == NULL) {
00547 cSharedBufferMsg->error = ANT_NOMEMORY;
00548 return ANT_NOMEMORY;
00549
00550 } else {
00551 antSharedBuffer asb = antSharedBufferManager::getSharedBuffer(bufferPtr, cSharedBufferMsg->size);
00552 cSharedBufferMsg->buffer = asb;
00553 cSharedBufferMsg->error = ANT_SUCCESS;
00554 return ANT_SUCCESS;
00555 }
00556
00557 }
00558 break;
00559
00560 case antEnv_Operation_DestroySharedBuffer:
00561 {
00562 antEnvDestroySharedBufferMsg *msg = (antEnvDestroySharedBufferMsg*)this;
00563 free(msg->buffer.GetAddress());
00564 msg->error = ANT_SUCCESS;
00565 return ANT_SUCCESS;
00566
00567 }
00568 break;
00569
00570 default:
00571 {
00572
00573
00574 OpenSdkEndpoint *endpoint = (OpenSdkEndpoint*)(&(this->dstModule));
00575
00576 switch (endpoint->protocol) {
00577
00578 case EndpointType_UDP:
00579 {
00580
00581 switch(this->dstOperation) {
00582
00583 case UDPEndpoint_Operation_Bind:
00584 {
00585 UDPEndpointBindMsg * udpBindMsg = (UDPEndpointBindMsg*)this;
00586
00587 struct sockaddr_in udpAddress;
00588 memset(&udpAddress, 0, sizeof(udpAddress));
00589 udpAddress.sin_family = AF_INET;
00590 udpAddress.sin_addr.s_addr = udpBindMsg->address.Address();
00591 udpAddress.sin_port = htons(udpBindMsg->port);
00592
00593 pthread_mutex_lock(&mapMut);
00594 OpenSdkEndpointInfo *osei = endpointMap[(int)endpoint->endpointId];
00595 pthread_mutex_unlock(&mapMut);
00596
00597 osei->socket = socket(PF_INET, SOCK_DGRAM, 0);
00598
00599 if (osei->socket < 0) {
00600 udpBindMsg->error = UDP_error();
00601 return ANT_FAIL;
00602 }
00603
00604 int bindRet = bind(osei->socket, (const struct sockaddr *)&udpAddress, sizeof(udpAddress));
00605
00606 if (bindRet != 0) {
00607 udpBindMsg->error = UDP_error();
00608 return ANT_FAIL;
00609 }
00610
00611 osei->state = OpenSdkEndpointInfo::ACTIVE;
00612 udpBindMsg->error = UDP_SUCCESS;
00613
00614 return ANT_SUCCESS;
00615
00616 }
00617 break;
00618
00619 default:
00620 OSYSLOG1((osyslogERROR, "antEnvMsg::Call: invalid UDP dstOperation (%d)", this->dstOperation));
00621 return ANT_FAIL;
00622
00623 }
00624
00625 }
00626 break;
00627
00628 default:
00629 OSYSLOG1((osyslogERROR, "antEnvMsg::Call : invalid protocol (%d)", endpoint->protocol));
00630 return ANT_FAIL;
00631 }
00632 }
00633 }
00634 break;
00635
00636 case Entry_antEnv_Initialize:
00637 {
00638 switch (this->dstOperation) {
00639 case antEnv_Operation_InitSetParam:
00640 {
00641 return ANT_FAIL;
00642 }
00643 break;
00644
00645 case antEnv_Operation_InitStart:
00646 {
00647 return ANT_FAIL;
00648 }
00649 break;
00650
00651 case antEnv_Operation_InitGetParam:
00652 {
00653 return ANT_FAIL;
00654 }
00655 break;
00656
00657 default:
00658 OSYSLOG1((osyslogERROR, "antEnvMsg::Call: invalid init dstOperation (%d)", this->dstOperation));
00659 return ANT_FAIL;
00660 }
00661 }
00662 break;
00663
00664 default:
00665 OSYSLOG1((osyslogERROR, "antEnvMsg::Call: invalid dstSelector (%d)", this->dstSelector));
00666 return ANT_FAIL;
00667 }
00668
00669 }
00670
00671
00672 antEnvMsg* antEnvMsg::Receive(ANTENVMSG _msg)
00673 {
00674 return (antEnvMsg*)_msg;
00675 }
00676
00677
00678 int32 antEnvMsg::GetOperation()
00679 {
00680 return dstOperation;
00681 }
00682
00683
00684 OID& antEnvMsg::GetSender()
00685 {
00686 return sender;
00687 }
00688
00689
00690
00691
00693 static TCPEndpointError TCP_error(void)
00694 {
00695 switch (errno) {
00696 case EADDRINUSE: return TCP_ADDRESSINUSE;
00697 case EADDRNOTAVAIL: return TCP_ADDRESSERROR;
00698 case ENETUNREACH: return TCP_NETWORK_UNREACHABLE;
00699 case EAGAIN: return TCP_WOULDBLOCK;
00700 #if EAGAIN != EWOULDBLOCK
00701 case EWOULDBLOCK: return TCP_WOULDBLOCK;
00702 #endif
00703 case EFAULT: return TCP_BUFFER_INVALID;
00704 default: return TCP_FAIL;
00705 }
00706 }
00707
00708
00710 static TCPEndpointError TCP_state_error(OpenSdkEndpointInfo::State state)
00711 {
00712 switch (state) {
00713 case OpenSdkEndpointInfo::NEW: return TCP_CONNECTION_CLOSED;
00714 case OpenSdkEndpointInfo::CLOSED: return TCP_CONNECTION_CLOSED;
00715 case OpenSdkEndpointInfo::LISTENING: return TCP_CONNECTION_BUSY;
00716 case OpenSdkEndpointInfo::CONNECTING: return TCP_CONNECTION_BUSY;
00717 default: return TCP_FAIL;
00718 }
00719 }
00720
00721
00722 static void* tcpListenThreadFunc(void *arg)
00723 {
00724 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00725 TCPEndpointListenMsg *listenMsg = (TCPEndpointListenMsg*)osei->msg;
00726 int listenSocket = osei->server_socket;
00727
00728 pthread_mutex_lock(osei->listen_mutex);
00729 int acceptRet = accept(listenSocket, NULL, NULL);
00730
00731 if (acceptRet <= 0) {
00732 listenMsg->error = TCP_error();
00733 } else {
00734 osei->socket = acceptRet;
00735 osei->state = OpenSdkEndpointInfo::ACTIVE;
00736 listenMsg->error = TCP_SUCCESS;
00737 }
00738
00739 pthread_mutex_unlock(osei->listen_mutex);
00740
00741 _sendMessage(osei->entry, listenMsg);
00742
00743 return NULL;
00744 }
00745
00746
00747 static void* tcpReceiveThreadFunc(void *arg)
00748 {
00749 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00750 int socket = osei->socket;
00751 int maxSize = osei->size;
00752 void *buffer = osei->buffer;
00753 TCPEndpointReceiveMsg *receiveMsg = (TCPEndpointReceiveMsg*)osei->msg;
00754
00755 int receiveRet = read(socket, buffer, maxSize);
00756
00757 if (receiveRet < 0) {
00758 receiveMsg->error = TCP_error();
00759
00760 } else if (receiveRet == 0) {
00761 receiveMsg->error = TCP_CONNECTION_CLOSED;
00762
00763 } else {
00764 receiveMsg->sizeMin = receiveRet;
00765 receiveMsg->sizeMax = receiveRet;
00766 receiveMsg->error = TCP_SUCCESS;
00767 }
00768
00769 _sendMessage(osei->entry, receiveMsg);
00770
00771 return NULL;
00772 }
00773
00774
00775 static void* tcpSendThreadFunc(void *arg)
00776 {
00777 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00778 int socket = osei->socket;
00779 int sendSize = osei->size;
00780 void *buffer = osei->buffer;
00781 TCPEndpointSendMsg *sendMsg = (TCPEndpointSendMsg*)osei->msg;
00782
00783 int writeRet = write(socket, buffer, sendSize);
00784
00785 if (writeRet <= 0) {
00786 sendMsg->error = TCP_error();
00787 } else {
00788 sendMsg->error = TCP_SUCCESS;
00789 }
00790
00791 _sendMessage(osei->entry, sendMsg);
00792
00793 return NULL;
00794 }
00795
00796
00797 static void* tcpConnectThreadFunc(void *arg)
00798 {
00799 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00800 TCPEndpointConnectMsg *connectMsg = (TCPEndpointConnectMsg*)osei->msg;
00801
00802 struct sockaddr_in addr;
00803 memset(&addr, 0, sizeof(addr));
00804
00805 addr.sin_addr.s_addr = osei->ip.Address();
00806 addr.sin_family = AF_INET;
00807 addr.sin_port = htons(osei->port);
00808
00809 int ret = connect(osei->socket, (struct sockaddr *)&addr, sizeof(addr));
00810
00811 if (ret < 0) {
00812 connectMsg->error = TCP_error();
00813
00814 } else {
00815 connectMsg->error = TCP_SUCCESS;
00816 osei->state = OpenSdkEndpointInfo::ACTIVE;
00817 }
00818
00819 _sendMessage(osei->entry, connectMsg);
00820
00821 return NULL;
00822 }
00823
00824
00825 static void* tcpCloseThreadFunc(void *arg)
00826 {
00827 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00828 TCPEndpointCloseMsg *closeMsg = (TCPEndpointCloseMsg*)osei->msg;
00829
00830 close(osei->socket);
00831
00832
00833
00834 osei->socket = NO_SOCKET;
00835 osei->server_socket = NO_SOCKET;
00836
00837 osei->state = OpenSdkEndpointInfo::CLOSED;
00838 closeMsg->error = TCP_SUCCESS;
00839
00840 _sendMessage(osei->entry, closeMsg);
00841
00842 return NULL;
00843 }
00844
00845
00847
00848
00850 static UDPEndpointError UDP_error(void)
00851 {
00852 switch (errno) {
00853 case EADDRINUSE: return UDP_ADDRESSINUSE;
00854 case EADDRNOTAVAIL: return UDP_ADDRESSERROR;
00855 case ENETUNREACH: return UDP_NETWORK_UNREACHABLE;
00856 case EAGAIN: return UDP_WOULDBLOCK;
00857 #if EAGAIN != EWOULDBLOCK
00858 case EWOULDBLOCK: return UDP_WOULDBLOCK;
00859 #endif
00860 case EFAULT: return UDP_BUFFER_INVALID;
00861 default: return UDP_FAIL;
00862 }
00863 }
00864
00865
00867 static UDPEndpointError UDP_state_error(OpenSdkEndpointInfo::State state)
00868 {
00869 switch (state) {
00870 case OpenSdkEndpointInfo::NEW: return UDP_CONNECTION_CLOSED;
00871 case OpenSdkEndpointInfo::CLOSED: return UDP_CONNECTION_CLOSED;
00872 default: return UDP_FAIL;
00873 }
00874 }
00875
00876
00877 static void* udpReceiveThreadFunc(void *arg)
00878 {
00879 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00880 int socket = osei->socket;
00881 int recvSize = osei->size;
00882 UDPEndpointReceiveMsg *receiveMsg = (UDPEndpointReceiveMsg*)osei->msg;
00883
00884 struct sockaddr_in fromAddress;
00885 socklen_t sizeOfAddr = sizeof(fromAddress);
00886 memset(&fromAddress, 0, sizeof(fromAddress));
00887
00888 int receiveRet = recvfrom(socket, osei->buffer, recvSize, 0, (struct sockaddr *) &fromAddress, &sizeOfAddr);
00889
00890 if (receiveRet <= 0) {
00891 receiveMsg->error = UDP_error();
00892
00893 } else {
00894 receiveMsg->size = receiveRet;
00895 receiveMsg->port = ntohs(fromAddress.sin_port);
00896 receiveMsg->address = IPAddress((uint32)fromAddress.sin_addr.s_addr);
00897 receiveMsg->error = UDP_SUCCESS;
00898 }
00899
00900 _sendMessage(osei->entry, receiveMsg);
00901
00902 delete osei;
00903 return NULL;
00904 }
00905
00906
00907 static void* udpSendThreadFunc(void *arg)
00908 {
00909 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00910 UDPEndpointSendMsg *sendMsg = (UDPEndpointSendMsg*)osei->msg;
00911 int socket = osei->socket;
00912 int sendSize = osei->size;
00913 void *buffer = osei->buffer;
00914
00915 struct sockaddr_in address;
00916
00917 memset(&address, 0, sizeof(address));
00918 address.sin_family = AF_INET;
00919 address.sin_port = htons(sendMsg->port);
00920 address.sin_addr.s_addr = osei->ip.Address();
00921
00922 int sendRet = sendto(socket, buffer, sendSize, 0, (struct sockaddr*)&address, sizeof(address));
00923
00924 if (sendRet <= 0) {
00925 sendMsg->error = UDP_error();
00926 } else {
00927 sendMsg->error = UDP_SUCCESS;
00928 }
00929
00930 _sendMessage(osei->entry, sendMsg);
00931
00932 delete osei;
00933 return NULL;
00934 }
00935
00936
00937 static void* udpCloseThreadFunc(void *arg)
00938 {
00939 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00940 UDPEndpointCloseMsg *closeMsg = (UDPEndpointCloseMsg*)osei->msg;
00941
00942 close(osei->socket);
00943
00944 osei->socket = NO_SOCKET;
00945 osei->state = OpenSdkEndpointInfo::CLOSED;
00946 closeMsg->error = UDP_SUCCESS;
00947
00948 _sendMessage(osei->entry, closeMsg);
00949
00950 delete osei;
00951 return NULL;
00952 }
00953
00954
00955 static DNSEndpointError DNS_error(int error)
00956 {
00957 switch (error) {
00958 #ifndef __CYGWIN__
00959 case EAI_AGAIN: return DNS_TRY_AGAIN;
00960 case EAI_NODATA: return DNS_NO_DATA;
00961 #endif
00962 default: return DNS_FAIL;
00963 }
00964 }
00965
00966
00967 static void* dnsGetHostByNameThreadFunc(void *arg)
00968 {
00969 OpenSdkEndpointInfo *osei = (OpenSdkEndpointInfo*)arg;
00970 DNSEndpointGetHostByNameMsg *msg = (DNSEndpointGetHostByNameMsg*)osei->msg;
00971
00972 #ifndef __CYGWIN__
00973 struct addrinfo *res;
00974 struct addrinfo hints;
00975 memset(&hints, 0, sizeof(hints));
00976
00977 hints.ai_family = AF_INET;
00978
00979 int ret = getaddrinfo(osei->hostname, NULL, &hints, &res);
00980 #else
00981 int ret = -1;
00982 #endif
00983 free(osei->hostname);
00984
00985 if (ret != 0) {
00986 msg->error = DNS_error(ret);
00987 msg->n_address = 0;
00988 msg->n_alias = 0;
00989
00990 } else {
00991 #ifndef __CYGWIN__
00992 msg->error = DNS_SUCCESS;
00993
00994 unsigned int count = 0;
00995
00996 for (struct addrinfo *r = res; r != NULL; r = r->ai_next) {
00997 if (!count++) {
00998 msg->host_address = IPAddress((uint32)((sockaddr_in*)r->ai_addr)->sin_addr.s_addr);
00999 }
01000 }
01001
01002 freeaddrinfo(res);
01003
01004 msg->n_address = count;
01005 msg->n_alias = 0;
01006 #endif
01007 }
01008
01009 _sendMessage(osei->entry, msg);
01010
01011 return NULL;
01012 }