gethostbyname in GoAhead

Chris Johns cjohns at cybertec.com.au
Wed May 24 14:12:21 UTC 2000


(Here again as I had the old list address :-()

gerke.kok at ascom.nl wrote:
> 
> I saw your comments on the mailing list and would like to see your C-code
> for this. I can't get my http server to run either.
> Thanks.
> wkr,
> Gerke
> PS Perhaps you could mail this to the list?
> 

Happy to mail the list as the file is small.

The `rtems_create_root_fs' can be made after the stack is initialised.

I have a structure passed from the boot ROM to the RTEMS initialisation
code with the results of a bootp request. Here it is :

/*
 * The configuration of the target.
 */

#define BOOT_CFG_MAX_LABEL (128)

typedef struct boot_configuration
{
  unsigned long rom_base;
  unsigned long rom_size;
  unsigned long ram_base;
  unsigned long ram_size;

  void          *printk;
  void          *sync_output;

  unsigned char client_mac[6];
  unsigned long client_ip;
  unsigned long client_nm;
  const char    client_name[BOOT_CFG_MAX_LABEL];

  unsigned long gateway_ip;

  unsigned long server_ip;
  const char    server_name[BOOT_CFG_MAX_LABEL];
  const char    domain_name[BOOT_CFG_MAX_LABEL];
  unsigned long log_server;
  unsigned long dns_server;
  
  const char    boot_file_name[BOOT_CFG_MAX_LABEL];
  
} boot_configuration;


-- 
 Chris Johns, mailto:cjohns at cybertec.com.au mailto:ccj at acm.org
-------------- next part --------------
/*
  ------------------------------------------------------------------------
  $Id: rootfs.c,v 1.1 2000/03/27 14:19:03 cjohns Exp $
  ------------------------------------------------------------------------
  
  Copyright (C) Objective Design Systems Ltd Pty 1992-1999
  All rights reserved (R) Objective Design Systems Ltd Pty 1992-1999

  Refer to the file LICENSE for detailed terms of usage and warranty.
  The LICENSE file must be provided with the file.

  This software with is provided ``as is'' and with NO WARRANTY.
  
  ------------------------------------------------------------------------

  Target initialisation.
  
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
#include <boot/configuration.h>

/*
 * Table of directorys to make.
 */

static const char *directories[2] =
{
  "/etc",
  0
};

/*
 * /etc/host.conf controls the resovler.
 */

static const char *etc_host_conf[] =
{
  "/etc/host.conf",
  "hosts,bind\n",
  0
};

/*
 * /etc/hosts is a list of hosts.
 */

static const char *etc_hosts[] =
{
  "/etc/hosts",
  "127.0.0.1  localhost.localdomain  localhost\n",
  0
};

/*
 * Create enough files to support the networking stack.
 */

static int
make_dirs (const char **dir_table)
{
  /*
   * Create the directory.
   */

  while (*dir_table)
  {
    if (mkdir (*dir_table, 0755) < 0)
    {
      printf ("root fs, cannot make `%s' : %s\n", *dir_table, strerror (errno));
      return -1;
    }
    dir_table++;
  }
  return 0;
}

/*
 * Create enough files to support the networking stack.
 */

static int
open_and_write_file (const char **file_table)
{
  int fd;
  
  /*
   * Create the file then write the preset data for the
   * file.
   */

  if ((fd = open (*file_table, O_CREAT | O_WRONLY)) < 0)
  {
    printf ("root fs, cannot open `%s' : %s\n", *file_table, strerror (errno));
    return -1;
  }

  file_table++;

  while (*file_table)
  {
    if (write (fd, *file_table, strlen (*file_table)) < 0)
    {
      close (fd);
      printf ("root fs, cannot open `%s' : %s\n", *file_table, strerror (errno));
      return -1;
    }
    file_table++;
  }
  return fd;
}

/*
 * Write file.
 */

static int
write_file (int fd, const char *buf, const char *fname)
{
  if (write (fd, buf, strlen (buf)) < 0)
  {
    close (fd);
    printf ("root fs, cannot write to `%s' : %s\n", fname, strerror (errno));
    return -1;
  }
  return 0;
}

/*
 * Write hosts record.
 */

static int
write_host_rec (int fd, unsigned long cip, const char *cname, const char *dname)
{
  char           buf[128];
  struct in_addr ip;

  ip.s_addr = cip;
  
  if (cname)
  {
    if (strlen (cname))
    {
      sprintf (buf, "%s  %s", inet_ntoa (ip), cname);
      
      if (write_file (fd, buf, "/etc/hosts") < 0)
        return -1;
    
      if (strlen (dname))
      {
        sprintf (buf, "  %s.%s",  cname, dname);

        if (write_file (fd, buf, "/etc/hosts") < 0)
          return -1;
      }

      strcpy (buf, "\n");
    
      if (write_file (fd, buf, "/etc/hosts") < 0)
        return -1;
    }
  }
  return 0;
}

/*
 * Create a root file system.
 */

int
rtems_create_root_fs (boot_configuration *bcfg)
{
  int  fd;
  
  /*
   * Create the directories.
   */

  if (make_dirs (directories) < 0)
    return -1;

  /*
   * Create a `/etc/host.conf' file.
   */

  if ((fd = open_and_write_file (etc_host_conf)) < 0)
    return -1;
  close (fd);

  /*
   * Create a `/etc/hosts' file.
   */

  if ((fd = open_and_write_file (etc_hosts)) < 0)
    return -1;

  if (write_host_rec (fd, bcfg->client_ip,
                      bcfg->client_name, bcfg->domain_name) < 0)
    return -1;
    
  if (write_host_rec (fd, bcfg->server_ip,
                      bcfg->server_name, bcfg->domain_name) < 0)
    return -1;
    
  close (fd);

  return 0;
}



More information about the users mailing list