From 842f149c7865a1be45fb1812dc10094b7fa01b33 Mon Sep 17 00:00:00 2001 From: Simon Piriou Date: Sat, 8 May 2021 00:36:30 +0200 Subject: [PATCH] netutils: add Wake-on-LAN util --- netutils/wakeonlan/Kconfig | 31 +++++++ netutils/wakeonlan/Make.defs | 23 +++++ netutils/wakeonlan/Makefile | 30 +++++++ netutils/wakeonlan/wol_main.c | 165 ++++++++++++++++++++++++++++++++++ 4 files changed, 249 insertions(+) create mode 100644 netutils/wakeonlan/Kconfig create mode 100644 netutils/wakeonlan/Make.defs create mode 100644 netutils/wakeonlan/Makefile create mode 100644 netutils/wakeonlan/wol_main.c diff --git a/netutils/wakeonlan/Kconfig b/netutils/wakeonlan/Kconfig new file mode 100644 index 000000000..92cdd1db2 --- /dev/null +++ b/netutils/wakeonlan/Kconfig @@ -0,0 +1,31 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config NETUTILS_WAKEONLAN + bool "WakeOnLAN support" + default n + depends on NET_UDP && NET_SOCKOPTS + ---help--- + Enable the wake on LAN command "wol". This command + sends a WakeOnLAN magic packet to target MAC address. + +if NETUTILS_WAKEONLAN + +config NETUTILS_WAKEONLAN_PROGNAME + string "Program name" + default "wol" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config NETUTILS_WAKEONLAN_PRIORITY + int "WakeOnLAN task priority" + default 100 + +config NETUTILS_WAKEONLAN_STACKSIZE + int "WakeOnLAN stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/netutils/wakeonlan/Make.defs b/netutils/wakeonlan/Make.defs new file mode 100644 index 000000000..e673d0be0 --- /dev/null +++ b/netutils/wakeonlan/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/netutils/wakeonlan/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_NETUTILS_WAKEONLAN),) +CONFIGURED_APPS += $(APPDIR)/netutils/wakeonlan +endif diff --git a/netutils/wakeonlan/Makefile b/netutils/wakeonlan/Makefile new file mode 100644 index 000000000..dc767528e --- /dev/null +++ b/netutils/wakeonlan/Makefile @@ -0,0 +1,30 @@ +############################################################################ +# apps/netutils/wakeonlan/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +PROGNAME = $(CONFIG_NETUTILS_WAKEONLAN_PROGNAME) +PRIORITY = $(CONFIG_NETUTILS_WAKEONLAN_PRIORITY) +STACKSIZE = $(CONFIG_NETUTILS_WAKEONLAN_STACKSIZE) +MODULE = $(CONFIG_NETUTILS_WAKEONLAN) + +MAINSRC = wol_main.c + +include $(APPDIR)/Application.mk diff --git a/netutils/wakeonlan/wol_main.c b/netutils/wakeonlan/wol_main.c new file mode 100644 index 000000000..668e10a96 --- /dev/null +++ b/netutils/wakeonlan/wol_main.c @@ -0,0 +1,165 @@ +/**************************************************************************** + * apps/netutils/wakeonlan/wol_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define WOL_DEFAULT_PORT 9 /* Discard Protocol / Wake-on-LAN */ +#define WOL_FRAME_SIZE 102 /* 6 + 16 * sizeof(struct ether_addr) */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int send_wol(FAR struct ether_addr *dest, + FAR struct sockaddr_in *s_in) +{ + int ret; + unsigned int i; + int sockfd; + int optval = 1; + uint8_t buffer[WOL_FRAME_SIZE] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff + }; + + sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sockfd < 0) + { + fprintf(stderr, "failed to create socket: %s\n", strerror(errno)); + return -1; + } + + ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, + (void *)&optval, sizeof(optval)); + if (ret < 0) + { + fprintf(stderr, "failed to set socket options: %s\n", strerror(errno)); + goto out; + } + + for (i = 1; i <= 16; ++i) + { + *((FAR struct ether_addr *)&buffer[6*i]) = *dest; + } + + ret = sendto(sockfd, buffer, WOL_FRAME_SIZE, 0, + (FAR struct sockaddr *)s_in, sizeof(*s_in)); + if (ret < 0) + { + fprintf(stderr, "failed to send frame: %s\n", strerror(errno)); + goto out; + } + +out: + close(sockfd); + return ret ? -1 : 0; +} + +static void show_usage(FAR const char *progname, int exitcode) +{ + fprintf(stderr, "Usage: %s [-i IP] [-p PORT] MAC_ADDRESS\n", progname); + fprintf(stderr, "WakeOnLAN command:\n" + " -i, destination IP address (default: 255.255.255.255)\n" + " -p, destination port (default: 9)\n" + "\n"); + exit(exitcode); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + int ret; + int option; + struct sockaddr_in s_in; + struct ether_addr destmac; + int exitcode = EXIT_FAILURE; + + bzero(&s_in, sizeof(s_in)); + s_in.sin_family = AF_INET; + s_in.sin_port = htons(WOL_DEFAULT_PORT); + s_in.sin_addr.s_addr = INADDR_BROADCAST; + + while ((option = getopt(argc, argv, "i:p:h")) != ERROR) + { + switch (option) + { + case 'i': + ret = inet_pton(AF_INET, optarg, &s_in.sin_addr); + if (ret <= 0) + { + fprintf(stderr, "invalid argument %s\n", optarg); + goto errout_with_usage; + } + break; + + case 'p': + ret = atoi(optarg); + if (ret <= 0 || ret > UINT16_MAX) + goto errout_with_usage; + + s_in.sin_port = htons(ret); + break; + + case 'h': + exitcode = EXIT_SUCCESS; + default: + goto errout_with_usage; + } + } + + if (optind >= argc || + ether_aton_r(argv[optind], &destmac) == NULL) + { + goto errout_with_usage; + } + + if (send_wol(&destmac, &s_in)) + { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; + +errout_with_usage: + show_usage(argv[0], exitcode); + return exitcode; +}