guowenxue
2020-08-21 02f4d9518378031c63df7a36c49d8b2eabdaab90
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue<Email:guowenxue@gmail.com QQ:281143292>
 *                  All rights reserved.
 *
 *       Filename:  date_time_server.c
 *    Description:  This is a sample socket server demo program.
 *                 
 *        Version:  1.0.0(06/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "06/08/2012 02:50:51 PM"
 *                 
 ********************************************************************************/
 
/* Some Unix Program Standard head file  */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <getopt.h> /*  getopt_long */
#include <libgen.h> /*  getopt_long */
 
/* Socket Program head file */
#include <sys/types.h>   
#include <sys/socket.h>
#include <netinet/in.h> /* sockaddr_in{} and other Internet define */
 
/* strerror(), perror(), errno head file*/
#include <errno.h>
#include <time.h>
 
 
#define LISTENQ                                1024 /* 2nd argument to listen () */
 
#define DEF_USER                               "lingyun"
#define DEF_PASSWD                             "www.iot-yun.com"
 
 
typedef struct link_node_s 
{
    int                   fd;    /* server/client socket fd  */
    int                   logon; /* client socket logon or not  */
    struct link_node_s   *next;
} link_node_t;  
 
 
int socket_server_init(char *listen_ip, int listen_port);
int proc_client_socket(link_node_t **head, fd_set *rdset );
 
static inline msleep(unsigned long ms) 
{
    struct timeval       tv; 
 
    tv.tv_sec = ms/1000;
    tv.tv_usec = (ms%1000)*1000; 
    
    select(0, NULL, NULL, NULL, &tv);
}
 
 
void print_usage(char *progname)
    printf("Usage: %s [OPTION]...\n", progname);
    
    printf(" %s is a socket server program, which used to verify client and echo back string from it\n", progname); 
    printf("\nMandatory arguments to long options are mandatory for short options too:\n"); 
    
    printf(" -b[daemon ]  set program running on background\n");
    printf(" -p[port    ]  Socket server port address\n");
    printf(" -h[help    ]  Display this help information\n");
    
 
    printf("\nExample: %s -b -p 8900\n", progname);
    return ;
}
 
 
int main(int argc, char **argv)
{
    int                       listenfd, connfd;
    int                       serv_port = 0;
    int                       daemon_run = 0;
    char                     *progname = NULL;
    pthread_t                 tid;
    int                       opt;
    fd_set                    rdset; 
    link_node_t              *client_list = NULL; /* client fd link list, we will not use arrary for it get max client limite */
    int                       rv;
 
    struct option             long_options[] = 
    {   
        {"daemon", no_argument, NULL, 'b'},
        {"port", required_argument, NULL, 'p'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };  
 
    progname = basename(argv[0]);
 
    /*  Parser the command line parameters */
    while ((opt = getopt_long(argc, argv, "bp:h", long_options, NULL)) != -1) 
    {   
        switch (opt)
        {   
            case 'b':
                daemon_run=1;
                break;
 
            case 'p':
                serv_port = atoi(optarg);
                break;
 
            case 'h':  /*  Get help information */
                print_usage(progname);
                return EXIT_SUCCESS;
 
            default:
                break;
        }   
    }   
 
    if( !serv_port ) 
    {   
        print_usage(progname);
        return -1; 
    }
 
    if( (listenfd=socket_server_init(NULL, serv_port)) < 0 )
    {
        printf("ERROR: %s server listen on port %d failure\n", argv[0],serv_port);
        return -2;
    }
    printf("%s server start to listen on port %d\n", argv[0],serv_port);
 
 
    if( daemon_run ) /* set program running on background */
    {
        daemon(0, 0);
    }
 
 
    printf("add listen socket[%d] into client list\n", listenfd );
    add_fd_list(&client_list, listenfd);
 
    for ( ; ; ) 
    {
        rv=start_select_listen(client_list, &rdset);
        if(rv < 0)
        {
            printf("select get error: %s\n", strerror(errno));
            break;
        }
        else if(rv == 0)
        {
            printf("seelct get timeout\n");
            continue;
        }
        
 
        /* some client get read event happened  */
        if ( FD_ISSET(listenfd, &rdset) ) /* listen socket get event means new client require connect */
        {
            if( (connfd=accept(listenfd, (struct sockaddr *)NULL, NULL)) <= 0)
            {
                printf("accept new client failure: %s\n", strerror(errno));
                continue;
            }
 
            printf("accept new client[%d] and add it into client list\n", connfd );
            add_fd_list(&client_list, connfd);
        }
        else /* already connected socket client send data arrive  */
        {
            proc_client_socket(&client_list, &rdset);
        }
    }
 
CleanUp:
    close(listenfd);  /* We must close socket File Description when program exit*/
    return 0;
}
 
int socket_server_init(char *listen_ip, int listen_port)
{
    struct sockaddr_in        servaddr;
    int                       listenfd = -1;
    int                       on = 1;
 
    /*
     * Open an IPV4(AF_INET) TCP(SOCK_STREAM) Socket File Description(listenfd), UDP socket 
     * should use SOCK_DGRAM,We can use linux command "man socket" to see this function manual
     */
    if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        /* strerror() is the 1st way to display the failure reason, argument 
         * errno is a globle variable defined in <errno.h>, we can use linux command 
         * "man strerror" to see this function manual*/
        printf("Use socket() to create a TCP socket failure: %s\n", strerror(errno));
        return -1;
    }
 
    /* Set socket port reuseable  */
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
    /* Now we set the Server Information, include IPV4 or IPV6, Listen IP address and Port */
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;   /* Set it as IPV4 protocal */
 
    /* set listen IP address  */
    if( !listen_ip ) 
    {
        /* Listen all the local IP address */
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);  
    }
    else 
    {
        /* listen the specified IP address  */
        if (inet_pton(AF_INET, listen_ip, &servaddr.sin_addr) <= 0)
        {
            printf("inet_pton() set listen IP address failure.\n");
            listenfd = -2;
            goto CleanUp;
        }
    }
 
    servaddr.sin_port = htons(listen_port); /*  set listen port */
 
    /*
     * When a socket is created with socket(2), it exists in a name space (address family) but 
     * has no address assigned to it. bind() assigns the address specified to by addr to the 
     * socket referred to by the file descriptor listenfd. We can use Linux command "man 2 bind" 
     * to see this function manual.
     */
    if(bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
    {
        printf("Use bind() to bind the TCP socket failure: %s\n", strerror(errno));
        listenfd = -3;
        goto CleanUp;
    }
 
    /*
     * listen() marks the socket referred to by listenfd as a passive socket, that is, as a socket 
     * that will be used to accept incoming connection requests using accept(2). We can use Linux
     * command "man listen" to see this function manual.
     */
    if(listen(listenfd, LISTENQ) < 0)
    {
        printf("Use bind() to bind the TCP socket failure: %s\n", strerror(errno));
        listenfd = -4;
        goto CleanUp;
    }
 
CleanUp:
    return listenfd;
 
}
 
int start_select_listen(link_node_t *head, fd_set *set)
{
    link_node_t          *node;
    int                  maxfd=0;
 
    if(!head || !set)
        return -2;
 
    FD_ZERO(set);
 
    for( node=head; node!=NULL; node=node->next )
    {
        maxfd = node->fd>maxfd ? node->fd : maxfd;
        FD_SET(node->fd, set); 
    }
 
    /* program will block here  */
    return select(maxfd+1, set, NULL, NULL, NULL);
}
 
int add_fd_list(link_node_t  **head, int fd)
{
    link_node_t          *node;
    link_node_t          *tmp;
 
    if( NULL == (node=(link_node_t *)malloc(sizeof(*node))) ) 
    {
        printf("malloc for new node failure\n");
        return -1;
    }
 
    memset(node, 0, sizeof(*node));
    node->fd = fd;
    node->next = NULL;
 
    if( *head == NULL )
    {
        *head = node;
        return 0;
    }
 
    tmp = *head;
    while( tmp->next != NULL )
    {
        tmp=tmp->next;
    }
 
    tmp->next = node;
}
 
 
int destroy_fd_list(link_node_t **head)
{
 
}
 
 
char      *fail_str[3]={"Verify user account get wrong format",
                        "Verify user account get wrong username",
                        "Verify user account get wrong password"};
int verify_user_logon(char *buf)
{
    char                *ptr = NULL;
 
    if( !(ptr=strchr(buf, ':')) )
    {
        return 1;
    }
 
    if( strncmp(buf, DEF_USER, strlen(DEF_USER)) )
    {
        return 2;
    }
 
    if( strncmp(ptr+1, DEF_PASSWD, strlen(DEF_PASSWD)) )
    {
        return 3;
    }
 
    return 0;
}
 
int proc_client_socket(link_node_t **head, fd_set *rdset )
{
    int                  client_fd;
    char                 buf[512];
    int                  rv;
    int                  maxfd=0;
    link_node_t         *node;
    link_node_t         *prev;
    int                  rmflag = 0;
 
    if(!head || !rdset)
        return -2;
 
 
    if( !rdset )
    {
        printf("Invalid arg\n");
        return -1;
    }
 
    node = *head;
    while( node != NULL )
    {
        //printf( "proc socket[%d] ##########\n", node->fd ); 
 
        if( FD_ISSET(node->fd, rdset) )
        {
            client_fd = node->fd;
            
            memset(buf, 0, sizeof(buf));
 
            if( (rv=read(client_fd, buf, sizeof(buf))) <= 0)
            {
                printf("socket[%d] disconncet or read get error\n", client_fd);
                rmflag = 1;
                goto REMOVE_CLIENT;
            }
 
            printf("socket[%d] read get %d bytes data\n", client_fd, rv);
#ifdef CONFIG_VERIFY_USER
            /* not logon before, then need logon information  */
            if( !node->logon )
            {
                if( 0 != (rv=verify_user_logon(buf)) )
                {
                    printf("client socket[%d] logon account [%s] invalid, disconncet it now\n", client_fd, buf);
                    write(client_fd, fail_str[rv-1], strlen(fail_str[rv-1]));
                    rmflag = 1;
                    goto REMOVE_CLIENT;
                }
                else
                {
                    printf("client socket[%d] logon successfully!\n", client_fd);
                    node->logon = 1;
                    write(client_fd, "passed", 6);
                }
            }
            else /* already logon  */
#endif
            {
                /* echo the data from client back  */
                write(client_fd, buf, rv);
            }
 
REMOVE_CLIENT:
            if( rmflag )
            {
                printf("remove socket client[%d] from list now\n", node->fd);
                /* node is first node  */
                if( node == *head )
                {
                    close(node->fd);
                    *head = node->next;
                    free(node);
                    node=*head;
                }
                else
                {
                    close(node->fd);
                    prev->next = node->next;
                    free(node);
                    node = prev->next;
                }
                rmflag = 0;
                continue;
            }
        }
 
        prev = node;
        node = node->next;
    }
 
    return 0;
}