1 | /* |
---|
2 | * Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify it under |
---|
5 | * the terms of the GNU Affero General Public License as published by the |
---|
6 | * Free Software Foundation, version 3. |
---|
7 | */ |
---|
8 | #include <sys/types.h> |
---|
9 | #include <ifaddrs.h> |
---|
10 | #include <string.h> |
---|
11 | #include <netinet/in.h> |
---|
12 | #include <sys/socket.h> |
---|
13 | #include <syslog.h> |
---|
14 | #include <stddef.h> |
---|
15 | #include <sys/types.h> |
---|
16 | #include <sys/stat.h> |
---|
17 | #include <sys/types.h> |
---|
18 | #include <sys/socket.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include "wol.h" |
---|
21 | #include "cfg.h" |
---|
22 | #include "ogAdmServer.h" |
---|
23 | |
---|
24 | int wol_socket_open(void) |
---|
25 | { |
---|
26 | unsigned int on = 1; |
---|
27 | int ret, s; |
---|
28 | |
---|
29 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
30 | if (s < 0) { |
---|
31 | syslog(LOG_ERR, "cannot create socket for magic packet\n"); |
---|
32 | return -1; |
---|
33 | } |
---|
34 | ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (unsigned int *) &on, |
---|
35 | sizeof(on)); |
---|
36 | if (ret < 0) { |
---|
37 | syslog(LOG_ERR, "cannot set broadcast socket\n"); |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | return s; |
---|
42 | } |
---|
43 | |
---|
44 | bool wake_up_send(int sd, struct sockaddr_in *client, |
---|
45 | const struct wol_msg *msg, const struct in_addr *addr) |
---|
46 | { |
---|
47 | int ret; |
---|
48 | |
---|
49 | client->sin_addr.s_addr = addr->s_addr; |
---|
50 | |
---|
51 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
52 | (struct sockaddr *)client, sizeof(*client)); |
---|
53 | if (ret < 0) { |
---|
54 | syslog(LOG_ERR, "failed to send wol\n"); |
---|
55 | return false; |
---|
56 | } |
---|
57 | |
---|
58 | return true; |
---|
59 | } |
---|
60 | |
---|
61 | bool wake_up_broadcast(int sd, struct sockaddr_in *client, |
---|
62 | const struct wol_msg *msg) |
---|
63 | { |
---|
64 | struct sockaddr_in *broadcast_addr, addr = {}; |
---|
65 | struct ifaddrs *ifaddr, *ifa; |
---|
66 | |
---|
67 | if (getifaddrs(&ifaddr) < 0) { |
---|
68 | syslog(LOG_ERR, "cannot get list of addresses\n"); |
---|
69 | return false; |
---|
70 | } |
---|
71 | |
---|
72 | addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
---|
73 | |
---|
74 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
---|
75 | if (ifa->ifa_addr == NULL || |
---|
76 | ifa->ifa_addr->sa_family != AF_INET || |
---|
77 | strcmp(ifa->ifa_name, ogconfig.wol.interface) != 0) |
---|
78 | continue; |
---|
79 | |
---|
80 | broadcast_addr = |
---|
81 | (struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr; |
---|
82 | addr.sin_addr.s_addr = broadcast_addr->sin_addr.s_addr; |
---|
83 | break; |
---|
84 | } |
---|
85 | freeifaddrs(ifaddr); |
---|
86 | |
---|
87 | return wake_up_send(sd, client, msg, &addr.sin_addr); |
---|
88 | } |
---|