/* // AUTHOR // N. Nielsen // // VERSION // 0.2 // // LICENSE // This software is in the public domain. // // The software is provided "as is", without warranty of any kind, // express or implied, including but not limited to the warranties // of merchantability, fitness for a particular purpose, and // noninfringement. In no event shall the author(s) be liable for any // claim, damages, or other liability, whether in an action of // contract, tort, or otherwise, arising from, out of, or in connection // with the software or the use or other dealings in the software. // // SUPPORT // Send bug reports to: */ #include #include #include #include #include #include #include #include void readIps(FILE* fIn); void writeIps(FILE* fOut); /* DB of traffic accessed recently */ DB* g_db = NULL; /* TODO: Use an actual compare function */ int main(int argc, char* argv[]) { BTREEINFO bi; memset(&bi, 0, sizeof(bi)); /* bi.compare = compareIp; */ g_db = dbopen(NULL, O_CREAT | O_RDWR, 0, DB_BTREE, (const void*)&bi); if(g_db == NULL) errx(1, "db initialization failed"); readIps(stdin); writeIps(stdout); } /* * Parse helpers */ static void eatSpace(FILE* file) { char ch; do { ch = getc(file); if(ch == '#') { while(ch != '\n') ch = getc(file); } } while(isspace(ch) && !feof(file) && !ferror(file)); ungetc(ch, file); } /* * Read Input */ void readIps(FILE* fIn) { DBT key, value; struct in_addr addr; char address[16]; key.data = &addr; key.size = sizeof(addr); value.data = &addr; value.size = sizeof(addr); eatSpace(fIn); while(!feof(fIn) && !ferror(fIn)) { char ch; int i = 0; /* Read a word and store the first 16 chars */ while(!feof(fIn) && !ferror(fIn)) { ch = fgetc(fIn); if(isspace(ch)) break; if(i < 16) address[i] = ch; i++; } address[i] = 0; if(!inet_pton(AF_INET, address, (void*)&addr)) warnx("invalid ip in address: %s", address); if(g_db->put(g_db, &key, &value, 0) < 0) err(1, "database error: %s\n"); eatSpace(fIn); } if(ferror(fIn)) err(1, "error reading addresses"); } /* * Write collated output back out */ void writeIps(FILE* fOut) { DBT key, value; int ret; struct in_addr prev; /* The previous address */ struct in_addr top; /* The address at the top of the current stack */ int size; /* The stack size */ memset(&key, 0, sizeof(key)); memset(&value, 0, sizeof(value)); /* Get first key from DB */ ret = g_db->seq(g_db, &key, &value, R_FIRST); while(ret == 0) { printf("%s\n", inet_ntoa(*((struct in_addr*)key.data))); ret = g_db->seq(g_db, &key, &value, R_NEXT); } }