亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? pcap-linux.c

?? 用來(lái)監(jiān)視網(wǎng)絡(luò)通信數(shù)據(jù)的源代碼和應(yīng)用程序,方便網(wǎng)絡(luò)程序底層開(kāi)發(fā).
?? C
?? 第 1 頁(yè) / 共 5 頁(yè)
字號(hào):
/*
 *  pcap-linux.c: Packet capture interface to the Linux kernel
 *
 *  Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
 *  		       Sebastian Krahmer  <krahmer@cs.uni-potsdam.de>
 *
 *  License: BSD
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *  3. The names of the authors may not 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.
 */
#ifndef lint
static const char rcsid[] =
    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.88 2003/01/23 07:24:52 guy Exp $ (LBL)";
#endif

/*
 * Known problems with 2.0[.x] kernels:
 *
 *   - The loopback device gives every packet twice; on 2.2[.x] kernels,
 *     if we use PF_PACKET, we can filter out the transmitted version
 *     of the packet by using data in the "sockaddr_ll" returned by
 *     "recvfrom()", but, on 2.0[.x] kernels, we have to use
 *     PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a
 *     "sockaddr_pkt" which doesn't give us enough information to let
 *     us do that.
 *
 *   - We have to set the interface's IFF_PROMISC flag ourselves, if
 *     we're to run in promiscuous mode, which means we have to turn
 *     it off ourselves when we're done; the kernel doesn't keep track
 *     of how many sockets are listening promiscuously, which means
 *     it won't get turned off automatically when no sockets are
 *     listening promiscuously.  We catch "pcap_close()" and, for
 *     interfaces we put into promiscuous mode, take them out of
 *     promiscuous mode - which isn't necessarily the right thing to
 *     do, if another socket also requested promiscuous mode between
 *     the time when we opened the socket and the time when we close
 *     the socket.
 *
 *   - MSG_TRUNC isn't supported, so you can't specify that "recvfrom()"
 *     return the amount of data that you could have read, rather than
 *     the amount that was returned, so we can't just allocate a buffer
 *     whose size is the snapshot length and pass the snapshot length
 *     as the byte count, and also pass MSG_TRUNC, so that the return
 *     value tells us how long the packet was on the wire.
 *
 *     This means that, if we want to get the actual size of the packet,
 *     so we can return it in the "len" field of the packet header,
 *     we have to read the entire packet, not just the part that fits
 *     within the snapshot length, and thus waste CPU time copying data
 *     from the kernel that our caller won't see.
 *
 *     We have to get the actual size, and supply it in "len", because
 *     otherwise, the IP dissector in tcpdump, for example, will complain
 *     about "truncated-ip", as the packet will appear to have been
 *     shorter, on the wire, than the IP header said it should have been.
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "pcap-int.h"
#include "sll.h"

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <net/if.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <net/if_arp.h>

#ifdef REMOTE
#include <pcap-remote.h>
#endif

/*
 * If PF_PACKET is defined, we can use {SOCK_RAW,SOCK_DGRAM}/PF_PACKET
 * sockets rather than SOCK_PACKET sockets.
 *
 * To use them, we include <linux/if_packet.h> rather than
 * <netpacket/packet.h>; we do so because
 *
 *	some Linux distributions (e.g., Slackware 4.0) have 2.2 or
 *	later kernels and libc5, and don't provide a <netpacket/packet.h>
 *	file;
 *
 *	not all versions of glibc2 have a <netpacket/packet.h> file
 *	that defines stuff needed for some of the 2.4-or-later-kernel
 *	features, so if the system has a 2.4 or later kernel, we
 *	still can't use those features.
 *
 * We're already including a number of other <linux/XXX.h> headers, and
 * this code is Linux-specific (no other OS has PF_PACKET sockets as
 * a raw packet capture mechanism), so it's not as if you gain any
 * useful portability by using <netpacket/packet.h>
 *
 * XXX - should we just include <linux/if_packet.h> even if PF_PACKET
 * isn't defined?  It only defines one data structure in 2.0.x, so
 * it shouldn't cause any problems.
 */
#ifdef PF_PACKET
# include <linux/if_packet.h>

 /*
  * On at least some Linux distributions (for example, Red Hat 5.2),
  * there's no <netpacket/packet.h> file, but PF_PACKET is defined if
  * you include <sys/socket.h>, but <linux/if_packet.h> doesn't define
  * any of the PF_PACKET stuff such as "struct sockaddr_ll" or any of
  * the PACKET_xxx stuff.
  *
  * So we check whether PACKET_HOST is defined, and assume that we have
  * PF_PACKET sockets only if it is defined.
  */
