File: simple_server.c

package info (click to toggle)
libest 3.2.0%2Bds-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,792 kB
  • sloc: ansic: 60,980; java: 12,082; sh: 4,956; python: 4,474; xml: 385; makefile: 290
file content (387 lines) | stat: -rw-r--r-- 9,803 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*------------------------------------------------------------------
 * simple_server.c - This is a very simple multi-threaded TCP
 *                   server used by the example EST server and EST
 *                   proxy applications. 
 *
 * August, 2013
 *
 * Copyright (c) 2013-2014, 2016 by cisco Systems, Inc.
 * All rights reserved.
 **------------------------------------------------------------------
 */
#include <stdio.h>
#include <unistd.h>
#ifndef DISABLE_PTHREADS
#include <pthread.h>
#endif
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <est.h>
#include <signal.h>

volatile int stop_flag = 0;
int sock;

#ifdef HAVE_LIBCOAP 
static int udp_port;
#endif
extern int coap_mode;

static int tcp_port;

static int family = AF_INET;
volatile int num_threads; 
static EST_CTX *ctx;
#ifndef DISABLE_PTHREADS
pthread_mutex_t mutex;    
pthread_cond_t cond;      
int queue[20];
volatile int head;     
volatile int tail;     
pthread_cond_t full;   
pthread_cond_t empty;  

typedef void * (*thread_func_t)(void *);

#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
#if HAVE_LIBCOAP
#define NUM_WORKER_THREADS 1
#else
#define NUM_WORKER_THREADS 5
#endif

static int grab_work (int *sp)
{
    pthread_mutex_lock(&mutex);

    // Check for new work
    while (head == tail && stop_flag == 0) {
        pthread_cond_wait(&full, &mutex);
    }

    if (head > tail) {
        *sp = queue[tail % ARRAY_SIZE(queue)];
        tail++;

        // Keep our pointers aligned
        while (tail > (int)ARRAY_SIZE(queue)) {
            tail -= ARRAY_SIZE(queue);
            head -= ARRAY_SIZE(queue);
        }
    }

    pthread_cond_signal(&empty);
    pthread_mutex_unlock(&mutex);

    return !stop_flag;
}

static void * worker_thread (void* data)
{
    int sock = 0;

    while (grab_work(&sock)) {
	/*
	 * Both SERVER and PROXY modes use the
	 * same entry point to hand off the socket
	 * to libest
	 */
        est_server_handle_request(ctx, sock);
        close(sock);
    }

    // Tell the boss that we're finishing 
    pthread_mutex_lock(&mutex);
    num_threads--;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
    return NULL;
}

static void process_socket (int fd)
{
    pthread_mutex_lock(&mutex);

    // Check if work queue is full and wait
    while (stop_flag == 0 && head - tail >= (int)ARRAY_SIZE(queue)) {
        pthread_cond_wait(&empty, &mutex);
    }

    if (head - tail < (int)ARRAY_SIZE(queue)) {
	// Put the accepted socket on to the work queue
        queue[head % ARRAY_SIZE(queue)] = fd;
        head++;
    }

    pthread_cond_signal(&full);
    pthread_mutex_unlock(&mutex);
}

#else
static void process_socket (int fd)
{
    est_server_handle_request(ctx, fd);
    close(fd);
}
#endif

static void * master_thread (void *data)
{
    struct addrinfo hints, *ai, *aiptr;
    char portstr[12];
    int on = 1;
    int rc;
    struct sockaddr *addr;
    int flags;
    int new;
    int unsigned len;
#if (HAVE_LIBCOAP)
    int processing_error = 0;
#endif

    if (coap_mode == 0){
        
        /*
         * Lookup the local address we'll use to bind too
         */
        memset(&hints, '\0', sizeof(struct addrinfo));
        hints.ai_family = family;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE; 
        snprintf(portstr, sizeof(portstr), "%u", tcp_port);
        rc = getaddrinfo(NULL, portstr, &hints, &aiptr);
        if (rc) {
            printf("\ngetaddrinfo call failed\n");
            printf("getaddrinfo(): %s\n", gai_strerror(rc));

            exit(1);
        }
        for (ai = aiptr; ai; ai = ai->ai_next) {
            sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
            if (sock == -1) {
                /* If we can't create a socket using this address, then
                 * try the next address */
                continue;
            }
            /*
             * Set some socket options for our server
             */
            if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on))) {
                printf("\nsetsockopt REUSEADDR call failed\n");
                exit(1);
            }
            if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&on, sizeof(on))) {
                printf("\nsetsockopt KEEPALIAVE call failed\n");
                exit(1);
            }
            flags = fcntl(sock, F_GETFL, 0);
            if (fcntl(sock, F_SETFL, flags | O_NONBLOCK)) {
                printf("\nfcntl SETFL call failed\n");
                exit(1);
            }
            /*
             * Bind to the socket 
             */
            rc = bind(sock, ai->ai_addr, ai->ai_addrlen);
            if (rc == -1) {
                printf("\nbind call failed\n");
                exit(1);
            }
            break;
        }

        if (ai) {
            addr = ai->ai_addr;
            listen(sock, SOMAXCONN);
        } else {
            printf("\nNo address info found\n");
            exit(1);
        }    

        while (stop_flag == 0) {
            len = sizeof(struct sockaddr);
            new = accept(sock, (struct sockaddr*)addr, &len);
            if (new < 0) {
                if (stop_flag != 0) {
                    break;
                }
                /*
                 * this is a bit cheesy, but much easier to implement than using select()
                 */
                usleep(100);
            } else {
                if (stop_flag == 0) {
                    process_socket(new);
                }
            }
        }
    
        close(sock);
        freeaddrinfo(aiptr);
    } else if (coap_mode == 1) {
#if !(HAVE_LIBCOAP)
        printf("\nestserver not built with coap support and --enable-coap has been specified.\n");
        exit(1);
#else
        while (stop_flag == 0 && processing_error == 0) {
            processing_error = est_server_coap_run_once(ctx);
        }
#endif
    } else {
        printf("\ninvalid setting for coap_mode.\n");
        exit(1);
    }    

#ifndef DISABLE_PTHREADS
    // Notify all the workers that it's time to shutdown
    pthread_cond_broadcast(&full);

    // Wait for the workers to finish
    pthread_mutex_lock(&mutex);
    while (num_threads > 0) {
        pthread_cond_wait(&cond, &mutex);
    }
    pthread_mutex_unlock(&mutex);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    pthread_cond_destroy(&empty);
    pthread_cond_destroy(&full);
#endif

    stop_flag = 2;
    return NULL;
}

#ifndef DISABLE_PTHREADS
static int start_thread (thread_func_t func)
{
    pthread_t thread_id;
    pthread_attr_t attr;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    return pthread_create(&thread_id, &attr, func, 0);
}
#endif

static void catch_int(int signo)
{
    stop_flag = 1;
}



/*
 * This is the entry point into the Simple TCP server.
 * This is designed to work with either the EST server
 * or EST proxy example applications.  This creates
 * a multi-threaded TCP server.  It opens a socket,
 * waits for incoming connections on the socket,
 * and dispatches work to a separate thread to process
 * the incoming EST request.
 *
 * Parameters:
 *     ectx	Pointer to the EST_CTX that was provided from
 *              calling est_server_init().
 *     port	The TCP port to listen too.
 *     delay	The number of seconds to wait before 
 *		automatically shutting down the server.
 *		Pass in 0 to run until keyboard input is 
 *		detected.
 *     v6       Set to non-zero value to enable IPv6
 */
void start_simple_server (EST_CTX *ectx, int port, int delay, int v6)
{
#ifndef DISABLE_PTHREADS
    int i;
#endif
    struct sigaction   sig_act;

    /*
     * Save a global reference to the context.
     * This code only supports running a single 
     * instance of the server.
     */
    ctx = ectx;

    /*
     * Make the port number visible to the master thread
     * by setting up a global
     */
#ifdef HAVE_LIBCOAP
    udp_port = port;
#endif
    tcp_port = port;
    
    if (v6) {
	family = AF_INET6;
    }

    /*
     * Install handler to catch ctrl-C to stop the process gracefully
     */
    memset(&sig_act, 0, sizeof(struct sigaction));
    sig_act.sa_handler = catch_int;
    sigemptyset(&sig_act.sa_mask);
    if (sigaction(SIGINT, &sig_act, NULL) == -1) {
        printf("\nCannot set handler for SIGINT\n");
    }

    /*
     * Indicate that the broken pipe signal during writes should be
     * ignored
     */
    memset(&sig_act, 0, sizeof(struct sigaction));
    sig_act.sa_handler = SIG_IGN;
    sigemptyset(&sig_act.sa_mask);
    if (sigaction(SIGPIPE, &sig_act, NULL) == -1) {
        printf("\nCannot set ignore action for SIGPIPE\n");
    }    
    
#ifndef DISABLE_PTHREADS
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);
    pthread_cond_init(&empty, NULL);
    pthread_cond_init(&full, NULL);

    // Start master (listening) thread
    start_thread(master_thread);

    // Start worker threads
    for (i = 0; i < NUM_WORKER_THREADS; i++) {
        if (start_thread(worker_thread) != 0) {
            printf("\nCannot start worker thread: %d\n", errno);
        } else {
	    pthread_mutex_lock(&mutex);
            num_threads++;
	    pthread_mutex_unlock(&mutex);
        }
    }
#else
    /*
     * We're configured w/o support for pthreads.
     * Just run the server on the current thread.
     */
    master_thread(NULL);
#endif


    if (delay > 0) {
	/* We will automatically shut down the server
	 * after <delay> seconds */
	sleep(delay);
	stop_flag = 1;
    } 

    while (!stop_flag) {
        usleep(10000);	
    }
}