Exemplo n.º 1
0
s64 now_usecs(void)
{
	struct timeval tv;
	if (gettimeofday(&tv, NULL) < 0)
		die_perror("gettimeofday");
	return timeval_to_usecs(&tv);
}
Exemplo n.º 2
0
void
stat_cache_test (char *path)
{
        struct timeval          starttime, endtime;
        u_int64_t               startusecs, endusecs;
        
        gettimeofday (&starttime, NULL);
        if (traverse_dir (path) < 0)
                return;
        gettimeofday (&endtime, NULL);

        startusecs = timeval_to_usecs (starttime);
        endusecs = timeval_to_usecs (endtime);
        fprintf (stdout, "Run: %"PRIu64" usecs ", (endusecs-startusecs));
        fflush (stdout);

}
Exemplo n.º 3
0
int packet_socket_receive(struct packet_socket *psock,
			  enum direction_t direction, u16 *ether_type,
			  struct packet *packet, int *in_bytes)
{
	int status = 0;
	struct pcap_pkthdr *pkt_header = NULL;
	const u8 *pkt_data = NULL;
	pcap_t *pcap;
	struct ether_header *ether;
	u32 address_family;

	DEBUGP("calling pcap_next_ex() for direction %s\n",
	       direction == DIRECTION_INBOUND ? "inbound" : "outbound");

	if (direction == DIRECTION_INBOUND)
		pcap = psock->pcap_in;
	else
		pcap = psock->pcap_out;

	/* Something about the way we're doing BIOCIMMEDIATE
	 * causes libpcap to return 0 if there's no packet
	 * yet, which forces us to spin in this loop until
	 * there's a packet available.  If, on the other hand,
	 * we hack libpcap itself to enable its internal
	 * BIOCIMMEDIATE code path that it currently only uses
	 * for AIX, then we don't have to spin
	 * here. TODO(ncardwell): fix this.
	 */
	while (1) {
		status = pcap_next_ex(pcap, &pkt_header, &pkt_data);
		if (status == 1)
			break;		/* got a packet */
		else if (status == 0)
			return STATUS_ERR;	/* no packet yet */
		else if (status == -1)
			die_pcap_perror(pcap, "pcap_next_ex");
		else if (status == -2)
			die("pcap_next_ex: EOF in save file?!\n");
		else
			die("pcap_next_ex: status: %d\n", status);
	}

	DEBUGP("time: %u . %u\n",
	       (u32)pkt_header->ts.tv_sec,
	       (u32)pkt_header->ts.tv_usec);

#if defined(__FreeBSD__) || defined(__NetBSD__)
	packet->time_usecs = timeval_to_usecs(&pkt_header->ts);
#elif defined(__OpenBSD__)
	packet->time_usecs = bpf_timeval_to_usecs(&pkt_header->ts);
#else
	packet->time_usecs = implement_me("implement me for your platform");
#endif  /* defined(__OpenBSD__) */

	DEBUGP("time_usecs= %llu\n", packet->time_usecs);

	DEBUGP("pcap_next_ex: caplen:%u len:%u offset:%d\n",
	       pkt_header->caplen, pkt_header->len, psock->pcap_offset);

	if (DEBUG_LOGGING) {
		/* Dump a hex dump of packet sniffed by pcap. */
		char *hex = NULL;
		hex_dump(pkt_data, pkt_header->caplen, &hex);
		DEBUGP("pkt from pcap:\n%s\n", hex);
		free(hex);
	}

	if (pkt_header->caplen != pkt_header->len) {
		die("libpcap unable to capture full packet: "
		    "caplen %u != len %u\n",
		    pkt_header->caplen, pkt_header->len);
	}
	assert(pkt_header->len <= packet->buffer_bytes);

	assert(pkt_header->len > psock->pcap_offset);
	switch (psock->data_link) {
	case DLT_EN10MB:
		ether = (struct ether_header *)pkt_data;
		*ether_type = ntohs(ether->ether_type);
		break;
	case DLT_LOOP:
	case DLT_NULL:
#if defined(__OpenBSD__)
		address_family = ntohl(*(u32 *)pkt_data);
#else
		address_family = *(u32 *)pkt_data;
#endif
		switch (address_family) {
		case AF_INET:
			*ether_type = ETHERTYPE_IP;
			break;
		case AF_INET6:
			*ether_type = ETHERTYPE_IPV6;
			break;
		default:
			DEBUGP("Unknown address family %d.\n", address_family);
			return STATUS_ERR;
		}
		break;
	default:
		assert(!"not reached");
		break;
	}
	DEBUGP("ether_type is 0x%04x\n", *ether_type);
	*in_bytes = pkt_header->len - psock->pcap_offset;
	memcpy(packet->buffer, pkt_data + psock->pcap_offset, *in_bytes);
	return STATUS_OK;
}