void mysql_poolinit(void) { pthread_t pthread[THREADS]; db_config dbc; int i; strlcpy(dbc.host, dbhost, strlen(dbhost) + 1); strlcpy(dbc.user, dbuser, strlen(dbuser) + 1); strlcpy(dbc.pass, dbpass, strlen(dbpass) + 1); strlcpy(dbc.name, dbname, strlen(dbname) + 1); dbc.port = 0; dbc.socket = NULL; if (!mysql_thread_safe()) { vbprintf("Using own threaded pool with %d mysql threads\n", THREADS); } else { vbprintf("Using threaded mysql connection pool of %d threads\n", THREADS); } vbprintf("DB Connections: %d, Threads: %d\n", CONPOOL, THREADS); //Pre - init for (i = 0; i < CONPOOL; i++) { dbm[i].db = mysql_conn(dbm[i].db, &dbc); pthread_mutex_init(&dbm[i].lock, NULL); } vbprintf("MySQL init done.\n"); }
int main() { struct pcap_pkthdr header; // The header that pcap gives us const u_char* packet; // The actual packet char* device; // Sniffing device char errbuf[PCAP_ERRBUF_SIZE]; // Error message buffer pcap_t* handle; // Session handle struct bpf_program fp; // The compiled filter expression char filter_exp[] = "dst port 80"; // char filter_exp[] = "dst net 188.93.212.0/24 and not src net 188.93.208.0/21 and dst port 80"; // The filter expression bpf_u_int32 mask; // The netmask of our sniffing device bpf_u_int32 net; // The IP of our sniffing device device = NULL; memset(errbuf, 0, PCAP_ERRBUF_SIZE); conn = NULL; int fd; struct stat sbuf; pid_t pid; // MYSQL connect if ( !(conn = mysql_conn())) { fprintf(stderr, "Can't connect to MySQL server"); exit(1); } if ( (fd = open("capturefile", O_RDWR)) < 0) { fprintf(stderr, "Can't open output file"); exit(1); } ftruncate(fd, FILE_SIZE); data = mmap((caddr_t)0, FILE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (stat("capturefile", &sbuf) <0) { fprintf(stderr, "Can't stat"); exit(1); } if (data == (caddr_t)(-1)) { fprintf(stderr, "Can't mmap"); exit(1); } // Get device for capture device = pcap_lookupdev(errbuf); printf("Device: %s\n", device); printf("filter: %s\n", filter_exp); if (pcap_lookupnet(device, &net, &mask, errbuf) == -1) { fprintf(stderr, "Can't get netmask for device %s\n", device); net = 0; mask = 0; } handle = pcap_open_live(device, 1514, 0, 1, errbuf); // Compile filter for capture if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) { fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle)); exit(1); } // Apply compiled filter if (pcap_setfilter(handle, &fp) == -1) { fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle)); exit(1); } if ( (pid = fork()) < 0) { fprintf(stderr, "Can't fork"); exit(1); } else if (pid == 0) { // Capture pcap_loop(handle, 0, pget, NULL); } else { while(1) { flock(fd, LOCK_EX); printf("sleep...\n"); sleep(10); printf("truncate!\n"); ftruncate(fd, 0); ftruncate(fd, FILE_SIZE); flock(fd, LOCK_UN); } } pcap_close(handle); mysql_close(conn); return 0; }