Book now with code EOY2025
About7Narwhal wrote: » Why not leave it at TCP if that works? Perhaps I am missing something?
#ifndef WOL_H #define WOL_H #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/ether.h> #include <netinet/in.h> #include <sys/socket.h> #define MAC_ADDRSTRLEN 17 #define WOL_DATA_LEN 102 #define MAC_ADDR_LEN 6 #define BROADCAST_STR "ff:ff:ff:ff:ff:ff" void get_params(char *, char *, char *, int, char **); void wol_err(char *); int wol_sock(); #endif
#include "wol.h" void get_params(char * inet_addr_str, char * mac_addr_str, char * port, int argc, char ** argv) { char optc; while((optc = getopt(argc, argv, "m:p:i:")) != -1) { switch(optc) { case 'm': //memcpy(inet_addr, optarg, INET_ADDRSTRLEN); strncpy(mac_addr_str, optarg, MAC_ADDRSTRLEN); break; case 'i': strncpy(inet_addr_str, optarg, INET_ADDRSTRLEN); break; case 'p': //memcpy(mac_addr, optarg, MAC_ADDRSTRLEN); strncpy(port, optarg, 6); break; case '?': exit(EXIT_FAILURE); } } }
#include "wol.h" int main(int argc, char ** argv) { int raw_sock, iteration = 0; struct ether_addr * mac_addr; char inet_addr_str[INET_ADDRSTRLEN], mac_addr_str[MAC_ADDRSTRLEN], port[6], data[WOL_DATA_LEN]; struct sockaddr_in peer; if(argc == 7) get_params(inet_addr_str, mac_addr_str, port, argc, argv); else{ printf("argc = %d\n", argc); wol_err("Usage: ./wol -i x.x.x.x -m xx[IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_mad.gif[/IMG]x[IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_mad.gif[/IMG]x[IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_mad.gif[/IMG]x[IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_mad.gif[/IMG]x[IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_mad.gif[/IMG]x -p xxxxxx"); } raw_sock = wol_sock(); peer.sin_family = AF_INET; peer.sin_port = htons(atoi(port)); if(inet_pton(AF_INET, inet_addr_str, &peer.sin_addr) != 1) { wol_err("inet_pton:"); } memcpy(data, (struct ether_addr *)ether_aton(BROADCAST_STR), MAC_ADDR_LEN); mac_addr = ether_aton(mac_addr_str); for(iteration = MAC_ADDR_LEN; iteration <= 96; iteration += MAC_ADDR_LEN) memcpy(data + iteration, mac_addr, MAC_ADDR_LEN); if(sendto(raw_sock, data, WOL_DATA_LEN, 0, (struct sockaddr *)&peer, sizeof(peer)) == -1) { wol_err("sendto:"); } return 0; }
#include "wol.h" void wol_err(char * wol_err_str) { fprintf(stderr, "%s\n", wol_err_str); exit(EXIT_FAILURE); }
#include "wol.h" int wol_sock() { int wol_sock = 0; if((wol_sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { wol_err("socket: "); } return wol_sock; }
Use code EOY2025 to receive $250 off your 2025 certification boot camp!