Skip to content

Commit be45e59

Browse files
committed
wol: add interface binding to context
1 parent 78a35fe commit be45e59

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

include/libwebsockets/lws-context-vhost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ struct lws_context_creation_info {
967967
* zero.
968968
*/
969969

970+
#if defined(LWS_WITH_NETWORK)
971+
const char *wol_if;
972+
/**< CONTEXT: NULL, or interface name to bind outgoing WOL packet to */
973+
#endif
974+
970975
/* Add new things just above here ---^
971976
* This is part of the ABI, don't needlessly break compatibility
972977
*

lib/core-net/wol.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,34 @@
2525
#include "private-lib-core.h"
2626

2727
int
28-
lws_wol(struct lws_context *ctx, const char *ip_or_NULL, uint8_t *mac_6_bytes)
28+
lws_wol(struct lws_context *cx, const char *ip_or_NULL, uint8_t *mac_6_bytes)
2929
{
3030
int n, m, ofs = 0, fd, optval = 1, ret = 1;
3131
uint8_t pkt[17 * ETHER_ADDR_LEN];
3232
struct sockaddr_in addr;
3333

3434
fd = (int)(intptr_t)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
3535
if (fd < 0) {
36-
lwsl_cx_err(ctx, "failed to open UDP, errno %d\n", errno);
36+
lwsl_cx_err(cx, "failed to open UDP, errno %d\n", errno);
3737
goto bail;
3838
}
3939

40+
#if defined(__linux__)
41+
if (cx->wol_if) {
42+
struct ifreq ifr;
43+
44+
memset(&ifr, 0, sizeof(ifr));
45+
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", cx->wol_if);
46+
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
47+
lwsl_cx_err(cx, "failed to BINDTODEVICE, errno %d\n", errno);
48+
goto bail;
49+
}
50+
}
51+
#endif
52+
4053
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
4154
(char *)&optval, sizeof(optval)) < 0) {
42-
lwsl_cx_err(ctx, "failed to set broadcast, errno %d\n", errno);
55+
lwsl_cx_err(cx, "failed to set broadcast, errno %d\n", errno);
4356
goto bail;
4457
}
4558

@@ -59,19 +72,19 @@ lws_wol(struct lws_context *ctx, const char *ip_or_NULL, uint8_t *mac_6_bytes)
5972

6073
if (!inet_pton(AF_INET, ip_or_NULL ? ip_or_NULL : "255.255.255.255",
6174
&addr.sin_addr)) {
62-
lwsl_cx_err(ctx, "failed to convert to ipv4 broadcast ads, errno %d\n",
75+
lwsl_cx_err(cx, "failed to convert to ipv4 broadcast ads, errno %d\n",
6376
errno);
6477
goto bail;
6578
}
6679

67-
lwsl_cx_notice(ctx, "Sending WOL to %02X:%02X:%02X:%02X:%02X:%02X %s\n",
80+
lwsl_cx_notice(cx, "Sending WOL to %02X:%02X:%02X:%02X:%02X:%02X %s '%s'\n",
6881
mac_6_bytes[0], mac_6_bytes[1], mac_6_bytes[2], mac_6_bytes[3],
69-
mac_6_bytes[4], mac_6_bytes[5], ip_or_NULL ? ip_or_NULL : "");
82+
mac_6_bytes[4], mac_6_bytes[5], ip_or_NULL ? ip_or_NULL : "", cx->wol_if);
7083

7184
/* arg2 is normally const void *, on mingw it's const char * */
7285
if (sendto(fd, (const char *)pkt, sizeof(pkt), 0, (struct sockaddr *)&addr,
7386
sizeof(addr)) < 0) {
74-
lwsl_cx_err(ctx, "failed to sendto broadcast ads, errno %d\n",
87+
lwsl_cx_err(cx, "failed to sendto broadcast ads, errno %d\n",
7588
errno);
7689
goto bail;
7790
}

lib/core/context.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ lws_create_context(const struct lws_context_creation_info *info)
665665
#if defined(LWS_WITH_NETWORK)
666666
context->event_loop_ops = plev->ops;
667667
context->us_wait_resolution = us_wait_resolution;
668+
context->wol_if = info->wol_if;
668669
#if defined(LWS_WITH_TLS_JIT_TRUST)
669670
{
670671
struct lws_cache_creation_info ci;

lib/core/private-lib-core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ struct lws_context {
635635
lws_ss_handle_t * ota_ss; /* opaque to platform */
636636
#endif
637637

638+
const char *wol_if;
639+
638640
/*
639641
* <====== LWS_WITH_NETWORK end
640642
*/

minimal-examples-lowlevel/raw/minimal-raw-wol/minimal-raw-wol.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int main(int argc, const char **argv)
1515
{
1616
struct lws_context_creation_info info;
1717
struct lws_context *ctx;
18-
const char *p, *ip = NULL;
18+
const char *p, *ip = NULL, *wol_if = NULL;
1919
uint8_t mac[LWS_ETHER_ADDR_LEN];
2020
int ret = 1;
2121

@@ -25,8 +25,12 @@ int main(int argc, const char **argv)
2525
if ((p = lws_cmdline_option(argc, argv, "-ip")))
2626
ip = p;
2727

28+
if ((p = lws_cmdline_option(argc, argv, "-i")))
29+
wol_if = p;
30+
31+
2832
if (argc < 2) {
29-
lwsl_user("lws-minimal-raw-wol XX:XX:XX:XX:XX:XX [-ip interface IP]\n");
33+
lwsl_user("lws-minimal-raw-wol XX:XX:XX:XX:XX:XX [-ip interface IP] [-i interface name]\n");
3034
goto bail1;
3135
}
3236

@@ -35,6 +39,8 @@ int main(int argc, const char **argv)
3539
goto bail1;
3640
}
3741

42+
info.wol_if = wol_if;
43+
3844
ctx = lws_create_context(&info);
3945
if (!ctx) {
4046
lwsl_err("lws init failed\n");

0 commit comments

Comments
 (0)