summaryrefslogtreecommitdiff
path: root/usr/src/cmd/dlutil/dlsend.c
blob: 995ba45eee8becd4e11f549f9739795718214bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright (c) 2018, Joyent, Inc.
 */

/*
 * Send a raw Ethernet frame once a second to a specified MAC address.
 */

#include <stdio.h>
#include <errno.h>
#include <strings.h>
#include <unistd.h>
#include <stdarg.h>
#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <libdlpi.h>
#include <stddef.h>
#include <stdint.h>
#include <endian.h>
#include <err.h>

#include "dlsend.h"

static uint_t dlsend_sap = DLSEND_SAP;
static const char *dlsend_msg = DLSEND_MSG;
static const char *dlsend_prog;

static void
dlsend_usage(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		(void) fprintf(stderr, "%s: ", dlsend_prog);
		va_start(ap, fmt);
		(void) vfprintf(stderr, fmt, ap);
		va_end(ap);
	}

	(void) fprintf(stderr, "Usage: %s [-s sap] device target-mac\n"
	    "\t-s sap\tspecify SAP to send on\n",
	    dlsend_prog);
}

int
main(int argc, char *argv[])
{
	int c, maclen, ret;
	unsigned long sap;
	char *eptr;
	uchar_t *mac;
	char host[MAXHOSTNAMELEN];
	uint_t bind_sap;
	dlpi_handle_t dh;
	uint64_t count;

	dlsend_prog = basename(argv[0]);

	while ((c = getopt(argc, argv, ":s:")) != -1) {
		switch (c) {
		case 's':
			errno = 0;
			sap = strtoul(optarg, &eptr, 10);
			if (errno != 0 || sap == 0 || sap >= UINT16_MAX ||
			    *eptr != '\0') {
				dlsend_usage("Invalid value for sap (-s): %s\n",
				    optarg);
				return (2);
			}
			dlsend_sap = sap;
			break;
		case ':':
			dlsend_usage("Option -%c requires an operand\n",
			    optopt);
			return (2);
		case '?':
			dlsend_usage("Unknown option: -%c\n", optopt);
			return (2);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc != 2) {
		dlsend_usage("missing required operands\n");
		return (2);
	}

	if ((mac = _link_aton(argv[1], &maclen)) == NULL) {
		warnx("failed to convert target address %s\n", argv[1]);
		return (1);
	}

	if (gethostname(host, sizeof (host)) != 0) {
		warnx("failed to obtain the system hostname: %s\n",
		    strerror(errno));
		(void) strlcpy(host, "<unknown host>", sizeof (host));
	}

	if ((ret = dlpi_open(argv[0], &dh, 0)) != DLPI_SUCCESS) {
		warnx("failed to open %s: %s\n", argv[0],
		    dlpi_strerror(ret));
		exit(1);
	}

	if ((ret = dlpi_bind(dh, dlsend_sap, &bind_sap)) != DLPI_SUCCESS) {
		warnx("failed to bind to sap 0x%x: %s\n", dlsend_sap,
		    dlpi_strerror(ret));
		exit(1);
	}

	if (bind_sap != dlsend_sap) {
		warnx("failed to bind to requested sap 0x%x, bound to "
		    "0x%x\n", dlsend_sap, bind_sap);
		exit(1);
	}

	count = 0;
	for (;;) {
		dlsend_msg_t msg;

		count++;
		bzero(&msg, sizeof (msg));
		msg.dm_count = htobe64(count);
		(void) strlcpy(msg.dm_host, host, sizeof (msg.dm_host));
		(void) strlcpy(msg.dm_mesg, dlsend_msg, sizeof (msg.dm_mesg));
		ret = dlpi_send(dh, mac, maclen, &msg, sizeof (msg), NULL);
		if (ret != DLPI_SUCCESS) {
			warnx("failed to send message: %s\n",
			    dlpi_strerror(ret));
			exit(1);
		}

		(void) sleep(1);
	}

	/* LINTED: E_STMT_NOT_REACHED */
	return (0);
}