File: inet.c

package info (click to toggle)
netris 0.52-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch
  • size: 512 kB
  • ctags: 1,100
  • sloc: ansic: 7,657; makefile: 11
file content (272 lines) | stat: -rw-r--r-- 6,632 bytes parent folder | download
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
/*
 * Netris -- A free networked version of T*tris
 * Copyright (C) 1994,1995,1996  Mark H. Weaver <mhw@netris.org>
 * 
 * This program 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.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: inet.c,v 1.18 1996/02/09 08:22:13 mhw Exp $
 */

#include "netris.h"
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>

#define HEADER_SIZE sizeof(netint2[2])

static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event);

static int sock = -1;
static EventGenRec netGen = { NULL, 0, FT_read, -1, NetGenFunc, EM_net };

static char netBuf[64];
static int netBufSize, netBufGoal = HEADER_SIZE;
static int isServer, lostConn, gotEndConn;

ExtFunc void InitNet(void)
{
	lostConn = 0;
	gotEndConn = 0;
	AtExit(CloseNet);
}

ExtFunc int WaitForConnection(char *portStr)
{
	struct sockaddr_storage addr;
	struct sockaddr_in *sa4 = (struct sockaddr_in *) &addr;
	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) &addr;
	struct hostent *host = NULL;
	struct addrinfo hints, *ai, *aiptr;
	char portStrDef[12];
	int sockListen, status;
	socklen_t addrLen;
	int val1;
	struct linger val2;

	if (!portStr || !strlen(portStr)) {
		snprintf(portStrDef, sizeof(portStrDef), "%u", DEFAULT_PORT);
		portStr = portStrDef;
	}
	/* XXX Error checking of port string. */

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET6;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_PASSIVE;

	if ( (status = getaddrinfo(NULL, portStr, &hints, &ai)) ) {
		fprintf(stderr, "getaddrinfo() failed: %s\n",
				gai_strerror(status));
		die("getaddrinfo");
	}

	for (aiptr = ai; aiptr; aiptr = aiptr->ai_next) {
		if ( (sockListen = socket(aiptr->ai_family,
						aiptr->ai_socktype,
						aiptr->ai_protocol))
				< 0 )
			continue;

		val1 = 1;
		setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR,
				(void *)&val1, sizeof(val1));
		val1 = 0;
		setsockopt(sockListen, IPPROTO_IPV6, IPV6_V6ONLY,
				(void *)&val1, sizeof(val1));

		if ( bind(sockListen, aiptr->ai_addr, aiptr->ai_addrlen)
				== 0 )
			if ( listen(sockListen, 1) >= 0 )
				break;

		close(sockListen);
	}

	freeaddrinfo(ai);
	if (aiptr == NULL)
		die("socket");

	addrLen = sizeof(addr);
	sock = accept(sockListen, (struct sockaddr *)&addr, &addrLen);
	if (sock < 0)
		die("accept");
	close(sockListen);
	val2.l_onoff = 1;
	val2.l_linger = 0;
	setsockopt(sock, SOL_SOCKET, SO_LINGER,
			(void *)&val2, sizeof(val2));
	netGen.fd = sock;
	strcpy(opponentHost, "???");
	switch (addr.ss_family) {
		case AF_INET6:
			host = gethostbyaddr((void *)&sa6->sin6_addr,
				sizeof(struct in6_addr), addr.ss_family);
			break;
		case AF_INET:
			host = gethostbyaddr((void *)&sa4->sin_addr,
				sizeof(struct in_addr), addr.ss_family);
	}
	if (host) {
		strncpy(opponentHost, host->h_name, sizeof(opponentHost)-1);
		opponentHost[sizeof(opponentHost)-1] = 0;
	}
	AddEventGen(&netGen);
	isServer = 1;
	return 0;
}

ExtFunc int InitiateConnection(char *hostStr, char *portStr)
{
	struct addrinfo hints, *ai, *aiptr;
	char portStrDef[12];
	int mySock, status;

	if (!portStr || !strlen(portStr)) {
		snprintf(portStrDef, sizeof(portStrDef), "%u", DEFAULT_PORT);
		portStr = portStrDef;
	}
	/* XXX Error checking of port string. */

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;

	if ( (status = getaddrinfo(hostStr, portStr, &hints, &ai)) ) {
		fprintf(stderr, "getaddrinfo() failed: %s\n",
				gai_strerror(status));
		die("getaddrinfo");
	}

	for (aiptr = ai; aiptr; aiptr = aiptr->ai_next) {
again:
		if ( (mySock = socket(aiptr->ai_family, aiptr->ai_socktype,
						aiptr->ai_protocol))
				< 0 )
			continue;
		while ( (status = connect(mySock, aiptr->ai_addr,
						aiptr->ai_addrlen)) < 0
				&& errno == ECONNREFUSED ) {
			close(mySock);
			sleep(1);
			goto again;
		}
		if (status >= 0)
			break;
		/* Failure to connect. */
		close(mySock);
	}

	if (aiptr == NULL) {
		freeaddrinfo(ai);
		die("socket/connect");
	}

	strncpy(opponentHost, aiptr->ai_canonname, sizeof(opponentHost)-1);
	opponentHost[sizeof(opponentHost)-1] = 0;
	freeaddrinfo(ai);
	netGen.fd = sock = mySock;
	AddEventGen(&netGen);
	return 0;
}

static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event)
{
	int result;
	short type, size;
	netint2 data[2];

	result = MyRead(sock, netBuf + netBufSize, netBufGoal - netBufSize);
	if (result < 0) {
		lostConn = 1;
		return E_lostConn;
	}
	netBufSize += result;
	if (netBufSize < netBufGoal)
		return E_none;
	memcpy(data, netBuf, sizeof(data));
	type = ntoh2(data[0]);
	size = ntoh2(data[1]);
	if (size >= sizeof(netBuf))
		fatal("Received an invalid packet (too large), possibly an attempt\n"
			  "  to exploit a vulnerability in versions before 0.52 !");
	netBufGoal = size;
	if (netBufSize < netBufGoal)
		return E_none;
	netBufSize = 0;
	netBufGoal = HEADER_SIZE;
	event->u.net.type = type;
	event->u.net.size = size - HEADER_SIZE;
	event->u.net.data = netBuf + HEADER_SIZE;
	if (type == NP_endConn) {
		gotEndConn = 1;
		return E_lostConn;
	}
	else if (type == NP_byeBye) {
		lostConn = 1;
		return E_lostConn;
	}
	return E_net;
}

ExtFunc void CheckNetConn(void)
{
}

ExtFunc void SendPacket(NetPacketType type, int size, void *data)
{
	netint2 header[2];

	header[0] = hton2(type);
	header[1] = hton2(size + HEADER_SIZE);
	if (MyWrite(sock, header, HEADER_SIZE) != HEADER_SIZE)
		die("write");
	if (size > 0 && data && MyWrite(sock, data, size) != size)
		die("write");
}

ExtFunc void CloseNet(void)
{
	MyEvent event;

	if (sock >= 0) {
		if (!lostConn) {
			SendPacket(NP_endConn, 0, NULL);
			if (isServer) {
				while (!lostConn)
					WaitMyEvent(&event, EM_net);
			}
			else {
				while (!gotEndConn)
					WaitMyEvent(&event, EM_net);
				SendPacket(NP_byeBye, 0, NULL);
			}
		}
		close(sock);
		sock = -1;
	}
	if (netGen.next)
		RemoveEventGen(&netGen);
}

/*
 * vi: ts=4 ai
 * vim: noai si
 */