📄 dhcp-sniff.c
字号:
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-sniff.c,v 1.8 2003/06/28 16:30:01 actmodern Exp $ * * Copyright 2002 Thamer Alharbash * * 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. * * dhcp-agent sniffer * */#define MODULE_NAME "main"#include "dhcp-local.h"#include "dhcp-libutil.h"#include "dhcp-librawnet.h"#include "dhcp-print.h"#include "dhcp-sniff-defaults.h"int interactive = 1;#if !defined(HAVE_PROGNAME)const char *__progname;#endif /* HAVE_PROGNAME */char *workdirectory = ".";static void usage(char *n){ fprintf(stderr, "usage: %s [-bv] [-i interface_name ]\n", n); exit(1);}int main(int argc, char *argv[]){ int c; char *interface_name = NULL; rawnet_t *net; int retval; int sport, dport; stringbuffer_t *filter; print_packet p_packet = print_dhcp_packet_verbose; list_t *available_interfaces;#if !defined(HAVE_PROGNAME) __progname = argv[0];#endif /* HAVE_PROGNAME */ while((c = getopt(argc, argv, "bvi:")) != -1) { switch (c) { case 'i': interface_name = xstrdup(optarg); break; case 'v': printf("%s version: %s\n", argv[0], VERSION); exit(0); case 'b': /* brief mode. */ p_packet = print_dhcp_packet_brief; break; default: usage(argv[0]); } } if(interface_name == NULL) { /* get the first available on. */ available_interfaces = rawnet_list_active_interfaces(); if(available_interfaces != NULL) { interface_name = xstrdup(list_first(available_interfaces)); list_destroy(available_interfaces, xfree); } else { FATAL_MESSAGE("unable to find suitable interface"); } } /* Get port numbers from services db. */ dport = rawnet_port_for_service("bootps", "udp"); sport = rawnet_port_for_service("bootpc", "udp"); if(dport == -1 || sport == -1) { dport = BOOTP_CLIENT; sport = BOOTP_SERVER; } else { dport = ntohs(dport); sport = ntohs(sport); } filter = stringbuffer_create(); stringbuffer_aprintf(filter, "arp or icmp or (udp and (port %d or port %d))", dport, sport); /* It's ok not to set ports since we're not writing any packets. */ net = rawnet_create(interface_name, stringbuffer_getstring(filter), DEFAULT_SNIFF_SNAPLEN, 0, 0, 1, 0); if(net == NULL) { FATAL_MESSAGE("unable to access raw network"); } stringbuffer_destroy(filter); INFO_MESSAGE("opened %s for packet capturing", interface_name); printf("\n"); /* Sniffing loop. */ while(1) { retval = rawnet_get_packet(net, NULL); switch (retval) { case RAWNET_PCAP_ERROR: FATAL_MESSAGE("encountered fatal error while sniffing"); case RAWNET_OK: /* Print it out */ if(net->type == RAWNET_DHCP) { p_packet(net); printf("\n"); fflush(stdout); dhcp_purge(net->dhcp_p); } break; case RAWNET_MALFORMED_PACKET: case RAWNET_UNHANDLED: default: break; /* don't handle. */ } } exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -