sock_test.c 9.47 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Bob Halley's avatar
update    
Bob Halley committed
2
 * Copyright (C) 1998, 1999  Internet Software Consortium.
Bob Halley's avatar
Bob Halley committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
17

Bob Halley's avatar
Bob Halley committed
18
19
#include <config.h>

20
21
22
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
23
#include <string.h>
24
25

#include <isc/assertions.h>
Bob Halley's avatar
Bob Halley committed
26
#include <isc/error.h>
Bob Halley's avatar
Bob Halley committed
27
#include <isc/mem.h>
28
29
30
31
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/result.h>
#include <isc/socket.h>
Michael Graff's avatar
Michael Graff committed
32
#include <isc/timer.h>
33

34
#include <sys/types.h>
Michael Graff's avatar
Michael Graff committed
35
36
#include <sys/socket.h>

37
#include <netinet/in.h>
Michael Graff's avatar
Michael Graff committed
38

39
40
#include <arpa/inet.h>

Michael Graff's avatar
Michael Graff committed
41
42
isc_mem_t *mctx;
isc_taskmgr_t *manager;
Michael Graff's avatar
Michael Graff committed
43

44
45
static void my_send(isc_task_t *task, isc_event_t *event);
static void my_recv(isc_task_t *task, isc_event_t *event);
Michael Graff's avatar
Michael Graff committed
46

47
static void
Bob Halley's avatar
Bob Halley committed
48
my_shutdown(isc_task_t *task, isc_event_t *event)
49
50
51
52
53
54
{
	char *name = event->arg;

	printf("shutdown %s (%p)\n", name, task);
	fflush(stdout);
	isc_event_free(&event);
Michael Graff's avatar
Michael Graff committed
55
56
}

57
static void
Bob Halley's avatar
Bob Halley committed
58
my_recv(isc_task_t *task, isc_event_t *event)
Michael Graff's avatar
Michael Graff committed
59
{
Bob Halley's avatar
Bob Halley committed
60
61
62
	isc_socket_t *sock;
	isc_socketevent_t *dev;
	isc_region_t region;
Michael Graff's avatar
Michael Graff committed
63
64
65
	char buf[1024];

	sock = event->sender;
Bob Halley's avatar
Bob Halley committed
66
	dev = (isc_socketevent_t *)event;
Michael Graff's avatar
Michael Graff committed
67
68
69
70
71

	printf("Socket %s (sock %p, base %p, length %d, n %d, result %d)\n",
	       (char *)(event->arg), sock,
	       dev->region.base, dev->region.length,
	       dev->n, dev->result);
Bob Halley's avatar
Bob Halley committed
72
73
	printf("\tFrom: %s port %d\n",
	       inet_ntoa(dev->address.type.sin.sin_addr),
Michael Graff's avatar
Michael Graff committed
74
75
76
77
78
	       ntohs(dev->address.type.sin.sin_port));

	if (dev->result != ISC_R_SUCCESS) {
		isc_socket_detach(&sock);

Michael Graff's avatar
Michael Graff committed
79
80
		isc_mem_put(event->mctx, dev->region.base,
			    dev->region.length);
Michael Graff's avatar
Michael Graff committed
81
82
		isc_event_free(&event);

Michael Graff's avatar
Michael Graff committed
83
		isc_task_shutdown(task);
84
		return;
Michael Graff's avatar
Michael Graff committed
85
86
87
88
89
	}

	/*
	 * Echo the data back
	 */
Michael Graff's avatar
Michael Graff committed
90
	if (strcmp(event->arg, "so2") != 0) {
Michael Graff's avatar
Michael Graff committed
91
		region = dev->region;
92
93
		sprintf(buf, "\r\nReceived: %.*s\r\n\r\n",
			(int)region.length, (char *)region.base);
Michael Graff's avatar
Michael Graff committed
94
95
		region.base = isc_mem_get(event->mctx, strlen(buf) + 1);
		region.length = strlen(buf) + 1;
Bob Halley's avatar
lint    
Bob Halley committed
96
		strcpy((char *)region.base, buf);  /* strcpy is safe */
Michael Graff's avatar
Michael Graff committed
97
98
99
		isc_socket_send(sock, &region, task, my_send, event->arg);
	} else {
		region = dev->region;
100
101
		printf("\r\nReceived: %.*s\r\n\r\n",
		       (int)region.length, (char *)region.base);
Michael Graff's avatar
Michael Graff committed
102
103
104
105
106
107
108
109
	}

	isc_socket_recv(sock, &dev->region, ISC_FALSE,
			task, my_recv, event->arg);

	isc_event_free(&event);
}

