File: tcp.c

package info (click to toggle)
s3d 0.2.2-14
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,572 kB
  • ctags: 5,265
  • sloc: ansic: 20,869; python: 488; perl: 98; makefile: 38; sh: 29
file content (246 lines) | stat: -rw-r--r-- 7,092 bytes parent folder | download | duplicates (3)
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
/*
 * tcp.c
 *
 * Copyright (C) 2004-2011  Simon Wunderlich <sw@simonwunderlich.de>
 *
 * This file is part of s3d, a 3d network display server.
 * See http://s3d.berlios.de/ for more updates.
 *
 * s3d is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * s3d is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with s3d; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "global.h"
#ifdef TCP
#include <errno.h>		/*  errno */
#include <string.h>		/*  memset() */
#ifdef WIN32			/*  sohn wars */
#include <winsock2.h>
#else /* sohn wars */
#include <sys/types.h>		/* fd_set, FD*, socket, accept ... */
#include <sys/socket.h>		/* socket, accept ... */
#include <sys/select.h>		/* fd_set,FD* */
#include <sys/time.h>		/* fd_set,FD* */
#include <netinet/in.h>		/* ntohs(),htons(),htonl(),ntohl() */
#include <arpa/inet.h>		/* network */
#endif /*  sohn wars */
#include <time.h>		/*  select() timeval things */
#include <fcntl.h>		/*  fcntl(),F_SETOWN */
#ifndef F_SETOWN		/* somehow it is not set with -ansi */
#define F_SETOWN 8
#endif
#include <unistd.h>		/*  read(),write(),getpid(),close() */
#include <stdlib.h>		/*  malloc(),free() */
#include <stdint.h>

static int tcp_sockid;

