[96b02b5] | 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 "ogAdmServer.h" |
---|
| 22 | |
---|
[060e31c] | 23 | int wol_socket_open(void) |
---|
| 24 | { |
---|
| 25 | unsigned int on = 1; |
---|
| 26 | int ret, s; |
---|
| 27 | |
---|
| 28 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
---|
| 29 | if (s < 0) { |
---|
| 30 | syslog(LOG_ERR, "cannot create socket for magic packet\n"); |
---|
| 31 | return -1; |
---|
| 32 | } |
---|
| 33 | ret = setsockopt(s, SOL_SOCKET, SO_BROADCAST, (unsigned int *) &on, |
---|
| 34 | sizeof(on)); |
---|
| 35 | if (ret < 0) { |
---|
| 36 | syslog(LOG_ERR, "cannot set broadcast socket\n"); |
---|
| 37 | return -1; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | return s; |
---|
| 41 | } |
---|
| 42 | |
---|
[96b02b5] | 43 | bool wake_up_send(int sd, struct sockaddr_in *client, |
---|
| 44 | const struct wol_msg *msg, const struct in_addr *addr) |
---|
| 45 | { |
---|
| 46 | int ret; |
---|
| 47 | |
---|
| 48 | client->sin_addr.s_addr = addr->s_addr; |
---|
| 49 | |
---|
| 50 | ret = sendto(sd, msg, sizeof(*msg), 0, |
---|
| 51 | (struct sockaddr *)client, sizeof(*client)); |
---|
| 52 | if (ret < 0) { |
---|
| 53 | syslog(LOG_ERR, "failed to send wol\n"); |
---|
| 54 | return false; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | return true; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | bool wake_up_broadcast(int sd, struct sockaddr_in *client, |
---|
| 61 | const struct wol_msg *msg) |
---|
| 62 | { |
---|
| 63 | struct sockaddr_in *broadcast_addr, addr = {}; |
---|
| 64 | struct ifaddrs *ifaddr, *ifa; |
---|
| 65 | |
---|
| 66 | if (getifaddrs(&ifaddr) < 0) { |
---|
| 67 | syslog(LOG_ERR, "cannot get list of addresses\n"); |
---|
| 68 | return false; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
---|
| 72 | |
---|
| 73 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
---|
| 74 | if (ifa->ifa_addr == NULL || |
---|
| 75 | ifa->ifa_addr->sa_family != AF_INET || |
---|
| 76 | strcmp(ifa->ifa_name, interface) != 0) |
---|
| 77 | continue; |
---|
| 78 | |
---|
| 79 | broadcast_addr = |
---|
| 80 | (struct sockaddr_in *)ifa->ifa_ifu.ifu_broadaddr; |
---|
| 81 | addr.sin_addr.s_addr = broadcast_addr->sin_addr.s_addr; |
---|
| 82 | break; |
---|
| 83 | } |
---|
| 84 | freeifaddrs(ifaddr); |
---|
| 85 | |
---|
| 86 | return wake_up_send(sd, client, msg, &addr.sin_addr); |
---|
| 87 | } |
---|