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

📄 pcap-dlpi.c

📁 该软件根据网络数据生成NetFlow记录。NetFlow可用于网络规划、负载均衡、安全监控等
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1993, 1994, 1995, 1996, 1997 *	The Regents of the University of California.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that: (1) source code distributions * retain the above copyright notice and this paragraph in its entirety, (2) * distributions including binary code include the above copyright notice and * this paragraph in its entirety in the documentation or other materials * provided with the distribution, and (3) all advertising materials mentioning * features or use of this software display the following acknowledgement: * ``This product includes software developed by the University of California, * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of * the University nor the names of its contributors may be used to endorse * or promote products derived from this software without specific prior * written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk), * University College London. *//* * Packet capture routine for dlpi under SunOS 5 * * Notes: * *    - Apparently the DLIOCRAW ioctl() is specific to SunOS. * *    - There is a bug in bufmod(7) such that setting the snapshot *      length results in data being left of the front of the packet. * *    - It might be desirable to use pfmod(7) to filter packets in the *      kernel. */#ifndef lintstatic const char rcsid[] =    "@(#) $Header: pcap-dlpi.c,v 1.52 97/10/03 19:47:47 leres Exp $ (LBL)";#endif#include <sys/types.h>#include <sys/time.h>#ifdef HAVE_SYS_BUFMOD_H#include <sys/bufmod.h>#endif#include <sys/dlpi.h>#ifdef HAVE_SYS_DLPI_EXT_H#include <sys/dlpi_ext.h>#endif#ifdef HAVE_HPUX9#include <sys/socket.h>#endif#ifdef DL_HP_PPA_ACK_OBS#include <sys/stat.h>#endif#include <sys/stream.h>#if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)#include <sys/systeminfo.h>#endif#ifdef HAVE_HPUX9#include <net/if.h>#endif#include <ctype.h>#ifdef HAVE_HPUX9#include <nlist.h>#endif#include <errno.h>#include <fcntl.h>#include <memory.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stropts.h>#include <unistd.h>#include "pcap-int.h"#include "gnuc.h"#ifdef HAVE_OS_PROTO_H#include "os-proto.h"#endif#ifndef PCAP_DEV_PREFIX#define PCAP_DEV_PREFIX "/dev"#endif#define	MAXDLBUF	8192/* Forwards */static int dlattachreq(int, bpf_u_int32, char *);static int dlbindack(int, char *, char *);static int dlbindreq(int, bpf_u_int32, char *);static int dlinfoack(int, char *, char *);static int dlinforeq(int, char *);static int dlokack(int, const char *, char *, char *);static int recv_ack(int, int, const char *, char *, char *);static int dlpromisconreq(int, bpf_u_int32, char *);#if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);#endifstatic int send_request(int, char *, int, char *, char *);#ifdef HAVE_SYS_BUFMOD_Hstatic int strioctl(int, int, int, char *);#endif#ifdef HAVE_HPUX9static int dlpi_kread(int, off_t, void *, u_int, char *);#endif#ifdef HAVE_DEV_DLPIstatic int get_dlpi_ppa(int, const char *, int, char *);#endifintpcap_stats(pcap_t *p, struct pcap_stat *ps){	*ps = p->md.stat;	return (0);}/* XXX Needed by HP-UX (at least) */static bpf_u_int32 ctlbuf[MAXDLBUF];static struct strbuf ctl = {	MAXDLBUF,	0,	(char *)ctlbuf};intpcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user){	register int cc, n, caplen, origlen;	register u_char *bp, *ep, *pk;	register struct bpf_insn *fcode;#ifdef HAVE_SYS_BUFMOD_H	register struct sb_hdr *sbp;#ifdef LBL_ALIGN	struct sb_hdr sbhdr;#endif#endif	int flags;	struct strbuf data;	struct pcap_pkthdr pkthdr;	flags = 0;	cc = p->cc;	if (cc == 0) {		data.buf = (char *)p->buffer + p->offset;		data.maxlen = MAXDLBUF;		data.len = 0;		do {			if (getmsg(p->fd, &ctl, &data, &flags) < 0) {				/* Don't choke when we get ptraced */				if (errno == EINTR) {					cc = 0;					continue;				}				strcpy(p->errbuf, pcap_strerror(errno));				return (-1);			}			cc = data.len;		} while (cc == 0);		bp = p->buffer + p->offset;	} else		bp = p->bp;	/* Loop through packets */	fcode = p->fcode.bf_insns;	ep = bp + cc;	n = 0;#ifdef HAVE_SYS_BUFMOD_H	while (bp < ep) {#ifdef LBL_ALIGN		if ((long)bp & 3) {			sbp = &sbhdr;			memcpy(sbp, bp, sizeof(*sbp));		} else#endif			sbp = (struct sb_hdr *)bp;		p->md.stat.ps_drop += sbp->sbh_drops;		pk = bp + sizeof(*sbp);		bp += sbp->sbh_totlen;		origlen = sbp->sbh_origlen;		caplen = sbp->sbh_msglen;#else		origlen = cc;		caplen = min(p->snapshot, cc);		pk = bp;		bp += caplen;#endif		++p->md.stat.ps_recv;		if (bpf_filter(fcode, pk, origlen, caplen)) {#ifdef HAVE_SYS_BUFMOD_H			pkthdr.ts = sbp->sbh_timestamp;#else			(void)gettimeofday(&pkthdr.ts, NULL);#endif			pkthdr.len = origlen;			pkthdr.caplen = caplen;			/* Insure caplen does not exceed snapshot */			if (pkthdr.caplen > p->snapshot)				pkthdr.caplen = p->snapshot;			(*callback)(user, &pkthdr, pk);			if (++n >= cnt && cnt >= 0) {				p->cc = ep - bp;				p->bp = bp;				return (n);			}		}#ifdef HAVE_SYS_BUFMOD_H	}#endif	p->cc = 0;	return (n);}pcap_t *pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf){	register char *cp;	char *eos;	register pcap_t *p;	register int ppa;	register dl_info_ack_t *infop;#ifdef HAVE_SYS_BUFMOD_H	bpf_u_int32 ss, flag;#ifdef HAVE_SOLARIS	register char *release;	bpf_u_int32 osmajor, osminor, osmicro;#endif#endif	bpf_u_int32 buf[MAXDLBUF];	char dname[100];#ifndef HAVE_DEV_DLPI	char dname2[100];#endif	p = (pcap_t *)malloc(sizeof(*p));	if (p == NULL) {		strcpy(ebuf, pcap_strerror(errno));		return (NULL);	}	memset(p, 0, sizeof(*p));	/*	** Determine device and ppa	*/	cp = strpbrk(device, "0123456789");	if (cp == NULL) {		sprintf(ebuf, "%s missing unit number", device);		goto bad;	}	ppa = strtol(cp, &eos, 10);	if (*eos != '\0') {		sprintf(ebuf, "%s bad unit number", device);		goto bad;	}	if (*device == '/')		strcpy(dname, device);	else		sprintf(dname, "%s/%s", PCAP_DEV_PREFIX, device);#ifdef HAVE_DEV_DLPI	/* Map network device to /dev/dlpi unit */	cp = "/dev/dlpi";	if ((p->fd = open(cp, O_RDWR)) < 0) {		sprintf(ebuf, "%s: %s", cp, pcap_strerror(errno));		goto bad;	}	/* Map network interface to /dev/dlpi unit */	ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf);	if (ppa < 0)		goto bad;#else	/* Try device without unit number */	strcpy(dname2, dname);	cp = strchr(dname, *cp);	*cp = '\0';	if ((p->fd = open(dname, O_RDWR)) < 0) {		if (errno != ENOENT) {			sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno));			goto bad;		}		/* Try again with unit number */		if ((p->fd = open(dname2, O_RDWR)) < 0) {			sprintf(ebuf, "%s: %s", dname2, pcap_strerror(errno));			goto bad;		}		/* XXX Assume unit zero */		ppa = 0;	}#endif	p->snapshot = snaplen;	/*	** Attach if "style 2" provider	*/	if (dlinforeq(p->fd, ebuf) < 0 ||	    dlinfoack(p->fd, (char *)buf, ebuf) < 0)		goto bad;	infop = &((union DL_primitives *)buf)->info_ack;	if (infop->dl_provider_style == DL_STYLE2 &&	    (dlattachreq(p->fd, ppa, ebuf) < 0 ||	    dlokack(p->fd, "attach", (char *)buf, ebuf) < 0))		goto bad;	/*	** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if	** using SINIX)	*/#if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix)	if (dlbindreq(p->fd, 0, ebuf) < 0 ||	    dlbindack(p->fd, (char *)buf, ebuf) < 0)		goto bad;#endif	if (promisc) {		/*		** Enable promiscuous		*/		if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||		    dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0)			goto bad;		/*		** Try to enable multicast (you would have thought		** promiscuous would be sufficient). (Skip if using		** HP-UX or SINIX)		*/#if !defined(__hpux) && !defined(sinix)		if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||		    dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0)			fprintf(stderr,			    "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf);#endif	}	/*	** Try to enable sap (when not in promiscuous mode when using	** using HP-UX and never under SINIX)	*/#ifndef sinix	if (#ifdef __hpux	    !promisc &&#endif	    (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||	    dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) {		/* Not fatal if promisc since the DL_PROMISC_PHYS worked */		if (promisc)			fprintf(stderr,			    "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf);		else			goto bad;	}#endif	/*	** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous	** options)	*/#if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20)	if (dlbindreq(p->fd, 0, ebuf) < 0 ||	    dlbindack(p->fd, (char *)buf, ebuf) < 0)		goto bad;#endif	/*	** Determine link type	*/	if (dlinforeq(p->fd, ebuf) < 0 ||	    dlinfoack(p->fd, (char *)buf, ebuf) < 0)		goto bad;	infop = &((union DL_primitives *)buf)->info_ack;	switch (infop->dl_mac_type) {	case DL_CSMACD:	case DL_ETHER:		p->linktype = DLT_EN10MB;		p->offset = 2;		break;	case DL_FDDI:		p->linktype = DLT_FDDI;		p->offset = 3;		break;	default:		sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type);		goto bad;	}#ifdef	DLIOCRAW	/*	** This is a non standard SunOS hack to get the ethernet header.	*/	if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {		sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno));		goto bad;	}#endif#ifdef HAVE_SYS_BUFMOD_H	/*	** Another non standard call to get the data nicely buffered	*/	if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {		sprintf(ebuf, "I_PUSH bufmod: %s", pcap_strerror(errno));		goto bad;	}	/*	** Now that the bufmod is pushed lets configure it.	**	** There is a bug in bufmod(7). When dealing with messages of	** less than snaplen size it strips data from the beginning not	** the end.	**	** This bug is supposed to be fixed in 5.3.2. Also, there is a	** patch available. Ask for bugid 1149065.

⌨️ 快捷键说明

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