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
/*********************************************************************************
 *      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"
 
 
void *socket_client_worker(void *arg);
 
 
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;
    struct sockaddr_in        servaddr;
    int                       serv_port = 0;
    int                       daemon_run = 0;
    char                     *progname = NULL;
    pthread_t                 tid;
    int                       opt;
    int                       on=1;
 
 
    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(0==serv_port) 
    {   
        print_usage(progname);
        return -1; 
    }
 
    /* set program running on background */
    if( daemon_run )
    {
        daemon(0, 0);
    }
 
    /*
     * 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 */
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);  /* Listen all the local IP address */
    servaddr.sin_port = htons(serv_port); /*  daytime server 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));
        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));
        goto CleanUp;
    }
    printf("%s server start to listen port %d\n", argv[0],serv_port);
 
 
    for ( ; ; ) 
    {
        /*
         * The accept() system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET).
         * It extracts the first connection request on the queue of pending connections for the listening 
         * socket linstenfd, creates a new connected socket(connfd), and returns a new file descriptor referring 
         * to that socket. The newly created socket is not in the listening state. The original socket 
         * listenfd is unaffected by this call.
         */
 
        if( (connfd=accept(listenfd, (struct sockaddr *)NULL, NULL)) > 0)
        {
            pthread_create(&tid, NULL, socket_client_worker, (void *)connfd);
        }
    }
 
CleanUp:
    close(listenfd);  /* We must close socket File Description when program exit*/
    return 0;
}
 
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);
}
 
 
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;
}
 
void *socket_client_worker(void *arg)
{
    int            client_fd;
    char           buf[512];
    int            rv;
 
    if( !arg )
    {
        printf("Invalid arg\n");
        return NULL;
    }
 
    client_fd = (int) arg;
 
#ifdef CONFIG_VERIFY_USER
    memset(buf, 0, sizeof(buf));
    if( (rv=read(client_fd, buf, sizeof(buf))) <= 0)
    {
        printf("socket disconncet or read get error\n");
        goto CleanUp;
    }
 
    if( 0 != (rv=verify_user_logon(buf)) )
    {
        printf("client logon account [%s] invalid, disconncet it now\n", buf);
        write(client_fd, fail_str[rv-1], strlen(fail_str[rv-1]));
        goto CleanUp;
    }
 
    write(client_fd, "passed", 6);
#endif
 
    while(1)
    {
        memset(buf, 0, sizeof(buf));
        if( (rv=read(client_fd, buf, sizeof(buf))) <= 0)
        {
            printf("socket disconncet or read get error\n");
            goto CleanUp;
        }
 
        /* echo the data from client back  */
        printf("socket[%d] read get %d bytes data and will echo it back\n", client_fd, rv);
        write(client_fd, buf, rv);
    }
 
 
CleanUp:
    printf("Thread worker for socket[%d] exit.\n", client_fd);
    msleep(500);
    close(client_fd);
}