/* * Copyright (c) 2023 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* ipdb database format: [uint32_t][0] file offset of network "0.0.0.0/8" [uint32_t][1] file offset of network "1.0.0.0/8" ... [uint32_t][255] file offset of network "255.0.0.0/8" [uint8_t] location 0 length [char][length] location 0 [uint8_t] location 1 length [char][length] location 1 ... [uint32_t] network 0 start (1.0.0.0) [uint32_t] network 0 end (1.0.0.255) [uint32_t] network 0 location pointer [uint32_t] network 1 start (1.0.1.0) [uint32_t] network 1 end (1.0.1.255) [uint32_t] network 1 location pointer ... For an ip "5.6.7.8", look up table[5] and seek there, then do a binary search between that location and that of table[6] (or end of file if 255). If the table location points to 0, that network is not in the file. */ #ifndef __IPDB_H__ #define __IPDB_H__ #include "util.h" struct ipdb_file { short fh; size_t fsize; u_int32_t starts[256]; }; struct ipdb_file * ipdb_open(char *filename); void ipdb_close(struct ipdb_file **ipdbp); char * ipdb_lookup(struct ipdb_file *ipdb, u_int32_t ip); #endif