vfs: check userland buffers before reading them.
[haiku.git] / src / bin / network / wakeonlan.cpp
blob9799c4950edc2160edad573904acfb7c16a766e2
1 /*
2 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3 * Distributed under the terms of the MIT License.
4 */
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
11 int
12 main(int argc, char *argv[])
14 if (argc < 2) {
15 printf("usage: %s <MAC address>\n", argv[0]);
16 return 1;
19 unsigned char mac[6];
20 if (sscanf(argv[1], "%2x%*c%2x%*c%2x%*c%2x%*c%2x%*c%2x", &mac[0], &mac[1],
21 &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
22 printf("unrecognized MAC format\n");
23 return 2;
26 char buffer[102];
27 memset(buffer, 0xff, 6);
28 for (int i = 1; i <= 16; i++)
29 memcpy(buffer + i * 6, mac, sizeof(mac));
31 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
32 if (sock < 0) {
33 printf("failed to create socket: %s\n", strerror(sock));
34 return 3;
37 int value = 1;
38 int result = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &value,
39 sizeof(value));
40 if (result < 0) {
41 printf("failed to set broadcast socket option: %s\n", strerror(result));
42 return 4;
45 sockaddr_in address;
46 address.sin_family = AF_INET;
47 address.sin_addr.s_addr = 0xffffffff;
48 address.sin_port = 0;
50 result = sendto(sock, buffer, sizeof(buffer), 0,
51 (struct sockaddr *)&address, sizeof(address));
52 if (result < 0) {
53 printf("failed to send magic packet: %s\n", strerror(result));
54 return 5;
57 printf("magic packet sent to %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0],
58 mac[1], mac[2], mac[3], mac[4], mac[5]);
59 return 0;