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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? pcap-linux.c

?? tcp數據流重放工具
?? C
?? 第 1 頁 / 共 4 頁
字號:
/*
 *  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 HAVE_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 HAVE_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 HAVE_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.
	 */
	return pcap_read_packet(handle, callback, user);
}

/*
 *  Read a packet from the socket calling the handler provided by
 *  the user. Returns the number of packets received or -1 if an
 *  error occured.
 */
static int
pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
{
	u_char			*bp;
	int			offset;
#ifdef HAVE_PF_PACKET_SOCKETS
	struct sockaddr_ll	from;
	struct sll_header	*hdrp;
#else
	struct sockaddr		from;
#endif
	socklen_t		fromlen;
	int			packet_len, caplen;
	struct pcap_pkthdr	pcap_header;

#ifdef HAVE_PF_PACKET_SOCKETS
	/*
	 * If this is a cooked device, leave extra room for a
	 * fake packet header.
	 */
	if (handle->md.cooked)
		offset = SLL_HDR_LEN;
	else
		offset = 0;
#else
	/*
	 * This system doesn't have PF_PACKET sockets, so it doesn't
	 * support cooked devices.
	 */
	offset = 0;
#endif

	/* Receive a single packet from the kernel */

	bp = handle->buffer + handle->offset;
	do {
		fromlen = sizeof(from);
		packet_len = recvfrom(

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
精品久久久久香蕉网| 精品亚洲国内自在自线福利| 日本二三区不卡| 美国一区二区三区在线播放| 日韩小视频在线观看专区| 老司机精品视频在线| 日本一区二区三级电影在线观看| 风间由美一区二区av101| 国产日韩成人精品| 91精品国产综合久久久久| 三级久久三级久久久| 欧美亚洲综合网| 美女尤物国产一区| 日韩码欧中文字| 日韩一区二区三区视频| 成人精品视频.| 另类小说综合欧美亚洲| 中文字幕一区二区三区精华液| 99精品欧美一区二区三区综合在线| 亚洲情趣在线观看| 亚洲国产精品成人综合| 欧美精品久久久久久久多人混战| caoporen国产精品视频| 99精品久久99久久久久| 91在线精品一区二区三区| 蜜桃视频在线观看一区二区| 亚洲一区免费在线观看| 中文字幕av不卡| 中文字幕一区二区三区乱码在线 | 欧美日韩色综合| 日本三级亚洲精品| 日韩精品91亚洲二区在线观看| 亚洲天堂网中文字| 亚洲三级在线免费观看| 亚洲手机成人高清视频| 综合久久久久久久| 亚洲综合丝袜美腿| 午夜成人免费电影| 免费观看91视频大全| 国产成人av一区二区三区在线观看| 精品亚洲porn| 成人免费高清视频| 91麻豆精品秘密| 欧美日韩午夜在线视频| 日韩久久免费av| 中文字幕中文在线不卡住| 亚洲精品国产a久久久久久| 日韩福利视频导航| 豆国产96在线|亚洲| 99精品热视频| 国产精品午夜电影| 日本一道高清亚洲日美韩| caoporen国产精品视频| 日韩欧美国产一二三区| 自拍偷自拍亚洲精品播放| 国内精品伊人久久久久av一坑| 99久久久久免费精品国产| 欧美日韩日日摸| 一区二区三区中文免费| 免费视频最近日韩| 91福利视频久久久久| 久久久不卡网国产精品一区| 亚洲与欧洲av电影| 99精品国产99久久久久久白柏| 91成人免费在线视频| 中文在线一区二区| 黑人精品欧美一区二区蜜桃| 欧美在线视频你懂得| 欧美一区二区高清| 亚洲人精品午夜| 99精品一区二区三区| 欧美激情综合在线| 激情综合五月天| 欧美mv日韩mv国产| 免费成人av在线| 精品国产一区二区三区四区四| 久久众筹精品私拍模特| 国产美女av一区二区三区| 欧美精品一区二区三区视频 | 色先锋久久av资源部| 亚洲女同一区二区| 一本大道久久a久久综合婷婷| 综合精品久久久| 欧美色综合天天久久综合精品| 亚洲另类一区二区| 日韩欧美一级特黄在线播放| 麻豆精品视频在线| 欧美成人三级在线| 91蜜桃免费观看视频| 亚洲va国产va欧美va观看| 91精品国产欧美日韩| 国产精品一二三区| 亚洲激情在线播放| 欧美久久久久中文字幕| 久久丁香综合五月国产三级网站| 欧美大片在线观看| 成人黄页毛片网站| 伦理电影国产精品| 国产精品久久久久久久久免费桃花| k8久久久一区二区三区 | 亚洲精品国产精华液| 日韩欧美你懂的| 在线观看视频一区二区欧美日韩| 一区二区激情小说| 国产香蕉久久精品综合网| 欧美日韩精品欧美日韩精品一 | 欧洲中文字幕精品| 国产精品一区二区三区网站| 亚洲国产精品传媒在线观看| 精品日韩99亚洲| 51精品视频一区二区三区| 成人黄色软件下载| 国产91精品在线观看| 韩国一区二区三区| 精品一二三四在线| 国产最新精品免费| 亚洲国产精品一区二区久久| 亚洲国产一区二区视频| 亚洲观看高清完整版在线观看| 国产精品乱码一区二区三区软件 | 亚洲综合精品自拍| 中文字幕一区二区5566日韩| 久久免费看少妇高潮| 久久亚洲综合色一区二区三区| 欧美一级视频精品观看| 日韩欧美的一区二区| 欧美一级艳片视频免费观看| 日韩欧美综合一区| 国产午夜亚洲精品理论片色戒| 欧美激情一区二区三区在线| 国产精品视频观看| 亚洲精品免费电影| 日韩成人一区二区| 国产在线精品不卡| 色综合中文字幕| 欧美日本在线一区| 4438x亚洲最大成人网| 国产精品久久综合| 一级做a爱片久久| 久久国产精品72免费观看| 成人av手机在线观看| 欧美日韩一级二级| 国产日产精品一区| 日本亚洲一区二区| 欧美午夜影院一区| 国产日韩欧美激情| 午夜久久电影网| 国产乱人伦偷精品视频不卡| 色综合久久中文综合久久97| 欧美日韩成人综合天天影院 | 丰满白嫩尤物一区二区| 欧美日韩成人一区二区| 中文字幕亚洲一区二区va在线| 免费成人在线网站| 欧美日韩精品久久久| 中文字幕日韩av资源站| 国产九九视频一区二区三区| 91福利区一区二区三区| 18成人在线视频| av一本久道久久综合久久鬼色| 日韩一级大片在线| 午夜精品免费在线观看| 欧美亚洲国产一卡| 亚洲乱码国产乱码精品精可以看 | 蜜臀av性久久久久蜜臀aⅴ流畅| 日本韩国欧美一区| 国产日韩精品一区| 91网站在线播放| 亚洲高清视频中文字幕| 色婷婷久久99综合精品jk白丝| 久久久99免费| 成人av免费观看| 亚洲三级免费观看| 666欧美在线视频| 捆绑变态av一区二区三区| 久久久久国产精品厨房| 波多野结衣在线一区| 亚洲一区二区三区中文字幕 | 久久老女人爱爱| 91蜜桃免费观看视频| 日韩二区在线观看| 国产精品高潮呻吟| 日韩视频不卡中文| 日本电影欧美片| 精品夜夜嗨av一区二区三区| 专区另类欧美日韩| 精品久久五月天| 欧美乱熟臀69xxxxxx| thepron国产精品| 国产美女视频一区| 久久疯狂做爰流白浆xx| 亚洲第一成人在线| 亚洲午夜久久久久久久久久久 | 精品一区二区三区在线观看国产| 亚洲精品免费看| 亚洲视频 欧洲视频| 日韩三级伦理片妻子的秘密按摩| 欧美性极品少妇| 欧美性生交片4| av网站免费线看精品|