int tcp_init(void)
{
	int yes = 1;
	struct sockaddr_in my_addr;
	s3dprintf(LOW, "server: creating socket");
#ifdef WIN32			/*  sohn wars */
	WSADATA datainfo;
	if (WSAStartup(257, &datainfo) != 0)
		errnf("startup()", 0);
#endif /*  auch sohn */
	if ((tcp_sockid = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		errnf("socket()", errno);

	s3dprintf(LOW, "server: binding my local socket");
	/*  allow addresses to be reused */
	/*  this seems to have something to do with servers using one port */
	if (setsockopt(tcp_sockid, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
		errn("setsockopt(...,SO_REUSEADDR...)", errno);
	memset((char *)&my_addr, 0, sizeof(my_addr));
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(S3D_PORT);
	my_addr.sin_addr.s_addr = htons(INADDR_ANY);
	if (bind(tcp_sockid, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
		errnf("bind()", errno);
	if (listen(tcp_sockid, 5) < 0)
		errnf("listen()", errno);
#ifdef SIGS
	if (fcntl(tcp_sockid, F_SETFL, O_ASYNC | O_NONBLOCK) < 0)
		errnf("fcntl()", errno);
	if (fcntl(tcp_sockid, F_SETOWN, getpid()) < 0)
		errnf("fcntl()", errno);
#endif
	return 0;

}

int tcp_quit(void)
{
	close(tcp_sockid);
#ifdef WIN32
	WSACleanup();
#endif
	return 0;
}

/*  watches the port for new connections */
int tcp_pollport(void)
{
	fd_set fs_port;		/*  filedescriptor set for listening port(s) */
	int newsd;		/*  new socket descriptor */
	struct timeval tv;	/*  time structure */
	struct t_process *new_p;	/*  pointer to new process */
	struct sockaddr client_addr;	/*  new client's address */
	socklen_t clilen = sizeof(client_addr);	/*  length of client's address */

	FD_ZERO(&fs_port);
	FD_SET(tcp_sockid, &fs_port);
select_again:
	tv.tv_sec = tv.tv_usec = 0;
	if (select(FD_SETSIZE, &fs_port, NULL, NULL, &tv) < 0) {
		if (errno == EINTR) {	/*  interruption by some evil signal, just do again :) */
			errn("tcp_pollport():select()", errno);
			goto select_again;	/*  oh no, a goto!! that's evil */
		} else
			errn("tcp_pollport():select()", errno);
	} else if (FD_ISSET(tcp_sockid, &fs_port)) {	/* redundant, I guess */
		s3dprintf(HIGH, "select(): new connection!!");
		if ((newsd = accept(tcp_sockid, (struct sockaddr *)&client_addr, &clilen)) < 0)
			errn("accept()", errno);
		else {
#ifdef SIGS
			if (fcntl(newsd, F_SETFL, O_ASYNC) < 0)
				errnf("fcntl()", errno);
			if (fcntl(newsd, F_SETOWN, getpid()) < 0)
				errnf("fcntl()", errno);
#endif
			new_p = process_add();
			new_p->con_type = CON_TCP;
			new_p->sockid = newsd;
			s3dprintf(HIGH, "registered new connection %d as pid %d", new_p->sockid, new_p->id);
		}
	}
	return 0;
}

/*  this is about looking for new data on the sockets */
/*  returns 1 when there was new data. */
int tcp_pollproc(void)
{
	fd_set fs_proc;		/*  filedescriptor set for listening port(s) */
	struct timeval tv;	/*  time structure */
	struct t_process *p;
	int found = 0;
	int i, unfinished, n, off;
	off = 0;
	do {
		FD_ZERO(&fs_proc);
		unfinished = 0;
		n = 0;
		for (i = off; i < procs_n; i++) {
			p = &procs_p[i];
			if (p->con_type == CON_TCP) {
				FD_SET(p->sockid, &fs_proc);
				n++;
				if (n >= FD_SETSIZE) {	/* don't overflow the setsize! */
					off = i;
					unfinished = 1;
					break;
				}
			}
		}
		/*  maybe having a global fd_set for all the processes would have been better */
		/*  than generating them new in every poll. to be optimized... */
select_again_poll:
		tv.tv_sec = tv.tv_usec = 0;
		if (select(FD_SETSIZE, &fs_proc, NULL, NULL, &tv) == -1) {
			if (errno == EINTR) {
				errn("tcp_pollproc():select()", errno);
				goto select_again_poll;
			} else {
				errn("tcp_pollproc():select()", errno);
			}
		} else {
			/*  data is available */
			for (i = 0; i < procs_n; i++) {
				p = &procs_p[i];
				if (p->con_type == CON_TCP) {
					if (FD_ISSET(p->sockid, &fs_proc)) {
						FD_CLR(p->sockid, &fs_proc);	/*  clear it from the fd */
						tcp_prot_com_in(p);
						found = 1;
					}
				}
			}
		}
	} while (unfinished);
	return found;
}

/* read some data from the line, pushes it into the buffer and calls prot_com_in */
int tcp_prot_com_in(struct t_process *p)
{
	uint16_t length;
	if (3 == tcp_readn(p->sockid, ibuf, 3)) {
		length = ntohs(*((uint16_t *) ((uint8_t *) ibuf + 1)));
		s3dprintf(VLOW, "command %d, length %d", ibuf[0], length);
		if (length > 0) {
			tcp_readn(p->sockid, ibuf + sizeof(int_least32_t), length);	/*  uint16_t is limited to 65536, so  */
			/*  length can't be bigger than that ... lucky */
		}
		prot_com_in(p, ibuf);
	} else {
		s3dprintf(LOW, "tcp_prot_com_in():n_readn():fd seems to be dead (pid %d, sock %d)", p->id, p->sockid);
		process_del(p->id);
	}
	return 0;
}

/*  shamelessly ripped from simple ftp server */
int tcp_readn(int sock, uint8_t * str, int s)
{
	int no_left, no_read;
	no_left = s;
	while (no_left > 0) {
		no_read = read(sock, str, no_left);
		if (no_read < 0) {
			errn("read()", errno);
			return no_read;
		}
		if (no_read == 0)
			break;
		no_left -= no_read;
		str += no_read;
	}
	return s - no_left;
}

int tcp_writen(int sock, uint8_t * str, int s)
{
	int no_left, no_written;
	no_left = s;
	while (no_left > 0) {
		no_written = write(sock, str, no_left);
		if (no_written <= 0) {
			errn("write()", errno);
			return no_written;
		}
		no_left -= no_written;
		str += no_written;
	}
	return s - no_left;
}

int tcp_remove(int sock)
{
	return close(sock);
}
#endif