/*====================================================================* * * Copyright (c) 2013 Qualcomm Atheros, Inc. * * All rights reserved. * *====================================================================*/ /*====================================================================* * * plcsnif.c - * * Contributor(s): * Charles Maier * *--------------------------------------------------------------------*/ /*====================================================================* * system header files; *--------------------------------------------------------------------*/ #include #include #include #include /*====================================================================* * custom header files; *--------------------------------------------------------------------*/ #include "../tools/getoptv.h" #include "../tools/putoptv.h" #include "../tools/memory.h" #include "../tools/number.h" #include "../tools/symbol.h" #include "../tools/types.h" #include "../tools/flags.h" #include "../tools/files.h" #include "../tools/error.h" #include "../plc/plc.h" #include "../nda/nda.h" /*====================================================================* * custom source files; *--------------------------------------------------------------------*/ #ifndef MAKEFILE #include "../plc/chipset.c" #include "../plc/Devices.c" #include "../plc/Confirm.c" #include "../plc/Display.c" #include "../plc/Failure.c" #include "../plc/Request.c" #include "../plc/ReadMME.c" #include "../plc/SendMME.c" #endif #ifndef MAKEFILE #include "../tools/getoptv.c" #include "../tools/putoptv.c" #include "../tools/version.c" #include "../tools/uintspec.c" #include "../tools/hexdump.c" #include "../tools/hexdecode.c" #include "../tools/hexencode.c" #include "../tools/hexstring.c" #include "../tools/hexout.c" #include "../tools/todigit.c" #include "../tools/synonym.c" #include "../tools/error.c" #include "../tools/typename.c" #endif #ifndef MAKEFILE #include "../ether/openchannel.c" #include "../ether/closechannel.c" #include "../ether/readpacket.c" #include "../ether/sendpacket.c" #include "../ether/channel.c" #endif #ifndef MAKEFILE #include "../mme/MMECode.c" #include "../mme/EthernetHeader.c" #include "../mme/QualcommHeader.c" #include "../mme/UnwantedMessage.c" #endif #ifndef MAKEFILE #include "../nda/Sniffer.c" #include "../nda/Monitor.c" #endif /*====================================================================* * program constants; *--------------------------------------------------------------------*/ #define DEFAULT_MODE 0 /*====================================================================* * * int main (int argc, char const * argv[]); * *--------------------------------------------------------------------*/ int main (int argc, char const * argv []) { extern struct channel channel; extern const struct _term_ devices []; static char const * optv [] = { "dbei:m:qt:v", "device [device] [...]", "Qualcomm Atheros Powerline Traffic Sniffer", "d\tdump sniffer data in hex format on stdout", "e\tredirect stderr messages to stdout", #if defined (WINPCAP) || defined (LIBPCAP) "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]", #else "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]", #endif "m n\tmode is (n) [" LITERAL (DEFAULT_MODE) "]", "q\tquiet mode", "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]", "v\tverbose mode", (char const *) (0) }; #include "../plc/plc.c" signed colon = '\0'; signed space = ' '; signed c; plc.action = DEFAULT_MODE; optind = 1; if (getenv (PLCDEVICE)) { #if defined (WINPCAP) || defined (LIBPCAP) channel.ifindex = atoi (getenv (PLCDEVICE)); #else channel.ifname = strdup (getenv (PLCDEVICE)); #endif } while (~ (c = getoptv (argc, argv, optv))) { switch (c) { case 'd': _setbits (plc.flags, PLC_MONITOR); break; case 'e': dup2 (STDOUT_FILENO, STDERR_FILENO); break; case 'i': #if defined (WINPCAP) || defined (LIBPCAP) channel.ifindex = atoi (optarg); #else channel.ifname = optarg; #endif break; case 'm': plc.action = (uint8_t) (uintspec (optarg, 0, UCHAR_MAX)); break; case 'q': _setbits (plc.flags, PLC_SILENCE); break; case 't': channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX)); break; case 'v': _setbits (channel.flags, CHANNEL_VERBOSE); _setbits (plc.flags, PLC_VERBOSE); break; default: break; } } argc -= optind; argv += optind; openchannel (& channel); if (! (plc.message = malloc (sizeof (* plc.message)))) { error (1, errno, PLC_NOMEMORY); } if (! argc) { if (_anyset (plc.flags, PLC_MONITOR)) { Monitor (& plc, colon, space); } else { Sniffer (& plc); } } while ((argc) && (* argv)) { if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices)))) { error (1, errno, PLC_BAD_MAC, * argv); } Sniffer (& plc); argv++; argc--; } free (plc.message); closechannel (& channel); exit (0); }