Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Jan 2000 14:12:20 -0700 (MST)
From:      Kurt Olsen <kurto@bootp.sls.usu.edu>
To:        freebsd-hackers@FreeBSD.ORG, mreimer@vpop.net
Subject:   Re: Porting ether-wake.c from Linux
Message-ID:  <200001042112.OAA19991@bootp.sls.usu.edu>
In-Reply-To: <38724BC2.767E9055@vpop.net>

next in thread | previous in thread | raw e-mail | index | archive | help
I've not looked at ether-wake, but I use the attached program to wake up
machines. It's not very fancy, but it does work for me. The limitation that
an ARP entry for the destination machine is somewhat annoying, but not
insurmountable. As you can see, it uses UDP packets instead of constructing
raw packets to be sent.

Kurt Olsen
kurto@bootp.sls.usu.edu

---

/*
 * the wakie-wakie program
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define BUF_LEN 256

void fatal_error(char *message)
{
  fprintf(stderr, "%s", message);
  exit(1);
}

void parse(char *addr_in, u_int32_t *addr_out)
{
  int i,j,k;

  j = 0;
  for (i = 0; i < 50 && addr_in[i] != '\0'; i++)
    {
    if (addr_in[i] == '.')
      j++;
    }

  if (j != 3)
    fatal_error("Need four octets in ip address.\n");

  *addr_out = 0;
  for (j = 0, i = 0; i < 4; i++)
    {
    k = atoi(&addr_in[j]);
    if (k < 0 || k > 255)
      fatal_error("Individual octets in ip address must be in the range 0-255.\n");
    *addr_out = *addr_out * 256 + k;
    while (i < 3 && addr_in[j] != '.')
      j++;
    j++;
    }
  *addr_out = htonl(*addr_out);
}

int main(int argc, char **argv)
{
  char buf[BUF_LEN];
  int i,j,k,l,m;
  int thesock;
  struct sockaddr_in to;

  memset (buf, 0, BUF_LEN);

  if (argc < 3 || ((argc-1)&0x1) == 1)
    {
    fprintf(stderr, "Need ip and ethernet address pairs.\n");
    fprintf(stderr, "\t%s <ip> <ether> [<ip <ether> ...]\n", *argv);
    exit(1);
    }

  for (m = 1; m < argc; m+=2, argv+=2)
    {
    thesock = socket(AF_INET, SOCK_DGRAM, 0);
    if (thesock < 0)
      fatal_error("Couldn't make a socket.\n");

    if (strlen(*(argv+2)) != 12)
      fatal_error("Ethernet addresses require 12 digits.\n");

    buf[0] = 0xff; buf[1] = 0xff; buf[2] = 0xff;
    buf[3] = 0xff; buf[4] = 0xff; buf[5] = 0xff;

    for (i = 1; i < 17; i++)
      {
      for (j = 0; j < 6; j++)
        {
        k = *(*(argv+2)+j*2);
        if (k >= 'a' && k <= 'f') k -= ('a'-'A');
        l = *(*(argv+2)+j*2+1);
        if (l >= 'a' && l <= 'f') l -= ('a'-'A');
        k -= '0'; if (k > 9) k -= 7;
        l -= '0'; if (l > 9) l -= 7;
        k = k * 16 + l;
        buf[j+i*6] = k;
        }
      }

    to.sin_len = sizeof(to);
    to.sin_family = AF_INET;
    to.sin_port = 5432;
    parse(*(argv+1), &(to.sin_addr.s_addr));
  
    if (sendto(thesock,&buf,BUF_LEN,0,(struct sockaddr *)&to,sizeof(to)) < 0)
      fatal_error("Couldn't send UDP packet.\n");
    close(thesock);
    }
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001042112.OAA19991>