networklayer_tcp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include "networklayer_tcp.h"
  6. #include <stdlib.h> // malloc, free
  7. #include <string.h> // memset
  8. #include <errno.h>
  9. #ifdef _WIN32
  10. # include <malloc.h>
  11. # include <winsock2.h>
  12. # include <ws2tcpip.h>
  13. # define CLOSESOCKET(S) closesocket(S)
  14. #else
  15. # include <fcntl.h>
  16. # include <sys/select.h>
  17. # include <netinet/in.h>
  18. # include <netinet/tcp.h>
  19. # include <sys/ioctl.h>
  20. # include <netdb.h> //gethostbyname for the client
  21. # include <unistd.h> // read, write, close
  22. # include <arpa/inet.h>
  23. # define CLOSESOCKET(S) close(S)
  24. #endif
  25. #ifdef UA_MULTITHREADING
  26. # include <urcu/uatomic.h>
  27. #endif
  28. /****************************/
  29. /* Generic Socket Functions */
  30. /****************************/
  31. static UA_StatusCode socket_write(UA_Connection *connection, UA_ByteString *buf, size_t buflen) {
  32. size_t nWritten = 0;
  33. while (nWritten < buflen) {
  34. UA_Int32 n = 0;
  35. do {
  36. #ifdef _WIN32
  37. n = send((SOCKET)connection->sockfd, (const char*)buf->data, buflen, 0);
  38. if(n < 0 && WSAGetLastError() != WSAEINTR && WSAGetLastError() != WSAEWOULDBLOCK)
  39. return UA_STATUSCODE_BADCONNECTIONCLOSED;
  40. #else
  41. n = send(connection->sockfd, (const char*)buf->data, buflen, MSG_NOSIGNAL);
  42. if(n == -1L && errno != EINTR && errno != EAGAIN)
  43. return UA_STATUSCODE_BADCONNECTIONCLOSED;
  44. #endif
  45. } while (n == -1L);
  46. nWritten += n;
  47. }
  48. #ifdef UA_MULTITHREADING
  49. UA_ByteString_deleteMembers(buf);
  50. #endif
  51. return UA_STATUSCODE_GOOD;
  52. }
  53. static void socket_close(UA_Connection *connection) {
  54. connection->state = UA_CONNECTION_CLOSED;
  55. shutdown(connection->sockfd,2);
  56. CLOSESOCKET(connection->sockfd);
  57. }
  58. static UA_StatusCode socket_recv(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout) {
  59. response->data = malloc(connection->localConf.recvBufferSize);
  60. if(!response->data) {
  61. UA_ByteString_init(response);
  62. return UA_STATUSCODE_GOOD; /* not enough memory retry */
  63. }
  64. struct timeval tmptv = {0, timeout * 1000};
  65. if(0 != setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tmptv, sizeof(struct timeval))){
  66. free(response->data);
  67. UA_ByteString_init(response);
  68. socket_close(connection);
  69. return UA_STATUSCODE_BADINTERNALERROR;
  70. }
  71. int ret = recv(connection->sockfd, (char*)response->data, connection->localConf.recvBufferSize, 0);
  72. if(ret == 0) {
  73. free(response->data);
  74. UA_ByteString_init(response);
  75. socket_close(connection);
  76. return UA_CONNECTION_CLOSED; /* ret == 0 -> server has closed the connection */
  77. } else if(ret < 0) {
  78. free(response->data);
  79. UA_ByteString_init(response);
  80. #ifdef _WIN32
  81. if(WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEWOULDBLOCK) {
  82. #else
  83. if(errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
  84. #endif
  85. return UA_STATUSCODE_GOOD; /* retry */
  86. } else {
  87. socket_close(connection);
  88. return UA_STATUSCODE_BADCONNECTIONCLOSED;
  89. }
  90. }
  91. response->length = ret;
  92. *response = UA_Connection_completeMessages(connection, *response);
  93. return UA_STATUSCODE_GOOD;
  94. }
  95. static UA_StatusCode socket_set_nonblocking(UA_Int32 sockfd) {
  96. #ifdef _WIN32
  97. u_long iMode = 1;
  98. if(ioctlsocket(sockfd, FIONBIO, &iMode) != NO_ERROR)
  99. return UA_STATUSCODE_BADINTERNALERROR;
  100. #else
  101. int opts = fcntl(sockfd, F_GETFL);
  102. if(opts < 0 || fcntl(sockfd, F_SETFL, opts|O_NONBLOCK) < 0)
  103. return UA_STATUSCODE_BADINTERNALERROR;
  104. #endif
  105. return UA_STATUSCODE_GOOD;
  106. }
  107. static void FreeConnectionCallback(UA_Server *server, void *ptr) {
  108. UA_Connection_deleteMembers((UA_Connection*)ptr);
  109. free(ptr);
  110. }
  111. /***************************/
  112. /* Server NetworkLayer TCP */
  113. /***************************/
  114. /**
  115. * For the multithreaded mode, assume a single thread that periodically "gets work" from the network
  116. * layer. In addition, several worker threads are asynchronously calling into the callbacks of the
  117. * UA_Connection that holds a single connection.
  118. *
  119. * Creating a connection: When "GetWork" encounters a new connection, it creates a UA_Connection
  120. * with the socket information. This is added to the mappings array that links sockets to
  121. * UA_Connection structs.
  122. *
  123. * Reading data: In "GetWork", we listen on the sockets in the mappings array. If data arrives (or
  124. * the connection closes), a WorkItem is created that carries the work and a pointer to the
  125. * connection.
  126. *
  127. * Closing a connection: Closing can happen in two ways. Either it is triggered by the server in an
  128. * asynchronous callback. Or the connection is close by the client and this is detected in
  129. * "GetWork". The server needs to do some internal cleanups (close attached securechannels, etc.).
  130. * So even when a closed connection is detected in "GetWork", we trigger the server to close the
  131. * connection (with a WorkItem) and continue from the callback.
  132. *
  133. * - Server calls close-callback: We close the socket, set the connection-state to closed and add
  134. * the connection to a linked list from which it is deleted later. The connection cannot be freed
  135. * right away since other threads might still be using it.
  136. *
  137. * - GetWork: We remove the connection from the mappings array. In the non-multithreaded case, the
  138. * connection is freed. For multithreading, we return a workitem that is delayed, i.e. that is
  139. * called only after all workitems created before are finished in all threads. This workitems
  140. * contains a callback that goes through the linked list of connections to be freed.
  141. *
  142. */
  143. #define MAXBACKLOG 100
  144. typedef struct {
  145. /* config */
  146. UA_Logger *logger;
  147. UA_UInt32 port;
  148. UA_ConnectionConfig conf; /* todo: rename to localconf. */
  149. #ifndef UA_MULTITHREADING
  150. UA_ByteString buffer; // message buffer that is reused
  151. #endif
  152. /* open sockets and connections */
  153. fd_set fdset;
  154. UA_Int32 serversockfd;
  155. UA_Int32 highestfd;
  156. size_t mappingsSize;
  157. struct ConnectionMapping {
  158. UA_Connection *connection;
  159. UA_Int32 sockfd;
  160. } *mappings;
  161. } ServerNetworkLayerTCP;
  162. static UA_StatusCode ServerNetworkLayerGetBuffer(UA_Connection *connection, UA_ByteString *buf) {
  163. #ifdef UA_MULTITHREADING
  164. return UA_ByteString_newMembers(buf, connection->remoteConf.recvBufferSize);
  165. #else
  166. ServerNetworkLayerTCP *layer = connection->handle;
  167. *buf = layer->buffer;
  168. return UA_STATUSCODE_GOOD;
  169. #endif
  170. }
  171. static void ServerNetworkLayerReleaseBuffer(UA_Connection *connection, UA_ByteString *buf) {
  172. #ifdef UA_MULTITHREADING
  173. UA_ByteString_deleteMembers(buf);
  174. #endif
  175. }
  176. /* after every select, we need to reset the sockets we want to listen on */
  177. static void setFDSet(ServerNetworkLayerTCP *layer) {
  178. FD_ZERO(&layer->fdset);
  179. #ifdef _WIN32
  180. FD_SET((UA_UInt32)layer->serversockfd, &layer->fdset);
  181. #else
  182. FD_SET(layer->serversockfd, &layer->fdset);
  183. #endif
  184. layer->highestfd = layer->serversockfd;
  185. for(size_t i = 0; i < layer->mappingsSize; i++) {
  186. #ifdef _WIN32
  187. FD_SET((UA_UInt32)layer->mappings[i].sockfd, &layer->fdset);
  188. #else
  189. FD_SET(layer->mappings[i].sockfd, &layer->fdset);
  190. #endif
  191. if(layer->mappings[i].sockfd > layer->highestfd)
  192. layer->highestfd = layer->mappings[i].sockfd;
  193. }
  194. }
  195. /* callback triggered from the server */
  196. static void ServerNetworkLayerTCP_closeConnection(UA_Connection *connection) {
  197. #ifdef UA_MULTITHREADING
  198. if(uatomic_xchg(&connection->state, UA_CONNECTION_CLOSED) == UA_CONNECTION_CLOSED)
  199. return;
  200. #else
  201. if(connection->state == UA_CONNECTION_CLOSED)
  202. return;
  203. connection->state = UA_CONNECTION_CLOSED;
  204. #endif
  205. shutdown(connection->sockfd, 2); /* only shut down here. this triggers the select, where the socket
  206. is closed in the main thread */
  207. }
  208. /* call only from the single networking thread */
  209. static UA_StatusCode ServerNetworkLayerTCP_add(ServerNetworkLayerTCP *layer, UA_Int32 newsockfd) {
  210. UA_Connection *c = malloc(sizeof(UA_Connection));
  211. if(!c)
  212. return UA_STATUSCODE_BADINTERNALERROR;
  213. UA_Connection_init(c);
  214. c->sockfd = newsockfd;
  215. c->handle = layer;
  216. c->localConf = layer->conf;
  217. c->write = socket_write;
  218. c->close = ServerNetworkLayerTCP_closeConnection;
  219. c->getBuffer = ServerNetworkLayerGetBuffer;
  220. c->releaseBuffer = ServerNetworkLayerReleaseBuffer;
  221. c->state = UA_CONNECTION_OPENING;
  222. struct ConnectionMapping *nm =
  223. realloc(layer->mappings, sizeof(struct ConnectionMapping)*(layer->mappingsSize+1));
  224. if(!nm) {
  225. free(c);
  226. return UA_STATUSCODE_BADINTERNALERROR;
  227. }
  228. layer->mappings = nm;
  229. layer->mappings[layer->mappingsSize] = (struct ConnectionMapping){c, newsockfd};
  230. layer->mappingsSize++;
  231. return UA_STATUSCODE_GOOD;
  232. }
  233. static UA_StatusCode ServerNetworkLayerTCP_start(UA_ServerNetworkLayer *nl, UA_Logger *logger) {
  234. ServerNetworkLayerTCP *layer = nl->handle;
  235. layer->logger = logger;
  236. #ifdef _WIN32
  237. if((layer->serversockfd = socket(PF_INET, SOCK_STREAM,0)) == (UA_Int32)INVALID_SOCKET) {
  238. UA_LOG_WARNING((*layer->logger), UA_LOGCATEGORY_COMMUNICATION, "Error opening socket, code: %d",
  239. WSAGetLastError());
  240. return UA_STATUSCODE_BADINTERNALERROR;
  241. }
  242. #else
  243. if((layer->serversockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  244. UA_LOG_WARNING((*layer->logger), UA_LOGCATEGORY_COMMUNICATION, "Error opening socket");
  245. return UA_STATUSCODE_BADINTERNALERROR;
  246. }
  247. #endif
  248. const struct sockaddr_in serv_addr =
  249. {.sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY,
  250. .sin_port = htons(layer->port), .sin_zero = {0}};
  251. int optval = 1;
  252. if(setsockopt(layer->serversockfd, SOL_SOCKET,
  253. SO_REUSEADDR, (const char *)&optval,
  254. sizeof(optval)) == -1) {
  255. UA_LOG_WARNING((*layer->logger), UA_LOGCATEGORY_COMMUNICATION, "Error during setting of socket options");
  256. CLOSESOCKET(layer->serversockfd);
  257. return UA_STATUSCODE_BADINTERNALERROR;
  258. }
  259. if(bind(layer->serversockfd, (const struct sockaddr *)&serv_addr,
  260. sizeof(serv_addr)) < 0) {
  261. UA_LOG_WARNING((*layer->logger), UA_LOGCATEGORY_COMMUNICATION, "Error during socket binding");
  262. CLOSESOCKET(layer->serversockfd);
  263. return UA_STATUSCODE_BADINTERNALERROR;
  264. }
  265. socket_set_nonblocking(layer->serversockfd);
  266. listen(layer->serversockfd, MAXBACKLOG);
  267. UA_LOG_INFO((*layer->logger), UA_LOGCATEGORY_COMMUNICATION, "Listening on %.*s",
  268. nl->discoveryUrl.length, nl->discoveryUrl.data);
  269. return UA_STATUSCODE_GOOD;
  270. }
  271. static UA_Int32 ServerNetworkLayerTCP_getJobs(UA_ServerNetworkLayer *nl, UA_Job **jobs, UA_UInt16 timeout) {
  272. ServerNetworkLayerTCP *layer = nl->handle;
  273. setFDSet(layer);
  274. struct timeval tmptv = {0, timeout};
  275. UA_Int32 resultsize;
  276. repeat_select:
  277. resultsize = select(layer->highestfd+1, &layer->fdset, NULL, NULL, &tmptv);
  278. if(resultsize < 0) {
  279. if(errno == EINTR)
  280. goto repeat_select;
  281. *jobs = NULL;
  282. return resultsize;
  283. }
  284. /* accept new connections (can only be a single one) */
  285. if(FD_ISSET(layer->serversockfd, &layer->fdset)) {
  286. resultsize--;
  287. struct sockaddr_in cli_addr;
  288. socklen_t cli_len = sizeof(cli_addr);
  289. int newsockfd = accept(layer->serversockfd, (struct sockaddr *) &cli_addr, &cli_len);
  290. int i = 1;
  291. setsockopt(newsockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
  292. if(newsockfd >= 0) {
  293. socket_set_nonblocking(newsockfd);
  294. ServerNetworkLayerTCP_add(layer, newsockfd);
  295. }
  296. }
  297. /* alloc enough space for a cleanup-connection and free-connection job per resulted socket */
  298. if(resultsize == 0)
  299. return 0;
  300. UA_Job *js = malloc(sizeof(UA_Job) * resultsize * 2);
  301. if(!js)
  302. return 0;
  303. /* read from established sockets */
  304. UA_Int32 j = 0;
  305. UA_ByteString buf = UA_BYTESTRING_NULL;
  306. for(size_t i = 0; i < layer->mappingsSize && j < resultsize; i++) {
  307. if(!(FD_ISSET(layer->mappings[i].sockfd, &layer->fdset)))
  308. continue;
  309. if(socket_recv(layer->mappings[i].connection, &buf, 0) == UA_STATUSCODE_GOOD) {
  310. if(!buf.data)
  311. continue;
  312. js[j].type = UA_JOBTYPE_BINARYMESSAGE;
  313. js[j].job.binaryMessage.message = buf;
  314. js[j].job.binaryMessage.connection = layer->mappings[i].connection;
  315. } else {
  316. UA_Connection *c = layer->mappings[i].connection;
  317. /* the socket is already closed */
  318. js[j].type = UA_JOBTYPE_DETACHCONNECTION;
  319. js[j].job.closeConnection = layer->mappings[i].connection;
  320. layer->mappings[i] = layer->mappings[layer->mappingsSize-1];
  321. layer->mappingsSize--;
  322. j++;
  323. i--; // iterate over the same index again
  324. js[j].type = UA_JOBTYPE_DELAYEDMETHODCALL;
  325. js[j].job.methodCall.method = FreeConnectionCallback;
  326. js[j].job.methodCall.data = c;
  327. }
  328. j++;
  329. }
  330. *jobs = js;
  331. return j;
  332. }
  333. static UA_Int32 ServerNetworkLayerTCP_stop(UA_ServerNetworkLayer *nl, UA_Job **jobs) {
  334. ServerNetworkLayerTCP *layer = nl->handle;
  335. UA_Job *items = malloc(sizeof(UA_Job) * layer->mappingsSize * 2);
  336. if(!items)
  337. return 0;
  338. for(size_t i = 0; i < layer->mappingsSize; i++) {
  339. socket_close(layer->mappings[i].connection);
  340. items[i*2].type = UA_JOBTYPE_DETACHCONNECTION;
  341. items[i*2].job.closeConnection = layer->mappings[i].connection;
  342. items[(i*2)+1].type = UA_JOBTYPE_DELAYEDMETHODCALL;
  343. items[(i*2)+1].job.methodCall.method = FreeConnectionCallback;
  344. items[(i*2)+1].job.methodCall.data = layer->mappings[i].connection;
  345. }
  346. #ifdef _WIN32
  347. WSACleanup();
  348. #endif
  349. *jobs = items;
  350. return layer->mappingsSize*2;
  351. }
  352. /* run only when the server is stopped */
  353. static void ServerNetworkLayerTCP_deleteMembers(UA_ServerNetworkLayer *nl) {
  354. ServerNetworkLayerTCP *layer = nl->handle;
  355. #ifndef UA_MULTITHREADING
  356. UA_ByteString_deleteMembers(&layer->buffer);
  357. #endif
  358. for(size_t i = 0; i < layer->mappingsSize; i++)
  359. free(layer->mappings[i].connection);
  360. free(layer->mappings);
  361. free(layer);
  362. }
  363. UA_ServerNetworkLayer ServerNetworkLayerTCP_new(UA_ConnectionConfig conf, UA_UInt32 port) {
  364. #ifdef _WIN32
  365. WORD wVersionRequested;
  366. WSADATA wsaData;
  367. wVersionRequested = MAKEWORD(2, 2);
  368. WSAStartup(wVersionRequested, &wsaData);
  369. #endif
  370. UA_ServerNetworkLayer nl;
  371. memset(&nl, 0, sizeof(UA_ServerNetworkLayer));
  372. ServerNetworkLayerTCP *layer = malloc(sizeof(ServerNetworkLayerTCP));
  373. if(!layer){
  374. return nl;
  375. }
  376. layer->conf = conf;
  377. layer->mappingsSize = 0;
  378. layer->mappings = NULL;
  379. layer->port = port;
  380. char hostname[256];
  381. gethostname(hostname, 255);
  382. UA_String_copyprintf("opc.tcp://%s:%d", &nl.discoveryUrl, hostname, port);
  383. #ifndef UA_MULTITHREADING
  384. layer->buffer = (UA_ByteString){.length = conf.maxMessageSize, .data = malloc(conf.maxMessageSize)};
  385. #endif
  386. nl.handle = layer;
  387. nl.start = ServerNetworkLayerTCP_start;
  388. nl.getJobs = ServerNetworkLayerTCP_getJobs;
  389. nl.stop = ServerNetworkLayerTCP_stop;
  390. nl.deleteMembers = ServerNetworkLayerTCP_deleteMembers;
  391. return nl;
  392. }
  393. /***************************/
  394. /* Client NetworkLayer TCP */
  395. /***************************/
  396. static UA_StatusCode ClientNetworkLayerGetBuffer(UA_Connection *connection, UA_ByteString *buf) {
  397. #ifndef UA_MULTITHREADING
  398. *buf = *(UA_ByteString*)connection->handle;
  399. return UA_STATUSCODE_GOOD;
  400. #else
  401. return UA_ByteString_newMembers(buf, connection->remoteConf.recvBufferSize);
  402. #endif
  403. }
  404. static void ClientNetworkLayerReleaseBuffer(UA_Connection *connection, UA_ByteString *buf) {
  405. #ifdef UA_MULTITHREADING
  406. UA_ByteString_deleteMembers(buf);
  407. #endif
  408. }
  409. static void ClientNetworkLayerClose(UA_Connection *connection) {
  410. if(connection->state == UA_CONNECTION_CLOSED)
  411. return;
  412. connection->state = UA_CONNECTION_CLOSED;
  413. socket_close(connection);
  414. #ifndef UA_MULTITHREADING
  415. UA_ByteString_delete(connection->handle);
  416. #endif
  417. }
  418. /* we have no networklayer. instead, attach the reusable buffer to the handle */
  419. UA_Connection ClientNetworkLayerTCP_connect(UA_ConnectionConfig localConf, char *endpointUrl,
  420. UA_Logger *logger) {
  421. UA_Connection connection;
  422. UA_Connection_init(&connection);
  423. connection.localConf = localConf;
  424. #ifndef UA_MULTITHREADING
  425. connection.handle = UA_ByteString_new();
  426. UA_ByteString_newMembers(connection.handle, localConf.maxMessageSize);
  427. #endif
  428. size_t urlLength = strlen(endpointUrl);
  429. if(urlLength < 11 || urlLength >= 512) {
  430. UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Server url size invalid");
  431. return connection;
  432. }
  433. if(strncmp(endpointUrl, "opc.tcp://", 10) != 0) {
  434. UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Server url does not begin with opc.tcp://");
  435. return connection;
  436. }
  437. UA_UInt16 portpos = 9;
  438. UA_UInt16 port = 0;
  439. for(;portpos < urlLength-1; portpos++) {
  440. if(endpointUrl[portpos] == ':') {
  441. port = atoi(&endpointUrl[portpos+1]);
  442. break;
  443. }
  444. }
  445. if(port == 0) {
  446. UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Port invalid");
  447. return connection;
  448. }
  449. char hostname[512];
  450. for(int i=10; i < portpos; i++)
  451. hostname[i-10] = endpointUrl[i];
  452. hostname[portpos-10] = 0;
  453. #ifdef _WIN32
  454. WORD wVersionRequested;
  455. WSADATA wsaData;
  456. wVersionRequested = MAKEWORD(2, 2);
  457. WSAStartup(wVersionRequested, &wsaData);
  458. if((connection.sockfd = socket(PF_INET, SOCK_STREAM,0)) == (UA_Int32)INVALID_SOCKET) {
  459. #else
  460. if((connection.sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  461. #endif
  462. UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Could not create socket");
  463. return connection;
  464. }
  465. struct hostent *server = gethostbyname(hostname);
  466. if(server == NULL) {
  467. UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "DNS lookup of %s failed", hostname);
  468. return connection;
  469. }
  470. struct sockaddr_in server_addr;
  471. memset(&server_addr, 0, sizeof(server_addr));
  472. memcpy((char *)&server_addr.sin_addr.s_addr, (char *)server->h_addr_list[0], server->h_length);
  473. server_addr.sin_family = AF_INET;
  474. server_addr.sin_port = htons(port);
  475. if(connect(connection.sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
  476. UA_LOG_WARNING((*logger), UA_LOGCATEGORY_COMMUNICATION, "Connection failed");
  477. return connection;
  478. }
  479. connection.state = UA_CONNECTION_OPENING;
  480. //socket_set_nonblocking(connection.sockfd);
  481. connection.write = socket_write;
  482. connection.recv = socket_recv;
  483. connection.close = ClientNetworkLayerClose;
  484. connection.getBuffer = ClientNetworkLayerGetBuffer;
  485. connection.releaseBuffer = ClientNetworkLayerReleaseBuffer;
  486. return connection;
  487. }