|
| 1 | +/* |
| 2 | + * libwebsockets - small server side websockets and web server implementation |
| 3 | + * |
| 4 | + * Copyright (C) 2010 - 2023 Andy Green <[email protected]> |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | + * IN THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "private-lib-core.h" |
| 26 | + |
| 27 | +#if defined(_WIN32) && !defined(IFHWADDRLEN) |
| 28 | +#define IFHWADDRLEN 6 |
| 29 | +#endif |
| 30 | + |
| 31 | +int |
| 32 | +lws_wol(struct lws_context *ctx, const char *ip_or_NULL, uint8_t *mac_6_bytes) |
| 33 | +{ |
| 34 | + int n, m, ofs = 0, fd, optval = 1, ret = 1; |
| 35 | + uint8_t pkt[17 * IFHWADDRLEN]; |
| 36 | + struct sockaddr_in addr; |
| 37 | + |
| 38 | + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 39 | + if (fd < 0) { |
| 40 | + lwsl_cx_err(ctx, "failed to open UDP, errno %d\n", errno); |
| 41 | + goto bail; |
| 42 | + } |
| 43 | + |
| 44 | + if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, |
| 45 | + (char *)&optval, sizeof(optval)) < 0) { |
| 46 | + lwsl_cx_err(ctx, "failed to set broadcast, errno %d\n", errno); |
| 47 | + goto bail; |
| 48 | + } |
| 49 | + |
| 50 | + /* |
| 51 | + * Lay out the magic packet |
| 52 | + */ |
| 53 | + |
| 54 | + for (n = 0; n < IFHWADDRLEN; n++) |
| 55 | + pkt[ofs++] = 0xff; |
| 56 | + for (m = 0; m < 16; m++) |
| 57 | + for (n = 0; n < IFHWADDRLEN; n++) |
| 58 | + pkt[ofs++] = mac_6_bytes[n]; |
| 59 | + |
| 60 | + memset(&addr, 0, sizeof(addr)); |
| 61 | + addr.sin_family = AF_INET; |
| 62 | + addr.sin_port = htons(9); |
| 63 | + |
| 64 | + if (!inet_aton(ip_or_NULL ? ip_or_NULL : "255.255.255.255", |
| 65 | + &addr.sin_addr)) { |
| 66 | + lwsl_cx_err(ctx, "failed to convert broadcast ads, errno %d\n", |
| 67 | + errno); |
| 68 | + goto bail; |
| 69 | + } |
| 70 | + |
| 71 | + lwsl_cx_notice(ctx, "Sending WOL to %02X:%02X:%02X:%02X:%02X:%02X %s\n", |
| 72 | + mac_6_bytes[0], mac_6_bytes[1], mac_6_bytes[2], mac_6_bytes[3], |
| 73 | + mac_6_bytes[4], mac_6_bytes[5], ip_or_NULL ? ip_or_NULL : ""); |
| 74 | + |
| 75 | + if (sendto(fd, pkt, sizeof(pkt), 0, (struct sockaddr *)&addr, |
| 76 | + sizeof(addr)) < 0) { |
| 77 | + lwsl_cx_err(ctx, "failed to sendto broadcast ads, errno %d\n", |
| 78 | + errno); |
| 79 | + goto bail; |
| 80 | + } |
| 81 | + |
| 82 | + ret = 0; |
| 83 | + |
| 84 | +bail: |
| 85 | + close(fd); |
| 86 | + |
| 87 | + return ret; |
| 88 | +} |
0 commit comments