# ifdef PACKET_HOST
#  define HAVE_PF_PACKET_SOCKETS
# endif /* PACKET_HOST */
#endif /* PF_PACKET */

#ifdef SO_ATTACH_FILTER
#include <linux/types.h>
#include <linux/filter.h>
#endif

#ifndef __GLIBC__
typedef int		socklen_t;
#endif

#ifndef MSG_TRUNC
/*
 * This is being compiled on a system that lacks MSG_TRUNC; define it
 * with the value it has in the 2.2 and later kernels, so that, on
 * those kernels, when we pass it in the flags argument to "recvfrom()"
 * we're passing the right value and thus get the MSG_TRUNC behavior
 * we want.  (We don't get that behavior on 2.0[.x] kernels, because
 * they didn't support MSG_TRUNC.)
 */
#define MSG_TRUNC	0x20
#endif

#ifndef SOL_PACKET
/*
 * This is being compiled on a system that lacks SOL_PACKET; define it
 * with the value it has in the 2.2 and later kernels, so that we can
 * set promiscuous mode in the good modern way rather than the old
 * 2.0-kernel crappy way.
 */
#define SOL_PACKET	263
#endif

#define MAX_LINKHEADER_SIZE	256

/*
 * When capturing on all interfaces we use this as the buffer size.
 * Should be bigger then all MTUs that occur in real life.
 * 64kB should be enough for now.
 */
#define BIGGER_THAN_ALL_MTUS	(64*1024)

/*
 * Prototypes for internal functions
 */
static void map_arphrd_to_dlt(pcap_t *, int, int);
static int live_open_old(pcap_t *, const char *, int, int, char *);
static int live_open_new(pcap_t *, const char *, int, int, char *);
static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);

/*
 * Wrap some ioctl calls
 */
#ifdef HAVE_PF_PACKET_SOCKETS
static int	iface_get_id(int fd, const char *device, char *ebuf);
#endif
static int	iface_get_mtu(int fd, const char *device, char *ebuf);
static int 	iface_get_arptype(int fd, const char *device, char *ebuf);
#ifdef HAVE_PF_PACKET_SOCKETS
static int 	iface_bind(int fd, int ifindex, char *ebuf);
#endif
static int 	iface_bind_old(int fd, const char *device, char *ebuf);

#ifdef SO_ATTACH_FILTER
static int	fix_program(pcap_t *handle, struct sock_fprog *fcode);
static int	fix_offset(struct bpf_insn *p);
static int	set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode);
static int	reset_kernel_filter(pcap_t *handle);

static struct sock_filter	total_insn
	= BPF_STMT(BPF_RET | BPF_K, 0);
static struct sock_fprog	total_fcode
	= { 1, &total_insn };
#endif

/*
 *  Get a handle for a live capture from the given device. You can
 *  pass NULL as device to get all packages (without link level
 *  information of course). If you pass 1 as promisc the interface
 *  will be set to promiscous mode (XXX: I think this usage should
 *  be deprecated and functions be added to select that later allow
 *  modification of that values -- Torsten).
 *
 *  See also pcap(3).
 */
pcap_t *
pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
    char *ebuf)
{
	pcap_t		*handle;
	int		mtu;
	int		err;
	int		live_open_ok = 0;
	struct utsname	utsname;

#ifdef REMOTE
	/*
		Retrofit; we have to make older applications compatible with the remote capture
		So, we're calling the pcap_open_remote() from here, that is a very dirty thing.
		Obviously, we cannot exploit all the new features; for instance, we cannot
		send authentication, we cannot use a UDP data connection, and so on.
	*/

	char host[PCAP_BUF_SIZE + 1];
	char port[PCAP_BUF_SIZE + 1];
	char name[PCAP_BUF_SIZE + 1];
	int srctype;

	if (pcap_parsesrcstr(device, &srctype, host, port, name, ebuf) )
		return NULL;

	if (srctype == PCAP_SRC_IFREMOTE)
	{
		handle= pcap_opensource_remote(device, NULL, ebuf);

		if (handle == NULL) 
			return NULL;

		handle->snapshot= snaplen;
		handle->md.timeout= to_ms;
		handle->rmt_flags= (promisc) ? PCAP_OPENFLAG_PROMISCUOUS : 0;

		return handle;
	}
#endif

        /* Allocate a handle for this session. */

	handle = malloc(sizeof(*handle));
	if (handle == NULL) {
		snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
			 pcap_strerror(errno));
		return NULL;
	}

	/* Initialize some components of the pcap structure. */

	memset(handle, 0, sizeof(*handle));
	handle->snapshot	= snaplen;
	handle->md.timeout	= to_ms;

	/*
	 * NULL and "any" are special devices which give us the hint to
	 * monitor all devices.
	 */
	if (!device || strcmp(device, "any") == 0) {
		device			= NULL;
		handle->md.device	= strdup("any");
		if (promisc) {
			promisc = 0;
			/* Just a warning. */
			snprintf(ebuf, PCAP_ERRBUF_SIZE,
			    "Promiscuous mode not supported on the \"any\" device");
		}

	} else
		handle->md.device	= strdup(device);

	if (handle->md.device == NULL) {
		snprintf(ebuf, PCAP_ERRBUF_SIZE, "strdup: %s",
			 pcap_strerror(errno) );
		free(handle);
		return NULL;
	}

	/*
	 * Current Linux kernels use the protocol family PF_PACKET to
	 * allow direct access to all packets on the network while
	 * older kernels had a special socket type SOCK_PACKET to
	 * implement this feature.
	 * While this old implementation is kind of obsolete we need
	 * to be compatible with older kernels for a while so we are
	 * trying both methods with the newer method preferred.
	 */

	if ((err = live_open_new(handle, device, promisc, to_ms, ebuf)) == 1)
		live_open_ok = 1;
	else if (err == 0) {
		/* Non-fatal error; try old way */
		if (live_open_old(handle, device, promisc, to_ms, ebuf))
			live_open_ok = 1;
	}
	if (!live_open_ok) {
		/*
		 * Both methods to open the packet socket failed. Tidy
		 * up and report our failure (ebuf is expected to be
		 * set by the functions above).
		 */

		if (handle->md.device != NULL)
			free(handle->md.device);
		free(handle);
		return NULL;
	}

	/*
	 * Compute the buffer size.
	 *
	 * If we're using SOCK_PACKET, this might be a 2.0[.x] kernel,
	 * and might require special handling - check.
	 */
	if (handle->md.sock_packet && (uname(&utsname) < 0 ||
	    strncmp(utsname.release, "2.0", 3) == 0)) {
		/*
		 * We're using a SOCK_PACKET structure, and either
		 * we couldn't find out what kernel release this is,
		 * or it's a 2.0[.x] kernel.
		 *
		 * In the 2.0[.x] kernel, a "recvfrom()" on
		 * a SOCK_PACKET socket, with MSG_TRUNC set, will
		 * return the number of bytes read, so if we pass
		 * a length based on the snapshot length, it'll
		 * return the number of bytes from the packet
		 * copied to userland, not the actual length
		 * of the packet.
		 *
		 * This means that, for example, the IP dissector
		 * in tcpdump will get handed a packet length less
		 * than the length in the IP header, and will
		 * complain about "truncated-ip".
		 *
		 * So we don't bother trying to copy from the
		 * kernel only the bytes in which we're interested,
		 * but instead copy them all, just as the older
		 * versions of libpcap for Linux did.
		 *
		 * The buffer therefore needs to be big enough to
		 * hold the largest packet we can get from this
		 * device.  Unfortunately, we can't get the MRU
		 * of the network; we can only get the MTU.  The
		 * MTU may be too small, in which case a packet larger
		 * than the buffer size will be truncated *and* we
		 * won't get the actual packet size.
		 *
		 * However, if the snapshot length is larger than
		 * the buffer size based on the MTU, we use the
		 * snapshot length as the buffer size, instead;
		 * this means that with a sufficiently large snapshot
		 * length we won't artificially truncate packets
		 * to the MTU-based size.
		 *
		 * This mess just one of many problems with packet
		 * capture on 2.0[.x] kernels; you really want a
		 * 2.2[.x] or later kernel if you want packet capture
		 * to work well.
		 */
		mtu = iface_get_mtu(handle->fd, device, ebuf);
		if (mtu == -1) {
			if (handle->md.clear_promisc)
				/* 2.0.x kernel */
				pcap_close_linux(handle);
			close(handle->fd);
			if (handle->md.device != NULL)
				free(handle->md.device);
			free(handle);
			return NULL;
		}
		handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
		if (handle->bufsize < handle->snapshot)
			handle->bufsize = handle->snapshot;
	} else {
		/*
		 * This is a 2.2[.x] or later kernel (we know that
		 * either because we're not using a SOCK_PACKET
		 * socket - PF_PACKET is supported only in 2.2
		 * and later kernels - or because we checked the
		 * kernel version).
		 *
		 * We can safely pass "recvfrom()" a byte count
		 * based on the snapshot length.
		 */
		handle->bufsize = handle->snapshot;
	}

	/* Allocate the buffer */

	handle->buffer	 = malloc(handle->bufsize + handle->offset);
	if (!handle->buffer) {
	        snprintf(ebuf, PCAP_ERRBUF_SIZE,
			 "malloc: %s", pcap_strerror(errno));
		if (handle->md.clear_promisc)
			/* 2.0.x kernel */
			pcap_close_linux(handle);
		close(handle->fd);
		if (handle->md.device != NULL)
			free(handle->md.device);
		free(handle);
		return NULL;
	}

	return handle;
}

/*
 *  Read at most max_packets from the capture stream and call the callback
 *  for each of them. Returns the number of packets handled or -1 if an
 *  error occured.
 */
int
pcap_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
{
#ifdef REMOTE
	if (handle->rmt_clientside)
	{
		/* We are on an remote capture */
		if (!handle->rmt_capstarted)
		{
			// if the capture has not started yet, please start it
			if (pcap_startcapture_remote(handle) )
				return -1;
			handle->rmt_capstarted= 1;
		}
		return pcap_read_remote(handle, max_packets, callback, user);
	}
#endif
	/*
	 * Currently, on Linux only one packet is delivered per read,
	 * so we don't loop.

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
精油按摩中文字幕久久| 国产欧美日韩综合精品一区二区| 亚洲成在线观看| 91精品国产麻豆| 精品一区二区三区日韩| 日韩和的一区二区| 欧美日本在线看| 国内久久精品视频| 一区二区三区欧美久久| 欧美精品一区二区三区在线| 99国产精品久久久久| 日本不卡高清视频| 成人美女在线观看| 五月天中文字幕一区二区| 国产欧美视频在线观看| 国产女同性恋一区二区| 国产午夜久久久久| 在线播放国产精品二区一二区四区 | 久久er精品视频| 一区二区三区加勒比av| 亚洲综合精品久久| 国产精品久久二区二区| 欧美大片在线观看| 欧美日韩在线免费视频| 成人国产精品视频| 国产一区二区三区高清播放| 亚洲成人第一页| 日本伊人午夜精品| 国产美女av一区二区三区| 成人教育av在线| 色系网站成人免费| 国产91清纯白嫩初高中在线观看 | 色综合一区二区| 国内精品自线一区二区三区视频| 国产一区二区在线观看免费| 成年人午夜久久久| 欧美日韩中文精品| 久久伊人中文字幕| xnxx国产精品| 日韩伦理免费电影| 国产欧美一区二区三区鸳鸯浴 | 国产欧美日韩精品一区| 亚洲免费大片在线观看| 国产精品卡一卡二| 亚洲国产精品一区二区久久| 亚洲精品免费一二三区| ㊣最新国产の精品bt伙计久久| 欧美激情一区不卡| 国产免费久久精品| 亚洲一区在线视频| 久久99精品久久久久久动态图| 成人av免费网站| 在线不卡一区二区| 中文在线免费一区三区高中清不卡| 欧美亚男人的天堂| 亚洲午夜久久久久久久久久久| 蜜桃一区二区三区四区| 精品在线视频一区| 93久久精品日日躁夜夜躁欧美| 在线不卡中文字幕| 一区二区中文字幕在线| 毛片一区二区三区| 色婷婷激情综合| 久久久精品国产免费观看同学| 久久综合狠狠综合久久综合88| 日韩美女视频19| 国产资源在线一区| 欧美日韩在线精品一区二区三区激情| 久久久久久黄色| 偷窥少妇高潮呻吟av久久免费| 国产99久久久精品| 91精品国产综合久久精品性色| 17c精品麻豆一区二区免费| 久久精品国产亚洲高清剧情介绍| 色综合久久66| 中文字幕免费不卡在线| 蜜桃视频在线一区| 欧美视频一区二| ...av二区三区久久精品| 久久99精品国产麻豆婷婷洗澡| 欧美亚洲综合久久| 亚洲色图在线视频| 粉嫩av一区二区三区粉嫩| 日韩一区二区三区电影在线观看 | 亚洲欧美日韩系列| 国产精品1024久久| 在线观看www91| 日韩欧美精品在线| 天天色综合成人网| 91精品1区2区| 2023国产精品视频| 日本在线不卡一区| 欧美二区三区91| 亚洲国产精品自拍| 欧美做爰猛烈大尺度电影无法无天| 国产精品女同互慰在线看| 亚洲自拍偷拍图区| 91免费在线看| 精品免费国产一区二区三区四区| 一区免费观看视频| 播五月开心婷婷综合| 国产欧美日韩视频在线观看| 国产一区二区三区精品欧美日韩一区二区三区 | 欧美一级欧美三级在线观看| 五月激情综合网| 欧美日韩国产在线观看| 亚洲一区二区三区影院| 欧美影院一区二区三区| 亚洲香肠在线观看| 欧美日韩精品高清| 手机精品视频在线观看| 欧美日韩精品电影| 日本欧美肥老太交大片| 日韩女优视频免费观看| 久久成人免费电影| 美女一区二区视频| 欧美军同video69gay| 日韩va亚洲va欧美va久久| 777奇米四色成人影色区| 日本人妖一区二区| 精品免费一区二区三区| 国产成人免费视频网站| 国产精品久久久久一区| 色综合视频在线观看| 亚洲图片欧美一区| 51精品国自产在线| 精品一区二区三区香蕉蜜桃| 国产亚洲精品久| 不卡一区二区在线| 亚洲香肠在线观看| 欧美成人乱码一区二区三区| 国产精品88av| 日韩免费福利电影在线观看| 精品一区二区在线播放| 国产精品人人做人人爽人人添| 91在线一区二区三区| 午夜一区二区三区视频| 日韩欧美色综合| 大白屁股一区二区视频| 亚洲在线观看免费| 欧美不卡在线视频| 成人黄动漫网站免费app| 亚洲福中文字幕伊人影院| 日韩欧美国产三级| 成人精品在线视频观看| 亚洲一区二区三区四区在线免费观看| 91精品在线免费| 国产99久久久国产精品潘金网站| 亚洲激情图片qvod| 欧美大度的电影原声| eeuss鲁片一区二区三区在线观看 eeuss鲁片一区二区三区在线看 | 99久久99久久精品国产片果冻 | 日韩高清一区在线| 国产欧美1区2区3区| 欧美日韩在线一区二区| 国产电影一区二区三区| 亚洲一级在线观看| 久久久久久久综合狠狠综合| 色婷婷综合中文久久一本| 久久狠狠亚洲综合| 亚洲美女屁股眼交3| 精品成人在线观看| 欧美综合视频在线观看| 国产精品一区二区三区乱码| 亚洲高清不卡在线观看| 中文字幕免费不卡| 欧美一级理论性理论a| av一区二区不卡| 久久66热偷产精品| 亚洲国产成人91porn| 中文字幕乱码久久午夜不卡 | 成人免费高清视频在线观看| 丝袜美腿亚洲一区二区图片| 中文字幕一区二区三| 精品国产成人在线影院| 欧美三级韩国三级日本三斤| 国产传媒久久文化传媒| 日本三级韩国三级欧美三级| 亚洲精品成人在线| 国产欧美一区二区精品婷婷| 欧美一区二区三区精品| 在线观看日韩av先锋影音电影院| 大胆欧美人体老妇| 国内久久婷婷综合| 人人超碰91尤物精品国产| 一级特黄大欧美久久久| 国产精品天干天干在线综合| 欧美成人福利视频| 91精品国产91久久久久久一区二区| 91在线观看视频| 国产成人精品一区二区三区四区| 免费成人在线网站| 天天综合天天综合色| 一区二区三区自拍| 成人欧美一区二区三区黑人麻豆 | 国产精品一二一区| 精品一区二区三区在线播放视频| 青青草97国产精品免费观看 | 日本福利一区二区| 99久久精品国产麻豆演员表|