networklayer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include "networklayer.h"
  2. #include "ua_transport_connection.h"
  3. #ifdef WIN32
  4. #pragma comment (lib,"ws2_32.lib")
  5. #include <sys/types.h>
  6. #include <Windows.h>
  7. #include <ws2tcpip.h>
  8. #define CLOSESOCKET(S) closesocket(S)
  9. #define IOCTLSOCKET ioctlsocket
  10. #else
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <sys/socketvar.h>
  14. #include <unistd.h> // read, write, close
  15. #define CLOSESOCKET(S) close(S)
  16. #define IOCTLSOCKET ioctl
  17. #endif
  18. #include <stdlib.h> // exit
  19. #include <errno.h> // errno, EINTR
  20. #include <memory.h> // memset
  21. #include <fcntl.h> // fcntl
  22. NL_Description NL_Description_TcpBinary = {
  23. NL_UA_ENCODING_BINARY,
  24. NL_CONNECTIONTYPE_TCPV4,
  25. NL_MAXCONNECTIONS_DEFAULT,
  26. {0,8192,8192,16384,1}
  27. };
  28. /* If we do not have multitasking, we implement a dispatcher-Pattern. All Connections
  29. * are collected in a list. From this list a fd_set is prepared and select then waits
  30. * for activities. We then iterate over the list, check if we've got some activites
  31. * and call the corresponding callback (reader, listener).
  32. */
  33. #ifndef MULTITASKING
  34. _Bool NL_ConnectionComparer(void *p1, void* p2) {
  35. NL_Connection* c1 = (NL_Connection*) p1;
  36. NL_Connection* c2 = (NL_Connection*) p2;
  37. return (c1->connectionHandle == c2->connectionHandle);
  38. }
  39. int NL_TCP_SetNonBlocking(int sock) {
  40. #ifdef WIN32
  41. UA_Int64 iMode = 1;
  42. int opts = IOCTLSOCKET(sock, FIONBIO, &iMode);
  43. if (opts != NO_ERROR){
  44. printf("ioctlsocket failed with error: %ld\n", opts);
  45. return - 1;
  46. }
  47. #else
  48. int opts = fcntl(sock,F_GETFL);
  49. if (opts < 0) {
  50. perror("fcntl(F_GETFL)");
  51. return -1;
  52. }
  53. opts = (opts | O_NONBLOCK);
  54. if (fcntl(sock,F_SETFL,opts) < 0) {
  55. perror("fcntl(F_SETFL)");
  56. return -1;
  57. }
  58. #endif
  59. return 0;
  60. }
  61. void NL_Connection_printf(void* payload) {
  62. NL_Connection* c = (NL_Connection*) payload;
  63. printf("ListElement connectionHandle = %d\n",c->connectionHandle);
  64. }
  65. void NL_addHandleToSet(UA_Int32 handle, NL_data* nl) {
  66. FD_SET(handle, &(nl->readerHandles));
  67. #ifdef WIN32
  68. // int err = WSAGetLastError();
  69. #endif
  70. nl->maxReaderHandle = (handle > nl->maxReaderHandle) ? handle : nl->maxReaderHandle;
  71. }
  72. void NL_setFdSet(void* payload) {
  73. NL_Connection* c = (NL_Connection*) payload;
  74. NL_addHandleToSet(c->connectionHandle, c->networkLayer);
  75. }
  76. void NL_checkFdSet(void* payload) {
  77. NL_Connection* c = (NL_Connection*) payload;
  78. if (FD_ISSET(c->connectionHandle, &(c->networkLayer->readerHandles))) {
  79. c->reader((void*)c);
  80. }
  81. }
  82. #if 0
  83. char _str_error[256];
  84. char* strerror(int errno) {
  85. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  86. NULL, errno,
  87. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  88. &_str_error[0], 256, NULL);
  89. return &_str_error[0];
  90. }
  91. #endif
  92. UA_Int32 NL_msgLoop(NL_data* nl, struct timeval *tv, UA_Int32(*worker)(void*), void *arg, UA_Boolean *running) {
  93. UA_Int32 result;
  94. UA_Int32 err;
  95. while (*running) {
  96. // determine the largest handle
  97. nl->maxReaderHandle = 0;
  98. UA_list_iteratePayload(&(nl->connections),NL_setFdSet);
  99. printf("\n------------\nUA_Stack_msgLoop - maxHandle=%d\n", nl->maxReaderHandle);
  100. // copy tv, some unixes do overwrite and return the remaining time
  101. struct timeval tmptv;
  102. memcpy(&tmptv,tv,sizeof(struct timeval));
  103. // and wait
  104. DBG_VERBOSE(printf("UA_Stack_msgLoop - enter select sec=%d,usec=%d\n",(UA_Int32) tmptv.tv_sec, (UA_Int32) tmptv.tv_usec));
  105. result = select(nl->maxReaderHandle + 1, &(nl->readerHandles), UA_NULL, UA_NULL, &tmptv);
  106. // printf("UA_Stack_msgLoop - leave select result=%d,sec=%d,usec=%d\n",result, (UA_Int32) tmptv.tv_sec, (UA_Int32) tmptv.tv_usec);
  107. // handle timeout (winsock: result=0, unix: result=0,errno=0||EAGAIN)
  108. // and errors (winsock: result=SOCKET_ERROR (-1), unix: result = 0)
  109. if (result <= 0) {
  110. #ifdef WIN32
  111. err = (result == SOCKET_ERROR) ? WSAGetLastError() : 0;
  112. #else
  113. err = errno;
  114. #endif
  115. switch (err) {
  116. // handle known errors
  117. #ifdef WIN32
  118. case WSANOTINITIALISED:
  119. case WSAEFAULT:
  120. case WSAENETDOWN:
  121. case WSAEINVAL:
  122. case WSAEINTR:
  123. case WSAEINPROGRESS:
  124. case WSAENOTSOCK:
  125. #else
  126. case EBADF:
  127. case EINTR:
  128. case EINVAL:
  129. #endif
  130. // FIXME: handle errors
  131. printf("UA_Stack_msgLoop - result=%d, errno={%d,%s}\n", result, errno, strerror(errno));
  132. break;
  133. // we've got a timeout
  134. #ifndef WIN32
  135. case EAGAIN:
  136. #endif
  137. default:
  138. DBG(printf("UA_Stack_msgLoop - result=%d, errno={%d,%s}\n", result, errno, strerror(errno)));
  139. DBG(printf("UA_Stack_msgLoop - call worker\n"));
  140. worker(arg);
  141. DBG(printf("UA_Stack_msgLoop - return from worker\n"));
  142. }
  143. } else { // activity on listener or client ports
  144. printf("UA_Stack_msgLoop - activities on %d handles\n",result);
  145. UA_list_iteratePayload(&(nl->connections),NL_checkFdSet);
  146. }
  147. // FIXME: Seems to be a conceptional flaw to call the worker here...
  148. // worker(arg);
  149. }
  150. #ifdef WIN32
  151. WSACleanup();
  152. #endif
  153. return UA_SUCCESS;
  154. }
  155. #endif /* MULTITASKING */
  156. /** the tcp reader function */
  157. void* NL_TCP_reader(NL_Connection *c) {
  158. UA_ByteString readBuffer;
  159. TL_Buffer localBuffers;
  160. UA_Int32 connectionState;
  161. UA_TL_Connection_getLocalConfig(c->connection, &localBuffers);
  162. UA_alloc((void**)&(readBuffer.data),localBuffers.recvBufferSize);
  163. UA_TL_Connection_getState(c->connection, &connectionState);
  164. if (connectionState != CONNECTIONSTATE_CLOSE) {
  165. DBG_VERBOSE(printf("NL_TCP_reader - enter read\n"));
  166. #ifdef WIN32
  167. readBuffer.length = recv(c->connectionHandle, readBuffer.data, localBuffers.recvBufferSize, 0);
  168. #else
  169. readBuffer.length = read(c->connectionHandle, readBuffer.data, localBuffers.recvBufferSize);
  170. #endif
  171. DBG_VERBOSE(printf("NL_TCP_reader - leave read\n"));
  172. DBG_VERBOSE(printf("NL_TCP_reader - src={%*.s}, ",c->connection.remoteEndpointUrl.length,c->connection.remoteEndpointUrl.data));
  173. DBG(UA_ByteString_printx("NL_TCP_reader - received=",&readBuffer));
  174. if (readBuffer.length > 0) {
  175. #ifdef DEBUG
  176. #include "ua_transport_binary_secure.h"
  177. UA_UInt32 pos = 0;
  178. UA_OPCUATcpMessageHeader header;
  179. UA_OPCUATcpMessageHeader_decodeBinary(&readBuffer, &pos, &header);
  180. pos = 24;
  181. if(header.messageType == UA_MESSAGETYPE_MSG)
  182. {
  183. UA_NodeId serviceRequestType;
  184. UA_NodeId_decodeBinary(&readBuffer, &pos,&serviceRequestType);
  185. UA_NodeId_printf("Service Type\n",&serviceRequestType);
  186. }
  187. #endif
  188. TL_Process((c->connection),&readBuffer);
  189. } else {
  190. perror("ERROR reading from socket1");
  191. UA_TL_Connection_setState(c->connection, CONNECTIONSTATE_CLOSE);
  192. }
  193. }
  194. UA_TL_Connection_getState(c->connection, &connectionState);
  195. printf("NL_TCP_reader - connectionState=%d\n",connectionState);
  196. if (connectionState == CONNECTIONSTATE_CLOSE) {
  197. // set connection's state to CONNECTIONSTATE_CLOSED and call callback to actually close
  198. UA_TL_Connection_close(c->connection);
  199. #ifndef MULTITHREADING
  200. DBG_VERBOSE(printf("NL_TCP_reader - search element to remove\n"));
  201. UA_list_Element* lec = UA_list_search(&(c->networkLayer->connections),NL_ConnectionComparer,c);
  202. DBG_VERBOSE(printf("NL_TCP_reader - remove connection for handle=%d\n",((NL_Connection*)lec->payload)->connection.connectionHandle));
  203. UA_list_removeElement(lec,UA_NULL);
  204. DBG_VERBOSE(UA_list_iteratePayload(&(c->networkLayer->connections),NL_Connection_printf));
  205. UA_free(c);
  206. #endif
  207. }
  208. UA_ByteString_deleteMembers(&readBuffer);
  209. return UA_NULL;
  210. }
  211. #ifdef MULTITHREADING
  212. /** the tcp reader thread */
  213. void* NL_TCP_readerThread(NL_Connection *c) {
  214. // just loop, NL_TCP_Reader will call the stack
  215. UA_Int32 connectionState;
  216. do {
  217. NL_TCP_reader(c);
  218. UA_TL_Connection_getState(c->connection, &connectionState);
  219. } while (connectionState != CONNECTIONSTATE_CLOSED);
  220. // clean up
  221. UA_free(c);
  222. pthread_exit(UA_NULL);
  223. }
  224. #endif
  225. /** write message provided in the gather buffers to a tcp transport layer connection */
  226. UA_Int32 NL_TCP_writer(UA_Int32 connectionHandle, UA_ByteString const * const * gather_buf, UA_UInt32 gather_len) {
  227. UA_UInt32 total_len = 0;
  228. #ifdef WIN32
  229. WSABUF *buf = malloc(gather_len * sizeof(WSABUF));
  230. int result = 0;
  231. for (UA_UInt32 i = 0; i<gather_len; i++) {
  232. buf[i].buf = gather_buf[i]->data;
  233. buf[i].len = gather_buf[i]->length;
  234. total_len += gather_buf[i]->length;
  235. // DBG(printf("NL_TCP_writer - gather_buf[%i]",i));
  236. // DBG(UA_ByteString_printx("=", gather_buf[i]));
  237. }
  238. #else
  239. struct iovec iov[gather_len];
  240. for(UA_UInt32 i=0;i<gather_len;i++) {
  241. iov[i].iov_base = gather_buf[i]->data;
  242. iov[i].iov_len = gather_buf[i]->length;
  243. total_len += gather_buf[i]->length;
  244. // DBG(printf("NL_TCP_writer - gather_buf[%i]",i));
  245. // DBG(UA_ByteString_printx("=", gather_buf[i]));
  246. }
  247. struct msghdr message;
  248. message.msg_name = UA_NULL;
  249. message.msg_namelen = 0;
  250. message.msg_iov = iov;
  251. message.msg_iovlen = gather_len;
  252. message.msg_control = UA_NULL;
  253. message.msg_controllen = 0;
  254. message.msg_flags = 0;
  255. #endif
  256. UA_UInt32 nWritten = 0;
  257. while (nWritten < total_len) {
  258. int n=0;
  259. do {
  260. DBG_VERBOSE(printf("NL_TCP_writer - enter write with %d bytes to write\n",total_len));
  261. #ifdef WIN32
  262. //result = WSASendMsg(connectionHandle,&message,0,&n,UA_NULL,UA_NULL);
  263. result = WSASend(connectionHandle, buf, gather_len , &n, 0, NULL, NULL);
  264. if(result != 0)
  265. {
  266. printf("NL_TCP_Writer - Error WSASend, code: %d \n", WSAGetLastError());
  267. }
  268. #else
  269. n = sendmsg(connectionHandle, &message, 0);
  270. #endif
  271. DBG_VERBOSE(printf("NL_TCP_writer - leave write with n=%d,errno={%d,%s}\n",n,(n>0)?0:errno,(n>0)?"":strerror(errno)));
  272. } while (n == -1L && errno == EINTR);
  273. if (n >= 0) {
  274. nWritten += n;
  275. break;
  276. // TODO: handle incompletely send messages
  277. } else {
  278. break;
  279. // TODO: error handling
  280. }
  281. }
  282. #ifdef WIN32
  283. free(buf);
  284. #endif
  285. return UA_SUCCESS;
  286. }
  287. //callback function which is called when the UA_TL_Connection_close() function is initiated
  288. UA_Int32 NL_Connection_close(UA_TL_Connection *connection)
  289. {
  290. NL_Connection *networkLayerData = UA_NULL;
  291. UA_TL_Connection_getNetworkLayerData(connection, (void**)&networkLayerData);
  292. if(networkLayerData != UA_NULL){
  293. DBG_VERBOSE(printf("NL_Connection_close - enter shutdown\n"));
  294. shutdown(networkLayerData->connectionHandle,2);
  295. DBG_VERBOSE(printf("NL_Connection_close - enter close\n"));
  296. CLOSESOCKET(networkLayerData->connectionHandle);
  297. FD_CLR(networkLayerData->connectionHandle, &networkLayerData->networkLayer->readerHandles);
  298. DBG_VERBOSE(printf("NL_Connection_close - leave close\n"));
  299. return UA_SUCCESS;
  300. }
  301. DBG_VERBOSE(printf("NL_Connection_close - ERROR: connection object invalid \n"));
  302. return UA_ERROR;
  303. }
  304. void* NL_Connection_init(NL_Connection* c, NL_data* tld, UA_Int32 connectionHandle, NL_Reader reader, TL_Writer writer)
  305. {
  306. UA_TL_Connection *connection = UA_NULL;
  307. //create new connection object
  308. UA_TL_Connection_new(&connection, tld->tld->localConf, writer, NL_Connection_close,connectionHandle,c);
  309. c->connection = connection;
  310. c->connectionHandle = connectionHandle;
  311. // network layer
  312. c->reader = reader;
  313. #ifdef MULTITHREADING
  314. c->readerThreadHandle = -1;
  315. #endif
  316. c->networkLayer = tld;
  317. return UA_NULL;
  318. }
  319. /** the tcp accept routine */
  320. void* NL_TCP_accept(NL_Connection* c) {
  321. NL_data* tld = c->networkLayer;
  322. if (tld->tld->maxConnections == -1 || tld->connections.size < tld->tld->maxConnections) {
  323. // accept only if not max number of connections exceeded
  324. struct sockaddr_in cli_addr;
  325. socklen_t cli_len = sizeof(cli_addr);
  326. DBG_VERBOSE(printf("NL_TCP_listen - enter accept\n"));
  327. int newsockfd = accept(c->connectionHandle, (struct sockaddr *) &cli_addr, &cli_len);
  328. DBG_VERBOSE(printf("NL_TCP_listen - leave accept\n"));
  329. if (newsockfd < 0) {
  330. DBG_ERR(printf("TL_TCP_listen - accept returns errno={%d,%s}\n",errno,strerror(errno)));
  331. perror("ERROR on accept");
  332. } else {
  333. DBG_VERBOSE(printf("NL_TCP_listen - new connection on %d\n",newsockfd));
  334. NL_Connection* cclient;
  335. UA_Int32 retval = UA_SUCCESS;
  336. retval |= UA_alloc((void**)&cclient,sizeof(NL_Connection));
  337. NL_Connection_init(cclient, tld, newsockfd, NL_TCP_reader, (TL_Writer) NL_TCP_writer);
  338. #ifdef MULTITHREADING
  339. pthread_create( &(cclient->readerThreadHandle), NULL, (void*(*)(void*)) NL_TCP_readerThread, (void*) cclient);
  340. #else
  341. UA_list_addPayloadToBack(&(tld->connections),cclient);
  342. NL_TCP_SetNonBlocking(cclient->connectionHandle);
  343. #endif
  344. }
  345. } else {
  346. // no action necessary to reject connection
  347. }
  348. return UA_NULL;
  349. }
  350. #ifdef MULTITHREADING
  351. void* NL_TCP_listenThread(NL_Connection* c) {
  352. NL_data* tld = c->networkLayer;
  353. do {
  354. DBG_VERBOSE(printf("NL_TCP_listenThread - enter listen\n"));
  355. int retval = listen(c->connectionHandle, tld->tld->maxConnections);
  356. DBG_VERBOSE(printf("NL_TCP_listenThread - leave listen, retval=%d\n", retval));
  357. if (retval < 0) {
  358. // TODO: Error handling
  359. perror("NL_TCP_listen");
  360. DBG_ERR(printf("NL_TCP_listen retval=%d, errno={%d,%s}\n", retval, errno, strerror(errno)));
  361. } else {
  362. NL_TCP_accept(c);
  363. }
  364. } while (UA_TRUE);
  365. UA_free(c);
  366. pthread_exit(UA_NULL);
  367. }
  368. #endif
  369. UA_Int32 NL_TCP_init(NL_data* tld, UA_Int32 port) {
  370. UA_Int32 retval = UA_SUCCESS;
  371. // socket variables
  372. int newsockfd;
  373. struct sockaddr_in serv_addr;
  374. // create socket for listening to incoming connections
  375. #ifdef WIN32
  376. WORD wVersionRequested;
  377. WSADATA wsaData;
  378. int err;
  379. /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
  380. wVersionRequested = MAKEWORD(2, 2);
  381. err = WSAStartup(wVersionRequested, &wsaData);
  382. newsockfd = socket(PF_INET, SOCK_STREAM,0);
  383. if (newsockfd == INVALID_SOCKET){
  384. UA_Int32 lasterror = WSAGetLastError();
  385. printf("ERROR opening socket, code: %d\n",WSAGetLastError());
  386. #else
  387. newsockfd = socket(PF_INET, SOCK_STREAM, 0);
  388. if (newsockfd < 0) {
  389. #endif
  390. perror("ERROR opening socket");
  391. retval = UA_ERROR;
  392. }
  393. else {
  394. // set port number, options and bind
  395. memset((void *)&serv_addr, sizeof(serv_addr), 1);
  396. serv_addr.sin_family = AF_INET;
  397. serv_addr.sin_addr.s_addr = INADDR_ANY;
  398. serv_addr.sin_port = htons(port);
  399. int optval = 1;
  400. if (setsockopt(newsockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) == -1) {
  401. perror("setsockopt");
  402. retval = UA_ERROR;
  403. }
  404. else {
  405. // bind to port
  406. if (bind(newsockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  407. perror("ERROR on binding");
  408. retval = UA_ERROR;
  409. }
  410. else {
  411. UA_String_copyprintf("opc.tcp://localhost:%d/", &(tld->endpointUrl), port);
  412. }
  413. }
  414. }
  415. // finally
  416. if (retval == UA_SUCCESS) {
  417. DBG_VERBOSE(printf("NL_TCP_init - new listener on %d\n",newsockfd));
  418. NL_Connection* c;
  419. UA_Int32 retval = UA_SUCCESS;
  420. retval |= UA_alloc((void**)&c,sizeof(NL_Connection));
  421. NL_Connection_init(c, tld, newsockfd, NL_TCP_accept, (TL_Writer) NL_TCP_writer);
  422. #ifdef MULTITHREADING
  423. pthread_create( &(c->readerThreadHandle), NULL, (void*(*)(void*)) NL_TCP_listenThread, (void*) c);
  424. #else
  425. UA_list_addPayloadToBack(&(tld->connections),c);
  426. NL_TCP_SetNonBlocking(c->connectionHandle);
  427. listen(c->connectionHandle, tld->tld->maxConnections);
  428. #endif
  429. }
  430. return retval;
  431. }
  432. /** checks arguments and dispatches to worker or refuses to init */
  433. NL_data* NL_init(NL_Description* tlDesc, UA_Int32 port) {
  434. NL_data* nl = UA_NULL;
  435. if (tlDesc->connectionType == NL_CONNECTIONTYPE_TCPV4 && tlDesc->encoding == NL_UA_ENCODING_BINARY) {
  436. UA_alloc((void**)&nl, sizeof(NL_data));
  437. nl->tld = tlDesc;
  438. FD_ZERO(&(nl->readerHandles));
  439. UA_list_init(&(nl->connections));
  440. NL_TCP_init(nl, port);
  441. }
  442. return nl;
  443. }