2525#include "private-lib-core.h"
2626
2727int
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 }
0 commit comments