forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathua_network_tcp.c
757 lines (691 loc) · 26.3 KB
/
ua_network_tcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501)
/* Assume the target is newer than Windows XP */
# undef WINVER
# undef _WIN32_WINDOWS
# undef _WIN32_WINNT
# define WINVER 0x0501
# define _WIN32_WINDOWS 0x0501
# define _WIN32_WINNT 0x0501
#endif
#include "ua_network_tcp.h"
#include <stdlib.h> // malloc, free
#include <stdio.h> // snprintf
#include <string.h> // memset
#include <errno.h>
#ifdef _WIN32
# ifndef __clang__
# include <malloc.h>
# endif
/* Fix redefinition of SLIST_ENTRY on mingw winnt.h */
# ifdef SLIST_ENTRY
# undef SLIST_ENTRY
# endif
/* inet_ntoa is deprecated on MSVC but used for compatibility */
# define _WINSOCK_DEPRECATED_NO_WARNINGS
# include <winsock2.h>
# include <ws2tcpip.h>
# define CLOSESOCKET(S) closesocket((SOCKET)S)
# define ssize_t int
# define WIN32_INT (int)
#else
# define CLOSESOCKET(S) close(S)
# define SOCKET int
# define WIN32_INT
# include <arpa/inet.h>
# include <netinet/in.h>
# include <sys/select.h>
# include <sys/ioctl.h>
# include <fcntl.h>
# include <unistd.h> // read, write, close
# include <netdb.h>
# ifdef __QNX__
# include <sys/socket.h>
# endif
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
# include <sys/param.h>
# if defined(BSD)
# include<sys/socket.h>
# endif
#endif
# ifndef __CYGWIN__
# include <netinet/tcp.h>
# endif
#endif
/* unsigned int for windows and workaround to a glibc bug */
/* Additionally if GNU_LIBRARY is not defined, it may be using musl libc (e.g. Docker Alpine) */
#if defined(_WIN32) || defined(__OpenBSD__) || \
(defined(__GNU_LIBRARY__) && (__GNU_LIBRARY__ <= 6) && \
(__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 16) || \
!defined(__GNU_LIBRARY__))
# define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
# define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
#else
# define UA_fd_set(fd, fds) FD_SET(fd, fds)
# define UA_fd_isset(fd, fds) FD_ISSET(fd, fds)
#endif
#ifdef UA_ENABLE_MULTITHREADING
# include <urcu/uatomic.h>
#endif
#ifdef _WIN32
#define errno__ WSAGetLastError()
# define INTERRUPTED WSAEINTR
# define WOULDBLOCK WSAEWOULDBLOCK
# define AGAIN WSAEWOULDBLOCK
#else
# define errno__ errno
# define INTERRUPTED EINTR
# define WOULDBLOCK EWOULDBLOCK
# define AGAIN EAGAIN
#endif
/****************************/
/* Generic Socket Functions */
/****************************/
static void
socket_close(UA_Connection *connection) {
connection->state = UA_CONNECTION_CLOSED;
shutdown((SOCKET)connection->sockfd,2);
CLOSESOCKET(connection->sockfd);
}
static UA_StatusCode
socket_write(UA_Connection *connection, UA_ByteString *buf) {
size_t nWritten = 0;
do {
ssize_t n = 0;
do {
/* If the OS throws EMSGSIZE, force a smaller packet size:
* size_t bytes_to_send = buf->length - nWritten > 1024 ? 1024 : buf->length - nWritten; */
size_t bytes_to_send = buf->length - nWritten;
n = send((SOCKET)connection->sockfd, (const char*)buf->data + nWritten,
WIN32_INT bytes_to_send, 0);
if(n < 0 && errno__ != INTERRUPTED && errno__ != AGAIN) {
connection->close(connection);
socket_close(connection);
UA_ByteString_deleteMembers(buf);
return UA_STATUSCODE_BADCONNECTIONCLOSED;
}
} while(n < 0);
nWritten += (size_t)n;
} while(nWritten < buf->length);
UA_ByteString_deleteMembers(buf);
return UA_STATUSCODE_GOOD;
}
static UA_StatusCode
socket_recv(UA_Connection *connection, UA_ByteString *response, UA_UInt32 timeout) {
response->data = malloc(connection->localConf.recvBufferSize);
if(!response->data) {
response->length = 0;
return UA_STATUSCODE_BADOUTOFMEMORY; /* not enough memory retry */
}
if(timeout > 0) {
/* currently, only the client uses timeouts */
#ifndef _WIN32
UA_UInt32 timeout_usec = timeout * 1000;
# ifdef __APPLE__
struct timeval tmptv = {(long int)(timeout_usec / 1000000), timeout_usec % 1000000};
# else
struct timeval tmptv = {(long int)(timeout_usec / 1000000), (long int)(timeout_usec % 1000000)};
# endif
int ret = setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO,
(const char *)&tmptv, sizeof(struct timeval));
#else
DWORD timeout_dw = timeout;
int ret = setsockopt(connection->sockfd, SOL_SOCKET, SO_RCVTIMEO,
(const char*)&timeout_dw, sizeof(DWORD));
#endif
if(0 != ret) {
UA_ByteString_deleteMembers(response);
socket_close(connection);
return UA_STATUSCODE_BADCONNECTIONCLOSED;
}
}
#ifdef __CYGWIN__
/* Workaround for https://cygwin.com/ml/cygwin/2013-07/msg00107.html */
ssize_t ret;
if(timeout > 0) {
fd_set fdset;
FD_ZERO(&fdset);
UA_fd_set(connection->sockfd, &fdset);
UA_UInt32 timeout_usec = timeout * 1000;
struct timeval tmptv = {(long int)(timeout_usec / 1000000),
(long int)(timeout_usec % 1000000)};
int retval = select(connection->sockfd+1, &fdset, NULL, NULL, &tmptv);
if(retval && UA_fd_isset(connection->sockfd, &fdset)) {
ret = recv(connection->sockfd, (char*)response->data,
connection->localConf.recvBufferSize, 0);
} else {
ret = 0;
}
} else {
ret = recv(connection->sockfd, (char*)response->data,
connection->localConf.recvBufferSize, 0);
}
#else
ssize_t ret = recv(connection->sockfd, (char*)response->data,
connection->localConf.recvBufferSize, 0);
#endif
/* server has closed the connection */
if(ret == 0) {
UA_ByteString_deleteMembers(response);
socket_close(connection);
return UA_STATUSCODE_BADCONNECTIONCLOSED;
}
/* error case */
if(ret < 0) {
UA_ByteString_deleteMembers(response);
if(errno__ == INTERRUPTED || (timeout > 0) ?
false : (errno__ == EAGAIN || errno__ == WOULDBLOCK))
return UA_STATUSCODE_GOOD; /* statuscode_good but no data -> retry */
socket_close(connection);
return UA_STATUSCODE_BADCONNECTIONCLOSED;
}
/* default case */
response->length = (size_t)ret;
return UA_STATUSCODE_GOOD;
}
static UA_StatusCode socket_set_nonblocking(SOCKET sockfd) {
#ifdef _WIN32
u_long iMode = 1;
if(ioctlsocket(sockfd, FIONBIO, &iMode) != NO_ERROR)
return UA_STATUSCODE_BADINTERNALERROR;
#else
int opts = fcntl(sockfd, F_GETFL);
if(opts < 0 || fcntl(sockfd, F_SETFL, opts|O_NONBLOCK) < 0)
return UA_STATUSCODE_BADINTERNALERROR;
#endif
return UA_STATUSCODE_GOOD;
}
static void FreeConnectionCallback(UA_Server *server, void *ptr) {
UA_Connection_deleteMembers((UA_Connection*)ptr);
free(ptr);
}
/***************************/
/* Server NetworkLayer TCP */
/***************************/
/**
* For the multithreaded mode, assume a single thread that periodically "gets
* work" from the network layer. In addition, several worker threads are
* asynchronously calling into the callbacks of the UA_Connection that holds a
* single connection.
*
* Creating a connection: When "GetJobs" encounters a new connection, it creates
* a UA_Connection with the socket information. This is added to the mappings
* array that links sockets to UA_Connection structs.
*
* Reading data: In "GetJobs", we listen on the sockets in the mappings array.
* If data arrives (or the connection closes), a WorkItem is created that
* carries the work and a pointer to the connection.
*
* Closing a connection: Closing can happen in two ways. Either it is triggered
* by the server in an asynchronous callback. Or the connection is close by the
* client and this is detected in "GetJobs". The server needs to do some
* internal cleanups (close attached securechannels, etc.). So even when a
* closed connection is detected in "GetJobs", we trigger the server to close
* the connection (with a WorkItem) and continue from the callback.
*
* - Server calls close-callback: We close the socket, set the connection-state
* to closed and add the connection to a linked list from which it is deleted
* later. The connection cannot be freed right away since other threads might
* still be using it.
*
* - GetJobs: We remove the connection from the mappings array. In the
* non-multithreaded case, the connection is freed. For multithreading, we
* return a workitem that is delayed, i.e. that is called only after all
* workitems created before are finished in all threads. This workitems
* contains a callback that goes through the linked list of connections to be
* freed. */
#define MAXBACKLOG 100
typedef struct {
UA_ConnectionConfig conf;
UA_UInt16 port;
UA_Logger logger; // Set during start
/* open sockets and connections */
UA_Int32 serversockfd;
size_t mappingsSize;
struct ConnectionMapping {
UA_Connection *connection;
UA_Int32 sockfd;
} *mappings;
} ServerNetworkLayerTCP;
static UA_StatusCode
ServerNetworkLayerGetSendBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
if(length > connection->remoteConf.recvBufferSize)
return UA_STATUSCODE_BADCOMMUNICATIONERROR;
return UA_ByteString_allocBuffer(buf, length);
}
static void
ServerNetworkLayerReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) {
UA_ByteString_deleteMembers(buf);
}
static void
ServerNetworkLayerReleaseRecvBuffer(UA_Connection *connection, UA_ByteString *buf) {
UA_ByteString_deleteMembers(buf);
}
/* after every select, we need to reset the sockets we want to listen on */
static UA_Int32
setFDSet(ServerNetworkLayerTCP *layer, fd_set *fdset) {
FD_ZERO(fdset);
UA_fd_set(layer->serversockfd, fdset);
UA_Int32 highestfd = layer->serversockfd;
for(size_t i = 0; i < layer->mappingsSize; ++i) {
UA_fd_set(layer->mappings[i].sockfd, fdset);
if(layer->mappings[i].sockfd > highestfd)
highestfd = layer->mappings[i].sockfd;
}
return highestfd;
}
/* callback triggered from the server */
static void
ServerNetworkLayerTCP_closeConnection(UA_Connection *connection) {
#ifdef UA_ENABLE_MULTITHREADING
if(uatomic_xchg(&connection->state, UA_CONNECTION_CLOSED) == UA_CONNECTION_CLOSED)
return;
#else
if(connection->state == UA_CONNECTION_CLOSED)
return;
connection->state = UA_CONNECTION_CLOSED;
#endif
#if UA_LOGLEVEL <= 300
//cppcheck-suppress unreadVariable
ServerNetworkLayerTCP *layer = connection->handle;
UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"Connection %i | Force closing the connection",
connection->sockfd);
#endif
/* only "shutdown" here. this triggers the select, where the socket is
"closed" in the mainloop */
shutdown(connection->sockfd, 2);
}
/* call only from the single networking thread */
static UA_StatusCode
ServerNetworkLayerTCP_add(ServerNetworkLayerTCP *layer, UA_Int32 newsockfd) {
UA_Connection *c = malloc(sizeof(UA_Connection));
if(!c)
return UA_STATUSCODE_BADINTERNALERROR;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(struct sockaddr_in);
int res = getpeername(newsockfd, (struct sockaddr*)&addr, &addrlen);
if(res == 0) {
UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"Connection %i | New connection over TCP from %s:%d",
newsockfd, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
} else {
UA_LOG_WARNING(layer->logger, UA_LOGCATEGORY_NETWORK,
"Connection %i | New connection over TCP, "
"getpeername failed with errno %i", newsockfd, errno);
}
memset(c, 0, sizeof(UA_Connection));
c->sockfd = newsockfd;
c->handle = layer;
c->localConf = layer->conf;
c->remoteConf = layer->conf;
c->send = socket_write;
c->close = ServerNetworkLayerTCP_closeConnection;
c->getSendBuffer = ServerNetworkLayerGetSendBuffer;
c->releaseSendBuffer = ServerNetworkLayerReleaseSendBuffer;
c->releaseRecvBuffer = ServerNetworkLayerReleaseRecvBuffer;
c->state = UA_CONNECTION_OPENING;
struct ConnectionMapping *nm;
nm = realloc(layer->mappings,
sizeof(struct ConnectionMapping)*(layer->mappingsSize+1));
if(!nm) {
UA_LOG_ERROR(layer->logger, UA_LOGCATEGORY_NETWORK,
"No memory for a new Connection");
free(c);
return UA_STATUSCODE_BADINTERNALERROR;
}
layer->mappings = nm;
layer->mappings[layer->mappingsSize].connection = c;
layer->mappings[layer->mappingsSize].sockfd = newsockfd;
++layer->mappingsSize;
return UA_STATUSCODE_GOOD;
}
static UA_StatusCode
ServerNetworkLayerTCP_start(UA_ServerNetworkLayer *nl, UA_Logger logger) {
ServerNetworkLayerTCP *layer = nl->handle;
layer->logger = logger;
/* get the discovery url from the hostname */
UA_String du = UA_STRING_NULL;
char hostname[256];
if(gethostname(hostname, 255) == 0) {
char discoveryUrl[256];
#ifndef _MSC_VER
du.length = (size_t)snprintf(discoveryUrl, 255, "opc.tcp://%s:%d",
hostname, layer->port);
#else
du.length = (size_t)_snprintf_s(discoveryUrl, 255, _TRUNCATE,
"opc.tcp://%s:%d", hostname, layer->port);
#endif
du.data = (UA_Byte*)discoveryUrl;
}
UA_String_copy(&du, &nl->discoveryUrl);
/* Create the server socket */
SOCKET newsock = socket(PF_INET, SOCK_STREAM, 0);
#ifdef _WIN32
if(newsock == INVALID_SOCKET)
#else
if(newsock < 0)
#endif
{
UA_LOG_WARNING(layer->logger, UA_LOGCATEGORY_NETWORK,
"Error opening the server socket");
return UA_STATUSCODE_BADINTERNALERROR;
}
/* Set socket options */
int optval = 1;
if(setsockopt(newsock, SOL_SOCKET, SO_REUSEADDR,
(const char *)&optval, sizeof(optval)) == -1 ||
socket_set_nonblocking(newsock) != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(layer->logger, UA_LOGCATEGORY_NETWORK,
"Error during setting of server socket options");
CLOSESOCKET(newsock);
return UA_STATUSCODE_BADINTERNALERROR;
}
/* Bind socket to address */
const struct sockaddr_in serv_addr = {
.sin_family = AF_INET, .sin_addr.s_addr = INADDR_ANY,
.sin_port = htons(layer->port), .sin_zero = {0}};
if(bind(newsock, (const struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
UA_LOG_WARNING(layer->logger, UA_LOGCATEGORY_NETWORK,
"Error during binding of the server socket");
CLOSESOCKET(newsock);
return UA_STATUSCODE_BADINTERNALERROR;
}
/* Start listening */
if(listen(newsock, MAXBACKLOG) < 0) {
UA_LOG_WARNING(layer->logger, UA_LOGCATEGORY_NETWORK,
"Error listening on server socket");
CLOSESOCKET(newsock);
return UA_STATUSCODE_BADINTERNALERROR;
}
layer->serversockfd = (UA_Int32)newsock; /* cast on win32 */
UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"TCP network layer listening on %.*s",
nl->discoveryUrl.length, nl->discoveryUrl.data);
return UA_STATUSCODE_GOOD;
}
static size_t
removeClosedConnections(ServerNetworkLayerTCP *layer, UA_Job *js) {
size_t c = 0;
for(size_t i = 0; i < layer->mappingsSize; ++i) {
if(layer->mappings[i].connection &&
layer->mappings[i].connection->state != UA_CONNECTION_CLOSED)
continue;
/* the socket was closed from remote */
UA_Connection *conn = layer->mappings[i].connection;
js[c].type = UA_JOBTYPE_DETACHCONNECTION;
js[c].job.closeConnection = conn;
layer->mappings[i] = layer->mappings[layer->mappingsSize-1];
--layer->mappingsSize;
++c;
js[c].type = UA_JOBTYPE_METHODCALL_DELAYED;
js[c].job.methodCall.method = FreeConnectionCallback;
js[c].job.methodCall.data = conn;
++c;
}
return c;
}
static size_t
ServerNetworkLayerTCP_getJobs(UA_ServerNetworkLayer *nl, UA_Job **jobs,
UA_UInt16 timeout) {
/* Every open socket can generate two jobs */
ServerNetworkLayerTCP *layer = nl->handle;
UA_Job *js = malloc(sizeof(UA_Job) * (size_t)((layer->mappingsSize * 2)));
if(!js)
return UA_STATUSCODE_BADOUTOFMEMORY;
/* Remove closed sockets */
size_t totalJobs = removeClosedConnections(layer, js);
/* Listen on open sockets (including the server) */
fd_set fdset, errset;
UA_Int32 highestfd = setFDSet(layer, &fdset);
setFDSet(layer, &errset);
struct timeval tmptv = {0, timeout * 1000};
UA_Int32 resultsize = select(highestfd+1, &fdset, NULL, &errset, &tmptv);
if(totalJobs == 0 && resultsize <= 0) {
free(js);
*jobs = NULL;
return 0;
}
/* Accept new connection via the server socket (can only be a single one) */
if(UA_fd_isset(layer->serversockfd, &fdset)) {
--resultsize;
SOCKET newsockfd = accept((SOCKET)layer->serversockfd, NULL, NULL);
#ifdef _WIN32
if(newsockfd != INVALID_SOCKET)
#else
if(newsockfd >= 0)
#endif
{
socket_set_nonblocking(newsockfd);
/* Do not merge packets on the socket (disable Nagle's algorithm) */
int i = 1;
setsockopt(newsockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
ServerNetworkLayerTCP_add(layer, (UA_Int32)newsockfd);
}
}
/* Read from established sockets */
UA_ByteString buf = UA_BYTESTRING_NULL;
size_t j = 0;
for(size_t i = 0; i < layer->mappingsSize && j < (size_t)resultsize; ++i) {
if(!UA_fd_isset(layer->mappings[i].sockfd, &errset) &&
!UA_fd_isset(layer->mappings[i].sockfd, &fdset))
continue;
UA_StatusCode retval = socket_recv(layer->mappings[i].connection, &buf, 0);
if(retval == UA_STATUSCODE_GOOD) {
js[totalJobs + j].job.binaryMessage.connection = layer->mappings[i].connection;
js[totalJobs + j].job.binaryMessage.message = buf;
js[totalJobs + j].type = UA_JOBTYPE_BINARYMESSAGE_NETWORKLAYER;
++j;
} else if (retval == UA_STATUSCODE_BADCONNECTIONCLOSED) {
UA_Connection *c = layer->mappings[i].connection;
UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"Connection %i | Connection closed from remote", c->sockfd);
/* the socket was closed from remote */
js[totalJobs + j].type = UA_JOBTYPE_DETACHCONNECTION;
js[totalJobs + j].job.closeConnection = c;
layer->mappings[i] = layer->mappings[layer->mappingsSize-1];
--layer->mappingsSize;
++totalJobs; /* increase j only once */
js[totalJobs + j].type = UA_JOBTYPE_METHODCALL_DELAYED;
js[totalJobs + j].job.methodCall.method = FreeConnectionCallback;
js[totalJobs + j].job.methodCall.data = c;
++j;
}
}
totalJobs += j;
if(totalJobs == 0) {
free(js);
js = NULL;
}
*jobs = js;
return totalJobs;
}
static size_t
ServerNetworkLayerTCP_stop(UA_ServerNetworkLayer *nl, UA_Job **jobs) {
ServerNetworkLayerTCP *layer = nl->handle;
UA_LOG_INFO(layer->logger, UA_LOGCATEGORY_NETWORK,
"Shutting down the TCP network layer with %d open connection(s)",
layer->mappingsSize);
shutdown((SOCKET)layer->serversockfd,2);
CLOSESOCKET(layer->serversockfd);
UA_Job *items = malloc(sizeof(UA_Job) * layer->mappingsSize * 2);
if(!items)
return 0;
for(size_t i = 0; i < layer->mappingsSize; ++i) {
socket_close(layer->mappings[i].connection);
items[i*2].type = UA_JOBTYPE_DETACHCONNECTION;
items[i*2].job.closeConnection = layer->mappings[i].connection;
items[(i*2)+1].type = UA_JOBTYPE_METHODCALL_DELAYED;
items[(i*2)+1].job.methodCall.method = FreeConnectionCallback;
items[(i*2)+1].job.methodCall.data = layer->mappings[i].connection;
}
#ifdef _WIN32
WSACleanup();
#endif
*jobs = items;
return layer->mappingsSize*2;
}
/* run only when the server is stopped */
static void ServerNetworkLayerTCP_deleteMembers(UA_ServerNetworkLayer *nl) {
ServerNetworkLayerTCP *layer = nl->handle;
free(layer->mappings);
free(layer);
UA_String_deleteMembers(&nl->discoveryUrl);
}
UA_ServerNetworkLayer
UA_ServerNetworkLayerTCP(UA_ConnectionConfig conf, UA_UInt16 port) {
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
#endif
UA_ServerNetworkLayer nl;
memset(&nl, 0, sizeof(UA_ServerNetworkLayer));
ServerNetworkLayerTCP *layer = calloc(1,sizeof(ServerNetworkLayerTCP));
if(!layer)
return nl;
layer->conf = conf;
layer->port = port;
nl.handle = layer;
nl.start = ServerNetworkLayerTCP_start;
nl.getJobs = ServerNetworkLayerTCP_getJobs;
nl.stop = ServerNetworkLayerTCP_stop;
nl.deleteMembers = ServerNetworkLayerTCP_deleteMembers;
return nl;
}
/***************************/
/* Client NetworkLayer TCP */
/***************************/
static UA_StatusCode
ClientNetworkLayerGetBuffer(UA_Connection *connection, size_t length,
UA_ByteString *buf) {
if(length > connection->remoteConf.recvBufferSize)
return UA_STATUSCODE_BADCOMMUNICATIONERROR;
if(connection->state == UA_CONNECTION_CLOSED)
return UA_STATUSCODE_BADCONNECTIONCLOSED;
return UA_ByteString_allocBuffer(buf, connection->remoteConf.recvBufferSize);
}
static void
ClientNetworkLayerReleaseBuffer(UA_Connection *connection, UA_ByteString *buf) {
UA_ByteString_deleteMembers(buf);
}
static void
ClientNetworkLayerClose(UA_Connection *connection) {
#ifdef UA_ENABLE_MULTITHREADING
if(uatomic_xchg(&connection->state, UA_CONNECTION_CLOSED) == UA_CONNECTION_CLOSED)
return;
#else
if(connection->state == UA_CONNECTION_CLOSED)
return;
connection->state = UA_CONNECTION_CLOSED;
#endif
socket_close(connection);
}
/* we have no networklayer. instead, attach the reusable buffer to the handle */
UA_Connection
UA_ClientConnectionTCP(UA_ConnectionConfig conf, const char *endpointUrl,
UA_Logger logger) {
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
#endif
UA_Connection connection;
memset(&connection, 0, sizeof(UA_Connection));
connection.state = UA_CONNECTION_OPENING;
connection.localConf = conf;
connection.remoteConf = conf;
connection.send = socket_write;
connection.recv = socket_recv;
connection.close = ClientNetworkLayerClose;
connection.getSendBuffer = ClientNetworkLayerGetBuffer;
connection.releaseSendBuffer = ClientNetworkLayerReleaseBuffer;
connection.releaseRecvBuffer = ClientNetworkLayerReleaseBuffer;
char hostname[512];
UA_UInt16 port = 0;
const char *path = NULL;
UA_StatusCode parse_retval = UA_EndpointUrl_split(endpointUrl, hostname, &port, &path);
if(parse_retval != UA_STATUSCODE_GOOD) {
if(parse_retval == UA_STATUSCODE_BADOUTOFRANGE)
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"Server url is invalid: %s", endpointUrl);
else if(parse_retval == UA_STATUSCODE_BADATTRIBUTEIDINVALID)
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"Server url does not begin with 'opc.tcp://' '%s'",
endpointUrl);
return connection;
}
if(port == 0) {
port = 4840;
UA_LOG_INFO(logger, UA_LOGCATEGORY_NETWORK,
"No port defined, using standard port %d", port);
}
struct addrinfo hints, *server;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
char portStr[6];
#ifndef _MSC_VER
snprintf(portStr, 6, "%d", port);
#else
_snprintf_s(portStr, 6, _TRUNCATE, "%d", port);
#endif
int error = getaddrinfo(hostname, portStr, &hints, &server);
if(error != 0 || !server) {
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"DNS lookup of %s failed with error %s",
hostname, gai_strerror(error));
return connection;
}
/* Get a socket */
SOCKET clientsockfd = socket(server->ai_family, server->ai_socktype,
server->ai_protocol);
#ifdef _WIN32
if(clientsockfd == INVALID_SOCKET) {
#else
if(clientsockfd < 0) {
#endif
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"Could not create client socket");
freeaddrinfo(server);
return connection;
}
/* Connect to the server */
connection.sockfd = (UA_Int32)clientsockfd; /* cast for win32 */
error = connect(clientsockfd, server->ai_addr, WIN32_INT server->ai_addrlen);
freeaddrinfo(server);
if(error < 0) {
ClientNetworkLayerClose(&connection);
#ifdef _WIN32
wchar_t *s = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&s, 0, NULL);
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"Connection to %s failed. Error: %d: %S",
endpointUrl, WSAGetLastError(), s);
LocalFree(s);
#else
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"Connection to %s failed. Error: %d: %s",
endpointUrl, errno, strerror(errno));
#endif
return connection;
}
#ifdef SO_NOSIGPIPE
int val = 1;
int sso_result = setsockopt(connection.sockfd,
SOL_SOCKET, SO_NOSIGPIPE,
(void*)&val, sizeof(val));
if(sso_result < 0)
UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK,
"Couldn't set SO_NOSIGPIPE");
#endif
return connection;
}