Groundwork for later
This commit is contained in:
parent
687ca490dd
commit
8f2342242e
|
@ -23,6 +23,10 @@ void log_error(const char * restrict format, ...) {
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void log_perror(const char *string) {
|
||||||
|
perror(string);
|
||||||
|
}
|
||||||
|
|
||||||
void setup_listen(void) {
|
void setup_listen(void) {
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *getaddrinfo_result;
|
struct addrinfo *getaddrinfo_result;
|
||||||
|
@ -50,33 +54,33 @@ void setup_listen(void) {
|
||||||
// Create socket
|
// Create socket
|
||||||
int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
if(sock == -1) {
|
if(sock == -1) {
|
||||||
perror("socket");
|
log_perror("socket");
|
||||||
exit(1);
|
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
|
// 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(res->ai_family == AF_INET6) {
|
||||||
if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
|
if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1) {
|
||||||
perror("setsockopt");
|
log_perror("setsockopt");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set reuseaddr
|
// Set reuseaddr
|
||||||
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
|
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
|
||||||
perror("setsockopt");
|
log_perror("setsockopt");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind onto given address
|
// Bind onto given address
|
||||||
if(bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
|
if(bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
|
||||||
perror("bind");
|
log_perror("bind");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen for incoming connections
|
// Listen for incoming connections
|
||||||
if(listen(sock, 1) == -1) {
|
if(listen(sock, 1) == -1) {
|
||||||
perror("listen");
|
log_perror("listen");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +94,7 @@ void setup_listen(void) {
|
||||||
listens = realloc(listens, num_listens * sizeof(struct pollfd));
|
listens = realloc(listens, num_listens * sizeof(struct pollfd));
|
||||||
|
|
||||||
if(listens == NULL) {
|
if(listens == NULL) {
|
||||||
perror("realloc");
|
log_perror("realloc");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,11 +111,11 @@ void drop_privileges(void) {
|
||||||
gid_t gid = getgid();
|
gid_t gid = getgid();
|
||||||
|
|
||||||
if(setresgid(gid, gid, gid) != 0) {
|
if(setresgid(gid, gid, gid) != 0) {
|
||||||
perror("setresgid");
|
log_perror("setresgid");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if(setresuid(uid, uid, uid) != 0) {
|
if(setresuid(uid, uid, uid) != 0) {
|
||||||
perror("setresuid");
|
log_perror("setresuid");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,6 +134,20 @@ ssize_t writeall(int fd, const char *buf, size_t amount) {
|
||||||
return written;
|
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) {
|
void handle_connection(int sock) {
|
||||||
size_t request_size = 0;
|
size_t request_size = 0;
|
||||||
char *request = NULL;
|
char *request = NULL;
|
||||||
|
@ -146,7 +164,7 @@ void handle_connection(int sock) {
|
||||||
close(sock);
|
close(sock);
|
||||||
return;
|
return;
|
||||||
} else if(amount_read < 0) {
|
} else if(amount_read < 0) {
|
||||||
perror("read");
|
log_perror("read");
|
||||||
shutdown(sock, SHUT_RDWR);
|
shutdown(sock, SHUT_RDWR);
|
||||||
close(sock);
|
close(sock);
|
||||||
return;
|
return;
|
||||||
|
@ -155,7 +173,7 @@ void handle_connection(int sock) {
|
||||||
size_t index = request_size;
|
size_t index = request_size;
|
||||||
request_size += amount_read;
|
request_size += amount_read;
|
||||||
if((request = realloc(request, request_size)) == NULL) {
|
if((request = realloc(request, request_size)) == NULL) {
|
||||||
perror("realloc");
|
log_perror("realloc");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
memmove(request + index, iobuf, amount_read);
|
memmove(request + index, iobuf, amount_read);
|
||||||
|
@ -169,15 +187,9 @@ void handle_connection(int sock) {
|
||||||
request[request_size-2] = '\0';
|
request[request_size-2] = '\0';
|
||||||
|
|
||||||
if(!strcmp(request, "")) {
|
if(!strcmp(request, "")) {
|
||||||
const char *response = "Who do you want to finger?\r\n";
|
handle_userlist(sock);
|
||||||
if(writeall(sock, response, strlen(response)) < 0) {
|
|
||||||
perror("write");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const char *response = "Lewd.\r\n";
|
handle_query(sock, request);
|
||||||
if(writeall(sock, response, strlen(response)) < 0) {
|
|
||||||
perror("write");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shutdown(sock, SHUT_RDWR);
|
shutdown(sock, SHUT_RDWR);
|
||||||
|
@ -191,7 +203,7 @@ int main(void) {
|
||||||
for(;;) {
|
for(;;) {
|
||||||
int amount_ready = poll(listens, num_listens, -1);
|
int amount_ready = poll(listens, num_listens, -1);
|
||||||
if(amount_ready < 0) {
|
if(amount_ready < 0) {
|
||||||
perror("poll");
|
log_perror("poll");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue