Advertisement
Guest User

Untitled

a guest
Oct 24th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.05 KB | None | 0 0
  1. int create_socket()
  2. {
  3.   struct sockaddr_in sin;
  4.  
  5.   int reuse_addr = 1;
  6.  
  7.   fd = socket(AF_INET, SOCK_STREAM, 0);
  8.  
  9.   if(fd < 0)
  10.     {
  11.       syslog(LOG_ERR, "Can't create socket!");
  12.       return -1;
  13.     }
  14.  
  15.   syslog(LOG_INFO, "Socket created, fd: %d", fd);
  16.  
  17.   /* So that we can re-bind to it without TIME_WAIT problems */
  18.   setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
  19.              sizeof(reuse_addr));
  20.  
  21.   setnonblocking(fd);
  22.  
  23.   memset(&sin, 0, sizeof(sin));
  24.   sin.sin_family = AF_INET;
  25.   sin.sin_addr.s_addr = INADDR_ANY;
  26.   sin.sin_port = htons(PORT);
  27.  
  28.   /* bind the socket to the port number */
  29.   if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
  30.     {
  31.       syslog(LOG_ERR, "Couldn't bind to socket");
  32.       close(fd);
  33.       return -1;
  34.     }
  35.  
  36.   listen(fd, MAXCONN);
  37.  
  38.   high_fd = fd;
  39.  
  40.   memset((char *) &connectlist, 0, sizeof(connectlist));
  41.   return 1;
  42. }
  43.  
  44. void read_socket()
  45. {
  46.   int listnum;
  47.  
  48.   /* OK, now socks will be set with whatever socket(s)
  49.      are ready for reading.  Lets first check our
  50.      "listening" socket, and then check the sockets
  51.      in connectlist. */
  52.  
  53.   /* If a client is trying to connect() to our listening
  54.      socket, select() will consider that as the socket
  55.      being 'readable'. Thus, if the listening socket is
  56.      part of the fd_set, we need to accept a new connection. */
  57.  
  58.   if (FD_ISSET(fd, &socks))
  59.     handle_new_connection();
  60.   /* Now check connectlist for available data */
  61.  
  62.   /* Run through our sockets and check to see if anything
  63.      happened with them, if so 'service' them. */
  64.  
  65.   for (listnum = 0; listnum < MAXCONN; listnum++)
  66.     {
  67.       if (FD_ISSET(connectlist[listnum], &socks))
  68.         {
  69.           connecttime[listnum] = time(NULL);
  70.           deal_with_data(listnum);
  71.         } /* for (all entries in queue) */
  72.     }
  73. }
  74.  
  75. void build_select_list()
  76. {
  77.   int listnum;
  78.  
  79.   FD_ZERO(&socks);
  80.   FD_SET(fd, &socks);
  81.  
  82.   for (listnum = 0; listnum < MAXCONN; listnum++)
  83.     {
  84.       if (connectlist[listnum] != 0)
  85.         {
  86.           FD_SET(connectlist[listnum], &socks);
  87.           if (connectlist[listnum] > high_fd)
  88.             high_fd = connectlist[listnum];
  89.         }
  90.     }
  91. }
  92.  
  93. void handle_new_connection()
  94. {
  95.   int listnum;
  96.   int connection;
  97.  
  98.   /* We have a new connection coming in!  We'll
  99.      try to find a spot for it in connectlist. */
  100.   connection = accept(fd, NULL, NULL);
  101.  
  102.   if (connection < 0)
  103.     {
  104.       syslog(LOG_ERR, "Connection accept failed.");
  105.       exit(EXIT_FAILURE);
  106.     }
  107.  
  108.   setnonblocking(connection);
  109.  
  110.   for (listnum = 0; (listnum < MAXCONN) && (connection != -1); listnum ++)
  111.     if (connectlist[listnum] == 0)
  112.       {
  113.         connectlist[listnum] = connection;
  114.         connecttime[listnum] = time(NULL);
  115.         connection = -1;
  116.       }
  117.  
  118.   if (connection != -1)
  119.     {
  120.       /* No room left in the queue! */
  121.       syslog(LOG_WARNING, "No room left for new client.");
  122.  
  123.       char foo[50];
  124.       sprintf(foo, "Sorry, this server is too busy. Try again later!\n");
  125.       send(connection, foo, strlen(foo), 0);
  126.       close(connection);
  127.     }
  128. }
  129.  
  130.  
  131. int main(int argc, char* argv[])
  132. {
  133.   int ret;
  134.   struct timeval timeout;
  135.   int readsocks;
  136.   int listnum;
  137.   time_t mytime, newtime;
  138.   struct sigaction act;
  139.  
  140.  
  141.   sigaction(SIGHUP, NULL, &act);
  142.   if(act.sa_handler != SIG_IGN)
  143.     {
  144.       memset(&act, 0, sizeof(act));
  145.       act.sa_handler = handler;
  146.       act.sa_flags = SA_RESTART;
  147.       sigemptyset(&act.sa_mask);
  148.       sigaction(SIGHUP, &act, NULL);
  149.     }
  150.  
  151.   sigaction(SIGTERM, NULL, &act);
  152.   if(act.sa_handler != SIG_IGN)
  153.     {
  154.       memset(&act, 0, sizeof(act));
  155.       act.sa_handler = handler;
  156.       sigemptyset(&act.sa_mask);
  157.       sigaction(SIGTERM, &act, NULL);
  158.     }
  159.  
  160.   memset(&act, 0, sizeof(act));
  161.   act.sa_handler = SIG_DFL;
  162.   act.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP;
  163.   sigaction(SIGCHLD, &act, NULL);
  164.  
  165.   if (daemon_init())
  166.     {
  167.       fprintf(stderr, "%s: Error initializing daemon\n", argv[0]);
  168.       return -1;
  169.     }
  170.  
  171.   ret = create_socket();
  172.  
  173.   if(ret < 0)
  174.     {
  175.       syslog(LOG_ERR, "couldn't create a socket!");
  176.       exit(EXIT_FAILURE);
  177.     }
  178.  
  179.   while (1)
  180.     { /* Main server loop - forever */
  181.       build_select_list();
  182.       timeout.tv_sec = 1;
  183.       timeout.tv_usec = 0;
  184.  
  185.       if(flag_reread_bdb)
  186.         {
  187.           read_bdb();
  188.           flag_reread_bdb = 0;
  189.         }
  190.  
  191.       readsocks = select(high_fd+1, &socks, (fd_set *) 0,
  192.                          (fd_set *) 0, &timeout);
  193.  
  194.       if (readsocks < 0 && errno != EINTR)
  195.         {
  196.           syslog(LOG_ERR, "select() failed");
  197.           exit(EXIT_FAILURE);
  198.         }
  199.  
  200.       if(readsocks > 0)
  201.         read_socket();
  202.  
  203.       for(listnum = 0; listnum < MAXCONN; listnum++)
  204.         {
  205.           if(connectlist[listnum] != 0)
  206.             {
  207.               if(newtime > (connecttime[listnum] + 60))
  208.                 {
  209.                   send_data("001 No data received in specified timeout, good bye.\n", &listnum);
  210.                   close_sock(listnum);
  211.                 }
  212.             }
  213.         }
  214.     }
  215.  
  216.   return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement