static gboolean gio_handler(GIOChannel *channel, GIOCondition cond, gpointer data) { struct msghdr msg; struct iovec iov; int sock; int len; guint8 ctrl[CMSG_SPACE(sizeof(struct timeval))]; struct cmsghdr *cmsg; struct timeval msg_timestamp; if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) { print_error("Sniffer IO error 0x%x\n", cond); if (watch > 0) g_source_remove(watch); watch = 0; gio_channel = NULL; return FALSE; } sock = g_io_channel_unix_get_fd(channel); memset(&msg, 0, sizeof(struct msghdr)); msg.msg_control = &ctrl; msg.msg_controllen = sizeof(ctrl); msg.msg_iov = &iov; msg.msg_iovlen = 1; iov.iov_base = buffer; iov.iov_len = BUFFER_LEN; len = recvmsg(sock, &msg, 0); if (len < 0) { print_error("recv: %s", strerror(errno)); return FALSE; } cmsg = CMSG_FIRSTHDR(&msg); if (cmsg && cmsg->cmsg_type == SCM_TIMESTAMP) memcpy(&msg_timestamp, CMSG_DATA(cmsg), sizeof(struct timeval)); else gettimeofday(&msg_timestamp, NULL); llcp_print_pdu(buffer, len, &msg_timestamp); pcap_file_write_packet(buffer, len, &msg_timestamp); return TRUE; }
void packet_callback(u_char *user, const struct pcap_pkthdr *pkthdr, const u_char *packet) { int fd = *(int *)user; pcap_file_write_packet(fd, packet, pkthdr->len); }