summaryrefslogtreecommitdiff
path: root/src/net/tcp.c
blob: 20ab3aabdf6f4253901ab342d7a5776f8e967dcf (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef __WIN32__
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#else /* __WIN32__ */
#include <windows.h>
#include <wininet.h>
#endif /* __WIN32__ */

#include <unistd.h>
#include <assert.h>

#include "tcp.h"

sock_tcp_t *sock_tcp_new(void)
{
	sock_tcp_t *new;

	new = malloc(sizeof(sock_tcp_t));
	memset(new, 0, sizeof(sock_tcp_t));

	return new;
}

void sock_tcp_destroy(sock_tcp_t *p)
{
	assert(p != NULL);
	free(p);
}

static int getProto(char *str)
{
	if (strstr(str, ".") != NULL)
		return PROTO_TCPv4;
	if (strstr(str, ":") != NULL)
		return PROTO_TCPv6;

	fatal("Network protocol not detected");
	return -1;
}

sock_tcp_t *sock_tcp_bind(char *address, int port)
{
	sock_tcp_t *new;
	unsigned long param_setsock = 1;
	int len;
	int ret;

	assert(port > 0 && port < 65535);

	new = sock_tcp_new();
	new->proto = getProto(address);
	ret = -1;	/* no warnings */

	assert(new != NULL);

	if (new->proto == PROTO_TCPv4) {
		new->sock = socket(AF_INET, SOCK_STREAM, 0);
	}
#ifdef SUPPORT_IPv6
	if (new->proto == PROTO_TCPv6) {
		new->sock = socket(AF_INET6, SOCK_STREAM, 0);
	}
#endif

	if (new->sock < 0) {
		error("Unable to create TCP socket");
		sock_tcp_destroy(new);
#ifdef __WIN32__
		WSACleanup();
#endif
		return NULL;
	}

	setsockopt(new->sock, SOL_SOCKET, SO_REUSEADDR, (char *) &param_setsock, sizeof(param_setsock));

	if (new->proto == PROTO_TCPv4) {
		new->sockAddr.sin_family = AF_INET;
#ifndef __WIN32__
		inet_pton(AF_INET, address, &(new->sockAddr.sin_addr));
#else
		new->sockAddr.sin_addr.s_addr = inet_addr(address);
#endif
		new->sockAddr.sin_port = htons(port);

		len = sizeof(new->sockAddr);
		ret = bind(new->sock, (struct sockaddr *) &new->sockAddr, len);
	}
#ifdef SUPPORT_IPv6
	if (new->proto == PROTO_TCPv6) {
		new->sockAddr6.sin6_family = AF_INET6;
		/*new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);*/
#ifndef __WIN32__
		inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr));
#else /* __WIN32__ */
		new->sockAddr6.sin6_addr.s_addr = inet_addr(address);
#endif /* __WIN32__ */
		new->sockAddr6.sin6_port = htons(port);

		len = sizeof(new->sockAddr6);
		ret = bind(new->sock, (struct sockaddr *) &new->sockAddr6, len);
	}
#endif /* SUPPORT_IPv6 */

	if (ret < 0) {
		error("Unable to bind to TCP port [%d]", port);
		sock_tcp_destroy(new);
#ifdef __WIN32__
		WSACleanup();
#endif
		return NULL;
	}

	listen(new->sock, 5);

	return new;
}

sock_tcp_t *sock_tcp_accept(sock_tcp_t *p)
{
	sock_tcp_t *new;
	int client_len;

	assert(p != NULL);
	assert(p->sock >= 0);

	new = sock_tcp_new();
	new->proto = p->proto;

	if (new->proto == PROTO_TCPv4) {
		client_len = sizeof(new->sockAddr);
#ifndef __WIN32__
		new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr, (socklen_t *) &client_len);
#else
		new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr, (long *) &client_len);
#endif
	}
#ifdef SUPPORT_IPv6
	if (new->proto == PROTO_TCPv6) {
		client_len = sizeof(new->sockAddr6);
#ifndef __WIN32__
		new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr6, (socklen_t *) &client_len);
#else /* __WIN32__ */
		new->sock = accept(p->sock, (struct sockaddr *) &new->sockAddr6, (long *) &client_len);
#endif /* __WIN32__ */
	}
#endif /* SUPPORT_IPv6 */

	if (new->sock < 0) {
		sock_tcp_destroy(new);
#ifdef __WIN32__
		WSACleanup();
#endif
		return NULL;
	}

	return new;
}