110
static void
Bob Halley's avatar
Bob Halley committed
111
my_send(isc_task_t *task, isc_event_t *event)
Michael Graff's avatar
Michael Graff committed
112
{
Bob Halley's avatar
Bob Halley committed
113
114
	isc_socket_t *sock;
	isc_socketevent_t *dev;
Michael Graff's avatar
Michael Graff committed
115
116

	sock = event->sender;
Bob Halley's avatar
Bob Halley committed
117
	dev = (isc_socketevent_t *)event;
Michael Graff's avatar
Michael Graff committed
118
119
120
121
122
123

	printf("my_send: %s task %p\n\t(sock %p, base %p, length %d, n %d, result %d)\n",
	       (char *)(event->arg), task, sock,
	       dev->region.base, dev->region.length,
	       dev->n, dev->result);

Michael Graff's avatar
Michael Graff committed
124
125
126
127
128
	if (dev->result != ISC_R_SUCCESS) {
		isc_socket_detach(&sock);
		isc_task_shutdown(task);
	}

Michael Graff's avatar
Michael Graff committed
129
130
131
132
	isc_mem_put(event->mctx, dev->region.base, dev->region.length);

	isc_event_free(&event);
}
Michael Graff's avatar
Michael Graff committed
133

134
static void
Bob Halley's avatar
Bob Halley committed
135
my_http_get(isc_task_t *task, isc_event_t *event)
136
{
Bob Halley's avatar
Bob Halley committed
137
138
	isc_socket_t *sock;
	isc_socketevent_t *dev;
139
140

	sock = event->sender;
Bob Halley's avatar
Bob Halley committed
141
	dev = (isc_socketevent_t *)event;
142

Michael Graff's avatar
Michael Graff committed
143
144
	printf("my_http_get: %s task %p\n\t(sock %p, base %p, length %d, n %d, result %d)\n",
	       (char *)(event->arg), task, sock,
145
146
	       dev->region.base, dev->region.length,
	       dev->n, dev->result);
Michael Graff's avatar
Michael Graff committed
147

Michael Graff's avatar
Michael Graff committed
148
149
150
151
152
153
154
	if (dev->result != ISC_R_SUCCESS) {
		isc_socket_detach(&sock);
		isc_task_shutdown(task);
		isc_event_free(&event);
		return;
	}

Michael Graff's avatar
Michael Graff committed
155
156
157
158
159
160
	isc_socket_recv(sock, &dev->region, ISC_FALSE, task, my_recv,
			event->arg);

	isc_event_free(&event);
}

161
static void
Bob Halley's avatar
Bob Halley committed
162
my_connect(isc_task_t *task, isc_event_t *event)
Michael Graff's avatar
Michael Graff committed
163
{
Bob Halley's avatar
Bob Halley committed
164
165
166
	isc_socket_t *sock;
	isc_socket_connev_t *dev;
	isc_region_t region;
Michael Graff's avatar
Michael Graff committed
167
168
169
	char buf[1024];

	sock = event->sender;
Bob Halley's avatar
Bob Halley committed
170
	dev = (isc_socket_connev_t *)event;
Michael Graff's avatar
Michael Graff committed
171
172
173

	printf("%s: Connection result:  %d\n", (char *)(event->arg),
	       dev->result);
174
175
176
177

	if (dev->result != ISC_R_SUCCESS) {
		isc_socket_detach(&sock);
		isc_event_free(&event);
Michael Graff's avatar
Michael Graff committed
178
		isc_task_shutdown(task);
179
		return;
180
181
	}

Michael Graff's avatar
Michael Graff committed
182
	/*
Michael Graff's avatar
Michael Graff committed
183
184
	 * Send a GET string, and set up to receive (and just display)
	 * the result.
Michael Graff's avatar
Michael Graff committed
185
	 */
Michael Graff's avatar
Michael Graff committed
186
	strcpy(buf, "GET /foo HTTP/1.1\r\nHost: www.flame.org\r\nConnection: Close\r\n\r\n");
Michael Graff's avatar
Michael Graff committed
187
188
	region.base = isc_mem_get(event->mctx, strlen(buf) + 1);
	region.length = strlen(buf) + 1;
Bob Halley's avatar
lint    
Bob Halley committed
189
	strcpy((char *)region.base, buf);  /* strcpy is safe */
190

Michael Graff's avatar
Michael Graff committed
191
	isc_socket_send(sock, &region, task, my_http_get, event->arg);
Michael Graff's avatar
Michael Graff committed
192

193
194
195
	isc_event_free(&event);
}

