/* Copyright (C) 1998-2004 Elliott Hughes <enh@jessies.org>
 *               2004 Stefan Bellon <sbellon@sbellon.de>
 *
 * This file is part of JetDirectFS.
 *
 * JetDirectFS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JetDirectFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <inetlib.h>
#include <netdb.h>

#include "errors.h"

static int
aredigit(char *s)
{
    while (*s)
        if (!isdigit(*s))
            return 0;
        else
            s++;

    return 1;
}

static int
crack_addr(char *addr, int *socket_type, struct sockaddr_in *sa)
{
    char *address;
    char *network = 0;
    char *netaddr = 0;
    char *service = 0;

    /* For these routines, addr is a network address of the form
    network!netaddr!service network!netaddr or simply netaddr. Network
    is either tcp or udp. Netaddr can be a host name, a domain name,
    a network address.
    */

    /* Copy the address into writable memory so's we can use strtok(3). */
    address = (char *) malloc(strlen(addr) + 1);
    if (address == 0) {
        error = ERR(err_NoMemory);
        return -1;
    }
    strcpy(address, addr);

    /* Split the addr into its three components. */
    network = strtok(address, "!");
    if (network != 0) {
        netaddr = strtok(NULL, "!");
        if (netaddr == 0) {
            netaddr = network;
            network = 0;
        }
    }
    if (netaddr != 0) {
        service = strtok(NULL, "!");
    }

    /* If no service is given, use 9100 as default */
    if (service == 0)
        service = "9100";

    /* If no network is given, use tcp default */
    if (network == 0)
        network = "tcp";        

    /* If no remote host address is given, bail out */
    if (netaddr == 0) {
        free(address);
        error = ERR(err_InvalidAddress);
        return -1;
    }

    /* Fill in the socket type. */
    if (socket_type != 0) {
        if (!strcmp(network, "tcp"))
            *socket_type = SOCK_STREAM;
        else if (!strcmp(network, "udp"))
            *socket_type = SOCK_DGRAM;
        else {
            free(address);
            error = ERR(err_UnknownNetwork);
            return -1;
        }
    }

    /* Initialise sockaddr_in. */
    memset(sa, 0, sizeof(sa));
    sa->sin_family = AF_INET;

    /* Fill in the port. */
    if (aredigit(service)) {
        sa->sin_port = htons((short) strtoul(service, 0, 0));
    } else {
        struct servent *se;

        se = getservbyname(service, network);
        if (se == 0) {
            free(address);
            error = ERR(err_UnknownService);
            return -1;
        }
        sa->sin_port = se->s_port;
    }

    /* Fill in the address data. */
    {
        struct hostent *hent;

        hent = gethostbyname(netaddr);
        if (hent == 0) {
            free(address);
            error = ERR(err_NameLookup);
            return -1;
        }

        memcpy(&(sa->sin_addr), hent->h_addr_list[0], hent->h_length);
    }

    free(address);
    return 0;
}

int
establish_connection(char *addr)
{
    int s;
    int socket_type;
    struct sockaddr_in remote_addr;

    if (crack_addr(addr, &socket_type, &remote_addr) == -1)
        return -1;
    s = socket(AF_INET, socket_type, 0);
    if (s == -1) {
        error = ERR(err_Socket);
        return -1;
    }

    /* Set remote address. */
    if (connect(s, (struct sockaddr*) &remote_addr, sizeof(remote_addr)) == -1)
    {
        close(s);
        error = ERR(err_RemoteAddress);
        return -1;
    }

    return s;
}
