File: mcsender.c

package info (click to toggle)
smcroute 2.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 636 kB
  • ctags: 221
  • sloc: ansic: 2,296; sh: 1,310; perl: 161; makefile: 61
file content (234 lines) | stat: -rw-r--r-- 5,589 bytes parent folder | download | duplicates (2)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* Multicast test tool
 *
 * Copyright (C) 2001-2005  Carsten Schill <carsten@cschill.de>
 * Copyright (C) 2006-2009  Julien BLACHE <jb@jblache.org>
 * Copyright (C) 2009       Todd Hayton <todd.hayton@gmail.com>
 * Copyright (C) 2009-2011  Micha Lenk <micha@debian.org>
 * Copyright (C) 2011-2013  Joachim Nilsson <troglobit@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "mclab.h"

int do_debug_logging = 0;

static const char Usage[] = "mcsender [-t<n>] [-i<ifname>] <ip-address:port>\n";
static const char McMsg[] = "this is the test message from mclab/mcsender\n";

static void usage(void);

static void SetOif4(int, char *);
static void SetTtl4(int, unsigned);

static void SetOif6(int, char *);
static void SetTtl6(int, unsigned);

static void getSockAdr(struct sockaddr *SaPt, socklen_t * SaLenPt, char *AddrSt, char *PortSt);

int main(int ArgCn, char *ArgVc[])
{
	unsigned TtlVal = 0;
	char *OifVal = NULL;
	char *AddrSt = NULL;
	char *PortSt = NULL;
	char *Pt;
	void (*SetTtl) (int, unsigned) = NULL;
	void (*SetOif) (int, char *) = NULL;
	struct sockaddr_storage TarAdr;
	socklen_t TarAdrLen = 0;

	if (ArgCn < 2) {
		usage();
		exit(1);
	}

	while (*++ArgVc) {
		Pt = *ArgVc;

		/* option */
		if (*Pt == '-') {
			switch (*++Pt) {

			case 'D':
				do_debug_logging = 1;
				break;

			case 't':
				if (sscanf(Pt + 1, " %3u", &TtlVal) != 1 || TtlVal < 1) {
					usage();
					exit(1);
				}
				break;

			case 'i':
				OifVal = Pt + 1;
				break;

			default:
				usage();
				exit(1);
			}
		} else {	/* argument */

			memset(&TarAdr, 0, sizeof(TarAdr));

			AddrSt = Pt;

			Pt = strrchr(AddrSt, ':');
			if (Pt == NULL) {
				usage();
				exit(1);
			}

			*Pt++ = '\0';
			PortSt = Pt;

			getSockAdr(SA(&TarAdr), &TarAdrLen, AddrSt, PortSt);

			SetTtl = (TarAdr.ss_family == AF_INET) ? SetTtl4 : SetTtl6;
			SetOif = (TarAdr.ss_family == AF_INET) ? SetOif4 : SetOif6;
		}
	}

	if (TarAdrLen)
	{
		int UdpSock = socket(TarAdr.ss_family, SOCK_DGRAM, IPPROTO_UDP);

		if (UdpSock < 0)
			smclog(LOG_ERR, errno, "UDP socket open");

		if (TtlVal)
			(*SetTtl) (UdpSock, TtlVal);
		if (OifVal)
			(*SetOif) (UdpSock, OifVal);

		while (1) {
			if (sendto(UdpSock, McMsg, sizeof(McMsg), 0, SA(&TarAdr), TarAdrLen) != sizeof(McMsg))
				smclog(LOG_WARNING, errno, "send to UDP socket");

			sleep(1);
		}
	}

	return 0;
}

static void usage(void)
{
	fprintf(stderr, "Usage: %s\n", Usage);
}

static void SetTtl4(int Sock, unsigned Ttl)
{
	if (setsockopt(Sock, IPPROTO_IP, IP_MULTICAST_TTL, &Ttl, sizeof(Ttl)))
		smclog(LOG_ERR, errno, "set IP_MULTICAST_TTL");
}

static void SetOif4(int Sock, char *ifname)
{
	struct ifreq IfReq;
	struct sockaddr_in *Sin4 = NULL;

	memset(&IfReq, 0, sizeof(IfReq));
	strncpy(IfReq.ifr_name, ifname, sizeof(IfReq.ifr_name));

	if (ioctl(Sock, SIOCGIFADDR, &IfReq) < 0)
		smclog(LOG_ERR, errno, "ioctl SIOCGIFADDR");

	switch (IfReq.ifr_addr.sa_family) {
	case AF_INET:
		Sin4 = SIN4(&IfReq.ifr_addr);
		break;

	default:
		fprintf(stderr, "SetOif4 - invalid address family: %d\n",
			IfReq.ifr_addr.sa_family);
		exit(1);
	}

	if (setsockopt(Sock, IPPROTO_IP, IP_MULTICAST_IF,
		       &Sin4->sin_addr, sizeof(struct in_addr)))
		smclog(LOG_ERR, errno, "set IP_MULTICAST_IF");
}

static void SetTtl6(int Sock, unsigned Ttl)
{
	if (setsockopt(Sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
		       &Ttl, sizeof(Ttl)))
		smclog(LOG_ERR, errno, "set IPV6_MULTICAST_HOPS");
}

static void SetOif6(int Sock, char *ifname)
{
	unsigned ifindex;

	ifindex = if_nametoindex(ifname);

	if (setsockopt(Sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
		       &ifindex, sizeof(ifindex)))
		smclog(LOG_ERR, errno, "set IPV6_MULTICAST_IF");
}

/*
 * Converts the internet address plus port string in 'St'
 * into their network byte order representations.
 *
 * returns: - 0 -> conversion failed
 *          - 1 -> only address part returned (inaddrPt)
 *          - 2 -> address and port returned
 *
 */
static void getSockAdr(struct sockaddr *SaPt, socklen_t * SaLenPt, char *AddrSt, char *PortSt)
{
	struct sockaddr_in *Sin4;
	struct sockaddr_in6 *Sin6;

	if (strchr(AddrSt, ':') == NULL) {
		Sin4 = SIN4(SaPt);
		memset(Sin4, 0, sizeof(*Sin4));

		Sin4->sin_family = AF_INET;
		Sin4->sin_port = htons(atoi(PortSt));

		if (inet_pton(AF_INET, AddrSt, &Sin4->sin_addr) <= 0)
			smclog(LOG_ERR, errno, "inet_pton failed for address %s", AddrSt);

		*SaLenPt = sizeof(struct sockaddr_in);
	} else {
		Sin6 = SIN6(SaPt);
		memset(Sin6, 0, sizeof(*Sin6));

		Sin6->sin6_family = AF_INET6;
		Sin6->sin6_port = htons(atoi(PortSt));

		if (inet_pton(AF_INET6, AddrSt, &Sin6->sin6_addr) <= 0)
			smclog(LOG_ERR, errno, "inet_pton failed for address %s", AddrSt);

		*SaLenPt = sizeof(struct sockaddr_in6);
	}
}

/**
 * Local Variables:
 *  version-control: t
 *  indent-tabs-mode: t
 *  c-file-style: "linux"
 * End:
 */