1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright (c) 2018, Joyent, Inc.
14 */
15
16 /*
17 * Send a raw Ethernet frame once a second to a specified MAC address.
18 */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <strings.h>
23 #include <unistd.h>
24 #include <stdarg.h>
25 #include <libgen.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <netdb.h>
30 #include <libdlpi.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <endian.h>
34 #include <err.h>
35
36 #include "dlsend.h"
37
38 static uint_t dlsend_sap = DLSEND_SAP;
39 static const char *dlsend_msg = DLSEND_MSG;
40 static const char *dlsend_prog;
41
42 static void
dlsend_usage(const char * fmt,...)43 dlsend_usage(const char *fmt, ...)
44 {
45 if (fmt != NULL) {
46 va_list ap;
47
48 (void) fprintf(stderr, "%s: ", dlsend_prog);
49 va_start(ap, fmt);
50 (void) vfprintf(stderr, fmt, ap);
51 va_end(ap);
52 }
53
54 (void) fprintf(stderr, "Usage: %s [-s sap] device target-mac\n"
55 "\t-s sap\tspecify SAP to send on\n",
56 dlsend_prog);
57 }
58
59 int
main(int argc,char * argv[])60 main(int argc, char *argv[])
61 {
62 int c, maclen, ret;
63 unsigned long sap;
64 char *eptr;
65 uchar_t *mac;
66 char host[MAXHOSTNAMELEN];
67 uint_t bind_sap;
68 dlpi_handle_t dh;
69 uint64_t count;
70
71 dlsend_prog = basename(argv[0]);
72
73 while ((c = getopt(argc, argv, ":s:")) != -1) {
74 switch (c) {
75 case 's':
76 errno = 0;
77 sap = strtoul(optarg, &eptr, 10);
78 if (errno != 0 || sap == 0 || sap >= UINT16_MAX ||
79 *eptr != '\0') {
80 dlsend_usage("Invalid value for sap (-s): %s\n",
81 optarg);
82 return (2);
83 }
84 dlsend_sap = sap;
85 break;
86 case ':':
87 dlsend_usage("Option -%c requires an operand\n",
88 optopt);
89 return (2);
90 case '?':
91 dlsend_usage("Unknown option: -%c\n", optopt);
92 return (2);
93 }
94 }
95
96 argc -= optind;
97 argv += optind;
98
99 if (argc != 2) {
100 dlsend_usage("missing required operands\n");
101 return (2);
102 }
103
104 if ((mac = _link_aton(argv[1], &maclen)) == NULL) {
105 warnx("failed to convert target address %s\n", argv[1]);
106 return (1);
107 }
108
109 if (gethostname(host, sizeof (host)) != 0) {
110 warnx("failed to obtain the system hostname: %s\n",
111 strerror(errno));
112 (void) strlcpy(host, "<unknown host>", sizeof (host));
113 }
114
115 if ((ret = dlpi_open(argv[0], &dh, 0)) != DLPI_SUCCESS) {
116 warnx("failed to open %s: %s\n", argv[0],
117 dlpi_strerror(ret));
118 exit(1);
119 }
120
121 if ((ret = dlpi_bind(dh, dlsend_sap, &bind_sap)) != DLPI_SUCCESS) {
122 warnx("failed to bind to sap 0x%x: %s\n", dlsend_sap,
123 dlpi_strerror(ret));
124 exit(1);
125 }
126
127 if (bind_sap != dlsend_sap) {
128 warnx("failed to bind to requested sap 0x%x, bound to "
129 "0x%x\n", dlsend_sap, bind_sap);
130 exit(1);
131 }
132
133 count = 0;
134 for (;;) {
135 dlsend_msg_t msg;
136
137 count++;
138 bzero(&msg, sizeof (msg));
139 msg.dm_count = htobe64(count);
140 (void) strlcpy(msg.dm_host, host, sizeof (msg.dm_host));
141 (void) strlcpy(msg.dm_mesg, dlsend_msg, sizeof (msg.dm_mesg));
142 ret = dlpi_send(dh, mac, maclen, &msg, sizeof (msg), NULL);
143 if (ret != DLPI_SUCCESS) {
144 warnx("failed to send message: %s\n",
145 dlpi_strerror(ret));
146 exit(1);
147 }
148
149 (void) sleep(1);
150 }
151
152 /* LINTED: E_STMT_NOT_REACHED */
153 return (0);
154 }
155