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

📄 win_adv_capture.c

📁 mobile ip 在linux下的一种实现
💻 C
字号:
/* $Id: win_adv_capture.c,v 1.2 2001/10/03 13:47:25 jm Exp $ * MN helper for getting L2 data for agent advertisement in Windows environment * * Dynamic hierarchial IP tunnel * Copyright (C) 2001, Jouni Malinen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <pcap.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <cygwin/in.h>#include "debug.h"#include "mn.h"/* ethhdr - 14 octets; iphdr - 20 octets */static struct bpf_insn agentadv_filter[] = {	/* A <- P[14+9:1] - load byte - IP protocol */	BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 14+9),	/* pc += (A == IPPROTO_ICMP) ? 0 : 3 */	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_ICMP, 0, 3),	/* A <- P[14+20:1] - load byte - ICMP type */	BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 14+20),	/* pc += (A == 9) ? 0 : 1 (agentadv) */	BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 9, 0, 1),	BPF_STMT(BPF_RET+BPF_K, (unsigned int)-1), /* accept the packet */	BPF_STMT(BPF_RET+BPF_K, 0) /* filter packet */};static struct bpf_program agentadv_fprog = {	sizeof(agentadv_filter) / sizeof(struct bpf_insn),	agentadv_filter};struct dst_data {	int s;	struct sockaddr_in addr;};static void dispatcher_handler(u_char *user, const struct pcap_pkthdr *header,			       const u_char *pkt_data){	struct dst_data *dst = (struct dst_data *) user;	DEBUG(DEBUG_INFO, "%lu:%lu len=%lu ==> %s:%i\n",	      header->ts.tv_sec, header->ts.tv_usec,	      (unsigned long) header->len,	      inet_ntoa(dst->addr.sin_addr), ntohs(dst->addr.sin_port));	/* send agent advertisement for dynmnd using UDP port 4344 */	/* buf=pkt_data, len=header->len */	if (sendto(dst->s, pkt_data, header->len, 0,		   (struct sockaddr *) &dst->addr, sizeof(dst->addr)) < 0) {		DEBUG(DEBUG_INFO, "dispatcher_handler: sendto failed: %s\n",		      strerror(errno));	}}pid_t init_pcap_for_advs(void){	pcap_t *fp;	char error[PCAP_ERRBUF_SIZE];	char *dev;	struct dst_data dst;	pid_t pid;	pid = fork();	if (pid < 0) {		DEBUG(DEBUG_INFO, "init_pcap_for_advs: fork failed: %s\n",		      strerror(errno));		return -1;	}	if (pid > 0) {		/* in parent - return */		return pid;	}	dst.s = socket(AF_INET, SOCK_DGRAM, 0);	if (dst.s < 0) {		perror("socket");		exit(1);	}	dst.addr.sin_family = AF_INET;	dst.addr.sin_addr.s_addr = htonl((127 << 24 | 1));	dst.addr.sin_port = htons(4344);	/* FIX: should support more than one interface and also detect new	 * interfaces when, e.g., wireless LAN card is inserted */	dev = pcap_lookupdev(error);	if (dev == NULL) {		DEBUG(DEBUG_INFO,		      "init_pcap_for_advs: Could not get device (%s)\n",		      error);		return -1;	}	DEBUG(DEBUG_INFO, "init_pcap_for_advs: using device '%s'\n", dev);	fp = pcap_open_live(dev, 1500, 0, 20, error);	if (fp == NULL) {		DEBUG(DEBUG_INFO, "pcap_open_live failed (%s)\n", error);		return -1;	}	if (pcap_setfilter(fp, &agentadv_fprog) < 0) {		DEBUG(DEBUG_INFO,		      "init_pcap_for_advs: Could not attach BPF\n");		return -1;	}	/* in child - start capturing packets */	DEBUG(DEBUG_INFO, "init_pcap_for_advs: child starting to capture "	      "packets\n");	pcap_loop(fp, 0, dispatcher_handler, (u_char *) &dst);	/* never reached */	DEBUG(DEBUG_INFO, "pcap_loop returned?!\n");	return 0;}

⌨️ 快捷键说明

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