From 1c79bf5f79365739fd07f08e0d21e973537fc5ec Mon Sep 17 00:00:00 2001 From: nirav Date: Sat, 21 Sep 2019 13:30:12 +0000 Subject: Add gph parser and client --- main.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 0d45d46..9a58508 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,3 @@ -#define _POSIX_C_SOURCE 200112L - #include #include #include @@ -41,6 +39,7 @@ create_server(const char *port) perror("getaddrinfo"); return -1; } + for (p = res; p != NULL; p = p->ai_next) { if ((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { @@ -94,9 +93,9 @@ accept_connection(int s_fd) struct sockaddr_storage sa; socklen_t salen; char host[HOSTMAX]; - int c_fd; + int cfd; - if ((c_fd = accept(s_fd, (struct sockaddr *) & sa, &salen)) == -1) { + if ((cfd = accept(s_fd, (struct sockaddr *) & sa, &salen)) == -1) { if (errno != EWOULDBLOCK || errno != EAGAIN) { perror("accept"); exit(1); @@ -110,17 +109,17 @@ accept_connection(int s_fd) } logmsg(LOG_INFO, "connection accepted from %s", host); - return c_fd; + return cfd; } static void -handle_client(int c_fd) +handle_client(int cfd) { int n; char buf[BUFSIZE]; char *lf; - n = recv(c_fd, &buf, BUFSIZE - 1, 0); + n = recv(cfd, &buf, BUFSIZE - 1, 0); switch (n) { case -1: if (errno != EWOULDBLOCK || errno != EAGAIN) { @@ -139,20 +138,20 @@ handle_client(int c_fd) *lf = '\0'; logmsg(LOG_INFO, "path: %s", buf); - handle_path(c_fd, buf); + handle_path(cfd, buf); } - close(c_fd); + close(cfd); } static void -main_loop(int s_fd) +main_loop(int sfd) { int i, rc, end_server = 0; - int c_fd, nfds = 1, curr_size = 0; + int cfd, nfds = 1, curr_size = 0; struct pollfd pfds[OPEN_MAX]; memset(pfds, 0, sizeof(pfds)); - pfds[0].fd = s_fd; + pfds[0].fd = sfd; pfds[0].events = POLLIN; do { @@ -175,15 +174,15 @@ main_loop(int s_fd) end_server = 1; break; } - if (pfds[i].fd == s_fd) { + if (pfds[i].fd == sfd) { do { - c_fd = accept_connection(s_fd); - if (c_fd != -1) { - pfds[nfds].fd = c_fd; + cfd = accept_connection(sfd); + if (cfd != -1) { + pfds[nfds].fd = cfd; pfds[nfds].events = POLLIN; nfds++; } - } while (c_fd != -1); + } while (cfd != -1); } else { handle_client(pfds[i].fd); pfds[i].fd = -1; -- cgit v1.2.3