Skip to content

Commit

Permalink
Merge pull request #8 from 1223421/master
Browse files Browse the repository at this point in the history
An option for the interface, the most necessary thing for WOL but not…
  • Loading branch information
GramThanos authored Jun 16, 2024
2 parents 9d72b9e + 5e049fc commit 736c736
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ ___

### Usage
```bash
./WakeOnLAN <mac address>[ <broadcast address>]
./WakeOnLAN <mac address>[ <broadcast address>][ <interface>]
```
The first parameter is the mac address of the target (usually your's network card's mac address).
The second parameter is optional and defines the broadcast address to send the packet.
The third parameter is optional and defines the source interface to send the packet from it (not for windows).

Example
```bash
./WakeOnLAN 00:11:22:33:44:55 192.168.1.255
./WakeOnLAN 00:11:22:33:44:55 192.168.1.255 eth0
```

___
Expand Down
14 changes: 12 additions & 2 deletions WakeOnLAN.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
Expand All @@ -47,6 +48,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#endif

#include <stdio.h>
Expand Down Expand Up @@ -99,7 +101,7 @@ int main(int argc, const char* argv[]){

// If no arguments
if(argc < 2){
printf("Usage:\n./wakeonlan <mac address> (<broadcast address>)\n");
printf("Usage:\n./wakeonlan <mac address> (<broadcast address>) (<interface>)\n");
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -149,7 +151,15 @@ int main(int argc, const char* argv[]){
printf("Failed to bind socket: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}

if(argv[3]) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", argv[3]);
if (setsockopt(udpSocket, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
printf("Failed to bind interface: '%s'.\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
// Set server end point (the broadcast addres)
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(broadcastAddress);
Expand Down

0 comments on commit 736c736

Please sign in to comment.