1
/* Copyright (c) 2007-2010 Sam Trenholme
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
15
* This software is provided 'as is' with no guarantees of correctness or
16
* fitness for purpose.
19
#ifndef __DWSYS_H_DEFINED__
20
#define __DWSYS_H_DEFINED__
32
/* Parameters that are currently hardcoded in the source code (This will
33
* change once we get something very basic that works) */
35
#define DW_UID 99 /* UID that we run as */
36
#define DW_MINTIME 1178488417 /* Minimum allowed timestamp */
38
/* Make this look like a function just in case we need to make it
39
* a function to make things thread-safe */
40
#define get_time() the_time
42
/* These are public functions */
44
/* Initialize the log */
48
void dw_tcp_log_init();
54
/* Log a char followed by an IP */
55
void dw_log_ip(char *string, ip_addr_T *ip, int min_log_level);
57
/* Log a string followed by the contents of a DwStr object */
58
void dw_log_dwstr(char *s1, dw_str *s2, int min_log_level);
60
/* Log a string followed by the contents of a DwStr object followed by
62
void dw_log_dwstr_str(char *s1, dw_str *s2, char *s3, int min_log_level);
64
/* Log a string; input: String to log; minimum log level that we log this
66
void dw_log_string(char *string, int min_log_level);
68
/* Log 3 strings; input: Strings to log; minimum log level that we log these
70
void dw_log_3strings(char *s1, char *s2, char *s3, int min_log_level);
72
/* Log a string, a number, and a string
73
* input: String #1, Number, and String #2 to log;
74
* minimum log level that we log this at */
75
void dw_log_number(char *s1, int number, char *s2, int min_log_level);
77
/* Log a string and a number in hex */
78
void dw_log_hex(char *s1, uint32_t number, int min_log_level);
80
/* Log 3 strings; input: Strings to log; minimum log level that we log these
81
* strings at; this always logs and is run before Dwood2rc file is parsed */
82
void dw_alog_3strings(char *s1, char *s2, char *s3);
84
/* Log a string, a number, and a string
85
* input: String #1, Number, and String #2 to log;
86
* minimum log level that we log this at
87
* This always logs and is run before Dwood2rc file is fully parsed */
88
void dw_alog_number(char *s1, int number, char *s2);
90
/* Exit with a fatal error and log it */
91
void dw_fatal(char *why);
93
/* Set the 64-bit timestamp starting at 290805600 unix() time (When
94
* the Blake's 7 episode Gambit was originally broadcast); this should
95
* be called once a second or so */
98
/* Read mararc parameters and set global variables based on those
100
void process_mararc();
102
/* Initialize the cache */
106
/* Assign handlers for TERM, HUP, and USR1 signals, so we can write
107
* the cache to a file */
108
void setup_signals();
111
/* Process a signal received, writing the cache to a file */
112
void process_signal(int number);
114
/* Initialize random number generator. */
117
/* Drop privileges and become unprivileged user */
120
/* Get, from the Mararc parameters, the list of bind addresses
121
* we will bind to; return this list as a comma-separated dw_str */
122
dw_str *get_bind_addrs();
124
/* Make sure a DNS packet is sane, and return the packet's
125
* query ID. If roy_arends_check has a value of 1, we also make sure
126
* it's a DNS question (and not a reply). This is named in honor of
127
* Roy Arends, who pointed out the original MaraDNS didn't check this.
128
* If this returns -1, the packet was not sane */
129
int32_t get_dns_qid(unsigned char *a, int len, int roy_arends_check);
131
/* Given a string with a DNS packet, and the length of that string,
132
* make the first two butes of the packet (the query ID) the third
133
* argument (qid), and return that random number. -1 on error */
134
int32_t set_dns_qid(unsigned char *packet, int len, uint16_t qid);
136
/* This function converts a dw_str object in to a null-terminated
137
* C-string with the last item in the comma-separated list in the
138
* dw_str, with any leading whitespace in the last item removed */
139
char *pop_last_item(dw_str *list);
141
/* This creates a netmask from a single number, storing the number in a
142
* string of octets. 0 just makes the string all 0s; 1 makes the string
143
* 0x80 0x00 0x00 etc.; 2 makes the string 0xc0 0x00 etc.; etc.
144
* The string of octets has a length len (4 for ipv4; 16 for ipv6) */
145
void make_netmask(int num, uint8_t *str, int len);
147
/* Get a numeric value from the mararc parameters and make sure it is
148
* within a range; if def is not -1, make an out-of-range parameter
150
int32_t get_key_n(int32_t get, int32_t min, int32_t max, int32_t def);
152
#endif /* __DWSYS_H_DEFINED__ */