/*
 *------------------------------------------------------------------
 *
 * $Source: $
 * $Revision: 1.1 $
 * $Date: 87/06/24 14:09:52 $
 * $State: Exp $
 * $Author: jon $
 * $Locker: jon $
 *
 * $Log:	rvd.c,v $
 * 
 *------------------------------------------------------------------
 */

#ifndef lint
static char *rcsid_utils_c = "$Header: infwd.c,v 1.3 87/06/24 14:11:49 jon Exp $";
#endif	lint

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

#include "ss.h"
#include "gadmin.h"

extern gadmin_invocation_info *gii;

int init_gw_socket (gw_socket, sci_idx)
     int *gw_socket;
{
  struct sockaddr_in sin;
  *gw_socket = socket(AF_INET, SOCK_DGRAM, 0);
  if (gw_socket < 0) {
    ss_perror (sci_idx, errno, "Creating socket");
    return (-1);
  }

  bzero((char *)&sin, sizeof (sin));
  sin.sin_family = AF_INET;
#ifdef ndef
  sin.sin_port = GW_CONTROL_PORT;
#endif
  if (bind(*gw_socket, &sin, sizeof (sin)) < 0) {
    ss_perror (sci_idx, errno, "Binding socket");
    return (-1);
 }
  return(0);
}

u_long	get_host_addr(host_str, primary_host_name, sci_idx)
char	*host_str, **primary_host_name;
int     sci_idx;
{
  struct hostent *hstent;
  long		host;
  char          msg[32];

    if ((*host_str < '0') || (*host_str > '9')) {
	if ((hstent = gethostbyname(host_str)) == NULL) {
	  /* until ss_perror calls com_err */
	    (void) sprintf (msg, "Don't know hostname %s", host_str);
	    ss_perror (gii->sci_idx, 0,  msg);
	    return (0);
	}
	*primary_host_name = hstent->h_name;
	return(* (u_long *)(hstent->h_addr));
    } else {
	if ((host = inet_addr(host_str)) == NULL) {
	    (void) sprintf (msg, "Don't know address %s", host_str);
	  ss_perror (sci_idx, 0,  msg);
	  return (0);
	}
	*primary_host_name = (char *) inet_ntoa ((struct in_addr *) host);
	return(host);
    }
}

char *host_string (name, addr)
     char   *name;
     u_long addr;
{
  static char buf[32];

  if (name == NULL) return (NULL);

  if ((*name < '0') || (*name > '9'))
    (void) sprintf (buf, "%s (%s)", name, inet_ntoa ((struct in_addr *) addr));
  else
    (void) sprintf (buf, "%s", inet_ntoa ((struct in_addr *) addr));
  return (buf);
}
