diff --git a/lewdfingerd.c b/lewdfingerd.c index 92ed741..f6c1b87 100644 --- a/lewdfingerd.c +++ b/lewdfingerd.c @@ -23,6 +23,10 @@ void log_error(const char * restrict format, ...) { va_end(args); } +void log_perror(const char *string) { + perror(string); +} + void setup_listen(void) { struct addrinfo hints; struct addrinfo *getaddrinfo_result; @@ -50,33 +54,33 @@ void setup_listen(void) { // Create socket int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(sock == -1) { - perror("socket"); + log_perror("socket"); exit(1); } // Disable the IPv4 over IPv6, as that results in IPv4 and IPv6 sockets conflicting and one of them not being able to be set up if(res->ai_family == AF_INET6) { if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) { - perror("setsockopt"); + log_perror("setsockopt"); exit(1); } } // Set reuseaddr if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { - perror("setsockopt"); + log_perror("setsockopt"); exit(1); } // Bind onto given address if(bind(sock, res->ai_addr, res->ai_addrlen) == -1) { - perror("bind"); + log_perror("bind"); exit(1); } // Listen for incoming connections if(listen(sock, 1) == -1) { - perror("listen"); + log_perror("listen"); exit(1); } @@ -90,7 +94,7 @@ void setup_listen(void) { listens = realloc(listens, num_listens * sizeof(struct pollfd)); if(listens == NULL) { - perror("realloc"); + log_perror("realloc"); exit(1); } @@ -107,11 +111,11 @@ void drop_privileges(void) { gid_t gid = getgid(); if(setresgid(gid, gid, gid) != 0) { - perror("setresgid"); + log_perror("setresgid"); exit(1); } if(setresuid(uid, uid, uid) != 0) { - perror("setresuid"); + log_perror("setresuid"); exit(1); } } @@ -130,6 +134,20 @@ ssize_t writeall(int fd, const char *buf, size_t amount) { return written; } +void handle_userlist(int sock) { + const char *response = "Who do you want to finger?\r\n"; + if(writeall(sock, response, strlen(response)) < 0) { + log_perror("write"); + } +} + +void handle_query(int sock, const char *username) { + const char *response = "Lewd.\r\n"; + if(writeall(sock, response, strlen(response)) < 0) { + log_perror("write"); + } +} + void handle_connection(int sock) { size_t request_size = 0; char *request = NULL; @@ -146,7 +164,7 @@ void handle_connection(int sock) { close(sock); return; } else if(amount_read < 0) { - perror("read"); + log_perror("read"); shutdown(sock, SHUT_RDWR); close(sock); return; @@ -155,7 +173,7 @@ void handle_connection(int sock) { size_t index = request_size; request_size += amount_read; if((request = realloc(request, request_size)) == NULL) { - perror("realloc"); + log_perror("realloc"); exit(1); } memmove(request + index, iobuf, amount_read); @@ -169,15 +187,9 @@ void handle_connection(int sock) { request[request_size-2] = '\0'; if(!strcmp(request, "")) { - const char *response = "Who do you want to finger?\r\n"; - if(writeall(sock, response, strlen(response)) < 0) { - perror("write"); - } + handle_userlist(sock); } else { - const char *response = "Lewd.\r\n"; - if(writeall(sock, response, strlen(response)) < 0) { - perror("write"); - } + handle_query(sock, request); } shutdown(sock, SHUT_RDWR); @@ -191,7 +203,7 @@ int main(void) { for(;;) { int amount_ready = poll(listens, num_listens, -1); if(amount_ready < 0) { - perror("poll"); + log_perror("poll"); exit(1); }