123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- /*====================================================================*
- *
- * Copyright (c) 2013 Qualcomm Atheros, Inc.
- *
- * All rights reserved.
- *
- *====================================================================*/
- /*====================================================================*
- *
- * arpc.c -
- *
- * Contributor(s):
- * Charles Maier <cmaier@qca.qualcomm.com>
- * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
- *
- *--------------------------------------------------------------------*/
- /*====================================================================*
- * system header files;
- *--------------------------------------------------------------------*/
- #include <unistd.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <time.h>
- #include <sys/time.h>
- /*====================================================================*
- * custom header files;
- *--------------------------------------------------------------------*/
- #include "../tools/getoptv.h"
- #include "../tools/putoptv.h"
- #include "../tools/memory.h"
- #include "../tools/number.h"
- #include "../tools/types.h"
- #include "../tools/flags.h"
- #include "../tools/files.h"
- #include "../tools/error.h"
- #include "../ether/channel.h"
- #include "../mme/mme.h"
- #include "../plc/plc.h"
- /*====================================================================*
- * custom source files;
- *--------------------------------------------------------------------*/
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/hexdump.c"
- #include "../tools/uintspec.c"
- #include "../tools/todigit.c"
- #include "../tools/hexwrite.c"
- #include "../tools/error.c"
- #endif
- #ifndef MAKEFILE
- #include "../ether/channel.c"
- #include "../ether/openchannel.c"
- #include "../ether/closechannel.c"
- #include "../ether/sendpacket.c"
- #include "../ether/readpacket.c"
- #endif
- #ifndef MAKEFILE
- #include "../mme/ARPCPrint.c"
- #include "../mme/ARPCWrite.c"
- #include "../mme/ARPCPeek.c"
- #endif
- /*====================================================================*
- * program constants;
- *--------------------------------------------------------------------*/
- #define PLCDEVICE "PLC"
- #if defined (WIN32)
- #define TIMEVALUE "%012ld.%06ld"
- #elif defined (__APPLE__)
- #define TIMEVALUE "%012lu.%06u"
- #elif defined (__linux__)
- #define TIMEVALUE "%012ld.%06ld"
- #elif defined (__OpenBSD__)
- #define TIMEVALUE "%012ld.%06ld"
- #else
- #error "Unknown environment"
- #endif
- /*====================================================================*
- *
- * void ARPCDump (struct channel * channel, void const * memory, size_t extent);
- *
- * print or write ARPC messages based on ARPCID.
- *
- *--------------------------------------------------------------------*/
- static void ARPCDump (struct channel * channel, void const * memory, size_t extent)
- {
- #ifndef __GNUC__
- #pragma pack (push,1)
- #endif
- struct __packed vs_arpc_data
- {
- uint32_t BYPASS;
- uint16_t ARPCID;
- uint16_t DATALENGTH;
- uint8_t DATAOFFSET;
- uint8_t RESERVED [3];
- uint16_t ARGOFFSET;
- uint16_t STROFFSET;
- uint16_t ARGLENGTH;
- uint16_t STRLENGTH;
- uint8_t LIST [1];
- }
- * data = (struct vs_arpc_data *) (memory);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- struct timeval tv;
- gettimeofday (& tv, NULL);
- if (LE16TOH (data->ARPCID) == 1)
- {
- if (_anyset (channel->flags, CHANNEL_MONITOR))
- {
- fprintf (stdout, TIMEVALUE " ", tv.tv_sec, tv.tv_usec);
- }
- ARPCPrint (stdout, memory, extent);
- return;
- }
- if (LE16TOH (data->ARPCID) == 2)
- {
- if (_anyset (channel->flags, CHANNEL_MONITOR))
- {
- if (isatty (fileno (stdout)))
- {
- hexwrite (fileno (stdout), & tv, sizeof (tv));
- }
- else
- {
- fwrite (& tv, sizeof (tv), 1, stdout);
- }
- }
- ARPCWrite (stdout, memory, extent);
- return;
- }
- return;
- }
- /*====================================================================*
- *
- * void function (struct channel * channel, signed fd);
- *
- * capture and record VS_ARPC messages from any Atheros powerline
- * device; devices must be programmed to send these messages;
- *
- * this function has no practical use under operation conditions;
- *
- *--------------------------------------------------------------------*/
- static void function (struct channel * channel)
- {
- struct message message;
- #ifndef __GNUC__
- #pragma pack (push,1)
- #endif
- struct __packed vs_arpc_ind
- {
- struct ethernet_hdr ethernet;
- struct qualcomm_hdr qualcomm;
- uint16_t RDATALENGTH;
- uint8_t RDATAOFFSET;
- uint8_t RDATA [1];
- }
- * indicate = (struct vs_arpc_ind *) (& message);
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- signed length;
- while ((length = readpacket (channel, & message, sizeof (message))) >= 0)
- {
- if (! length)
- {
- continue;
- }
- if (ntohs (indicate->ethernet.MTYPE) != ETH_P_HPAV)
- {
- continue;
- }
- if (indicate->qualcomm.MMV != 0)
- {
- continue;
- }
- if (LE16TOH (indicate->qualcomm.MMTYPE) != (VS_ARPC | MMTYPE_IND))
- {
- continue;
- }
- ARPCDump (channel, & indicate->RDATA [indicate->RDATAOFFSET], LE16TOH (indicate->RDATALENGTH) - indicate->RDATAOFFSET);
- }
- return;
- }
- /*====================================================================*
- *
- * void timeout ();
- *
- * print current time as both hh:mm:ss and seconds.milliseconds;
- *
- * POSIX says ...
- *
- * struct { time_t tv_sec; suseconds_t tv_usec; } timeval;
- *
- * on OSX ...
- *
- * typedef __darwin_time_t time_t;
- * typesef __darwin_suseconds_t suseconds_t;
- *
- * typedef long __darwin_time_t;
- * typedef __int32_t __darwin_suseconds_t;
- *
- * on Windows ...
- *
- * on Linux ...
- *
- *--------------------------------------------------------------------*/
- static void timeout ()
- {
- char time [9];
- struct timeval tv;
- gettimeofday (& tv, NULL);
- strftime (time, sizeof (time), "%H:%M:%S", localtime ((const time_t *) (& tv.tv_sec)));
- printf ("%s " TIMEVALUE "\n", time, tv.tv_sec, tv.tv_usec);
- printf ("sizeof (tv.tv_sec) is " SIZE_T_SPEC " bytes\n", sizeof (tv.tv_sec));
- printf ("sizeof (tv.tv_usec) is " SIZE_T_SPEC " bytes\n", sizeof (tv.tv_usec));
- return;
- }
- /*====================================================================*
- *
- * int main (int argc, char * argv[]);
- *
- *--------------------------------------------------------------------*/
- int main (int argc, char const * argv [])
- {
- extern struct channel channel;
- static char const * optv [] =
- {
- "i:mqt:vz",
- PUTOPTV_S_DIVINE,
- "Qualcomm Atheros Asynchronous Remote Procedure Call Monitor",
- #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\tprefix messages with system time in seconds.milliseconds",
- "q\tsuppress output on stdout",
- "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
- "v\tverbose messages on stderr",
- "z\tprint sample time as hh:mm:ss and seconds.milliseconds then exit",
- (char const *) (0)
- };
- signed c;
- 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 'i':
- #if defined (WINPCAP) || defined (LIBPCAP)
- channel.ifindex = atoi (optarg);
- #else
- channel.ifname = optarg;
- #endif
- break;
- case 'm':
- _setbits (channel.flags, CHANNEL_MONITOR);
- break;
- case 'q':
- _setbits (channel.flags, CHANNEL_SILENCE);
- break;
- case 't':
- channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
- break;
- case 'v':
- _setbits (channel.flags, CHANNEL_VERBOSE);
- break;
- case 'z':
- timeout ();
- return (0);
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (argc)
- {
- error (1, ENOTSUP, ERROR_TOOMANY);
- }
- #ifdef WIN32
- setmode (STDOUT_FILENO, O_BINARY);
- #endif
- openchannel (& channel);
- function (& channel);
- closechannel (& channel);
- fflush (stdout);
- return (0);
- }
|