Esempio n. 1
0
void nfnl_dump_packet(struct nlmsghdr *nlh, int received_len, char *desc)
{
	void *nlmsg_data = NLMSG_DATA(nlh);
	struct nfattr *nfa = NFM_NFA(NLMSG_DATA(nlh));
	int len = NFM_PAYLOAD(nlh);

	printf("%s called from %s\n", __FUNCTION__, desc);
	printf("  nlmsghdr = %p, received_len = %u\n", nlh, received_len);
	printf("  NLMSG_DATA(nlh) = %p (+%td bytes)\n", nlmsg_data,
	       (nlmsg_data - (void *)nlh));
	printf("  NFM_NFA(NLMSG_DATA(nlh)) = %p (+%td bytes)\n",
		nfa, ((void *)nfa - (void *)nlh));
	printf("  NFM_PAYLOAD(nlh) = %u\n", len);
	printf("  nlmsg_type = %u, nlmsg_len = %u, nlmsg_seq = %u "
		"nlmsg_flags = 0x%x\n", nlh->nlmsg_type, nlh->nlmsg_len,
		nlh->nlmsg_seq, nlh->nlmsg_flags);

	while (NFA_OK(nfa, len)) {
		printf("    nfa@%p: nfa_type=%u, nfa_len=%u\n",
			nfa, NFA_TYPE(nfa), nfa->nfa_len);
		nfa = NFA_NEXT(nfa,len);
	}
}
ulog_packet_msg_t *ipulog_get_packet(struct ipulog_handle *h,
				     const unsigned char *buf,
				     size_t len)
{
	struct nlmsghdr *nlh;
	struct nfattr *tb[NFULA_MAX];
	struct nfulnl_msg_packet_hdr *hdr;

	if (!h->last_nlh) {
		printf("first\n");
		nlh = nfnl_get_msg_first(nflog_nfnlh(h->nfulh), buf, len);
	}else {
next_msg:	printf("next\n");
		nlh = nfnl_get_msg_next(nflog_nfnlh(h->nfulh), buf, len);
	}
	h->last_nlh = nlh;

	if (!nlh)
		return NULL;

	nfnl_parse_attr(tb, NFULA_MAX, NFM_NFA(NLMSG_DATA(nlh)),
			NFM_PAYLOAD(nlh));
	
	if (!tb[NFULA_PACKET_HDR-1])
		goto next_msg;

	/* now build the fake ulog_packet_msg */
	hdr = NFA_DATA(tb[NFULA_PACKET_HDR-1]);
	h->upmsg.hook = hdr->hook;

	if (tb[NFULA_MARK-1])
		h->upmsg.mark = ntohl(*(u_int32_t *)NFA_DATA(tb[NFULA_MARK-1]));
	else
		h->upmsg.mark = 0;

	if (tb[NFULA_TIMESTAMP]) {
		/* FIXME: 64bit network-to-host */
		h->upmsg.timestamp_sec = h->upmsg.timestamp_usec = 0;
	} else
		h->upmsg.timestamp_sec = h->upmsg.timestamp_usec = 0;

	if (tb[NFULA_IFINDEX_INDEV-1]) {
		/* FIXME: ifindex lookup */	
		h->upmsg.indev_name[0] = '\0';
	} else
		h->upmsg.indev_name[0] = '\0';

	if (tb[NFULA_IFINDEX_OUTDEV-1]) {
		/* FIXME: ifindex lookup */	
		h->upmsg.outdev_name[0] = '\0';
	} else
		h->upmsg.outdev_name[0] = '\0';

	if (tb[NFULA_HWADDR-1]) {
		struct nfulnl_msg_packet_hw *phw = NFA_DATA(tb[NFULA_HWADDR-1]);
		h->upmsg.mac_len = ntohs(phw->hw_addrlen);
		memcpy(h->upmsg.mac, phw->hw_addr, 8);
	} else
		h->upmsg.mac_len = 0;

	if (tb[NFULA_PREFIX-1]) {
		int plen = NFA_PAYLOAD(tb[NFULA_PREFIX-1]);
		if (ULOG_PREFIX_LEN < plen)
			plen = ULOG_PREFIX_LEN;
		memcpy(h->upmsg.prefix, NFA_DATA(tb[NFULA_PREFIX-1]), plen);
		h->upmsg.prefix[ULOG_PREFIX_LEN-1] = '\0';
	}

	if (tb[NFULA_PAYLOAD-1]) {
		memcpy(h->upmsg.payload, NFA_DATA(tb[NFULA_PAYLOAD-1]),
			NFA_PAYLOAD(tb[NFULA_PAYLOAD-1]));
		h->upmsg.data_len = NFA_PAYLOAD(tb[NFULA_PAYLOAD-1]);
	} else
		h->upmsg.data_len = 0;
	
	return &h->upmsg;
}