void sock_tcp_get_ip(sock_tcp_t *p, char *str_ip, int len)
{
	assert(p != NULL);
	assert(str_ip != NULL);

	if (p->proto == PROTO_TCPv4) {
#ifndef __WIN32__
		inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len);
#else
		strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
#endif
	}
#ifdef SUPPORT_IPv6
	if (p->proto == PROTO_TCPv6) {
#ifndef __WIN32__
		inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len);
#else /* __WIN32__ */
		/* NOT TESTED */
		strcpy(str_ip, inet_ntoa(p->sockAddr6.sin6_addr));
#endif /* __WIN32__ */
	}
#endif /* SUPPORT_IPv6 */
}

int sock_tcp_get_port(sock_tcp_t *p)
{
	assert(p != NULL);

	if (p->proto == PROTO_TCPv4) {
		return htons(p->sockAddr.sin_port);
	}
#ifdef SUPPORT_IPv6
	if (p->proto == PROTO_TCPv6) {
		return htons(p->sockAddr6.sin6_port);
	}
#endif

	fatal("Bad IP protocol");

	return -1;
}

sock_tcp_t *sock_tcp_connect(char *ip, int port)
{
	sock_tcp_t *new;
	int len;
	int ret;

	assert(ip != NULL);
	assert(port > 0 && port < 65535);

	new = sock_tcp_new();
	new->proto = getProto(ip);
	ret = -1;	/* no warnings */

	if (new->proto == PROTO_TCPv4) {
		new->sock = socket(AF_INET, SOCK_STREAM, 0);
	}
#ifdef SUPPORT_IPv6
	if (new->proto == PROTO_TCPv6) {
		new->sock = socket(AF_INET6, SOCK_STREAM, 0);
	}
#endif

	if (new->sock < 0) {
		error("Unable to create TCP socket for connecting");
		sock_tcp_destroy(new);
#ifdef __WIN32__
		WSACleanup();
#endif
		return NULL;
	}

	if (new->proto == PROTO_TCPv4) {
		new->sockAddr.sin_family = AF_INET;
		new->sockAddr.sin_addr.s_addr = inet_addr(ip);
		new->sockAddr.sin_port = htons(port);

		len = sizeof(new->sockAddr);
		ret = connect(new->sock, (struct sockaddr *) &new->sockAddr, len);
	}
#ifdef SUPPORT_IPv6
	if (new->proto == PROTO_TCPv6) {
		new->sockAddr6.sin6_family = AF_INET6;
		inet_pton(AF_INET6, ip, &(new->sockAddr6.sin6_addr));
		new->sockAddr6.sin6_port = htons(port);

		len = sizeof(new->sockAddr6);
		ret = connect(new->sock, (struct sockaddr *) &new->sockAddr6, len);
	}
#endif

	if (ret < 0) {
		error("Unable to connect to [%s]:%d", ip, port);
		sock_tcp_destroy(new);
#ifdef __WIN32__
		WSACleanup();
#endif
		return NULL;
	}

	return new;
}

int sock_tcp_disable_nagle(sock_tcp_t *p)
{
	int flag = 1;
	int result;

	result = setsockopt(p->sock,		/* affected socket */
			    IPPROTO_TCP,	/* set option at TCP level */
			    TCP_NODELAY,	/* name of the option */
			    (char *) &flag,	/* the cast is a historical cruft */
			    sizeof(int));	/* length of the option value */

	return result;
}

int sock_tcp_set_non_block(sock_tcp_t *p)
{
	/* set to nonblocking socket mode */
#ifndef __WIN32__
	int oldFlag;

	oldFlag = fcntl(p->sock, F_GETFL, 0);

	if (fcntl(p->sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
		return -1;
	}
#else
	unsigned long arg = 1;
	/* operation is FIONBIO; parameter is a pointer on non-zero number */
	if (ioctlsocket(p->sock, FIONBIO, &arg) == SOCKET_ERROR) {
		WSACleanup();
		return -1;
	}
#endif
	return 0;
}

int sock_tcp_read(sock_tcp_t *p, void *address, int len)
{
	assert(p != NULL);
	assert(address != NULL);

	return read(p->sock, address, len);
}

int sock_tcp_write(sock_tcp_t *p, void *address, int len)
{
	assert(p != NULL);
	assert(address != NULL);

	return write(p->sock, address, len);
}

void sock_tcp_close(sock_tcp_t *p)
{
	assert(p != NULL);

	close(p->sock);
	sock_tcp_destroy(p);
#ifdef __WIN32__
	WSACleanup();
#endif
}