196
static void
Bob Halley's avatar
Bob Halley committed
197
my_listen(isc_task_t *task, isc_event_t *event)
Michael Graff's avatar
Michael Graff committed
198
199
{
	char *name = event->arg;
Bob Halley's avatar
Bob Halley committed
200
201
202
	isc_socket_newconnev_t *dev;
	isc_region_t region;
	isc_socket_t *oldsock;
Michael Graff's avatar
Michael Graff committed
203
	isc_task_t *newtask;
Michael Graff's avatar
Michael Graff committed
204

Bob Halley's avatar
Bob Halley committed
205
	dev = (isc_socket_newconnev_t *)event;
206
207
208

	printf("newcon %s (task %p, oldsock %p, newsock %p, result %d)\n",
	       name, task, event->sender, dev->newsocket, dev->result);
Michael Graff's avatar
Michael Graff committed
209
210
	fflush(stdout);

211
212
213
214
215
216
	if (dev->result == ISC_R_SUCCESS) {
		/*
		 * queue another listen on this socket
		 */
		isc_socket_accept(event->sender, task, my_listen, event->arg);

Michael Graff's avatar
Michael Graff committed
217
		region.base = isc_mem_get(event->mctx, 20);
Michael Graff's avatar
Michael Graff committed
218
		region.length = 20;
219
220

		/*
Michael Graff's avatar
Michael Graff committed
221
222
		 * Create a new task for this socket, and queue up a
		 * recv on it.
223
		 */
Michael Graff's avatar
Michael Graff committed
224
225
226
		newtask = NULL;
		RUNTIME_CHECK(isc_task_create(manager, NULL, 0, &newtask)
			      == ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
227
		isc_socket_recv(dev->newsocket, &region, ISC_FALSE,
Michael Graff's avatar
Michael Graff committed
228
229
				newtask, my_recv, event->arg);
		isc_task_detach(&newtask);
230
	} else {
Michael Graff's avatar
Michael Graff committed
231
232
233
234
235
		printf("detaching from socket %p\n", event->sender);
		oldsock = event->sender;

		isc_socket_detach(&oldsock);

Michael Graff's avatar
Michael Graff committed
236
		isc_event_free(&event);
237
238
		isc_task_shutdown(task);
		return;
239
	}
Michael Graff's avatar
Michael Graff committed
240

241
	isc_event_free(&event);
Michael Graff's avatar
Michael Graff committed
242
243
}

244
static void
Bob Halley's avatar
Bob Halley committed
245
timeout(isc_task_t *task, isc_event_t *event)
Michael Graff's avatar
Michael Graff committed
246
{
Bob Halley's avatar
Bob Halley committed
247
	isc_socket_t *sock = event->arg;
Michael Graff's avatar
Michael Graff committed
248

Bob Halley's avatar
Bob Halley committed
249
	printf("Timeout, canceling IO on socket %p (task %p)\n", sock, task);
Michael Graff's avatar
Michael Graff committed
250
251

	isc_socket_cancel(sock, NULL, ISC_SOCKCANCEL_ALL);
Bob Halley's avatar
Bob Halley committed
252
	isc_timer_detach((isc_timer_t **)&event->sender);
Michael Graff's avatar
Michael Graff committed
253
	isc_event_free(&event);
254
255
256
257
258
}

int
main(int argc, char *argv[])
{
Michael Graff's avatar
Michael Graff committed
259
260
261
	isc_task_t *t1, *t2;
	isc_timermgr_t *timgr;
	isc_time_t expires;
Bob Halley's avatar
Bob Halley committed
262
	isc_interval_t interval;
Michael Graff's avatar
Michael Graff committed
263
	isc_timer_t *ti1;
264
	unsigned int workers;
Bob Halley's avatar
Bob Halley committed
265
266
267
	isc_socketmgr_t *socketmgr;
	isc_socket_t *so1, *so2;
	isc_sockaddr_t sockaddr;
268
	unsigned int addrlen;
Michael Graff's avatar
Michael Graff committed
269
270
271
272

	memset(&sockaddr, 0, sizeof(sockaddr));
	sockaddr.type.sin.sin_port = htons(5544);
	addrlen = sizeof(struct sockaddr_in);
273
274
275
276
277
278
279

	if (argc > 1)
		workers = atoi(argv[1]);
	else
		workers = 2;
	printf("%d workers\n", workers);

Michael Graff's avatar
Michael Graff committed
280
281
282
283
	/*
	 * EVERYTHING needs a memory context.
	 */
	mctx = NULL;
Bob Halley's avatar
Bob Halley committed
284
	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
285

Michael Graff's avatar
Michael Graff committed
286
287
288
289
	/*
	 * The task manager is independent (other than memory context)
	 */
	manager = NULL;
Bob Halley's avatar
Bob Halley committed
290
291
	RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
		      ISC_R_SUCCESS);
292

Michael Graff's avatar
Michael Graff committed
293
294
295
296
297
298
299
	/*
	 * Timer manager depends only on the memory context as well.
	 */
	timgr = NULL;
	RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);

	t1 = NULL;
