b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | --- /dev/null |
| 2 | +++ b/networking/netmsg.c |
| 3 | @@ -0,0 +1,76 @@ |
| 4 | +/* |
| 5 | + * Copyright (C) 2006 Felix Fietkau <nbd@nbd.name> |
| 6 | + * |
| 7 | + * This is free software, licensed under the GNU General Public License v2. |
| 8 | + */ |
| 9 | + |
| 10 | +//config:config NETMSG |
| 11 | +//config: bool "netmsg" |
| 12 | +//config: default n |
| 13 | +//config: help |
| 14 | +//config: simple program for sending udp broadcast messages |
| 15 | + |
| 16 | +//applet:IF_NETMSG(APPLET(netmsg, BB_DIR_BIN, BB_SUID_REQUIRE)) |
| 17 | + |
| 18 | +//kbuild:lib-$(CONFIG_NETMSG) += netmsg.o |
| 19 | + |
| 20 | +//usage:#define netmsg_trivial_usage NOUSAGE_STR |
| 21 | +//usage:#define netmsg_full_usage "" |
| 22 | + |
| 23 | +#include <sys/types.h> |
| 24 | +#include <sys/socket.h> |
| 25 | +#include <netinet/in.h> |
| 26 | +#include <netdb.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <string.h> |
| 30 | +#include "busybox.h" |
| 31 | + |
| 32 | +#ifndef CONFIG_NETMSG |
| 33 | +int main(int argc, char **argv) |
| 34 | +#else |
| 35 | +int netmsg_main(int argc, char **argv) |
| 36 | +#endif |
| 37 | +{ |
| 38 | + int s; |
| 39 | + struct sockaddr_in addr; |
| 40 | + int optval = 1; |
| 41 | + unsigned char buf[1001]; |
| 42 | + |
| 43 | + if (argc != 3) { |
| 44 | + fprintf(stderr, "usage: %s <ip> \"<message>\"\n", argv[0]); |
| 45 | + exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 49 | + perror("Opening socket"); |
| 50 | + exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + memset(&addr, 0, sizeof(addr)); |
| 54 | + addr.sin_family = AF_INET; |
| 55 | + addr.sin_addr.s_addr = inet_addr(argv[1]); |
| 56 | + addr.sin_port = htons(0x1337); |
| 57 | + |
| 58 | + memset(buf, 0, 1001); |
| 59 | + buf[0] = 0xde; |
| 60 | + buf[1] = 0xad; |
| 61 | + |
| 62 | + strncpy(buf + 2, argv[2], 998); |
| 63 | + |
| 64 | + if (setsockopt (s, SOL_SOCKET, SO_BROADCAST, (caddr_t) &optval, sizeof (optval)) < 0) { |
| 65 | + perror("setsockopt()"); |
| 66 | + goto fail; |
| 67 | + } |
| 68 | + |
| 69 | + if (sendto(s, buf, 1001, 0, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 70 | + perror("sendto()"); |
| 71 | + goto fail; |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | + |
| 76 | +fail: |
| 77 | + close(s); |
| 78 | + exit(1); |
| 79 | +} |