summaryrefslogtreecommitdiff
path: root/mod_ip.c
blob: cdcea3886d6a00dc66f7df88d963c3e9aa231e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
   Copyright 2013 Jason Fesler

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/


/* This module reports the IP the user came from, as a JSONP response. GET requests required; CGI style arguments are permitted.
 * callback=[name]     to call a function name of your choosing size=[number]       to bad the http data portion of the response.
 * (Does not attempt to offset header response) If the callback is "?", it will output to the screen (calling it "callback"). Any
 * other, and a JSON mime type is used instead. */


/* NOTICE
 * 
 * Portions of this module are inspired by: "The Apache Modules Book: Application Development with Apache"  by Nick Kew; published by
 * Prentice Hall, January 27 2007.
 * 
 */

/* Changes:
 * 
 * 2020-04-18 - jfesler@gigo.com
 *              Support X-Real-IP
 *
 * 2013-04-29 - jfesler@gigo.com
 *              Add headers to output (content type, expires, cache control)
 *              Stop depending on mod_headers (and user config)
 * 
 * 2013-04-28 - jfesler@gigo.com
 *              Add built in definitions for 6to4 and Teredo
 *              Seperate the function that adds new prefixes, from the config parser/hook
 *              Made ./configure friendly
 *
 * 2013-04-14 - jfesler@gigo.com
 *            - ASN queries also populate a string with the entire list of ASNs
 *
 * 2013-03-23 - jfesler@gigo.com
 *            - ASN queries always compiled in.  Triggers on asn=1 in request arguments.
 *            - ALWAYS_PADDING now full time.
 *            - Conditional compiling directives removed.  Troublesome across platforms.
 *
 * 2013-01-25 - eric@vyncke.org
 *	      - conditional compile for ASN related DNS queries
 *	      - also report asn_name
 *
 * 2012-12-26 - eric@vyncke.org
 *            - also return the ASN number (to be used to detect tunnels by the client) code from mysasn.code.google.com
 *
 * 2011-02-07 -  ondrej.sury@nic.cz
 *            - use apache (per request) memory pools and not static buffers 
 *            - drop my_strtok (now thread safe code) 
 *            - optimize parse_form_from_string 
 *            - rewrite output function 
 *            - limit maximum padding size to 1600 (see the #define) 
 *            - always add padding="" if compiled with -DALWAYS_PADDING 
 *            - limit the maximum length of callback function name to 64 (#define) 
 *            - some more micro optimizations to make code less nested, etc. 
 *            - limit callback name characters (security)
 * 
 * 2011-02-20 - jfesler@gigo.com 
 *            - new parameter: testip=  (40 chars or less; alphanum) for testing purposes 
 *            - prefixes are now parsed  on startup; and do cidr notation. 
 *            - http "Via" lines are now reported.
 *
 * 2011-02-27 - jfesler@gigo.com - use config file instead.  mod_ip_prefix 2002::/16 "6to4"
 *            - Also, disabled proxy reporting; just returns empty string or 'yes' now.  Suspect in core dumps.
 * 
 * 2011-04-19 - ondrej@sury.org - Generate size buffer on-the-fly, not in the buffer
 *
 * 2011-05-02 - ondrej@sury.org - Move MAX_PADDING_SIZE to config file (mod_ip_max_padding)
 *
 */



#include <ctype.h>
#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <http_log.h>
#include <apr_hash.h>
#include <apr_strings.h>
#include <apr_network_io.h>

#include "ip2asn.h"

/* Those unfortunately look like they claim the autoconf variables, so let's undefine them */

#undef PACKAGE_VERSION
#undef PACKAGE_URL
#undef PACKAGE_STRING
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_TARNAME

/* We need some libraries besides what Apache provides. */

#include "config.h"



#define MAX_PADD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YW1jb3JwZXIgZnJpbmdpbGxhLiBQcm9pbiBmZWxpcyBtYXVyaXMsIGFjY3Vt";

#define DELIM "&;"

#define ASNLIST_LENGTH 64

#if AP_SERVER_MAJORVERSION_NUMBER > 2 || \
    (AP_SERVER_MAJORVERSION_NUMBER == 2 && AP_SERVER_MINORVERSION_NUMBER >= 4)
#define CLIENT_IP(request) ((request)->useragent_ip)
#else
#define CLIENT_IP(request) ((request)->connection->remote_ip)
#endif

typedef struct mod_ip_request_t
{
	apr_int64_t size;
	apr_byte_t getasn;
	char *callback;
	char *testip;
} mod_ip_request_t;


typedef struct mod_ip_prefixes_t
{
	apr_ipsubnet_t *ip;
	char *provider;
	void *next;
} mod_ip_prefixes_t;

mod_ip_prefixes_t *mod_ip_prefix_head = NULL;



typedef struct
{
	apr_ipsubnet_t *cidr;
	char *info;
} cidrinfo_type;

typedef struct
{
	apr_array_header_t *cidrinfo;
	apr_int64_t max_padding;
} mod_ip_svr_cfg;


static void *mod_ip_create_svr_conf (apr_pool_t * pool, server_rec *x);
static void *mod_ip_merge_svr_conf (apr_pool_t * pool, void *BASE, void *ADD);
static void mod_ip_hooks (apr_pool_t * pool);
static const char *mod_ip_config_subnet (cmd_parms * cmd, void *CFG, const char *arg1, const char *arg2);
static const char *mod_ip_max_padding(cmd_parms * cmd, void *CFG, const char *arg);
module AP_MODULE_DECLARE_DATA mod_ip_module;


/* Parse form data from a string. The input string is NOT preserved. */
static mod_ip_request_t *
parse_form_from_string (request_rec * r, char *args, mod_ip_request_t * formdata)
{
	char *pair;
	char *eq;
	char *last = NULL;
	mod_ip_svr_cfg *cfg = ap_get_module_config (r->server->module_config, &mod_ip_module);

	/* Sanity check */
	if (formdata == NULL) {
		return NULL;
	}
	/* Defaults */
	formdata->size = 0;
	formdata->callback = NULL;
	formdata->testip = NULL;

	/* No arguments?  No parsing! */
	if (args == NULL) {
		return formdata;
	}
	/* Split the input on '&' */
	for (pair = apr_strtok (args, DELIM, &last); pair != NULL; pair = apr_strtok (NULL, DELIM, &last)) {

		/* Unescape '+' to ' ' */
		for (eq = pair; *eq; ++eq) {
			if (*eq == '+') {
				*eq = ' ';
			}
		}

		if (strncmp (pair, "size=", 5) == 0) {
			formdata->size = apr_atoi64 (pair + 5);
			if (formdata->size > cfg->max_padding) {
				ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_ERR, 0,
					      r, "[mod_ip.c] size: `%lld' >= max_padding_size `%d'", (long long) formdata->size,
					      MAX_PADDING_SIZE);
				return NULL;
			}
		} else if (strncmp (pair, "asn=", 4) == 0) {
			formdata->getasn = (apr_atoi64 (pair + 4) > 0);
		} else if (strncmp (pair, "callback=", 9) == 0) {
			if (strlen (pair + 9) > MAX_CALLBACK_SIZE) {
				/* Max function name length */
				ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_ERR, 0,
					       r,
					       "[mod_ip.c] function name length: `%s' (`%zu') >= max_callback_size `%d'",
					       pair + 9, strlen (pair + 9), MAX_CALLBACK_SIZE);
				return NULL;
			}
			ap_unescape_url (pair + 9);
			ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_DEBUG, 0, r, "[mod_ip.c] checking function name: `%s'",
				       pair + 9);
			if (strcmp (pair + 9, "?") != 0) {	/* If callback=? then use plain_text */
				for (eq = pair + 9; *eq; ++eq) {
					if (!isalnum (*eq) && (*eq != '_')) {
						/* Callback function name violation */
						ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_ERR, 0,
							       r, "[mod_ip.c] function name: `%s' violates naming convention: `%c'",
							       pair + 9, *eq);
					}
				}
				formdata->callback = apr_pstrdup (r->pool, (const char *) pair + 9);
			}
		} else if (strncmp (pair, "testip=", 7) == 0) {
			if (strlen (pair + 7) > MAX_TESTIP_SIZE) {
				ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_ERR, 0,
					       r,
					       "[mod_ip.c] function name length: `%s' (`%zu') >= max_callback_size `%d'",
					       pair + 9, strlen (pair + 9), MAX_CALLBACK_SIZE);
				return NULL;

			}
			for (eq = pair + 7; *eq; ++eq) {
				if (!isalnum (*eq) && (*eq != ':') && (*eq != '.')) {
					/* Callback function name violation */
					ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_ERR, 0,
						       r, "[mod_ip.c] testip value: `%s' violates naming convention: `%c'",
						       pair + 7, *eq);
					return NULL;
				}
			}
			formdata->testip = apr_pstrdup (r->pool, (const char *) pair + 7);
		}
	}

	return formdata;
}

static mod_ip_request_t *
parse_form_from_GET (request_rec * r, mod_ip_request_t * formdata)
{
	return parse_form_from_string (r, r->args, formdata);
}



static char    *
escape_string(request_rec * r, char *str)
{
	char           *newstr = NULL;
	char           *source = NULL;
	char           *dest = NULL;
	char           *maxdest = NULL;
	int		l;

	if (str == NULL) {
		return "";
	}
	/* We might potentially escape EVERY character.  Worst case. */
	l = strlen(str);
	newstr = apr_palloc(r->pool, l * 2 + 2);

	source = str;
	dest = newstr;
	maxdest = newstr + l * 2;

	while ((*source) && (dest < maxdest)) {
		switch (*source) {
		case '"':
			*(dest++) = '\\';
			*(dest++) = '\"';
			source++;
			break;
		case '\r':
			*(dest++) = '\\';
			*(dest++) = 'r';
			source++;
			break;
		case '\n':
			*(dest++) = '\\';
			*(dest++) = 'n';
			source++;
			break;
		case '\b':
			*(dest++) = '\\';
			*(dest++) = 'b';
			source++;
			break;
		default:
			*(dest++) = *(source++);
		}
	}
	*dest = '\0';
	return newstr;
}

char *
mod_ip_find_info (request_rec * r, char *myip)
{
	int i = 0;
	apr_array_header_t *arr;
	mod_ip_svr_cfg *cfg = ap_get_module_config (r->server->module_config, &mod_ip_module);
	apr_sockaddr_t *sockaddr = NULL;
	apr_status_t rv;
	cidrinfo_type *cidrinfo = NULL;
	apr_ipsubnet_t *cidr;
	char *info;
	 

	if ((cfg) && (cfg->cidrinfo) && ((rv = apr_sockaddr_info_get (&sockaddr, myip, AF_INET6, 0, 0, r->pool)) == APR_SUCCESS)) {
		arr = cfg->cidrinfo;  /* apr_array_header_t */
/*                cidrinfo = (cidrinfo_type**) arr->elts;*/
		for (i = 0; i < arr->nelts; i++) {

                       
		       cidrinfo = &((cidrinfo_type*) arr->elts)[i];
		       cidr = cidrinfo->cidr;
		       info = cidrinfo->info;
		       
			if (apr_ipsubnet_test (cidr, sockaddr)) {
				return info;
			}
		}
	}

	return "";
}

void
gen_output (request_rec * r, struct mod_ip_request_t *formdata)
{
	size_t output_len;
	char *padding = ",\"padding\":\"";
	char *p = NULL;
	int added = 0;
	char *myip = CLIENT_IP(r);
	char *mytype = "ipv4";
	char *mysubtype = "";
	char *VIA = "";
	char *RIP = "";
	char *asnlist = NULL;
	

	/* Identify X-Forwarded-For; include in results after stripping characters. */
	RIP = escape_string (r, (char *) apr_table_get (r->headers_in, "X-Real-IP"));
	VIA = escape_string (r, (char *) apr_table_get (r->headers_in, "Via"));

        if (strlen(RIP)>0) 
                myip = RIP;
	if (!myip)
		myip = "0.0.0.0 undefined";
	if (formdata->testip)
		myip = formdata->testip;

        /* If Teredo or 6to4, don't do ASN lookups.  The data will always show *some* ISP, but it isn't ours.*/
	if ((strncmp(myip, "2001:0:", 7) == 0) || (strncmp(myip, "2002:", 5) == 0)) {
	  formdata->getasn=0;
	}	
		

	if (strchr (myip, ':')) {
		mytype = "ipv6";
		mysubtype = mod_ip_find_info(r,myip);
	}
	/* Start generating a reply */

	p = apr_psprintf (r->pool,
			  "%s({\"ip\":\"%s\",\"type\":\"%s\",\"subtype\":\"%s\",\"via\":\"%s\"",
			  formdata->callback ? formdata->callback : "callback", myip, mytype, mysubtype, VIA);
	ap_rputs(p, r);
	output_len = strlen (p) + 4;

	unsigned long int my_asn ;
	char * asn_name ;

	if (formdata->getasn) {
		my_asn = GetASN(r, myip, &asnlist);
		if (my_asn > 0) {
			p = apr_psprintf (r->pool, ",\"asn\":\"%ld\"", my_asn);
			ap_rputs(p, r);
			output_len += strlen (p) ;
			asn_name = apr_palloc(r->pool, 64) ;
			if ((asn_name != NULL) && (GetASNName(my_asn, asn_name, 64) >= 0)) {
				p = apr_psprintf (r->pool, ",\"asn_name\":\"%s\"", asn_name);
				ap_rputs(p, r);
				output_len += strlen (p) ;
			}
		}
		if ((asnlist) && (asnlist[0])) {
		  p = apr_psprintf(r->pool,  ",\"asnlist\":\"%s\"", asnlist);
		  ap_rputs(p, r);
		  output_len += strlen (p) ;
		}
	}

	ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0,
		       r, "[mod_ip.c] size: `%lld', output_len: `%zd'", (long long) formdata->size, output_len);

	if (formdata->size > output_len) {
		added = formdata->size - output_len - strlen (padding);
		if (added < 0) {
			added = 0;
		}
	} else {
		added = 0;
	}

	ap_log_rerror (APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "[mod_ip.c] added: `%d', padding: `%s'", added, padding);

	if (padding) {
		output_len += strlen (padding) + added;
	}

	if (padding) {
		ap_rputs(padding, r);
		while (added > 0) {
			ap_rwrite(padding_buffer, (added > PADDING_BUFFER_SIZE)?PADDING_BUFFER_SIZE:added, r);
			added -= PADDING_BUFFER_SIZE;
		}
		ap_rputc('\"', r);
	}
	ap_rputs("})\n", r);
}


static int
mod_ip_handler (request_rec * r)
{
	mod_ip_request_t *formdata;

	if ((r->handler == NULL) || (strcmp (r->handler, "mod_ip") != 0)) {
		return DECLINED;
	}
	
	/* Set the headers */
	ap_set_content_type(r,"text/javascript;charset=UTF-8");
	apr_table_set(r->headers_out,"Cache-Control","no-cache");
	apr_table_set(r->headers_out,"Pragma","no-cache");
	apr_table_set(r->headers_out,"Expires","Thu, 01 Jan 1971 00:00:00 GMT");
	apr_table_set(r->headers_out,"X-Mod-Ip",PACKAGE_VERSION);
	
	
	/* pcalloc sets memory to '\0' */
	formdata = (mod_ip_request_t *) apr_pcalloc (r->pool, sizeof (mod_ip_request_t));

	/* GET requests: parse data.  Else, abort. */
	if (r->method_number != M_GET) {
		return HTTP_METHOD_NOT_ALLOWED;
	}
	formdata = parse_form_from_GET (r, formdata);
	if (formdata == NULL) {
		ap_rputs ("<p>Error reading form data!</p>", r);
		/* Bad user. No cookie. */
		return HTTP_BAD_REQUEST;
	}
	/* Display data. */
	if (formdata->callback == NULL) {
		/* interactive user called (no callback specified) */
		ap_set_content_type (r, "text/plain;charset=UTF-8");
	} else {
		/* jsonp called */
		ap_set_content_type (r, "application/javascript;charset=UTF-8");
	}

	/* Report to the user their IP */
	/* ap_rputs (gen_output (r, formdata), r); */
	gen_output(r, formdata);

	/* ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Survived line %u", __LINE__); */

	return OK;
}



/*
 * ################################################################
 * # Apache config file parsing                                   #
 * ################################################################
 */


static const command_rec mod_ip_cmds[] = {
	AP_INIT_TAKE2 ("mod_ip_prefix", mod_ip_config_subnet, NULL, OR_ALL,
		       "Define a known subnet"),
	AP_INIT_TAKE1 ("mod_ip_max_padding", mod_ip_max_padding, NULL, OR_ALL,
		       "Define maximum padding size"),
	{NULL}
};

module AP_MODULE_DECLARE_DATA mod_ip_module = {
	STANDARD20_MODULE_STUFF,
	NULL,				/* Per-directory configuration handler */
	NULL,				/* Merge handler for per-directory configurations */
	mod_ip_create_svr_conf,		/* Per-server configuration handler */
	mod_ip_merge_svr_conf,		/* Merge handler for per-server configurations */
	mod_ip_cmds,			/* Any directives we may have for httpd */
	mod_ip_hooks			/* Our hook registering function */
};





static const char *
mod_ip_config_subnet_parsed (apr_pool_t * pool, void *CFG, const char *arg1, const char *arg2)
{
//  asm("int3");  /* DEBUGGER */

	char *where = NULL;
	char *bits = NULL;
	apr_status_t rv;
	mod_ip_svr_cfg *cfg = CFG;

	if (!cfg) {
		return "Failed to get config pointer";
	}


	cidrinfo_type *newelt = apr_array_push (cfg->cidrinfo);
	newelt->info = apr_pstrdup (pool, arg2);


	where = apr_pstrdup (pool, arg1);
	if ((bits = ap_strchr (where, '/')) != NULL) {
		*bits++ = '\0';
	}
	rv = apr_ipsubnet_create (&newelt->cidr, where, bits, pool);
	if (rv != APR_SUCCESS) {
		return ("mod_ip_prefix: Bad prefix");
	}
	if (!newelt->cidr) {
	  return "mod_ip_prefix: Bad prefix";
	}


	return (NULL);
};


static const char *
mod_ip_config_subnet (cmd_parms * cmd, void *CFG, const char *arg1, const char *arg2)
{
	/* See Section 9.4.1 Configuration functions */
//  asm("int3");  /* DEBUGGER */

	apr_pool_t *pool = cmd->pool;

	server_rec *s = cmd->server;
	mod_ip_svr_cfg *cfg = ap_get_module_config (s->module_config, &mod_ip_module);

	if (!cfg) {
		return "Failed to get config pointer";
	}
	
	return mod_ip_config_subnet_parsed(pool,cfg,arg1,arg2);
};


void *
mod_ip_create_svr_conf (apr_pool_t * pool, server_rec *x)
{
	mod_ip_svr_cfg *svr = apr_pcalloc (pool, sizeof (mod_ip_svr_cfg));
	svr->cidrinfo = apr_array_make (pool, MAX_PREFIXES, sizeof (cidrinfo_type));
	svr->max_padding = PADDING_DEFAULT_SIZE;
	
	mod_ip_config_subnet_parsed(pool,svr,"2001:0::/32","Teredo");
	mod_ip_config_subnet_parsed(pool,svr,"2002::/16","6to4");
	return svr;
}




static const char *
mod_ip_max_padding(cmd_parms * cmd, void *CFG, const char *arg)
{
	apr_int64_t padding;
	server_rec *s = cmd->server;
	mod_ip_svr_cfg *cfg = ap_get_module_config (s->module_config, &mod_ip_module);
	char buf[120];
	
	padding = apr_atoi64(arg);

	if (padding < 1600)
		return apr_pstrcat(cmd->temp_pool, "mod_ip_max_padding \"", arg,
				   "\" must be a greater than 1600",
				   NULL);

	if (padding > MAX_PADDING_SIZE)
		return apr_pstrcat(cmd->temp_pool, "mod_ip_max_padding \"", arg,
				   "\" too large (>", apr_ltoa(cmd->temp_pool, MAX_PADDING_SIZE), ")",
				   NULL);

	if (padding < 0)
		return apr_pstrcat(cmd->temp_pool, "mod_ip_max_padding \"", arg,
				   "\" must be a non-negative integer",
				   NULL);

	cfg->max_padding = padding;

	return (NULL);
}

static void *
mod_ip_merge_svr_conf (apr_pool_t * pool, void *BASE, void *ADD)
{
	mod_ip_svr_cfg *base = (mod_ip_svr_cfg *) BASE;
	mod_ip_svr_cfg *add = (mod_ip_svr_cfg *) ADD;
	mod_ip_svr_cfg *conf = apr_palloc (pool, sizeof (mod_ip_svr_cfg));
	/* With an APR data type we can delegate all the real work to APR */
	conf->cidrinfo = apr_array_append (pool, base->cidrinfo, add->cidrinfo);
	conf->max_padding = (base->max_padding > add->max_padding)?base->max_padding:add->max_padding;
	return conf;
};





/* Hook our handler into Apache at startup */
static void
mod_ip_hooks (apr_pool_t * pool)
{
	ap_hook_handler (mod_ip_handler, NULL, NULL, APR_HOOK_MIDDLE);
}