Bob Halley's avatar
Bob Halley committed
300
	RUNTIME_CHECK(isc_task_create(manager, NULL, 0, &t1) == ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
301
	t2 = NULL;
Bob Halley's avatar
Bob Halley committed
302
	RUNTIME_CHECK(isc_task_create(manager, NULL, 0, &t2) == ISC_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
303
304
305
306
	RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") ==
		      ISC_R_SUCCESS);
307
308
309
310
311

	printf("task 1 = %p\n", t1);
	printf("task 2 = %p\n", t2);

	socketmgr = NULL;
Bob Halley's avatar
Bob Halley committed
312
	RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
313
314
315
316

	/*
	 * open up a listener socket
	 */
317
	so1 = NULL;
Michael Graff's avatar
Michael Graff committed
318
319
320
321
	memset(&sockaddr, 0, sizeof(sockaddr));
	sockaddr.type.sin.sin_family = AF_INET;
	sockaddr.type.sin.sin_port = htons(5544);
	addrlen = sizeof(struct sockaddr_in);
Bob Halley's avatar
Bob Halley committed
322
323
324
325
326
	RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_tcp, &so1) ==
		      ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_socket_bind(so1, &sockaddr,
				      (int)addrlen) == ISC_R_SUCCESS);
	RUNTIME_CHECK(isc_socket_listen(so1, 0) == ISC_R_SUCCESS);
327

Michael Graff's avatar
Michael Graff committed
328
329
330
	/*
	 * queue up the first accept event
	 */
Bob Halley's avatar
Bob Halley committed
331
332
	RUNTIME_CHECK(isc_socket_accept(so1, t1, my_listen,
					"so1") == ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
333
	isc_time_settoepoch(&expires);
Michael Graff's avatar
Michael Graff committed
334
	isc_interval_set(&interval, 10, 0);
Michael Graff's avatar
Michael Graff committed
335
	ti1 = NULL;
Bob Halley's avatar
Bob Halley committed
336
337
338
	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
				       &interval, t1, timeout, so1, &ti1) ==
		      ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
339

Michael Graff's avatar
Michael Graff committed
340
341
342
343
	/*
	 * open up a socket that will connect to www.flame.org, port 80.
	 * Why not.  :)
	 */
344
	so2 = NULL;
Michael Graff's avatar
Michael Graff committed
345
346
347
	memset(&sockaddr, 0, sizeof(sockaddr));
	sockaddr.type.sin.sin_port = htons(80);
	sockaddr.type.sin.sin_family = AF_INET;
Michael Graff's avatar
Michael Graff committed
348
	sockaddr.type.sin.sin_addr.s_addr = inet_addr("204.152.186.34");
Michael Graff's avatar
Michael Graff committed
349
	addrlen = sizeof(struct sockaddr_in);
Bob Halley's avatar
Bob Halley committed
350
351
	RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_tcp,
					&so2) == ISC_R_SUCCESS);
Michael Graff's avatar
Michael Graff committed
352
	RUNTIME_CHECK(isc_socket_connect(so2, &sockaddr, (int)addrlen, t2,
Bob Halley's avatar
Bob Halley committed
353
					 my_connect, "so2") == ISC_R_SUCCESS);
354

Michael Graff's avatar
Michael Graff committed
355
356
357
358
	/*
	 * Detaching these is safe, since the socket will attach to the
	 * task for any outstanding requests.
	 */
Michael Graff's avatar
Michael Graff committed
359
360
361
	isc_task_detach(&t1);
	isc_task_detach(&t2);

Michael Graff's avatar
Michael Graff committed
362
363
364
	/*
	 * wait a short while.
	 */
Michael Graff's avatar
Michael Graff committed
365
366
367
	sleep(10);

	fprintf(stderr, "Destroying socket manager\n");
368
	isc_socketmgr_destroy(&socketmgr);
Michael Graff's avatar
Michael Graff committed
369

Michael Graff's avatar
Michael Graff committed
370
	fprintf(stderr, "Destroying timer manager\n");
Michael Graff's avatar
Michael Graff committed
371
372
	isc_timermgr_destroy(&timgr);

Michael Graff's avatar
Michael Graff committed
373
	fprintf(stderr, "Destroying task manager\n");
374
	isc_taskmgr_destroy(&manager);
Michael Graff's avatar
Michael Graff committed
375

376
	isc_mem_stats(mctx, stdout);
Bob Halley's avatar
Bob Halley committed
377
	isc_mem_destroy(&mctx);
378
379
380

	return (0);
}