Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int create_socket()
- {
- struct sockaddr_in sin;
- int reuse_addr = 1;
- fd = socket(AF_INET, SOCK_STREAM, 0);
- if(fd < 0)
- {
- syslog(LOG_ERR, "Can't create socket!");
- return -1;
- }
- syslog(LOG_INFO, "Socket created, fd: %d", fd);
- /* So that we can re-bind to it without TIME_WAIT problems */
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
- sizeof(reuse_addr));
- setnonblocking(fd);
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = INADDR_ANY;
- sin.sin_port = htons(PORT);
- /* bind the socket to the port number */
- if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1)
- {
- syslog(LOG_ERR, "Couldn't bind to socket");
- close(fd);
- return -1;
- }
- listen(fd, MAXCONN);
- high_fd = fd;
- memset((char *) &connectlist, 0, sizeof(connectlist));
- return 1;
- }
- void read_socket()
- {
- int listnum;
- /* OK, now socks will be set with whatever socket(s)
- are ready for reading. Lets first check our
- "listening" socket, and then check the sockets
- in connectlist. */
- /* If a client is trying to connect() to our listening
- socket, select() will consider that as the socket
- being 'readable'. Thus, if the listening socket is
- part of the fd_set, we need to accept a new connection. */
- if (FD_ISSET(fd, &socks))
- handle_new_connection();
- /* Now check connectlist for available data */
- /* Run through our sockets and check to see if anything
- happened with them, if so 'service' them. */
- for (listnum = 0; listnum < MAXCONN; listnum++)
- {
- if (FD_ISSET(connectlist[listnum], &socks))
- {
- connecttime[listnum] = time(NULL);
- deal_with_data(listnum);
- } /* for (all entries in queue) */
- }
- }
- void build_select_list()
- {
- int listnum;
- FD_ZERO(&socks);
- FD_SET(fd, &socks);
- for (listnum = 0; listnum < MAXCONN; listnum++)
- {
- if (connectlist[listnum] != 0)
- {
- FD_SET(connectlist[listnum], &socks);
- if (connectlist[listnum] > high_fd)
- high_fd = connectlist[listnum];
- }
- }
- }
- void handle_new_connection()
- {
- int listnum;
- int connection;
- /* We have a new connection coming in! We'll
- try to find a spot for it in connectlist. */
- connection = accept(fd, NULL, NULL);
- if (connection < 0)
- {
- syslog(LOG_ERR, "Connection accept failed.");
- exit(EXIT_FAILURE);
- }
- setnonblocking(connection);
- for (listnum = 0; (listnum < MAXCONN) && (connection != -1); listnum ++)
- if (connectlist[listnum] == 0)
- {
- connectlist[listnum] = connection;
- connecttime[listnum] = time(NULL);
- connection = -1;
- }
- if (connection != -1)
- {
- /* No room left in the queue! */
- syslog(LOG_WARNING, "No room left for new client.");
- char foo[50];
- sprintf(foo, "Sorry, this server is too busy. Try again later!\n");
- send(connection, foo, strlen(foo), 0);
- close(connection);
- }
- }
- int main(int argc, char* argv[])
- {
- int ret;
- struct timeval timeout;
- int readsocks;
- int listnum;
- time_t mytime, newtime;
- struct sigaction act;
- sigaction(SIGHUP, NULL, &act);
- if(act.sa_handler != SIG_IGN)
- {
- memset(&act, 0, sizeof(act));
- act.sa_handler = handler;
- act.sa_flags = SA_RESTART;
- sigemptyset(&act.sa_mask);
- sigaction(SIGHUP, &act, NULL);
- }
- sigaction(SIGTERM, NULL, &act);
- if(act.sa_handler != SIG_IGN)
- {
- memset(&act, 0, sizeof(act));
- act.sa_handler = handler;
- sigemptyset(&act.sa_mask);
- sigaction(SIGTERM, &act, NULL);
- }
- memset(&act, 0, sizeof(act));
- act.sa_handler = SIG_DFL;
- act.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP;
- sigaction(SIGCHLD, &act, NULL);
- if (daemon_init())
- {
- fprintf(stderr, "%s: Error initializing daemon\n", argv[0]);
- return -1;
- }
- ret = create_socket();
- if(ret < 0)
- {
- syslog(LOG_ERR, "couldn't create a socket!");
- exit(EXIT_FAILURE);
- }
- while (1)
- { /* Main server loop - forever */
- build_select_list();
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- if(flag_reread_bdb)
- {
- read_bdb();
- flag_reread_bdb = 0;
- }
- readsocks = select(high_fd+1, &socks, (fd_set *) 0,
- (fd_set *) 0, &timeout);
- if (readsocks < 0 && errno != EINTR)
- {
- syslog(LOG_ERR, "select() failed");
- exit(EXIT_FAILURE);
- }
- if(readsocks > 0)
- read_socket();
- for(listnum = 0; listnum < MAXCONN; listnum++)
- {
- if(connectlist[listnum] != 0)
- {
- if(newtime > (connecttime[listnum] + 60))
- {
- send_data("001 No data received in specified timeout, good bye.\n", &listnum);
- close_sock(listnum);
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement