source: rtems/testsuites/samples/loopback/init.c @ cdf30f05

4.115
Last change on this file since cdf30f05 was f87ede5, checked in by Sebastian Huber <sebastian.huber@…>, on 01/15/15 at 13:13:19

libnetworking: Fix close of active sockets

Send a special event to notify tasks waiting for a socket state change
in case this socket gets closed. This prevents a use after free.

Close #785.

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 *  The license and distribution terms for this file may be
3 *  found in the file LICENSE in this distribution or at
4 *  http://www.rtems.org/license/LICENSE.
5 */
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include <rtems/test.h>
12
13#include <bsp.h>
14
15const char rtems_test_name[] = "LOOPBACK";
16
17#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
18#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
19#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
20
21#define CONFIGURE_EXECUTIVE_RAM_SIZE        (512*1024)
22#define CONFIGURE_MAXIMUM_SEMAPHORES        20
23#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES    20
24#define CONFIGURE_MAXIMUM_TASKS            20
25
26#define CONFIGURE_MICROSECONDS_PER_TICK    10000
27#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 50
28
29#define CONFIGURE_INIT_TASK_STACK_SIZE    (10*1024)
30#define CONFIGURE_INIT_TASK_PRIORITY    50
31#define CONFIGURE_INIT_TASK_INITIAL_MODES (RTEMS_PREEMPT | \
32                                           RTEMS_NO_TIMESLICE | \
33                                           RTEMS_NO_ASR | \
34                                           RTEMS_INTERRUPT_LEVEL(0))
35
36#define CONFIGURE_INIT
37rtems_task Init(rtems_task_argument argument);
38
39#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
40
41#include <rtems/confdefs.h>
42
43#include <rtems/rtems_bsdnet.h>
44#include <rtems/error.h>
45#include <stdio.h>
46#include <stdarg.h>
47#include <stdlib.h>
48#include <string.h>
49#include <errno.h>
50#include <sys/socket.h>
51#include <netinet/in.h>
52
53/*
54 * Network configuration
55 */
56
57struct rtems_bsdnet_config rtems_bsdnet_config = {
58    NULL,                   /* Network interface */
59    NULL,                   /* Use fixed network configuration */
60    0,                      /* Default network task priority */
61    0,                      /* Default mbuf capacity */
62    0,                      /* Default mbuf cluster capacity */
63    "testSystem",           /* Host name */
64    "nowhere.com",          /* Domain name */
65    "127.0.0.1",            /* Gateway */
66    "127.0.0.1",            /* Log host */
67    {"127.0.0.1" },         /* Name server(s) */
68    {"127.0.0.1" },         /* NTP server(s) */
69    0,
70    0,
71    0,
72    0,
73    0
74};
75
76/*
77 * Thread-safe output routines
78 */
79static rtems_id printMutex;
80static void printSafe(const char *fmt, ...)
81{
82    va_list args;
83    va_start(args, fmt);
84    rtems_semaphore_obtain(printMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
85    vprintf(fmt, args);
86    rtems_semaphore_release(printMutex);
87    va_end(args);
88}
89#define printf printSafe
90
91/*
92 * Spawn a task
93 */
94static void spawnTask(rtems_task_entry entryPoint, rtems_task_priority priority, rtems_task_argument arg)
95{
96    rtems_status_code sc;
97    rtems_id tid;
98
99    sc = rtems_task_create(rtems_build_name('t','a','s','k'),
100            priority,
101            RTEMS_MINIMUM_STACK_SIZE+(8*1024),
102            RTEMS_PREEMPT|RTEMS_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
103            RTEMS_FLOATING_POINT|RTEMS_LOCAL,
104            &tid);
105    if (sc != RTEMS_SUCCESSFUL)
106        rtems_panic("Can't create task: %s", rtems_status_text(sc));
107    sc = rtems_task_start(tid, entryPoint, arg);
108    if (sc != RTEMS_SUCCESSFUL)
109        rtems_panic("Can't start task: %s", rtems_status_text(sc));
110}
111
112/*
113 * Server subtask
114 */
115static rtems_task workerTask(rtems_task_argument arg)
116{
117    int s = arg;
118    char msg[80];
119    char reply[100];
120    int i;
121
122    for (;;) {
123        if ((i = read(s, msg, sizeof msg)) < 0) {
124            printf("Server couldn't read message from client: %s\n", strerror(errno));
125            break;
126        }
127        if (i == 0)
128            break;
129        rtems_task_wake_after(20); /* Simulate some processing delay */
130        i = sprintf(reply, "Server received %d (%s)", i, msg);
131        if ((i = write(s, reply, i+1)) < 0) {
132            printf("Server couldn't write message to client: %s\n", strerror(errno));
133            break;
134        }
135    }
136    if (close(s) < 0)
137        printf("Can't close worker task socket: %s\n", strerror(errno));
138    printf("Worker task terminating.\n");
139    rtems_task_delete(RTEMS_SELF);
140}
141
142/*
143 * Server Task
144 */
145static rtems_task serverTask(rtems_task_argument arg)
146{
147    int s, s1;
148    socklen_t addrlen;
149        struct sockaddr_in myAddr, farAddr;
150    rtems_task_priority myPriority;
151
152        printf("Create socket.\n");
153        s = socket(AF_INET, SOCK_STREAM, 0);
154        if (s < 0)
155                rtems_panic("Can't create socket: %s\n", strerror(errno));
156    memset(&myAddr, 0, sizeof myAddr);
157        myAddr.sin_family = AF_INET;
158        myAddr.sin_port = htons(1234);
159        myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
160        printf("Bind socket.\n");
161        if (bind(s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0)
162                rtems_panic("Can't bind socket: %s\n", strerror(errno));
163        if (listen(s, 5) < 0)
164                printf("Can't listen on socket: %s\n", strerror(errno));
165    rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &myPriority);
166    for(;;) {
167        addrlen = sizeof farAddr;
168        s1 = accept(s, (struct sockaddr *)&farAddr, &addrlen);
169        if (s1 < 0)
170            if (errno == ENXIO)
171                rtems_task_delete(RTEMS_SELF);
172            else
173                rtems_panic("Can't accept connection: %s", strerror(errno));
174        else
175            printf("ACCEPTED:%lX\n", ntohl(farAddr.sin_addr.s_addr));
176        spawnTask(workerTask, myPriority, s1);
177    }
178}
179
180/*
181 * The real part of the client
182 */
183static rtems_task clientWorker(int arg)
184{
185    int s;
186        struct sockaddr_in myAddr, farAddr;
187    char cbuf[50];
188    int i;
189
190        s = socket(AF_INET, SOCK_STREAM, 0);
191        if (s < 0) {
192                printf("Can't create client socket: %s\n", strerror(errno));
193        return;
194    }
195    memset(&myAddr, 0, sizeof myAddr);
196        myAddr.sin_family = AF_INET;
197        myAddr.sin_port = htons(0);
198        myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
199        if (bind(s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0) {
200                printf("Can't bind socket: %s\n", strerror(errno));
201        goto close;
202    }
203    memset(&farAddr, 0, sizeof farAddr);
204        farAddr.sin_family = AF_INET;
205        farAddr.sin_port = htons(1234);
206        farAddr.sin_addr.s_addr = htonl(INADDR_ANY);
207        printf("Connect to server.\n");
208        if (connect(s, (struct sockaddr *)&farAddr, sizeof farAddr) < 0) {
209                printf("Can't connect to server: %s\n", strerror(errno));
210        goto close;
211    }
212    rtems_task_wake_after(20); /* Simulate client delay */
213    i = sprintf(cbuf, "Hi there, server (%d).", arg);
214    i++;    /* Send the '\0', too */
215    printf("Write %d-byte message to server.\n", i);
216    if (write(s, cbuf, i) < 0) {
217                printf("Can't write to server: %s\n", strerror(errno));
218        goto close;
219    }
220    if ((i = read(s, cbuf, sizeof cbuf)) < 0) {
221                printf("Can't read from server: %s\n", strerror(errno));
222        goto close;
223    }
224    printf("Read %d from server: %.*s\n", i, i, cbuf);
225    rtems_task_wake_after(20); /* Simulate client delay */
226  close:
227    printf("Client closing connection.\n");
228    if (close(s) < 0)
229        printf("Can't close client task socket: %s\n", strerror(errno));
230}
231
232/*
233 * Client Task
234 */
235static rtems_task clientTask(rtems_task_argument arg)
236{
237    clientWorker(arg);
238    printf("Client task terminating.\n");
239    rtems_task_delete( RTEMS_SELF );
240}
241
242/*
243 * RTEMS Startup Task
244 */
245rtems_task
246Init (rtems_task_argument ignored)
247{
248    rtems_status_code sc;
249
250    rtems_test_begin();
251
252    sc = rtems_semaphore_create(rtems_build_name('P','m','t','x'),
253                1,
254                RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE|RTEMS_INHERIT_PRIORITY|
255                                    RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL,
256                0,
257                &printMutex);
258    if (sc != RTEMS_SUCCESSFUL)
259        rtems_panic("Can't create printf mutex:", rtems_status_text(sc));
260    printf("\"Network\" initializing!\n");
261    rtems_bsdnet_initialize_network();
262    printf("\"Network\" initialized!\n");
263
264    printf("Try running client with no server present.\n");
265    printf("Should fail with `connection refused'.\n");
266    clientWorker(0);
267
268    printf("\nStart server.\n");
269    spawnTask(serverTask, 150, 0);
270
271    printf("\nTry running client with server present.\n");
272    spawnTask(clientTask, 120, 1);
273    rtems_task_wake_after(500);
274
275    printf("\nTry running two clients.\n");
276    spawnTask(clientTask, 120, 2);
277    spawnTask(clientTask, 120, 3);
278    rtems_task_wake_after(500);
279
280    printf("\nTry running three clients.\n");
281    spawnTask(clientTask, 120, 4);
282    spawnTask(clientTask, 120, 5);
283    spawnTask(clientTask, 120, 6);
284
285    rtems_task_wake_after(500);
286    rtems_test_end();
287    exit( 0 );
288}
Note: See TracBrowser for help on using the repository browser.