Download
jcs
/subtext
/ipdb.h
(View History)
jcs ipdb: Add IP geolocation database lookup module | Latest amendment: 509 on 2023-06-15 |
1 | /* |
2 | * Copyright (c) 2023 joshua stein <jcs@jcs.org> |
3 | * |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
17 | /* ipdb database format: |
18 | |
19 | [uint32_t][0] file offset of network "0.0.0.0/8" |
20 | [uint32_t][1] file offset of network "1.0.0.0/8" |
21 | ... |
22 | [uint32_t][255] file offset of network "255.0.0.0/8" |
23 | [uint8_t] location 0 length |
24 | [char][length] location 0 |
25 | [uint8_t] location 1 length |
26 | [char][length] location 1 |
27 | ... |
28 | [uint32_t] network 0 start (1.0.0.0) |
29 | [uint32_t] network 0 end (1.0.0.255) |
30 | [uint32_t] network 0 location pointer |
31 | [uint32_t] network 1 start (1.0.1.0) |
32 | [uint32_t] network 1 end (1.0.1.255) |
33 | [uint32_t] network 1 location pointer |
34 | ... |
35 | |
36 | For an ip "5.6.7.8", look up table[5] and seek there, then do a binary |
37 | search between that location and that of table[6] (or end of file if |
38 | 255). |
39 | |
40 | If the table location points to 0, that network is not in the file. |
41 | */ |
42 | |
43 | #ifndef __IPDB_H__ |
44 | #define __IPDB_H__ |
45 | |
46 | #include "util.h" |
47 | |
48 | struct ipdb_file { |
49 | short fh; |
50 | size_t fsize; |
51 | u_int32_t starts[256]; |
52 | }; |
53 | |
54 | struct ipdb_file * ipdb_open(char *filename); |
55 | void ipdb_close(struct ipdb_file **ipdbp); |
56 | char * ipdb_lookup(struct ipdb_file *ipdb, u_int32_t ip); |
57 | |
58 | #endif |