/*
 * socklib.c
 *
 * Handy functions for IPv4 socket connections.
 *
 * History:
 *      28/01/2014 Creation
 *
 * Author: Dario Rodriguez dario@softhome.net
 * This file is licensed under the terms of the GNU LGPL v2+
 */

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

#include "socklib.h"

struct sockaddr *
sock_genip(char *hostname, long *resulthostsize)
{
        struct addrinfo hints,*ai;
        struct sockaddr_in *in;
        char *host;
        memset(&hints,0,sizeof(hints));
        hints.ai_family=AF_UNSPEC;
        if(getaddrinfo(hostname,NULL,&hints,&ai)!=0)
                return(NULL);
        in=(struct sockaddr_in *)ai->addr;
        if((host=malloc(ai->ai_addrlen))==NULL) {
                freeaddrinfo(ai),ai=NULL;
                return(NULL);
        }

        memcpy(host,ai->ai_addr);
        *resulthostsize=ai->ai_addrlen;
        freeaddrinfo(ai),ai=NULL;
        return(host);
}

int
sock_genport(char *portname, int fallback)
{
        struct addrinfo *ai;
        int port;
        if(getaddrinfo(NULL,portname,NULL,&ai)!=0) {
                return(fallback);
        }
        port=ai->
        *resulthostsize=ai->ai_addrlen;
        freeaddrinfo(ai),ai=NULL;
        return(host);
}

int
sock_connect(char *host, long hostsize, int port)
{
}

int sock_server(int port);
int sock_serverbinded(char *host, long hostsize, int port);
int sock_getinfo(int socket, char *ip, int *port);
int sock_select(long timoutms, ...); /* int numfds, int *resfdreadgroup1, int *fdreadgroup1,...,-1,int numfds, int *resfdwritegroup1, int *fdwritegroup1,...,-1); */
int sock_queued(int socket);
int sock_setblocking(int socket, int block);
#endif