123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include "../tools/getoptv.h"
- #include "../tools/putoptv.h"
- #include "../tools/error.h"
- #include "../tools/memory.h"
- #include "../tools/number.h"
- #include "../tools/symbol.h"
- #include "../tools/flags.h"
- #include "../ether/channel.h"
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/error.c"
- #include "../tools/hexencode.c"
- #include "../tools/hexload.c"
- #include "../tools/hexdump.c"
- #include "../tools/todigit.c"
- #include "../tools/uintspec.c"
- #include "../tools/basespec.c"
- #include "../tools/synonym.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
- #define EFSU_INTERFACE "PLC"
- #define EFSU_ETHERTYPE 0x88E1
- #define EFSU_PAUSE 0
- #define EFSU_DELAY 0
- #define EFSU_LOOP 1
- #ifndef ETHER_CRC_LEN
- #define ETHER_CRC_LEN 4
- #endif
- static const struct _term_ protocols [] =
- {
- {
- "hp10",
- "887B"
- },
- {
- "hpav",
- "88E1"
- }
- };
- static void function (struct channel * channel, void * memory, ssize_t extent)
- {
- struct ether_header * frame = (struct ether_header *)(memory);
- unsigned length;
- while ((length = (unsigned)(hexload (memory, extent, stdin))) > 0)
- {
- if (length < (ETHER_MIN_LEN - ETHER_CRC_LEN))
- {
- error (1, ENOTSUP, "Frame size of %d is less than %d bytes", length, (ETHER_MIN_LEN - ETHER_CRC_LEN));
- }
- if (length > (ETHER_MAX_LEN - ETHER_CRC_LEN))
- {
- error (1, ENOTSUP, "Frame size of %d is more than %d bytes", length, (ETHER_MAX_LEN - ETHER_CRC_LEN));
- }
- if (_anyset (channel->flags, CHANNEL_UPDATE_TARGET))
- {
- memcpy (frame->ether_dhost, channel->peer, sizeof (frame->ether_dhost));
- }
- if (_anyset (channel->flags, CHANNEL_UPDATE_SOURCE))
- {
- memcpy (frame->ether_shost, channel->host, sizeof (frame->ether_shost));
- }
- sendpacket (channel, memory, length);
- if (_anyset (channel->flags, CHANNEL_LISTEN))
- {
- while (readpacket (channel, memory, extent) > 0);
- }
- }
- return;
- }
- static void iterate (int argc, char const * argv [], struct channel * channel, unsigned pause)
- {
- byte buffer [ETHER_MAX_LEN];
- if (!argc)
- {
- function (channel, buffer, sizeof (buffer));
- }
- while ((argc) && (* argv))
- {
- if (!freopen (* argv, "rb", stdin))
- {
- error (1, errno, "Can't open %s", * argv);
- }
- function (channel, buffer, sizeof (buffer));
- argc--;
- argv++;
- if ((argc) && (* argv))
- {
- sleep (pause);
- }
- }
- return;
- }
- int main (int argc, char const * argv [])
- {
- extern struct channel channel;
- static char const * optv [] =
- {
- "d:e:hi:l:p:t:vw:",
- PUTOPTV_S_FUNNEL,
- "Ethernet Frame Send Utility",
- "d x\treplace destination address with (x)",
- "e x\techo return frames having ethertype (x) [" LITERAL (EFSU_ETHERTYPE) "]",
- "h\treplace source address with host address",
- #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
- "l n\trepeat file sequence (n) times [" LITERAL (EFSU_LOOP) "]",
- "p n\twait (n) seconds between files [" LITERAL (EFSU_PAUSE) "]",
- "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
- "v\tverbose messages",
- "w n\twait (n) seconds between loops [" LITERAL (EFSU_DELAY) "]",
- (char const *) (0)
- };
- unsigned pause = EFSU_PAUSE;
- unsigned delay = EFSU_DELAY;
- unsigned loop = EFSU_LOOP;
- signed c;
- channel.type = EFSU_ETHERTYPE;
- if (getenv (EFSU_INTERFACE))
- {
- #if defined (WINPCAP) || defined (LIBPCAP)
- channel.ifindex = atoi (getenv (EFSU_INTERFACE));
- #else
- channel.ifname = strdup (getenv (EFSU_INTERFACE));
- #endif
- }
- optind = 1;
- while ((c = getoptv (argc, argv, optv)) != -1)
- {
- switch (c)
- {
- case 'd':
- _setbits (channel.flags, CHANNEL_UPDATE_TARGET);
- if (!hexencode (channel.peer, sizeof (channel.peer), optarg))
- {
- error (1, errno, "%s", optarg);
- }
- break;
- case 'e':
- _setbits (channel.flags, CHANNEL_LISTEN);
- channel.type = (uint16_t)(basespec (synonym (optarg, protocols, SIZEOF (protocols)), 16, sizeof (channel.type)));
- break;
- case 'h':
- _setbits (channel.flags, CHANNEL_UPDATE_SOURCE);
- break;
- case 'i':
- #if defined (WINPCAP) || defined (LIBPCAP)
- channel.ifindex = atoi (optarg);
- #else
- channel.ifname = optarg;
- #endif
- break;
- case 'l':
- loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
- break;
- case 'p':
- pause = (unsigned)(uintspec (optarg, 0, 1200));
- 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 'w':
- delay = (unsigned)(uintspec (optarg, 0, 1200));
- break;
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- openchannel (&channel);
- while (loop--)
- {
- iterate (argc, argv, &channel, pause);
- if (loop)
- {
- sleep (delay);
- }
- }
- closechannel (&channel);
- return (0);
- }
|