/* Copyright (C) 2017 Paweł Redman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "shared.h" #include int server_sockfd; static struct timespec time_ref; uint64_t get_time(void) { static struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return (uint64_t)(now.tv_sec - time_ref.tv_sec) * TIME_SECOND + now.tv_nsec + 1; } static int parse_req_addr(char *begin, char *end, uint32_t *out) { char *p; size_t i = 0, parts[4] = {0, 0, 0, 0}; for (p = begin; p < end; p++) { if (*p >= '0' && *p <= '9') { parts[i] *= 10; parts[i] += *p - '0'; if (parts[i] > 255) return 1; } else if (*p == '.') { i++; if (i > 3) return 1; } else break; } if (i != 3) return 1; *out = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]; return 0; } void handle_request(struct sockaddr_in *ret_addr, uint32_t query) { db_entry *entry; job_t *reply_job; uint64_t now = get_time(); DEBUG("query %08X\n", query); entry = db_find(query); if (!entry) { eprintf("handle_request: out of memory\n"); return; } reply_job = job_create(JOB_REPLY, entry); reply_job->ipv4 = query; memcpy(&reply_job->ret_addr, ret_addr, sizeof(struct sockaddr_in)); if (entry->revdns.state == DB_VALID && entry->revdns.exp_time <= now) { entry->revdns.state = DB_INVALID; DEBUG("revDNS for %08X expired\n", query); } if (entry->whois.state == DB_VALID && entry->whois.exp_time <= now) { entry->whois.state = DB_INVALID; DEBUG("WHOIS for %08X expired\n", query); } if (entry->revdns.state == DB_VALID && entry->whois.state == DB_VALID) { job_enqueue(reply_job); goto out; } if (entry->revdns.state == DB_INVALID) { job_t *job; DEBUG("revDNS cache miss for %08X\n", query); job = job_create(JOB_REVDNS, entry); job->ipv4 = query; job_enqueue(job); entry->revdns.state = DB_IN_PROGRESS; } if (entry->whois.state == DB_INVALID) { job_t *job; DEBUG("WHOIS cache miss for %08X\n", query); job = job_create(JOB_WHOIS, entry); job->ipv4 = query; job_enqueue(job); entry->whois.state = DB_IN_PROGRESS; } eli_append(&entry->waiting_jobs, reply_job, waiting_list); out: pthread_mutex_unlock(&entry->mutex); } #define NUM_WORKERS 8 // FIXME: shouldn't be hardcoded int main(void) { int error = 0; // return value struct sockaddr_in sockaddr; pthread_t workers[NUM_WORKERS]; size_t i; clock_gettime(CLOCK_MONOTONIC_RAW, &time_ref); if (lists_load("schachts.list", 0)) { eprintf("fatal error: couldn't load the lists\n"); goto error_lists; } server_sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (server_sockfd == -1) { perror("socket"); error = 1; goto error_socket; } sockaddr.sin_family = AF_INET; sockaddr.sin_addr.s_addr = INADDR_ANY; sockaddr.sin_port = htons(1337); if (bind(server_sockfd, (void*)&sockaddr, sizeof(sockaddr)) == -1) { perror("bind"); error = 1; goto error_bind; } for (i = 0; i < NUM_WORKERS; i++) pthread_create(workers + i, NULL, worker_main, NULL); while (1) { char buffer[256]; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); ssize_t size; uint32_t query; size = recvfrom(server_sockfd, buffer, sizeof(buffer), 0, (void*)&addr, &addrlen); if (size == -1) { perror("recvfrom"); goto error_recvfrom; } if ((ntohl(addr.sin_addr.s_addr) & LOCALHOST_MASK) != LOCALHOST_NETWORK) continue; if (size < REQUEST_HEADER_LEN) continue; if (memcmp(buffer, REQUEST_HEADER, REQUEST_HEADER_LEN)) continue; if (parse_req_addr(buffer + REQUEST_HEADER_LEN, buffer + size, &query)) continue; handle_request(&addr, query); } job_quit(); for (i = 0; i < NUM_WORKERS; i++) pthread_join(workers[i], NULL); db_destroy(); // todo: clear all jobs error_recvfrom: error_bind: shutdown(server_sockfd, SHUT_RDWR); close(server_sockfd); error_socket: lists_destroy(); error_lists: return error; }