⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gencode.c

📁 该软件根据网络数据生成NetFlow记录。NetFlow可用于网络规划、负载均衡、安全监控等
💻 C
📖 第 1 页 / 共 3 页
字号:
		b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);		b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);		gen_and(b0, b1);		return b1;	case Q_OR:	case Q_DEFAULT:		b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);		b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);		gen_or(b0, b1);		return b1;	default:		abort();	}	b0 = gen_linktype(proto);	b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask);	gen_and(b0, b1);	return b1;}static struct block *gen_ehostop(eaddr, dir)	register const u_char *eaddr;	register int dir;{	register struct block *b0, *b1;	switch (dir) {	case Q_SRC:		return gen_bcmp(6, 6, eaddr);	case Q_DST:		return gen_bcmp(0, 6, eaddr);	case Q_AND:		b0 = gen_ehostop(eaddr, Q_SRC);		b1 = gen_ehostop(eaddr, Q_DST);		gen_and(b0, b1);		return b1;	case Q_DEFAULT:	case Q_OR:		b0 = gen_ehostop(eaddr, Q_SRC);		b1 = gen_ehostop(eaddr, Q_DST);		gen_or(b0, b1);		return b1;	}	abort();	/* NOTREACHED */}/* * Like gen_ehostop, but for DLT_FDDI */static struct block *gen_fhostop(eaddr, dir)	register const u_char *eaddr;	register int dir;{	struct block *b0, *b1;	switch (dir) {	case Q_SRC:#ifdef PCAP_FDDIPAD		return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr);#else		return gen_bcmp(6 + 1, 6, eaddr);#endif	case Q_DST:#ifdef PCAP_FDDIPAD		return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr);#else		return gen_bcmp(0 + 1, 6, eaddr);#endif	case Q_AND:		b0 = gen_fhostop(eaddr, Q_SRC);		b1 = gen_fhostop(eaddr, Q_DST);		gen_and(b0, b1);		return b1;	case Q_DEFAULT:	case Q_OR:		b0 = gen_fhostop(eaddr, Q_SRC);		b1 = gen_fhostop(eaddr, Q_DST);		gen_or(b0, b1);		return b1;	}	abort();	/* NOTREACHED */}/* * This is quite tricky because there may be pad bytes in front of the * DECNET header, and then there are two possible data packet formats that * carry both src and dst addresses, plus 5 packet types in a format that * carries only the src node, plus 2 types that use a different format and * also carry just the src node. * * Yuck. * * Instead of doing those all right, we just look for data packets with * 0 or 1 bytes of padding.  If you want to look at other packets, that * will require a lot more hacking. * * To add support for filtering on DECNET "areas" (network numbers) * one would want to add a "mask" argument to this routine.  That would * make the filter even more inefficient, although one could be clever * and not generate masking instructions if the mask is 0xFFFF. */static struct block *gen_dnhostop(addr, dir, base_off)	bpf_u_int32 addr;	int dir;	u_int base_off;{	struct block *b0, *b1, *b2, *tmp;	u_int offset_lh;	/* offset if long header is received */	u_int offset_sh;	/* offset if short header is received */	switch (dir) {	case Q_DST:		offset_sh = 1;	/* follows flags */		offset_lh = 7;	/* flgs,darea,dsubarea,HIORD */		break;	case Q_SRC:		offset_sh = 3;	/* follows flags, dstnode */		offset_lh = 15;	/* flgs,darea,dsubarea,did,sarea,ssub,HIORD */		break;	case Q_AND:		/* Inefficient because we do our Calvinball dance twice */		b0 = gen_dnhostop(addr, Q_SRC, base_off);		b1 = gen_dnhostop(addr, Q_DST, base_off);		gen_and(b0, b1);		return b1;	case Q_OR:	case Q_DEFAULT:		/* Inefficient because we do our Calvinball dance twice */		b0 = gen_dnhostop(addr, Q_SRC, base_off);		b1 = gen_dnhostop(addr, Q_DST, base_off);		gen_or(b0, b1);		return b1;	default:		abort();	}	b0 = gen_linktype(ETHERTYPE_DN);	/* Check for pad = 1, long header case */	tmp = gen_mcmp(base_off + 2, BPF_H,	    (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));	b1 = gen_cmp(base_off + 2 + 1 + offset_lh,	    BPF_H, (bpf_int32)ntohs(addr));	gen_and(tmp, b1);	/* Check for pad = 0, long header case */	tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);	b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));	gen_and(tmp, b2);	gen_or(b2, b1);	/* Check for pad = 1, short header case */	tmp = gen_mcmp(base_off + 2, BPF_H,	    (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));	b2 = gen_cmp(base_off + 2 + 1 + offset_sh,	    BPF_H, (bpf_int32)ntohs(addr));	gen_and(tmp, b2);	gen_or(b2, b1);	/* Check for pad = 0, short header case */	tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);	b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));	gen_and(tmp, b2);	gen_or(b2, b1);	/* Combine with test for linktype */	gen_and(b0, b1);	return b1;}static struct block *gen_host(addr, mask, proto, dir)	bpf_u_int32 addr;	bpf_u_int32 mask;	int proto;	int dir;{	struct block *b0, *b1;	switch (proto) {	case Q_DEFAULT:		b0 = gen_host(addr, mask, Q_IP, dir);		b1 = gen_host(addr, mask, Q_ARP, dir);		gen_or(b0, b1);		b0 = gen_host(addr, mask, Q_RARP, dir);		gen_or(b1, b0);		return b0;	case Q_IP:		return gen_hostop(addr, mask, dir, ETHERTYPE_IP,				  off_nl + 12, off_nl + 16);	case Q_RARP:		return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP,				  off_nl + 14, off_nl + 24);	case Q_ARP:		return gen_hostop(addr, mask, dir, ETHERTYPE_ARP,				  off_nl + 14, off_nl + 24);	case Q_TCP:		bpf_error("'tcp' modifier applied to host");	case Q_UDP:		bpf_error("'udp' modifier applied to host");	case Q_ICMP:		bpf_error("'icmp' modifier applied to host");	case Q_IGMP:		bpf_error("'igmp' modifier applied to host");	case Q_IGRP:		bpf_error("'igrp' modifier applied to host");	case Q_ATALK:		bpf_error("ATALK host filtering not implemented");	case Q_DECNET:		return gen_dnhostop(addr, dir, off_nl);	case Q_SCA:		bpf_error("SCA host filtering not implemented");	case Q_LAT:		bpf_error("LAT host filtering not implemented");	case Q_MOPDL:		bpf_error("MOPDL host filtering not implemented");	case Q_MOPRC:		bpf_error("MOPRC host filtering not implemented");	default:		abort();	}	/* NOTREACHED */}static struct block *gen_gateway(eaddr, alist, proto, dir)	const u_char *eaddr;	bpf_u_int32 **alist;	int proto;	int dir;{	struct block *b0, *b1, *tmp;	if (dir != 0)		bpf_error("direction applied to 'gateway'");	switch (proto) {	case Q_DEFAULT:	case Q_IP:	case Q_ARP:	case Q_RARP:		if (linktype == DLT_EN10MB)			b0 = gen_ehostop(eaddr, Q_OR);		else if (linktype == DLT_FDDI)			b0 = gen_fhostop(eaddr, Q_OR);		else			bpf_error(			    "'gateway' supported only on ethernet or FDDI");		b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);		while (*alist) {			tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);			gen_or(b1, tmp);			b1 = tmp;		}		gen_not(b1);		gen_and(b0, b1);		return b1;	}	bpf_error("illegal modifier of 'gateway'");	/* NOTREACHED */}struct block *gen_proto_abbrev(proto)	int proto;{	struct block *b0, *b1;	switch (proto) {	case Q_TCP:		b0 = gen_linktype(ETHERTYPE_IP);		b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)IPPROTO_TCP);		gen_and(b0, b1);		break;	case Q_UDP:		b0 =  gen_linktype(ETHERTYPE_IP);		b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)IPPROTO_UDP);		gen_and(b0, b1);		break;	case Q_ICMP:		b0 =  gen_linktype(ETHERTYPE_IP);		b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)IPPROTO_ICMP);		gen_and(b0, b1);		break;	case Q_IGMP:		b0 =  gen_linktype(ETHERTYPE_IP);		b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)2);		gen_and(b0, b1);		break;#ifndef	IPPROTO_IGRP#define	IPPROTO_IGRP	9#endif	case Q_IGRP:		b0 =  gen_linktype(ETHERTYPE_IP);		b1 = gen_cmp(off_nl + 9, BPF_B, (long)IPPROTO_IGRP);		gen_and(b0, b1);		break;	case Q_IP:		b1 =  gen_linktype(ETHERTYPE_IP);		break;	case Q_ARP:		b1 =  gen_linktype(ETHERTYPE_ARP);		break;	case Q_RARP:		b1 =  gen_linktype(ETHERTYPE_REVARP);		break;	case Q_LINK:		bpf_error("link layer applied in wrong context");	case Q_ATALK:		b1 =  gen_linktype(ETHERTYPE_ATALK);		break;	case Q_DECNET:		b1 =  gen_linktype(ETHERTYPE_DN);		break;	case Q_SCA:		b1 =  gen_linktype(ETHERTYPE_SCA);		break;	case Q_LAT:		b1 =  gen_linktype(ETHERTYPE_LAT);		break;	case Q_MOPDL:		b1 =  gen_linktype(ETHERTYPE_MOPDL);		break;	case Q_MOPRC:		b1 =  gen_linktype(ETHERTYPE_MOPRC);		break;	default:		abort();	}	return b1;}static struct block *gen_ipfrag(){	struct slist *s;	struct block *b;	/* not ip frag */	s = new_stmt(BPF_LD|BPF_H|BPF_ABS);	s->s.k = off_nl + 6;	b = new_block(JMP(BPF_JSET));	b->s.k = 0x1fff;	b->stmts = s;	gen_not(b);	return b;}static struct block *gen_portatom(off, v)	int off;	bpf_int32 v;{	struct slist *s;	struct block *b;	s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);	s->s.k = off_nl;	s->next = new_stmt(BPF_LD|BPF_IND|BPF_H);	s->next->s.k = off_nl + off;	b = new_block(JMP(BPF_JEQ));	b->stmts = s;	b->s.k = v;	return b;}struct block *gen_portop(port, proto, dir)	int port, proto, dir;{	struct block *b0, *b1, *tmp;	/* ip proto 'proto' */	tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto);	b0 = gen_ipfrag();	gen_and(tmp, b0);	switch (dir) {	case Q_SRC:		b1 = gen_portatom(0, (bpf_int32)port);		break;	case Q_DST:		b1 = gen_portatom(2, (bpf_int32)port);		break;	case Q_OR:	case Q_DEFAULT:		tmp = gen_portatom(0, (bpf_int32)port);		b1 = gen_portatom(2, (bpf_int32)port);		gen_or(tmp, b1);		break;	case Q_AND:		tmp = gen_portatom(0, (bpf_int32)port);		b1 = gen_portatom(2, (bpf_int32)port);		gen_and(tmp, b1);		break;	default:		abort();	}	gen_and(b0, b1);	return b1;}static struct block *gen_port(port, ip_proto, dir)	int port;	int ip_proto;	int dir;{	struct block *b0, *b1, *tmp;	/* ether proto ip */	b0 =  gen_linktype(ETHERTYPE_IP);	switch (ip_proto) {	case IPPROTO_UDP:	case IPPROTO_TCP:		b1 = gen_portop(port, ip_proto, dir);		break;	case PROTO_UNDEF:		tmp = gen_portop(port, IPPROTO_TCP, dir);		b1 = gen_portop(port, IPPROTO_UDP, dir);		gen_or(tmp, b1);		break;	default:		abort();	}	gen_and(b0, b1);	return b1;}static intlookup_proto(name, proto)	register const char *name;	register int proto;{	register int v;	switch (proto) {	case Q_DEFAULT:	case Q_IP:		v = pcap_nametoproto(name);		if (v == PROTO_UNDEF)			bpf_error("unknown ip proto '%s'", name);		break;	case Q_LINK:		/* XXX should look up h/w protocol type based on linktype */		v = pcap_nametoeproto(name);		if (v == PROTO_UNDEF)			bpf_error("unknown ether proto '%s'", name);		break;	default:		v = PROTO_UNDEF;		break;	}	return v;}static struct block *gen_proto(v, proto, dir)	int v;	int proto;	int dir;{	struct block *b0, *b1;	if (dir != Q_DEFAULT)		bpf_error("direction applied to 'proto'");	switch (proto) {	case Q_DEFAULT:	case Q_IP:		b0 = gen_linktype(ETHERTYPE_IP);		b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v);		gen_and(b0, b1);		return b1;	case Q_ARP:		bpf_error("arp does not encapsulate another protocol");		/* NOTREACHED */	case Q_RARP:		bpf_error("rarp does not encapsulate another protocol");		/* NOTREACHED */	case Q_ATALK:		bpf_error("atalk encapsulation is not specifiable");		/* NOTREACHED */	case Q_DECNET:		bpf_error("decnet encapsulation is not specifiable");		/* NOTREACHED */	case Q_SCA:		bpf_error("sca does not encapsulate another protocol");		/* NOTREACHED */	case Q_LAT:		bpf_error("lat does not encapsulate another protocol");		/* NOTREACHED */	case Q_MOPRC:		bpf_error("moprc does not encapsulate another protocol");		/* NOTREACHED */	case Q_MOPDL:		bpf_error("mopdl does not encapsulate another protocol");		/* NOTREACHED */	case Q_LINK:		return gen_linktype(v);	case Q_UDP:		bpf_error("'udp proto' is bogus");		/* NOTREACHED */	case Q_TCP:		bpf_error("'tcp proto' is bogus");		/* NOTREACHED */	case Q_ICMP:		bpf_error("'icmp proto' is bogus");		/* NOTREACHED */	case Q_IGMP:		bpf_error("'igmp proto' is bogus");		/* NOTREACHED */	case Q_IGRP:		bpf_error("'igrp proto' is bogus");		/* NOTREACHED */	default:		abort();		/* NOTREACHED */	}	/* NOTREACHED */}struct block *gen_scode(name, q)	register const char *name;	struct qual q;{	int proto = q.proto;	int dir = q.dir;	int tproto;	u_char *eaddr;	bpf_u_int32 mask, addr, **alist;	struct block *b, *tmp;	int port, real_proto;	switch (q.addr) {	case Q_NET:		addr = pcap_nametonetaddr(name);		if (addr == 0)			bpf_error("unknown network '%s'", name);		/* Left justify network addr and calculate its network mask */		mask = 0xffffffff;		while (addr && (addr & 0xff000000) == 0) {			addr <<= 8;			mask <<= 8;		}		return gen_host(addr, mask, proto, dir);	case Q_DEFAULT:	case Q_HOST:		if (proto == Q_LINK) {			switch (linktype) {			case DLT_EN10MB:				eaddr = pcap_ether_hostton(name);				if (eaddr == NULL)					bpf_error(					    "unknown ether host '%s'", name);				return gen_ehostop(eaddr, dir);			case DLT_FDDI:				eaddr = pcap_ether_hostton(name);				if (eaddr == NULL)					bpf_error(					    "unknown FDDI host '%s'", name);				return gen_fhostop(eaddr, dir);			default:				bpf_error(			"only ethernet/FDDI supports link-level host name");				break;			}		} else if (proto == Q_DECNET) {			unsigned short dn_addr = __pcap_nametodnaddr(name);			/*			 * I don't think DECNET hosts can be multihomed, so			 * there is no need to build up a list of addresses

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -