/* $Cambridge: hermes/src/mailchk/support.c,v 1.1 2003/08/10 22:27:44 dpc22 Exp $ */

#include "mailchk.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netdb.h>
#include <sys/file.h>

void *
xmalloc(unsigned long size)
{
    void *result;

    if (!(result = malloc(size)))
        log_fatal("Failed to allocate: %lu bytes\n", size);
    return(result);
}

char *
xstrdup(char *s)
{
    void *result;

    if (!(result = strdup(s)))
        log_fatal("Failed to strdup: %d bytes\n", strlen(s));

    return(result);
}

void *
xrealloc(void *old, unsigned long size)
{
    void *result;

    if ((result = realloc(old, size)))
        log_fatal("Failed to allocate: %lu bytes\n", size);

    return(result);
}

int
os_connect_inet_socket(char *host, unsigned long port)
{
    struct hostent *hostent;
    struct sockaddr_in serv_addr;
    int sockfd;

    /* Open the socket */
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        return (-1);

    /* Set up the socket */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);

    if ((hostent = gethostbyname(host)) == NIL) {
        close(sockfd);
        return (-1);
    }
    bcopy(hostent->h_addr, (char *) &serv_addr.sin_addr,
          hostent->h_length);

    if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        close(sockfd);
        return (-1);
    }

    return (sockfd);
}



