aboutsummaryrefslogtreecommitdiffstats
path: root/package/utils/busybox/patches/210-add_netmsg_util.patch
blob: d7b2ae7c4bbb4deab80de06cdec67e98fde13b35 (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
--- /dev/null
+++ b/networking/netmsg.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name>
+ *
+ * This is free software, licensed under the GNU General Public License v2.
+ */
+
+//config:config NETMSG
+//config:	bool "netmsg"
+//config:	default n
+//config:	help
+//config:	  simple program for sending udp broadcast messages
+
+//applet:IF_NETMSG(APPLET(netmsg, BB_DIR_BIN, BB_SUID_REQUIRE))
+
+//kbuild:lib-$(CONFIG_NETMSG) += netmsg.o
+
+//usage:#define netmsg_trivial_usage NOUSAGE_STR
+//usage:#define netmsg_full_usage ""
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "busybox.h"
+
+#ifndef CONFIG_NETMSG
+int main(int argc, char **argv)
+#else
+int netmsg_main(int argc, char **argv)
+#endif
+{
+	int s;
+	struct sockaddr_in addr;
+	int optval = 1;
+	unsigned char buf[1001];
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s <ip> \"<message>\"\n", argv[0]);
+		exit(1);
+	}
+
+	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+		perror("Opening socket");
+		exit(1);
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sin_family = AF_INET;
+	addr.sin_addr.s_addr = inet_addr(argv[1]);
+	addr.sin_port = htons(0x1337);
+
+	memset(buf, 0, 1001);
+	buf[0] = 0xde;
+	buf[1] = 0xad;
+
+	strncpy(buf + 2, argv[2], 998);
+
+	if (setsockopt (s, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval, sizeof (optval)) < 0) {
+		perror("setsockopt()");
+		goto fail;
+	}
+
+	if (sendto(s, buf, 1001, 0, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		perror("sendto()");
+		goto fail;
+	}
+
+	return 0;
+
+fail:
+	close(s);
+	exit(1);
+}