/*********************************************************************************
|
* 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;
|
}
|