sock_test.c revision f21dcf7d07476d850ecc9c1f81a4e1b3e91b08d8
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1998, 1999 Internet Software Consortium.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * Permission to use, copy, modify, and distribute this software for any
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * purpose with or without fee is hereby granted, provided that the above
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
75ec9bc9c7b4f2485647414330122e7b8e188097Andreas Gustafssonstatic void my_send(isc_task_t *task, isc_event_t *event);
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halleystatic void my_recv(isc_task_t *task, isc_event_t *event);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellingtonmy_recv(isc_task_t *task, isc_event_t *event)
f9df80f4348ef68043903efa08299480324f4823Michael Graff printf("Socket %s (sock %p, base %p, length %d, n %d, result %d)\n",
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer * Echo the data back
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence region.base = isc_mem_get(mctx, strlen(buf) + 1);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer strcpy((char *)region.base, buf); /* strcpy is safe */
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_socket_send(sock, ®ion, task, my_send, event->arg);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_socket_recv(sock, &dev->region, ISC_FALSE,
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer printf("my_send: %s task %p\n\t(sock %p, base %p, length %d, n %d, result %d)\n",
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_mem_put(mctx, dev->region.base, dev->region.length);
f9df80f4348ef68043903efa08299480324f4823Michael Graffmy_http_get(isc_task_t *task, isc_event_t *event)
f9df80f4348ef68043903efa08299480324f4823Michael Graff printf("my_http_get: %s task %p\n\t(sock %p, base %p, length %d, n %d, result %d)\n",
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff isc_socket_recv(sock, &dev->region, ISC_FALSE, task, my_recv,
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graffmy_connect(isc_task_t *task, isc_event_t *event)
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff printf("%s: Connection result: %d\n", (char *)(event->arg),
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * Send a GET string, and set up to receive (and just display)
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * the result.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff strcpy(buf, "GET /foo HTTP/1.1\r\nHost: www.flame.org\r\nConnection: Close\r\n\r\n");
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff region.base = isc_mem_get(mctx, strlen(buf) + 1);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff strcpy((char *)region.base, buf); /* strcpy is safe */
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_socket_send(sock, ®ion, task, my_http_get, event->arg);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graffmy_listen(isc_task_t *task, isc_event_t *event)
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff printf("newcon %s (task %p, oldsock %p, newsock %p, result %d)\n",
f9df80f4348ef68043903efa08299480324f4823Michael Graff name, task, event->sender, dev->newsocket, dev->result);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * queue another listen on this socket
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff isc_socket_accept(event->sender, task, my_listen, event->arg);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * Create a new task for this socket, and queue up a
f9df80f4348ef68043903efa08299480324f4823Michael Graff * recv on it.
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_task_create(manager, NULL, 0, &newtask)
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff isc_socket_recv(dev->newsocket, ®ion, ISC_FALSE,
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff printf("detaching from socket %p\n", event->sender);
4556681e191b7c1654639895ce719d98f2822ee2Michael Graff printf("Timeout, canceling IO on socket %p (task %p)\n", sock, task);
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff isc_socket_cancel(sock, NULL, ISC_SOCKCANCEL_ALL);
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff isc_timer_detach((isc_timer_t **)&event->sender);
70fd62761dfe44f2254fb63ac3ded1b02663713fMichael Graff unsigned int workers;
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff sockaddr.length = sizeof (struct sockaddr_in);
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff * EVERYTHING needs a memory context.
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * The task manager is independent (other than memory context)
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff * Timer manager depends only on the memory context as well.
70fd62761dfe44f2254fb63ac3ded1b02663713fMichael Graff RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_task_create(manager, NULL, 0, &t1) == ISC_R_SUCCESS);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff RUNTIME_CHECK(isc_task_create(manager, NULL, 0, &t2) == ISC_R_SUCCESS);
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") ==
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") ==
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff * open up a listener socket
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington sockaddr.length = sizeof (struct sockaddr_in);
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_tcp, &so1) ==
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington RUNTIME_CHECK(isc_socket_bind(so1, &sockaddr) == ISC_R_SUCCESS);
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington RUNTIME_CHECK(isc_socket_listen(so1, 0) == ISC_R_SUCCESS);
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington * queue up the first accept event
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington RUNTIME_CHECK(isc_socket_accept(so1, t1, my_listen,
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
55f3daa4ea84859f9753089831a950a4fd9678c3Brian Wellington * open up a socket that will connect to www.flame.org, port 80.
f9df80f4348ef68043903efa08299480324f4823Michael Graff * Why not. :)
e223094b2248afa2697c531f75e6f84855638becMichael Graff sockaddr.type.sin.sin_addr.s_addr = inet_addr("204.152.186.34");
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_tcp,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence RUNTIME_CHECK(isc_socket_connect(so2, &sockaddr, t2,
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff * Detaching these is safe, since the socket will attach to the
823e45c1273512a8048cd5e7e57f31f58c964f7fMichael Graff * task for any outstanding requests.
5caab9f99d19ab9ebb0a0ba64c09c8de80e89e29Brian Wellington * wait a short while.
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff fprintf(stderr, "Destroying socket manager\n");
f9df80f4348ef68043903efa08299480324f4823Michael Graff fprintf(stderr, "Destroying timer manager\n");
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington fprintf(stderr, "Destroying task manager\n");