diff options
Diffstat (limited to 'src/shared.h')
-rw-r--r-- | src/shared.h | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/shared.h b/src/shared.h new file mode 100644 index 0000000..a615f83 --- /dev/null +++ b/src/shared.h @@ -0,0 +1,140 @@ +/* +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 <stdlib.h> +#include <stdint.h> +#include <stdbool.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include <stdarg.h> + +#include <netinet/in.h> +#include <netinet/udp.h> +#include <sys/socket.h> +#include <unistd.h> + +#include <pthread.h> + +#include "eli.h" + +#define eprintf(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) +#define DEBUG(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__) + +#define LOCALHOST_NETWORK 0x7F000000 +#define LOCALHOST_MASK 0xFF000000 + +#define REQUEST_HEADER "query " +#define REQUEST_HEADER_LEN (sizeof(REQUEST_HEADER) - 1) +#define RESPONSE_HEADER "reply " + +// main.c + +extern int server_sockfd; + +// worker.c + +typedef enum { + JOB_TEST, + JOB_REVDNS, + JOB_WHOIS +} job_type_t; + +typedef struct job_s job_t; +struct job_s { + job_type_t type; + + uint32_t query; + struct sockaddr_in reply_to; + + eli_header list, dep_list; + job_t *parent, *deps; + size_t num_deps, deps_done; + pthread_mutex_t mutex; +}; + +job_t *job_create(void); +void job_enqueue(job_t *job); +void job_quit(void); +void *worker_main(void *unused); + +// cache.c + +typedef struct { + uint32_t ipv4; + + char *revdns; + uint64_t revdns_exptime; // FIXME: implement this already + char *whois; + uint64_t whois_exptime; + + eli_header ht_list; + pthread_mutex_t mutex; +} cache_entry_t; + +cache_entry_t *cache_find(uint32_t ipv4); +void cache_destroy(void); + +// lexer.c + +typedef struct { + char *data; + size_t size, alloc; +} vstr_t; + +#define VSTR_INITIALIZER ((vstr_t){NULL, 0, 0}) + +void vstr_init(vstr_t *vstr); +void vstr_destroy(vstr_t *vstr); +void vstr_clear(vstr_t *vstr); +int vstr_putc(vstr_t *vstr, char ch); +int vstr_cmp(vstr_t *vstr, const char *str); +char *vstr_strdup(vstr_t *vstr); +char *vstr_to_cstr(vstr_t *vstr); + +#define LEXER_BUFFER 1024 + +typedef struct { + int error; + const char *path; + FILE *fp; + bool eof; + + vstr_t *token; + char buf[LEXER_BUFFER]; + char *buf_c, *buf_e; + + size_t cc, lc, Cc; // character, line, and column counters + char last; + + bool in_token; + bool in_quote; + bool in_comment; +} lexer_state_t; + +int lexer_open(lexer_state_t *ls, const char *path, vstr_t *token); +void lexer_close(lexer_state_t *ls); +int lexer_get_token(lexer_state_t *ls); +void lexer_perror(lexer_state_t *ls, const char *fmt, ...); +void lexer_perror_eg(lexer_state_t *ls, const char *expected); + +// lists.c + +int lists_load(const char *file, size_t depth); +void lists_destroy(void); +int lists_test(const char *revdns, const char *whois); |