12 typedef struct _ipslot {
30 static int readline(int fd,char **outp) {
32 char *curp = stream.data + stream.cur;
33 char *endp = stream.data + stream.end;
35 while ( curp < endp ) {
36 if ( *(curp++) == '\n' ) {
37 stream.cur = curp - stream.data;
42 if ( top != stream.data ) {
44 while ( top < endp ) {
48 stream.end = endp - stream.data;
51 ssize_t n = read( fd, endp, BUFSZ - stream.end );
53 if ( stream.end == 0 ) {
54 return -1; // No more data
56 (*outp) = stream.data;
64 // Scan to NUL, CR or c. Return pointer not including character.
65 static char *scanto(char *p, char c) {
66 while ( *p && *p != '\n' && *p != c ) {
72 static int parse_addr(char *line,ipslot *addr) {
73 char *end = scanto( line, '\n' );
74 char *slash = scanto( line, '/' );
77 if ( scanto( line, ':' ) != slash ) {
78 fprintf( stdout, "AF_INET6: %s\n", line );
79 if ( inet_pton( AF_INET6, line, addr->data ) == 1 ) {
80 addr->family = AF_INET6;
85 } else if ( inet_pton( AF_INET, line, addr->data ) == 1 ) {
86 fprintf( stdout, "AF_INET: %s\n", line );
87 addr->family = AF_INET;
92 if ( slash != end && sscanf( slash+1, "%u", &addr->bits ) != 1 ) {
98 static void add_entry(ipslot *tmp) {
99 ipslot *p = (ipslot *) malloc( sizeof( ipslot ) );
100 memmove( p, tmp, sizeof( ipslot ) );
101 if ( table.data.size == table.fill ) {
102 (void) Vector_resize( &table.data, table.fill + 256, 0, 0 );
104 Vector_set( &table.data, table.fill++, p );
107 static void load_file(const char *filename) {
108 int fd = open( filename, O_RDONLY );
115 while ( ( n = readline( fd, &line ) ) >= 0 ) {
117 if ( parse_addr( line, &addr ) ) {
118 fprintf( stderr, "Bad address: %s\n", line );
125 static int int_reclaim(Vector *pv,unsigned long index,void *item,void *data) {
129 static void dumpitem(const VectorIndex index,const void *item) {
130 fprintf( stdout, "[%ld] %p\n", index, item );
133 static void dump_ipslot(const VectorIndex index,const void *item) {
134 static char buffer[100];
135 ipslot *ip = (ipslot*) item;
136 const char *p = inet_ntop( ip->family, ip->data, buffer, 100 );
137 fprintf( stdout, "[%ld] %s/%d\n", index, p, ip->bits );
140 int main(int argc,char **argv) {
142 Vector_resize( &test, 100, 0, 0 );
143 Vector_set( &test, 5, (void*) 500 );
144 Vector_set( &test, 55, (void*) 600 );
145 Vector_resize( &test, 300, 0, 0 );
146 Vector_set( &test, 55, (void*) 650 );
147 Vector_resize( &test, 30000, 0, 0 );
148 Vector_set( &test, 22255, (void*) 26 );
149 Vector_dump( &test, dumpitem );
150 Vector_resize( &test, 100, int_reclaim, 0 );
151 Vector_set( &test, 5, (void*) 2 );
152 Vector_dump( &test, dumpitem );
155 for ( i = 1; i < argc; i++ ) {
156 load_file( argv[ i ] );
158 Vector_dump( &table.data, dump_ipslot );