static int handle_line(struct parsedfile *config, char *line, int lineno) { char *words[10]; static char savedline[MAXLINE]; int nowords = 0, i; /* Save the input string */ strncpy(savedline, line, MAXLINE - 1); savedline[MAXLINE - 1] = (char) 0; /* Tokenize the input string */ nowords = tokenize(line, 10, words); /* Set the spare slots to an empty string to simplify */ /* processing */ for (i = nowords; i < 10; i++) words[i] = ""; if (nowords > 0) { /* Now this can either be a "path" block starter or */ /* ender, otherwise it has to be a pair (<name> = */ /* <value>) */ if (!strcmp(words[0], "path")) { handle_path(config, lineno, nowords, words); } else if (!strcmp(words[0], "}")) { handle_endpath(config, lineno, nowords, words); } else { /* Has to be a pair */ if ((nowords != 3) || (strcmp(words[1], "="))) { show_msg(MSGERR, "Malformed configuration pair " "on line %d in configuration " "file, \"%s\"\n", lineno, savedline); } else if (!strcmp(words[0], "reaches")) { handle_reaches(config, lineno, words[2]); } else if (!strcmp(words[0], "server")) { handle_server(config, lineno, words[2]); } else if (!strcmp(words[0], "server_port")) { handle_port(config, lineno, words[2]); } else if (!strcmp(words[0], "server_type")) { handle_type(config, lineno, words[2]); } else if (!strcmp(words[0], "default_user")) { handle_defuser(config, lineno, words[2]); } else if (!strcmp(words[0], "default_pass")) { handle_defpass(config, lineno, words[2]); } else if (!strcmp(words[0], "local")) { handle_local(config, lineno, words[2]); } else { show_msg(MSGERR, "Invalid pair type (%s) specified " "on line %d in configuration file, " "\"%s\"\n", words[0], lineno, savedline); } } } return(0); }
static void serv(struct sstate *ss, char *dev, int port, int chan) { int max; fd_set fds; struct client *c; int card_fd; open_card_and_sock(ss, dev, port, chan); card_fd = wi_fd(ss->ss_wi); while (1) { /* server */ max = ss->ss_s; FD_ZERO(&fds); FD_SET(max, &fds); /* clients */ c = ss->ss_clients.c_next; while (c != &ss->ss_clients) { FD_SET(c->c_s, &fds); if (c->c_s > max) max = c->c_s; c = c->c_next; } /* card */ FD_SET(card_fd, &fds); if (card_fd > max) max = card_fd; if (select(max+1, &fds, NULL, NULL, NULL) == -1) err(1, "select()"); /* handle clients */ c = ss->ss_clients.c_next; while (c != &ss->ss_clients) { if (FD_ISSET(c->c_s, &fds)) handle_client(ss, c); c = c->c_next; } /* handle server */ if (FD_ISSET(ss->ss_s, &fds)) handle_server(ss); if (FD_ISSET(card_fd, &fds)) handle_card(ss); } }
/** * Start the interface and main client loop */ int interface(struct config* config){ CONFIG = config; init_interface(); fd_set master; fd_set readset; FD_ZERO(&readset); FD_ZERO(&master); FD_SET(config->self.socket, &master); FD_SET(STDIN, &master); int max_sock = config->self.socket; struct timeval tv; while(EXIT == 0){ readset = master; tv.tv_sec = 0; tv.tv_usec = 100; select(max_sock+1, &readset, NULL, NULL, &tv); if(tv.tv_sec == 0 && tv.tv_usec == 0){ if(handle_user() != 0){ perror("BAD THINGS HAPPENED!"); EXIT = 1; } continue; } for(int i = 0; i < max_sock+1; ++i){ if (FD_ISSET(i, &readset)){ if(i == config->self.socket){ if(handle_server(config->self.socket) < 0){ close_interface(); perror("Connection closed to server"); return -1; } }else if(i == STDIN){ handle_user(); } } } refresh(); } return 0; }
int run_server(int argc, char ** argv) { DEBUG_OUT("---DEBUG MODE---\n"); if (argc < 4) { printf("Usage: %s <port> <adminname> <adminpass>\n", argv[0]); return 0; } server_port = atoi(argv[1]); DEBUG_OUT("Port: %d\n", server_port); strcpy(admin_name, argv[2]); strcpy(admin_pass, argv[3]); /* Create server socket */ server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket == -1) { perror("socket"); exit(-1); } DEBUG_OUT("Got server socket\n"); /* Reuse address */ socket_flags = 1; if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &socket_flags, sizeof(socket_flags)) < 0) { perror("setsockopt"); exit(-1); } /* Make socket non blocking */ socket_flags = fcntl(server_socket, F_GETFL, 0); if (fcntl(server_socket, F_SETFL, socket_flags | O_NONBLOCK) != 0) { perror("fcntl"); exit(-1); } DEBUG_OUT("Server socket is non blocking\n"); /* Bind to our port */ memset(&server_addr, 0, sizeof(struct sockaddr_in)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(server_port); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("bind"); exit(-1); } DEBUG_OUT("Server socket is bound\n"); /* Allocate queue for 10 sockets */ listen(server_socket, 10); DEBUG_OUT("Server socket has room for 10 incoming connections\n"); /* Add to readset */ FD_ZERO(&readset); FD_SET(server_socket, &readset); /* Initialize client buffer */ memset(client_buffer, 0, sizeof(client_buffer)); for (i = 0; i < MAX_CLIENTS - 1; i++) { client_buffer[i].next = &client_buffer[i + 1]; } free_clients = &client_buffer[0]; /* Run IO loop */ while (1) { reads = readset; DEBUG_OUT("Waiting forever for IO. "); DEBUG_OUT("Active client count: %d", count_clients(active_clients)); DEBUG_OUT(" Free client count: %d\n", count_clients(free_clients)); selected = select(FD_SETSIZE, &reads, NULL, NULL, NULL); if (selected < 0) { perror("select"); exit(-1); } DEBUG_OUT("select() returned %d\n", selected); if (selected > 0) { handle_clients(); handle_server(); } } return 0; }