File: tcp.c

package info (click to toggle)
pmud 0.10-9.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 404 kB
  • ctags: 257
  • sloc: ansic: 2,984; sh: 486; tcl: 290; makefile: 104; perl: 35
file content (312 lines) | stat: -rw-r--r-- 6,287 bytes parent folder | download | duplicates (5)
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
/* 
 * ---------------------------------------------------------------------------
 * $Id: tcp.c,v 1.1.1.1 2001/12/07 11:31:48 sleemburg Exp $
 * ---------------------------------------------------------------------------
 * $Log: tcp.c,v $
 * Revision 1.1.1.1  2001/12/07 11:31:48  sleemburg
 * Initial CVS import of the unreleased pmud-0.8 to apmud (new project name
 * because of a name clash at sourceforge.net).
 *
 * Revision 1.3  1999/01/12 08:29:17  stephan
 * in tcp_open, close the socket if the connection fails.
 *
 * Revision 1.2  1998/09/15 07:13:31  stephan
 * win32 modifications by Job de Haas (job@dsl.nl)
 *
 * Revision 1.1  1998/04/02 12:54:49  stephan
 * Initial revision
 *
 * ---------------------------------------------------------------------------
 */
static char rcs[]="@(#)$Id: tcp.c,v 1.1.1.1 2001/12/07 11:31:48 sleemburg Exp $";

#include <stdio.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef WIN32
#include <winsock.h>
#include <io.h>

#define open	 _open
#define close	_close
#else
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif


#if defined( __STDC__ ) || defined( WIN32 )
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#define FATAL(error)	{ errno = error; return (-1); }

/*
 * ---------------------------------------------------------------------------
 * public interface
 * ---------------------------------------------------------------------------
 */
int tcp_open(char *, char *, unsigned short);
int tcp_bind(unsigned short);
void tcp_listen(int);
int tcp_read(int, const char *, size_t);
int tcp_write(int, const char *, size_t);
#ifdef __STDC__
int tcp_printf(int, char *, ...);
#else
int tcp_printf();
#endif

#ifdef SOCKS
#define connect		Rconnect 
#define getsockname	Rgetsockname 
#define bind		Rbind 
#define accept		Raccept
#define listen		Rlisten 
#define select		Rselect
#endif

#ifdef WIN32
int first = 1;
#endif


int tcp_open(char *host, char *service, unsigned short port)
{
	struct sockaddr_in server;
	struct hostent *hostinfo;
	struct servent *servinfo;
#ifdef WIN32
	SOCKET sock_fd; 
	int iResult;
 
	if(first)
	{
		/* Start the WSA */
		WSADATA wsadata;
		iResult= WSAStartup( MAKEWORD( 1, 1 ), &wsadata );
		if( iResult != 0 )
			FATAL( iResult );

		first = 0;
	}
#else
	int sock_fd; 
#endif

	/* use localhost if none specified */
	if(! host || ! *host)
		host = "localhost";

	/* check arguments */
	if( (!service || !*service ) && port <= 0)
#ifdef WIN32
		FATAL(WSAEDESTADDRREQ)
#else
		FATAL(EDESTADDRREQ)
#endif

	/* initialise the server information */
	memset((char *) &server, 0, sizeof(server));
	server.sin_family      = AF_INET;
	server.sin_port = htons(port);

	/* try to get the server specification, if fail then use port */
	if( service && *service )
	{
		servinfo = getservbyname(service, "tcp");
		if(servinfo)
			server.sin_port = servinfo->s_port;
	}

	/* try to get the address of the host */
	server.sin_addr.s_addr = inet_addr(host);
	if( server.sin_addr.s_addr == INADDR_NONE )
	{
		hostinfo = gethostbyname(host);
		if(!hostinfo)
#ifdef WIN32
			FATAL(WSAEADDRNOTAVAIL)
#else
			FATAL(EADDRNOTAVAIL)
#endif

		memcpy(	(char *) &server.sin_addr,	hostinfo->h_addr, 
			hostinfo->h_length
			);
	}

	/* create socket */
	sock_fd = socket(AF_INET, SOCK_STREAM, 0);
	if( sock_fd < 0 )
		FATAL(errno)


	/* connect to the server */
	if(connect(sock_fd, (struct sockaddr *) &server, sizeof(server)) < 0)
	{
		close(sock_fd);
		FATAL(errno)
	}

	return(sock_fd);
} 

int tcp_bind(unsigned short port)
{
	int fd;
	struct sockaddr_in server;

	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(fd < 0)
		return fd;

	memset((char*)&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(port);

	if(bind(fd, (struct sockaddr*) &server, sizeof(server)) < 0)
	{
		close(fd);
		return -1;
	}
	return fd;
}

void tcp_listen(int fd)
{
	if(fd>=0)
		listen(fd, 5);
}

int tcp_accept(int fd)
{
	return accept(fd, (struct sockaddr*)0, (int*)0);
}

int tcp_close(int fd)
{

#ifdef WIN32
	if (!first) 
	{
		int iResult = WSACleanup();
		if(iResult != 0)
			FATAL(iResult)
	}
#endif

	return close(fd);
}

char *tcp_identify(int fd)
{
	struct sockaddr_in client;
	int len = sizeof(client);
	char *inetaddr;
	struct hostent *remote;
	struct in_addr *remaddr;
	register int i;
	int match;

	if(getpeername(fd, (struct sockaddr*) &client, &len))
		return (char*)0;

	if(client.sin_family != AF_INET)
		return 0;
	
	inetaddr = inet_ntoa(client.sin_addr);
	if(inetaddr <= (char*)0)
		return (char*)0;

	remote=gethostbyaddr((char *)&client.sin_addr, sizeof(client.sin_addr), 
				(int)client.sin_family);
	if(!remote)
		return (char*)0;

	for(match=i=0; remote->h_addr_list[i]; i++)
	{
		remaddr = (struct in_addr*) remote->h_addr_list[i];

		if(remaddr->s_addr == client.sin_addr.s_addr)
			match++;
	}
	return match ? remote->h_name : (char*)0;
}

int tcp_write(int fildes, const char *buffer, size_t nbytes)
{
	register char *data;
	register size_t right;
	register size_t left;

	for(data=(char *)buffer, left=nbytes;;)

		if((right=send(fildes, data, left, 0)) < 0) {
			return right;
		} else
		{
			left -= right;
			data += right;

			/* if right == 0 then EOF, return #bytes written */
			if(right == 0 || left == 0) 
				return nbytes - left;
		}
	/*NOTREACHED*/
}

int tcp_read(int fildes, const char *buffer, size_t nbytes)
{
	register char *data;
	register size_t right;
	register size_t left;

	for(data=(char *)buffer, left=nbytes;;)

		if((right=recv(fildes, data, left, 0)) < 0) {
			return right;
		} else
		{
			left -= right;
			data += right;

			/* if right == 0 then EOF, return #bytes already read */
			if(right == 0 || left == 0) 
				return nbytes - left;
		}
	/*NOTREACHED*/
}

/*VARARGS*/
int
#if defined( __STDC__ ) || defined( WIN32 )
tcp_printf(int sock, char *fmt, ...)
#else
tcp_printf(sock, fmt, va_alist)
	int sock;
	char *fmt;
	va_dcl
#endif
{
	va_list ap;
	static char buf[2*BUFSIZ];
#if defined( __STDC__ ) || defined( WIN32 )
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	vsprintf(buf, fmt, ap);
	va_end(ap);
	return tcp_write(sock, buf, strlen(buf));
}