snoop.c

来自「wm PNE 3.3 source code, running at more 」· C语言 代码 · 共 216 行

C
216
字号
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/lib/snoop.c,v 1.1.1.1 2001/11/05 17:48:43 tneale Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved.  Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** *  Copyright 1993-1997 Epilogue Technology Corporation. *  Copyright 1998 Integrated Systems, Inc. *  All rights reserved. ****************************************************************************//* * $Log: snoop.c,v $ * Revision 1.1.1.1  2001/11/05 17:48:43  tneale * Tornado shuffle * * Revision 1.7  2001/01/19 22:23:53  paul * Update copyright. * * Revision 1.6  2000/03/17 00:12:46  meister * Update copyright message * * Revision 1.5  1998/02/25 04:57:40  sra * Update copyrights. * * Revision 1.4  1997/03/20 06:53:12  sra * DFARS-safe copyright text.  Zap! * * Revision 1.3  1997/02/25 10:58:16  sra * Update copyright notice, dust under the bed. * * Revision 1.2  1997/02/19 08:10:29  sra * More fun merging snmptalk into snark, general snark cleanup. * * Revision 1.1  1996/11/13 15:06:46  mrf * Initial revision * * Revision 2.2  1996/03/22  10:05:39  sra * Update copyrights prior to Attache 3.2 release. * * Revision 2.1  1995/06/22  05:29:06  sra * Preliminary changes for multicast and OSPF. * * Revision 2.0  1995/05/10  22:38:15  sra * Attache release 3.0. * * Revision 1.4  1995/01/06  00:52:48  sra * Update copyright notice for 2.1 release. * * Revision 1.3  1994/09/02  21:15:48  sra * Add still more parentheses to the for() loop from hell in bpf_find(). * * Revision 1.2  1993/07/29  04:30:26  sra * Don't use the inline versions of the byteswap routines, GCC gets * confused. * * Revision 1.1  1993/07/05  21:53:30  sra * Initial revision * *//* [clearcase]modification history-------------------01a,19apr05,job  update copyright notices*//* * SNOOP interface to ethernet for Attache testing under Irix. * * All access to SNOOP should be through this module, the only thing we * expect our caller to do with our file descriptor is use it in * select() calls.  There are enough name conflicts between the unix * networking code and the Attache networking code that it's probably * hopeless to have the two mingled in the same file. */#include <stdio.h>#include <errno.h>#include <string.h>#include <unistd.h>#include <stdlib.h>#include <sys/param.h>#include <sys/types.h>#include <sys/time.h>#include <sys/socket.h>#include <sys/file.h>#include <sys/ioctl.h>#include <net/raw.h>#include <net/if.h>#include <netinet/in.h>#include <netinet/if_ether.h>int snoop_open(char *ifname, unsigned mtu, unsigned char *mac){  int fd;  struct sockaddr_raw sr;  struct snoopfilter sf;  u_int cc, on;/* get a snoop socket */  fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);  if (fd < 0) {    perror("Couldn't open SNOOP socket");    return -1;  }/* bind the socket */  bzero((char *)&sr, sizeof(sr));  sr.sr_family = AF_RAW;  sr.sr_port = 0;  (void)strncpy(sr.sr_ifname, ifname, sizeof(sr.sr_ifname));  if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {    (void)close(fd);    perror("Couldn't bind SNOOP socket");    return -1;  }/* set snoop filter */  bzero((char *)&sf, sizeof(sf));  if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {    perror("Couldn't set SNOOP filter");    (void)close(fd);    return -1;  }/* set receive length for packets and start snooping.*/  cc = 64 * 1024; /* largest packet is 64K */  (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&cc, sizeof(cc));  if (ioctl(fd, SIOCSNOOPING, &on) < 0) {    perror ("Couldn't start SNOOPing");    (void)close(fd);    return -1;  }  return fd;}#define ETHERHDRPAD RAW_HDRPAD(sizeof(struct ether_header))struct etherpacket {  struct snoopheader	snoop;  char			pad[ETHERHDRPAD];  struct ether_header	ether;  char			data[ETHERMTU];};int snoop_read  (int fd,   unsigned mtu,   void (*handler)(unsigned char *, unsigned, unsigned, void *),   void *cookie){  struct etherpacket *ep;  struct snoopheader *sh;  int datalen, caplen;    if ((ep = (struct etherpacket *) malloc(sizeof(struct etherpacket)))  == 0) {    fprintf(stderr, "malloc(%d) failed\n", mtu);    return -1;  }  /*   * Try to read a packet, restart if we get screwed by the debugger.   * Return on other error or on EOF.   */  do {    caplen = read(fd, ep, mtu);  } while (caplen < 0 && errno == EINTR);  if (caplen <= 0) {    perror ("Read from SNOOP socket failed");    return caplen;  }  sh = (struct snoopheader *)ep;  datalen = sh->snoop_packetlen;  handler((char *)&(ep->ether), datalen, datalen, cookie);  return 1;}int snoop_write(int fd, unsigned char *data, unsigned datalen){  return write(fd, data, datalen);}void snoop_close(int fd){  (void) close(fd);}/* * Find "all" ethernet interfaces. * Apparently doing this is hard on Irix, so just hardwire it for now. */void snoop_find(void (*handler)(char *, void *), void *cookie){  handler("ec0", cookie);}

⌨️ 快捷键说明

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