int run_serveur(t_serv *serv) { t_user *user_list; fd_set readfd; int max; int socks; char *buff; max = serv->s; user_list = NULL; if ((buff = malloc(4096 * sizeof(char))) == NULL) return (EXIT_FAILURE); while (1) { reset_list(&readfd, user_list, serv, max); if (FD_ISSET(serv->s, &readfd)) { accept_connect(&socks, serv); if (read_client(socks, buff) != 0) { client_connexion(&max, socks, &readfd); add_user(&user_list, DEFAULT_USER, socks, NULL); } } else send_rcv(&user_list, &readfd); } }
int epoll_loop() { #define MAX_EVENTS 10 int epollfd, nfds, n, listen_sock, conn_sock; struct epoll_event ev, events[MAX_EVENTS]; epollfd = epoll_create1(EPOLL_CLOEXEC); if (epollfd == -1) { return -1; } listen_sock = open_listen_socket(); ev.events = EPOLLIN; ev.data.fd = listen_sock; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) { perror("epoll_ctl: listen_sock"); //exit(EXIT_FAILURE); } for (;;) { nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); if (nfds == -1) { } for (n = 0; n < nfds; ++n) { if (events[n].data.fd == listen_sock) { conn_sock = accept_connect(listen_sock); if (setnonblocking(conn_sock) < 0) { perror("setnonblocking"); } ev.events = EPOLLIN | EPOLLET; ev.data.fd = conn_sock; if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) { } } else { int fd = events[n].data.fd; char buf[255]; int len = recv(fd, buf, 255, MSG_DONTWAIT); buf[len] = '\0'; printf("%s\n", buf); } } } return 0; }
int main(int argc, char *argv[]) { int flags; /* register at exit handler */ if (atexit(exit_nice) < 0) { fprintf(stderr, "atexit failed\n"); exit(1); } /* reg sig handlers */ (void) signal(SIGHUP, sig_catch); (void) signal(SIGINT, sig_catch); (void) signal(SIGQUIT, sig_catch); (void) signal(SIGTERM, sig_catch); (void) signal(SIGPIPE, sig_pipe); /* init the global structs and arrays */ init_structs(); /* Create socket */ if((accept_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("Socket open failed"); exit(1); } FD_SET(accept_sock, &sock_read); FD_SET(accept_sock, &sock_except); /* Bind to given port */ if(bind(accept_sock, (struct sockaddr *) &uart_addr, sizeof(uart_addr)) < 0) { perror("Bind failed"); exit(1); } listen(accept_sock, 5); flags = fcntl (accept_sock, F_GETFL); flags |= O_NONBLOCK; fcntl (accept_sock, F_SETFL, flags); sock_max = accept_sock; /* Now the uart_sock is in the listen state, we can monitor it * for incomming connections. when a connection is accepted, the new * socket is added to the array */ while (1) { int nevents; int i; nevents = select(sock_max + 1, &sock_read, &sock_write, &sock_except, NULL); if (nevents <= 0) { perror("select"); goto FD_READD; } /* see if this is a new connection on accept_sock */ if (FD_ISSET(accept_sock, &sock_read)) { nevents--; accept_connect(accept_sock); } /* see if any other socket is ready for reading */ while (nevents-- > 0) { int sock_index; int read_fd, write_fd; sock_index = get_next_fd(&read_fd, &write_fd); if (sock_index < 0) { printf ("Error for event %d\n", nevents + 1); goto FD_READD; } if (read_write(read_fd, write_fd) < 0) { /* close connection */ del_sockindex(sock_index); goto FD_READD; } } /* add fds to fd set */ FD_READD: { int count = 0; int *uart = sock_pair.uart; int *client = sock_pair.client; FD_ZERO(&sock_read); FD_SET(accept_sock, &sock_read); sock_max = accept_sock; for (i=0; i <MAX_CONN && count <= sock_pair.len; i++) { if (uart[i] != -1 && client[i] != -1) { FD_SET(uart[i], &sock_read); FD_SET(client[i], &sock_read); /* set the max sock count */ if (uart[i] > client[i] && uart[i] > sock_max) sock_max = uart[i]; else if (client[i] > uart[i] && client[i] > sock_max) sock_max = client[i]; count++; } } //printf ("Added total of %d FDs\n", 2*count+1); } } }
int main (int argc, char **argv) { int server_fd, fifo_fd, res, i, nelems; char buf[LINE_MAX], packet[PACKET_SIZE]; entropy_src[0].id = 0; entropy_src[0].path = "/dev/random"; entropy_src[0].estimate = 0.4; entropy_src[0].len = 100; entropy_src[0].id = 1; entropy_src[0].path = "/dev/urandom"; entropy_src[0].estimate = 0.5; entropy_src[0].len = 120; entropy_src[0].id = 2; entropy_src[0].path = NULL; entropy_src[0].estimate = 0.8; entropy_src[0].len = 120; mkfifo(FIFO_PATH, 0777); fifo_fd = open(FIFO_PATH, O_NONBLOCK); if (fifo_fd == -1) printf("open returned %d: %s\n", fifo_fd, strerror(fifo_fd)); nsources = 2; // this value will be taken from entropy_pool.nsources // create children for accumulate entropy from i source // All information about source will be obtained from config file. for (i = 0; i < nsources; i++) { if ((pid = fork()) < 0) { printf("Fork returned %d: %s\n", pid, strerror(pid)); exit(1); } else if (pid == 0) { accumulate_samples(i); } server_fd = sock_unix_listen("/var/run/yarrow.socket"); if (server_fd <= 0) { perror("Sock unix listen"); exit(1); } printf("server_fd %d\n", server_fd); sock_nonblock(server_fd); poll_fd = calloc(2, sizeof(struct pollfd)); if (poll_fd == NULL) { perror("Calloc returned NULL."); exit(1); } poll_fd[0].fd = server_fd; poll_fd[0].events = POLLIN; poll_fd[1].fd = fifo_fd; poll_fd[1].events = (POLLIN|POLLPRI); peer_ctx = calloc(2, sizeof(struct peer)); if (peer_ctx == NULL) { perror("Calloc returned NULL"); exit(1); } peer_ctx[0].sfd = server_fd; peer_ctx[0].bufused = 0; peer_ctx[1].sfd = fifo_fd; peer_ctx[1].bufused = 0; nelems = 2; i = 0; while (1) { res = poll(poll_fd, nelems, -1); if (res > 0) { if (poll_fd[0].revents & POLLIN) { accept_connect(&nelems); } else if (poll_fd[1].revents & POLLIN) { accumulate_entropy(packet); } process_events(nelems); } else if (res < 0 && errno != EINTR) { printf("poll returned %d: %s\n", res, strerror(errno)); break; } } close(server_fd); unlink("/var/run/yarrow.socket"); return 0; }