123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- /*====================================================================*
- *
- * Copyright (c) 2013 Qualcomm Atheros, Inc.
- *
- * All rights reserved.
- *
- *====================================================================*/
- /*====================================================================*
- *
- * solicit.c - Qualcomm Atheros Network Solicitor;
- *
- * Contributor(s):
- * Charles Maier <cmaier@qca.qualcomm.com>
- *
- *--------------------------------------------------------------------*/
- /*====================================================================*
- * system header files;
- *--------------------------------------------------------------------*/
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <errno.h>
- #include <sys/time.h>
- /*====================================================================*
- * custom header files;
- *--------------------------------------------------------------------*/
- #include "../ether/channel.h"
- #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/files.h"
- #include "../tools/flags.h"
- #include "../tools/error.h"
- #include "../tools/timer.h"
- #include "../lldp/lldp.h"
- #include "../mme/mme.h"
- /*====================================================================*
- * custom source files;
- *--------------------------------------------------------------------*/
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/uintspec.c"
- #include "../tools/hexdecode.c"
- #include "../tools/hexencode.c"
- #include "../tools/hexdump.c"
- #include "../tools/hexstring.c"
- #include "../tools/decdecode.c"
- #include "../tools/decstring.c"
- #include "../tools/todigit.c"
- #include "../tools/strfbits.c"
- #include "../tools/synonym.c"
- #include "../tools/memswap.c"
- #include "../tools/error.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/EthernetHeader.c"
- #endif
- #ifndef MAKEFILE
- #include "../lldp/TLVPack.c"
- #include "../lldp/TLVPackOS.c"
- #include "../lldp/TLVPick.c"
- #include "../lldp/TLVPeek.c"
- #include "../lldp/lldp.c"
- #endif
- /*====================================================================*
- * program constants;
- *--------------------------------------------------------------------*/
- #define SOLICIT_VERBOSE (1 << 0)
- #define SOLICIT_SILENCE (1 << 1)
- #define PLCDEVICE "PLC"
- #define PROFILE "solicit.ini"
- #define SECTION "default"
- /*====================================================================*
- *
- * void solicit (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section);
- *
- *--------------------------------------------------------------------*/
- static void solicit (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section)
- {
- memset (frame, 0, sizeof (* frame));
- EthernetHeader (frame, channel->peer, channel->host, ETH_P_LLDP);
- if (sendpacket (channel, frame, (ETHER_MIN_LEN - ETHER_CRC_LEN)) > 0)
- {
- while (readpacket (channel, frame, sizeof (* frame)) > 0)
- {
- if (! memcmp (frame->frame_dhost, channel->host, sizeof (frame->frame_dhost)))
- {
- TLVPeek (frame->frame_data, sizeof (frame->frame_data));
- }
- }
- }
- return;
- }
- /*====================================================================*
- *
- * void monitor (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section);
- *
- *--------------------------------------------------------------------*/
- static void monitor (struct channel * channel, struct ethernet_frame * frame, char const * profile, char const * section)
- {
- struct timeval ts;
- struct timeval tc;
- ssize_t length;
- if (gettimeofday (& ts, NULL) == - 1)
- {
- error (1, errno, CANT_START_TIMER);
- }
- while ((length = readpacket (channel, frame, sizeof (* frame))) >= 0)
- {
- if (! memcmp (frame->frame_dhost, channel->peer, sizeof (frame->frame_dhost)))
- {
- TLVPeek (frame->frame_data, sizeof (frame->frame_data));
- EthernetHeader (frame, frame->frame_shost, channel->host, ETH_P_LLDP);
- memset (frame->frame_data, 0, sizeof (frame->frame_data));
- if (sendpacket (channel, frame, (ETHER_MIN_LEN - ETHER_CRC_LEN)) <= 0)
- {
- error (0, errno, CHANNEL_CANTSEND);
- }
- }
- if (gettimeofday (& tc, NULL) == - 1)
- {
- error (1, errno, CANT_RESET_TIMER);
- }
- if (channel->timeout < 0)
- {
- continue;
- }
- if (channel->timeout > MILLISECONDS (ts, tc))
- {
- continue;
- }
- break;
- }
- return;
- }
- /*====================================================================*
- *
- * int main (int argc, char const * argv[]);
- *
- *
- *--------------------------------------------------------------------*/
- int main (int argc, char const * argv [])
- {
- extern struct channel channel;
- extern struct _term_ const addresses [LLDP_ADDRESSES];
- extern byte const broadcast [ETHER_ADDR_LEN];
- static char const * optv [] =
- {
- "b:i:p:qs:t:vw:",
- "",
- "Qualcomm Atheros Network Solicitor",
- "b x\tbroadcast address is (x) [" LITERAL (BROADCAST) "]",
- #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
- "p s\tconfiguration profile is (s) [" LITERAL (PROFILE) "]",
- "q\tquiet mode",
- "s s\tconfiguration section is (s) [" LITERAL (SECTION) "]",
- "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
- "v\tverbose mode",
- "w n\twakeup every (n) milliseconds [" LITERAL (LLDP_TIMER) "]",
- (char const *) (0)
- };
- struct ethernet_frame frame;
- char const * profile = PROFILE;
- char const * section = SECTION;
- signed c;
- memcpy (channel.peer, broadcast, sizeof (channel.peer));
- channel.type = ETH_P_LLDP;
- channel.timeout = LLDP_TIMER;
- if (getenv (PLCDEVICE))
- {
- #if defined (WINPCAP) || defined (LIBPCAP)
- channel.ifindex = atoi (getenv (PLCDEVICE));
- #else
- channel.ifname = strdup (getenv (PLCDEVICE));
- #endif
- }
- optind = 1;
- while (~ (c = getoptv (argc, argv, optv)))
- {
- switch (c)
- {
- case 'b':
- if (! hexencode (channel.peer, sizeof (channel.peer), synonym (optarg, addresses, SIZEOF (addresses))))
- {
- error (1, errno, LLDP_BAD_MAC, optarg);
- }
- break;
- case 'i':
- #if defined (WINPCAP) || defined (LIBPCAP)
- channel.ifindex = atoi (optarg);
- #else
- channel.ifname = optarg;
- #endif
- break;
- case 'p':
- profile = optarg;
- break;
- case 'q':
- _setbits (channel.flags, CHANNEL_SILENCE);
- break;
- case 's':
- section = optarg;
- break;
- case 't':
- channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
- break;
- case 'v':
- _setbits (channel.flags, CHANNEL_VERBOSE);
- break;
- case 'w':
- channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
- break;
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (argc)
- {
- error (1, 0, ERROR_TOOMANY);
- }
- if (access (profile, R_OK))
- {
- error (1, errno, FILE_CANTSTAT, profile);
- }
- openchannel (& channel);
- while (true)
- {
- solicit (& channel, & frame, profile, section);
- monitor (& channel, & frame, profile, section);
- }
- closechannel (& channel);
- exit (0);
- }
|