summaryrefslogtreecommitdiff
path: root/minix/commands/swifi/tests/socket.c
blob: 87e5c1a8b04f4a1cd0d065037a0ad762bf3d7474 (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
/*
socket.c

Created:	Feb 2001 by Philip Homburg <philip@f-mnx.phicoh.com>

Open a TCP connection
*/

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/wait.h>

#include <net/hton.h>
#include <net/netlib.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <netdb.h>
#include <net/gen/socket.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <arpa/inet.h>

#define BUF_SIZE	10240

char *progname;
int tcpfd= -1;
char buf[BUF_SIZE];
static int bulk= 0;
static int push= 0;
static int stdout_issocket= 0;
static int timeout;

static void do_conn(char *hostname, char *portname);
static void alrm_conn(int sig);
static void alrm_io(int sig);
static void fullduplex(void);
static void fatal(char *msg, ...);
static void usage(void);

int main(int argc, char *argv[])
{
	int c;
	char *hostname;
	char *portname;
	char *check;
	int B_flag, P_flag, s_flag;
	char *t_arg;

	(progname=strrchr(argv[0],'/')) ? progname++ : (progname=argv[0]);

	B_flag= 0;
	P_flag= 0;
	s_flag= 0;
	t_arg= NULL;
	while (c= getopt(argc, argv, "BPst:?"), c != -1)
	{
		switch(c)
		{
		case 'B':	B_flag= 1; break;
		case 'P':	P_flag= 1; break;
		case 's':	s_flag= 1; break;
		case 't':	t_arg= optarg; break;
		case '?':	usage();
		default:
			fatal("getopt failed: '%c'", c);
		}
	}
	if (t_arg)
	{
		timeout= strtol(t_arg, &check, 0);
		if (check[0] != '\0')
			fatal("unable to parse timeout '%s'\n", t_arg);
		if (timeout <= 0)
			fatal("bad timeout '%d'\n", timeout);
	}
	else
		timeout= 0;

	if (optind+2 != argc)
		usage();
	hostname= argv[optind++];
	portname= argv[optind++];

	bulk= B_flag;
	push= P_flag;
	stdout_issocket= s_flag;

	do_conn(hostname, portname);

	/* XXX */
	if (timeout)
	{
		signal(SIGALRM, alrm_io);
		alarm(timeout);
	}

	fullduplex();
	exit(0);
}

static void do_conn(char *hostname, char *portname)
{
	ipaddr_t addr;
	tcpport_t port;
	struct hostent *he;
	struct servent *se;
	char *tcp_device, *check;
	nwio_tcpconf_t tcpconf;
	nwio_tcpcl_t tcpcl;
	nwio_tcpopt_t tcpopt;

	if (!inet_aton(hostname, (struct in_addr *)&addr))
	{
		he= gethostbyname(hostname);
		if (he == NULL)
			fatal("unknown hostname '%s'", hostname);
		if (he->h_addrtype != AF_INET || he->h_length != sizeof(addr))
			fatal("bad address for '%s'", hostname);
		memcpy(&addr, he->h_addr, sizeof(addr));
	}

	port= strtol(portname, &check, 0);
	if (check[0] != 0)
	{
		se= getservbyname(portname, "tcp");
		if (se == NULL)
			fatal("unkown port '%s'", portname);
		port= ntohs(se->s_port);
	}

	tcp_device= getenv("TCP_DEVICE");
	if (tcp_device == NULL) tcp_device= TCP_DEVICE;

	tcpfd= open(tcp_device, O_RDWR);
	if (tcpfd == -1)
		fatal("unable to open '%s': %s", tcp_device, strerror(errno));
	tcpconf.nwtc_flags= NWTC_EXCL | NWTC_LP_SEL | NWTC_SET_RA |
		NWTC_SET_RP;
	tcpconf.nwtc_remaddr= addr;
	tcpconf.nwtc_remport= htons(port);;
	if (ioctl(tcpfd, NWIOSTCPCONF, &tcpconf) == -1)
		fatal("NWIOSTCPCONF failed: %s", strerror(errno));

	if (timeout)
	{
		signal(SIGALRM, alrm_conn);
		alarm(timeout);
	}

	tcpcl.nwtcl_flags= 0;
	if (ioctl(tcpfd, NWIOTCPCONN, &tcpcl) == -1)
	{
		fatal("unable to connect to %s:%u: %s",
			inet_ntoa(*(struct in_addr *)&addr),
			ntohs(tcpconf.nwtc_remport), strerror(errno));
	}

	alarm(0);

	if (bulk)
	{
		tcpopt.nwto_flags= NWTO_BULK;
		if (ioctl(tcpfd, NWIOSTCPOPT, &tcpopt) == -1)
			fatal("NWIOSTCPOPT failed: %s", strerror(errno));
	}
}

static void alrm_conn(int sig)
{
	fatal("timeout during connect");
}

static void alrm_io(int sig)
{
	fatal("timeout during io");
}

static void fullduplex(void)
{
	pid_t cpid;
	int o, r, s, s_errno, loc;

	cpid= fork();
	switch(cpid)
	{
	case -1:	fatal("fork failed: %s", strerror(errno));
	case 0:
		/* Read from TCP, write to stdout. */
		for (;;)
		{
			r= read(tcpfd, buf, BUF_SIZE);
			if (r == 0)
				break;
			if (r == -1)
			{
				r= errno;
				if (stdout_issocket)
					ioctl(1, NWIOTCPSHUTDOWN, NULL);
				fatal("error reading from TCP conn.: %s",
					strerror(errno));
			}
			s= r;
			for (o= 0; o<s; o += r)
			{
				r= write(1, buf+o, s-o);
				if (r <= 0)
				{
					fatal("error writing to stdout: %s",
						r == 0 ? "EOF" :
						strerror(errno));
				}
			}
		}
		if (stdout_issocket)
		{
			r= ioctl(1, NWIOTCPSHUTDOWN, NULL);
			if (r == -1)
			{
				fatal("NWIOTCPSHUTDOWN failed on stdout: %s",
					strerror(errno));
			}
		}
		exit(0);
	default:
		break;
	}

	/* Read from stdin, write to TCP. */
	for (;;)
	{
		r= read(0, buf, BUF_SIZE);
		if (r == 0)
			break;
		if (r == -1)
		{
			s_errno= errno;
			kill(cpid, SIGTERM);
			fatal("error reading from stdin: %s",
				strerror(s_errno));
		}
		s= r;
		for (o= 0; o<s; o += r)
		{
			r= write(tcpfd, buf+o, s-o);
			if (r <= 0)
			{
				s_errno= errno;
				kill(cpid, SIGTERM);
				fatal("error writing to TCP conn.: %s",
					r == 0 ? "EOF" :
					strerror(s_errno));
			}
		}
		if (push)
			ioctl(tcpfd, NWIOTCPPUSH, NULL);
	}
	if (ioctl(tcpfd, NWIOTCPSHUTDOWN, NULL) == -1)
	{
		s_errno= errno;
		kill(cpid, SIGTERM);
		fatal("unable to shut down TCP conn.: %s", strerror(s_errno));
	}

	r= waitpid(cpid, &loc, 0);
	if (r == -1)
	{
		s_errno= errno;
		kill(cpid, SIGTERM);
		fatal("waitpid failed: %s", strerror(s_errno));
	}
	if (WIFEXITED(loc))
		exit(WEXITSTATUS(loc));
	kill(getpid(), WTERMSIG(loc));
	exit(1);
}

static void fatal(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	fprintf(stderr, "%s: ", progname);
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
	va_end(ap);
	exit(1);
}

static void usage(void)
{
	fprintf(stderr, "Usage: %s [-BPs] [-t timeout] hostname portname\n",
		progname);
	exit(1);
}

/*
 * $PchId: socket.c,v 1.3 2005/01/31 22:33:20 philip Exp $
 */