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
|
#include "simplemotion_private.h"
#include "tcpclient.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#if defined(_WIN32)
#if defined(CM_NONE)
#undef CM_NONE
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#define close closesocket
#define read(SOCKET, BUF, LEN) recv((SOCKET), (BUF), (LEN), 0)
#define write(SOCKET, BUF, LEN) send((SOCKET), (BUF), (LEN), 0)
#ifdef errno
#undef errno
#endif
#define errno (WSAGetLastError())
#ifdef EINPROGRESS
#undef EINPROGRESS
#endif
#define EINPROGRESS WSAEWOULDBLOCK
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#endif
#if defined(_WIN32)
static int initwsa()
{
WORD req;
WSADATA data;
req = MAKEWORD(2, 2);
int err = WSAStartup(req, &data);
if (err != 0)
{
printf("WSAStartup failed\n");
return 0;
}
return 1;
}
#endif
int OpenTCPPort(const char * ip_addr, int port)
{
int sockfd;
struct sockaddr_in server;
struct timeval tv;
fd_set myset;
int res, valopt;
socklen_t lon;
unsigned long arg;
#if defined(_WIN32)
initwsa();
#endif
//Create socket
sockfd = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
if (sockfd == -1)
{
return -1;
}
// Set OFF NAGLE algorithm to reduce latency with small packets
/*
int one = 1;
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&one, sizeof(one));
*/
server.sin_addr.s_addr = inet_addr(ip_addr);
server.sin_family = AF_INET;
server.sin_port = htons(port);
// Set non-blocking when trying to establish the connection
#if !defined(_WIN32)
arg = fcntl(sockfd, F_GETFL, NULL);
arg |= O_NONBLOCK;
fcntl(sockfd, F_SETFL, arg);
#else
arg = 1;
ioctlsocket(sockfd, FIONBIO, &arg);
#endif
res = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
if (res < 0)
{
if (errno == EINPROGRESS)
{
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&myset);
FD_SET((unsigned int)sockfd, &myset);
if (select(sockfd+1, NULL, &myset, NULL, &tv) > 0)
{
lon = sizeof(int);
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
if (valopt)
{
return -1;
}
}
else
{
return -1;
}
}
else
{
return -1;
}
}
// Set to blocking mode again
#if !defined(_WIN32)
arg = fcntl(sockfd, F_GETFL, NULL);
arg &= (~O_NONBLOCK);
fcntl(sockfd, F_SETFL, arg);
#else
arg = 0;
ioctlsocket(sockfd, FIONBIO, &arg);
#endif
return sockfd;
}
// Read bytes from socket
int PollTCPPort(int sockfd, unsigned char *buf, int size)
{
int n;
fd_set input;
FD_ZERO(&input);
FD_SET((unsigned int)sockfd, &input);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = readTimeoutMs * 1000;
n = select(sockfd + 1, &input, NULL, NULL, &timeout);
// Error or timeout
if (n < 1)
{
return(-1);
}
if(!FD_ISSET(sockfd, &input))
{
return(-1);
}
n = read(sockfd, (char*)buf, size);
return(n);
}
int SendTCPByte(int sockfd, unsigned char byte)
{
int n;
n = write(sockfd, (char*)&byte, 1);
if(n<0)
return(1);
return(0);
}
int SendTCPBuf(int sockfd, unsigned char *buf, int size)
{
int sent = write(sockfd, (char*)buf, size);
if (sent != size)
{
return sent;
}
return sent;
}
void CloseTCPport(int sockfd)
{
close(sockfd);
#if defined(_WIN32)
WSACleanup();
#endif
}
//accepted TCP/IP address format is nnn.nnn.nnn.nnn:pppp where n is IP address numbers and p is port number
int validateIpAddress(const char *s, const char **pip_end,
const char **pport_start)
{
int octets = 0;
int ch = 0, prev = 0;
int len = 0;
const char *ip_end = NULL;
const char *port_start = NULL;
while (*s)
{
ch = *s;
if (isdigit(ch))
{
++len;
// Octet len must be 1-3 digits
if (len > 3)
{
return -1;
}
}
else if (ch == '.' && isdigit(prev))
{
++octets;
len = 0;
// No more than 4 octets please
if (octets > 4)
{
return -1;
}
}
else if (ch == ':' && isdigit(prev))
{
++octets;
// We want exactly 4 octets at this point
if (octets != 4)
{
return -1;
}
ip_end = s;
++s;
port_start = s;
while (isdigit((ch = *s)))
++s;
// After port we want the end of the string
if (ch != '\0')
return -1;
// This will skip over the ++s below
continue;
}
else
{
return -1;
}
prev = ch;
++s;
}
// We reached the end of the string and did not encounter the port
if (*s == '\0' && ip_end == NULL)
{
++octets;
ip_end = s;
}
// Check that there are exactly 4 octets
if (octets != 4)
return -1;
if (pip_end)
*pip_end = ip_end;
if (pport_start)
*pport_start = port_start;
return 0;
}
int parseIpAddress(const char *s, char *ip, size_t ipsize, short *port)
{
const char *ip_end, *port_start;
//ip_end and port_start are pointers to memory area of s, not offsets or indexes to s
if (validateIpAddress(s, &ip_end, &port_start) == -1)
return -1;
// If ip=NULL, we just report that the parsing was ok
if (!ip)
return 0;
if (ipsize < (size_t)(ip_end - s + 1))
return -1;
memcpy(ip, s, ip_end - s);
ip[ip_end - s] = '\0';
if (port_start)
{
*port = 0;
while (*port_start)
{
*port = *port * 10 + (*port_start - '0');
++port_start;
}
}
return 0;
}
|