Blame src/net/sock.c

Packit Service 102f81
/* -*- c-basic-offset: 4; -*- */
Packit Service 102f81
/* sock.c: General Socket Functions
Packit Service 102f81
 *
Packit Service 102f81
 * Copyright (c) 1999 the icecast team
Packit Service 102f81
 *
Packit Service 102f81
 *  This library is free software; you can redistribute it and/or
Packit Service 102f81
 *  modify it under the terms of the GNU Library General Public
Packit Service 102f81
 *  License as published by the Free Software Foundation; either
Packit Service 102f81
 *  version 2 of the License, or (at your option) any later version.
Packit Service 102f81
 *
Packit Service 102f81
 *  This library is distributed in the hope that it will be useful,
Packit Service 102f81
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 102f81
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 102f81
 *  Library General Public License for more details.
Packit Service 102f81
 *
Packit Service 102f81
 *  You should have received a copy of the GNU Library General Public
Packit Service 102f81
 *  License along with this library; if not, write to the Free
Packit Service 102f81
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit Service 102f81
 */
Packit Service 102f81
Packit Service 102f81
#ifdef HAVE_CONFIG_H
Packit Service 102f81
 #include <config.h>
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
#include <stdio.h>
Packit Service 102f81
#include <stdlib.h>
Packit Service 102f81
#include <stdarg.h>
Packit Service 102f81
#include <sys/types.h>
Packit Service 102f81
#include <ctype.h>
Packit Service 102f81
#include <string.h>
Packit Service 102f81
#include <fcntl.h>
Packit Service 102f81
#include <errno.h>
Packit Service 102f81
#ifdef HAVE_SYS_SELECT_H
Packit Service 102f81
#include <sys/select.h>
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
#ifndef _WIN32
Packit Service 102f81
#include <unistd.h>
Packit Service 102f81
#include <sys/socket.h>
Packit Service 102f81
#include <netinet/in.h>
Packit Service 102f81
#include <netinet/tcp.h>
Packit Service 102f81
#include <arpa/inet.h>
Packit Service 102f81
#include <sys/time.h>
Packit Service 102f81
#include <netdb.h>
Packit Service 102f81
#else
Packit Service 102f81
#include <winsock2.h>
Packit Service 102f81
#define vsnprintf _vsnprintf
Packit Service 102f81
#define EINPROGRESS WSAEINPROGRESS
Packit Service 102f81
#define ENOTSOCK WSAENOTSOCK
Packit Service 102f81
#define EWOULDBLOCK WSAEWOULDBLOCK
Packit Service 102f81
#define EALREADY WSAEALREADY
Packit Service 102f81
#define socklen_t    int
Packit Service 102f81
#ifndef __MINGW32__
Packit Service 102f81
#define va_copy(ap1, ap2) memcpy(&ap1, &ap2, sizeof(va_list))
Packit Service 102f81
#endif
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
#include "sock.h"
Packit Service 102f81
#include "resolver.h"
Packit Service 102f81
Packit Service 102f81
/* sock_initialize
Packit Service 102f81
**
Packit Service 102f81
** initializes the socket library.  you must call this
Packit Service 102f81
** before using the library!
Packit Service 102f81
*/
Packit Service 102f81
void sock_initialize(void)
Packit Service 102f81
{
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
    WSADATA wsad;
Packit Service 102f81
    WSAStartup(0x0101, &wsad);
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
    resolver_initialize();
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_shutdown
Packit Service 102f81
**
Packit Service 102f81
** shutdown the socket library.  remember to call this when you're
Packit Service 102f81
** through using the lib
Packit Service 102f81
*/
Packit Service 102f81
void sock_shutdown(void)
Packit Service 102f81
{
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
    WSACleanup();
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
    resolver_shutdown();
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_get_localip
Packit Service 102f81
**
Packit Service 102f81
** gets the local ip address for the machine
Packit Service 102f81
** the ip it returns *should* be on the internet.
Packit Service 102f81
** in any case, it's as close as we can hope to get
Packit Service 102f81
** unless someone has better ideas on how to do this
Packit Service 102f81
*/
Packit Service 102f81
char *sock_get_localip(char *buff, int len)
Packit Service 102f81
{
Packit Service 102f81
    char temp[1024];
Packit Service 102f81
Packit Service 102f81
    if (gethostname(temp, sizeof(temp)) != 0)
Packit Service 102f81
        return NULL;
Packit Service 102f81
Packit Service 102f81
    if (resolver_getip(temp, buff, len))
Packit Service 102f81
        return buff;
Packit Service 102f81
Packit Service 102f81
    return NULL;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_error
Packit Service 102f81
** 
Packit Service 102f81
** returns the last socket error
Packit Service 102f81
*/
Packit Service 102f81
int sock_error(void)
Packit Service 102f81
{
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
    return WSAGetLastError();
Packit Service 102f81
#else
Packit Service 102f81
    return errno;
Packit Service 102f81
#endif
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
static void sock_set_error(int val)
Packit Service 102f81
{
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
     WSASetLastError (val);
Packit Service 102f81
#else
Packit Service 102f81
     errno = val;
Packit Service 102f81
#endif
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_recoverable
Packit Service 102f81
**
Packit Service 102f81
** determines if the socket error is recoverable
Packit Service 102f81
** in terms of non blocking sockets
Packit Service 102f81
*/
Packit Service 102f81
int sock_recoverable(int error)
Packit Service 102f81
{
Packit Service 102f81
    switch (error)
Packit Service 102f81
    {
Packit Service 102f81
    case 0:
Packit Service 102f81
    case EAGAIN:
Packit Service 102f81
    case EINTR:
Packit Service 102f81
    case EINPROGRESS:
Packit Service 102f81
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
Packit Service 102f81
    case EWOULDBLOCK:
Packit Service 102f81
#endif
Packit Service 102f81
#ifdef ERESTART
Packit Service 102f81
    case ERESTART:
Packit Service 102f81
#endif
Packit Service 102f81
        return 1;
Packit Service 102f81
    default:
Packit Service 102f81
        return 0;
Packit Service 102f81
    }
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_stalled (int error)
Packit Service 102f81
{
Packit Service 102f81
    switch (error)
Packit Service 102f81
    {
Packit Service 102f81
    case EAGAIN:
Packit Service 102f81
    case EINPROGRESS:
Packit Service 102f81
    case EALREADY:
Packit Service 102f81
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
Packit Service 102f81
    case EWOULDBLOCK:
Packit Service 102f81
#endif
Packit Service 102f81
#ifdef ERESTART
Packit Service 102f81
    case ERESTART:
Packit Service 102f81
#endif
Packit Service 102f81
        return 1;
Packit Service 102f81
    default:
Packit Service 102f81
        return 0;
Packit Service 102f81
    }
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
Packit Service 102f81
static int sock_connect_pending (int error)
Packit Service 102f81
{
Packit Service 102f81
    return error == EINPROGRESS || error == EALREADY;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_valid_socket
Packit Service 102f81
**
Packit Service 102f81
** determines if a sock_t represents a valid socket
Packit Service 102f81
*/
Packit Service 102f81
int sock_valid_socket(sock_t sock)
Packit Service 102f81
{
Packit Service 102f81
    int ret;
Packit Service 102f81
    int optval;
Packit Service 102f81
    socklen_t optlen;
Packit Service 102f81
Packit Service 102f81
    optlen = sizeof(int);
Packit Service 102f81
    /* apparently on windows getsockopt.optval is a char * */
Packit Service 102f81
    ret = getsockopt(sock, SOL_SOCKET, SO_TYPE, (void*) &optval, &optlen);
Packit Service 102f81
Packit Service 102f81
    return (ret == 0);
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* inet_aton
Packit Service 102f81
**
Packit Service 102f81
** turns an ascii ip address into a binary representation
Packit Service 102f81
*/
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
int inet_aton(const char *s, struct in_addr *a)
Packit Service 102f81
{
Packit Service 102f81
    int lsb, b2, b3, msb;
Packit Service 102f81
Packit Service 102f81
    if (sscanf(s, "%d.%d.%d.%d", &lsb, &b2, &b3, &msb) < 4) {
Packit Service 102f81
        return 0;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    a->s_addr = inet_addr(s);
Packit Service 102f81
    
Packit Service 102f81
    return (a->s_addr != INADDR_NONE);
Packit Service 102f81
}
Packit Service 102f81
#endif /* _WIN32 */
Packit Service 102f81
Packit Service 102f81
/* sock_set_blocking
Packit Service 102f81
**
Packit Service 102f81
** set the sock blocking or nonblocking
Packit Service 102f81
** SOCK_BLOCK for blocking
Packit Service 102f81
** SOCK_NONBLOCK for nonblocking
Packit Service 102f81
*/
Packit Service 102f81
int sock_set_blocking(sock_t sock, const int block)
Packit Service 102f81
{
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
#ifdef __MINGW32__
Packit Service 102f81
    u_long varblock = block;
Packit Service 102f81
#else
Packit Service 102f81
    int varblock = block;
Packit Service 102f81
#endif
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
    if ((!sock_valid_socket(sock)) || (block < 0) || (block > 1))
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
    return ioctlsocket(sock, FIONBIO, &varblock);
Packit Service 102f81
#else
Packit Service 102f81
    return fcntl(sock, F_SETFL, (block == SOCK_BLOCK) ? 0 : O_NONBLOCK);
Packit Service 102f81
#endif
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_set_nolinger(sock_t sock)
Packit Service 102f81
{
Packit Service 102f81
    struct linger lin = { 0, 0 };
Packit Service 102f81
    return setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&lin, 
Packit Service 102f81
            sizeof(struct linger));
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_set_nodelay(sock_t sock)
Packit Service 102f81
{
Packit Service 102f81
    int nodelay = 1;
Packit Service 102f81
Packit Service 102f81
    return setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&nodelay,
Packit Service 102f81
            sizeof(int));
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_set_keepalive(sock_t sock)
Packit Service 102f81
{
Packit Service 102f81
    int keepalive = 1;
Packit Service 102f81
    return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive, 
Packit Service 102f81
            sizeof(int));
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_close
Packit Service 102f81
**
Packit Service 102f81
** close the socket
Packit Service 102f81
*/
Packit Service 102f81
int sock_close(sock_t sock)
Packit Service 102f81
{
Packit Service 102f81
#ifdef _WIN32
Packit Service 102f81
    return closesocket(sock);
Packit Service 102f81
#else
Packit Service 102f81
    return close(sock);
Packit Service 102f81
#endif
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_writev
Packit Service 102f81
 *
Packit Service 102f81
 * write multiple buffers at once, return bytes actually written
Packit Service 102f81
 */
Packit Service 102f81
#ifdef HAVE_WRITEV
Packit Service 102f81
Packit Service 102f81
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count)
Packit Service 102f81
{
Packit Service 102f81
    return writev (sock, iov, count);
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#else
Packit Service 102f81
Packit Service 102f81
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count)
Packit Service 102f81
{
Packit Service 102f81
    int i = count, accum = 0, ret;
Packit Service 102f81
    const struct iovec *v = iov;
Packit Service 102f81
Packit Service 102f81
    while (i)
Packit Service 102f81
    {
Packit Service 102f81
        if (v->iov_base && v->iov_len)
Packit Service 102f81
        {
Packit Service 102f81
            ret = sock_write_bytes (sock, v->iov_base, v->iov_len);
Packit Service 102f81
            if (ret == -1 && accum==0)
Packit Service 102f81
                return -1;
Packit Service 102f81
            if (ret == -1)
Packit Service 102f81
                ret = 0;
Packit Service 102f81
            accum += ret;
Packit Service 102f81
            if (ret < (int)v->iov_len)
Packit Service 102f81
                break;
Packit Service 102f81
        }
Packit Service 102f81
        v++;
Packit Service 102f81
        i--;
Packit Service 102f81
    }
Packit Service 102f81
    return accum;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
/* sock_write_bytes
Packit Service 102f81
**
Packit Service 102f81
** write bytes to the socket
Packit Service 102f81
** this function will _NOT_ block
Packit Service 102f81
*/
Packit Service 102f81
int sock_write_bytes(sock_t sock, const void *buff, const size_t len)
Packit Service 102f81
{
Packit Service 102f81
    /* sanity check */
Packit Service 102f81
    if (!buff) {
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
    } else if (len <= 0) {
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
    } /*else if (!sock_valid_socket(sock)) {
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
    } */
Packit Service 102f81
Packit Service 102f81
    return send(sock, buff, len, 0);
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_write_string
Packit Service 102f81
**
Packit Service 102f81
** writes a string to a socket
Packit Service 102f81
** This function must only be called with a blocking socket.
Packit Service 102f81
*/
Packit Service 102f81
int sock_write_string(sock_t sock, const char *buff)
Packit Service 102f81
{
Packit Service 102f81
    return (sock_write_bytes(sock, buff, strlen(buff)) > 0);
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_write
Packit Service 102f81
**
Packit Service 102f81
** write a formatted string to the socket
Packit Service 102f81
** this function must only be called with a blocking socket.
Packit Service 102f81
** will truncate the string if it's greater than 1024 chars.
Packit Service 102f81
*/
Packit Service 102f81
int sock_write(sock_t sock, const char *fmt, ...)
Packit Service 102f81
{
Packit Service 102f81
    int rc;
Packit Service 102f81
    va_list ap;
Packit Service 102f81
Packit Service 102f81
    va_start (ap, fmt);
Packit Service 102f81
    rc = sock_write_fmt (sock, fmt, ap);
Packit Service 102f81
    va_end (ap);
Packit Service 102f81
Packit Service 102f81
    return rc;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#ifdef HAVE_OLD_VSNPRINTF
Packit Service 102f81
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
Packit Service 102f81
{
Packit Service 102f81
    va_list ap_local;
Packit Service 102f81
    unsigned int len = 1024;
Packit Service 102f81
    char *buff = NULL;
Packit Service 102f81
    int ret;
Packit Service 102f81
Packit Service 102f81
    /* don't go infinite, but stop at some huge limit */
Packit Service 102f81
    while (len < 2*1024*1024)
Packit Service 102f81
    {
Packit Service 102f81
        char *tmp = realloc (buff, len);
Packit Service 102f81
        ret = -1;
Packit Service 102f81
        if (tmp == NULL)
Packit Service 102f81
            break;
Packit Service 102f81
        buff = tmp;
Packit Service 102f81
        va_copy (ap_local, ap);
Packit Service 102f81
        ret = vsnprintf (buff, len, fmt, ap_local);
Packit Service 102f81
        if (ret > 0)
Packit Service 102f81
        {
Packit Service 102f81
            ret = sock_write_bytes (sock, buff, ret);
Packit Service 102f81
            break;
Packit Service 102f81
        }
Packit Service 102f81
        len += 8192;
Packit Service 102f81
    }
Packit Service 102f81
    free (buff);
Packit Service 102f81
    return ret;
Packit Service 102f81
}
Packit Service 102f81
#else
Packit Service 102f81
int sock_write_fmt(sock_t sock, const char *fmt, va_list ap)
Packit Service 102f81
{
Packit Service 102f81
    char buffer [1024], *buff = buffer;
Packit Service 102f81
    int len;
Packit Service 102f81
    int rc = SOCK_ERROR;
Packit Service 102f81
    va_list ap_retry;
Packit Service 102f81
Packit Service 102f81
    va_copy (ap_retry, ap);
Packit Service 102f81
Packit Service 102f81
    len = vsnprintf (buff, sizeof (buffer), fmt, ap);
Packit Service 102f81
Packit Service 102f81
    if (len > 0)
Packit Service 102f81
    {
Packit Service 102f81
        if ((size_t)len < sizeof (buffer))   /* common case */
Packit Service 102f81
            rc = sock_write_bytes(sock, buff, (size_t)len);
Packit Service 102f81
        else
Packit Service 102f81
        {
Packit Service 102f81
            /* truncated */
Packit Service 102f81
            buff = malloc (++len);
Packit Service 102f81
            if (buff)
Packit Service 102f81
            {
Packit Service 102f81
                len = vsnprintf (buff, len, fmt, ap_retry);
Packit Service 102f81
                if (len > 0)
Packit Service 102f81
                    rc = sock_write_bytes (sock, buff, len);
Packit Service 102f81
                free (buff);
Packit Service 102f81
            }
Packit Service 102f81
        }
Packit Service 102f81
    }
Packit Service 102f81
    va_end (ap_retry);
Packit Service 102f81
Packit Service 102f81
    return rc;
Packit Service 102f81
}
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
Packit Service 102f81
int sock_read_bytes(sock_t sock, char *buff, const int len)
Packit Service 102f81
{
Packit Service 102f81
Packit Service 102f81
    /*if (!sock_valid_socket(sock)) return 0; */
Packit Service 102f81
    if (!buff) return 0;
Packit Service 102f81
    if (len <= 0) return 0;
Packit Service 102f81
Packit Service 102f81
    return recv(sock, buff, len, 0);
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* sock_read_line
Packit Service 102f81
**
Packit Service 102f81
** Read one line of at max len bytes from sock into buff.
Packit Service 102f81
** If ok, return 1 and nullterminate buff. Otherwize return 0.
Packit Service 102f81
** Terminating \n is not put into the buffer.
Packit Service 102f81
**
Packit Service 102f81
** this function will probably not work on sockets in nonblocking mode
Packit Service 102f81
*/
Packit Service 102f81
int sock_read_line(sock_t sock, char *buff, const int len)
Packit Service 102f81
{
Packit Service 102f81
    char c = '\0';
Packit Service 102f81
    int read_bytes, pos;
Packit Service 102f81
  
Packit Service 102f81
    /*if (!sock_valid_socket(sock)) {
Packit Service 102f81
        return 0;
Packit Service 102f81
    } else*/ if (!buff) {
Packit Service 102f81
        return 0;
Packit Service 102f81
    } else if (len <= 0) {
Packit Service 102f81
        return 0;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    pos = 0;
Packit Service 102f81
    read_bytes = recv(sock, &c, 1, 0);
Packit Service 102f81
Packit Service 102f81
    if (read_bytes < 0) {
Packit Service 102f81
        return 0;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    while ((c != '\n') && (pos < len) && (read_bytes == 1)) {
Packit Service 102f81
        if (c != '\r')
Packit Service 102f81
            buff[pos++] = c;
Packit Service 102f81
        read_bytes = recv(sock, &c, 1, 0);
Packit Service 102f81
    }
Packit Service 102f81
    
Packit Service 102f81
    if (read_bytes == 1) {
Packit Service 102f81
        buff[pos] = '\0';
Packit Service 102f81
        return 1;
Packit Service 102f81
    } else {
Packit Service 102f81
        return 0;
Packit Service 102f81
    }
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* see if a connection has been established. If timeout is < 0 then wait
Packit Service 102f81
 * indefinitely, else wait for the stated number of seconds.
Packit Service 102f81
 * return SOCK_TIMEOUT for timeout
Packit Service 102f81
 * return SOCK_ERROR for failure
Packit Service 102f81
 * return 0 for try again, interrupted
Packit Service 102f81
 * return 1 for ok 
Packit Service 102f81
 */
Packit Service 102f81
int sock_connected (int sock, int timeout)
Packit Service 102f81
{
Packit Service 102f81
    fd_set wfds;
Packit Service 102f81
    int val = SOCK_ERROR;
Packit Service 102f81
    socklen_t size = sizeof val;
Packit Service 102f81
    struct timeval tv, *timeval = NULL;
Packit Service 102f81
Packit Service 102f81
    /* make a timeout of <0 be indefinite */
Packit Service 102f81
    if (timeout >= 0)
Packit Service 102f81
    {
Packit Service 102f81
        tv.tv_sec = timeout;
Packit Service 102f81
        tv.tv_usec = 0;
Packit Service 102f81
        timeval = &tv;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    FD_ZERO(&wfds);
Packit Service 102f81
    FD_SET(sock, &wfds);
Packit Service 102f81
Packit Service 102f81
    switch (select(sock + 1, NULL, &wfds, NULL, timeval))
Packit Service 102f81
    {
Packit Service 102f81
        case 0:
Packit Service 102f81
            return SOCK_TIMEOUT;
Packit Service 102f81
        default:
Packit Service 102f81
            /* on windows getsockopt.val is defined as char* */
Packit Service 102f81
            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*) &val, &size) == 0)
Packit Service 102f81
            {
Packit Service 102f81
                if (val == 0)
Packit Service 102f81
                    return 1;
Packit Service 102f81
                sock_set_error (val);
Packit Service 102f81
            }
Packit Service 102f81
            /* fall through */
Packit Service 102f81
        case -1:
Packit Service 102f81
            if (sock_recoverable (sock_error()))
Packit Service 102f81
                return 0;
Packit Service 102f81
            return SOCK_ERROR;
Packit Service 102f81
    }
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#ifdef HAVE_GETADDRINFO
Packit Service 102f81
Packit Service 102f81
int sock_connect_non_blocking (const char *hostname, const unsigned port)
Packit Service 102f81
{
Packit Service 102f81
    int sock = SOCK_ERROR;
Packit Service 102f81
    struct addrinfo *ai, *head, hints;
Packit Service 102f81
    char service[8];
Packit Service 102f81
Packit Service 102f81
    memset (&hints, 0, sizeof (hints));
Packit Service 102f81
    hints.ai_family = AF_UNSPEC;
Packit Service 102f81
    hints.ai_socktype = SOCK_STREAM;
Packit Service 102f81
Packit Service 102f81
    snprintf (service, sizeof (service), "%u", port);
Packit Service 102f81
Packit Service 102f81
    if (getaddrinfo (hostname, service, &hints, &head))
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
    ai = head;
Packit Service 102f81
    while (ai)
Packit Service 102f81
    {
Packit Service 102f81
        if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) 
Packit Service 102f81
                > -1)
Packit Service 102f81
        {
Packit Service 102f81
            sock_set_blocking (sock, SOCK_NONBLOCK);
Packit Service 102f81
            if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 && 
Packit Service 102f81
                    !sock_connect_pending(sock_error()))
Packit Service 102f81
            {
Packit Service 102f81
                sock_close (sock);
Packit Service 102f81
                sock = SOCK_ERROR;
Packit Service 102f81
            }
Packit Service 102f81
            else
Packit Service 102f81
                break;
Packit Service 102f81
        }
Packit Service 102f81
        ai = ai->ai_next;
Packit Service 102f81
    }
Packit Service 102f81
    if (head) freeaddrinfo (head);
Packit Service 102f81
    
Packit Service 102f81
    return sock;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
/* issue a connect, but return after the timeout (seconds) is reached. If
Packit Service 102f81
 * timeout is 0 or less then we will wait until the OS gives up on the connect
Packit Service 102f81
 * The socket is returned
Packit Service 102f81
 */
Packit Service 102f81
sock_t sock_connect_wto(const char *hostname, int port, int timeout)
Packit Service 102f81
{
Packit Service 102f81
    int sock = SOCK_ERROR;
Packit Service 102f81
    struct addrinfo *ai, *head, hints;
Packit Service 102f81
    char service[8];
Packit Service 102f81
Packit Service 102f81
    memset (&hints, 0, sizeof (hints));
Packit Service 102f81
    hints.ai_family = AF_UNSPEC;
Packit Service 102f81
    hints.ai_socktype = SOCK_STREAM;
Packit Service 102f81
    snprintf (service, sizeof (service), "%u", port);
Packit Service 102f81
Packit Service 102f81
    if (getaddrinfo (hostname, service, &hints, &head))
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
    ai = head;
Packit Service 102f81
    while (ai)
Packit Service 102f81
    {
Packit Service 102f81
        if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
Packit Service 102f81
        {
Packit Service 102f81
            if (timeout > 0)
Packit Service 102f81
                sock_set_blocking (sock, SOCK_NONBLOCK);
Packit Service 102f81
Packit Service 102f81
            if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0)
Packit Service 102f81
                break;
Packit Service 102f81
Packit Service 102f81
            /* loop as the connect maybe async */
Packit Service 102f81
            while (sock != SOCK_ERROR)
Packit Service 102f81
            {
Packit Service 102f81
                if (sock_recoverable (sock_error()))
Packit Service 102f81
                {
Packit Service 102f81
                    int connected = sock_connected (sock, timeout);
Packit Service 102f81
                    if (connected == 0)  /* try again, interrupted */
Packit Service 102f81
                        continue;
Packit Service 102f81
                    if (connected == 1) /* connected */
Packit Service 102f81
                    {
Packit Service 102f81
                        if (timeout >= 0)
Packit Service 102f81
                            sock_set_blocking(sock, SOCK_BLOCK);
Packit Service 102f81
                        break;
Packit Service 102f81
                    }
Packit Service 102f81
                }
Packit Service 102f81
                sock_close (sock);
Packit Service 102f81
                sock = SOCK_ERROR;
Packit Service 102f81
            }
Packit Service 102f81
            if (sock != SOCK_ERROR)
Packit Service 102f81
                break;
Packit Service 102f81
        }
Packit Service 102f81
        ai = ai->ai_next;
Packit Service 102f81
    }
Packit Service 102f81
    if (head)
Packit Service 102f81
        freeaddrinfo (head);
Packit Service 102f81
Packit Service 102f81
    return sock;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
#else
Packit Service 102f81
Packit Service 102f81
int sock_try_connection (int sock, const char *hostname, const unsigned port)
Packit Service 102f81
{
Packit Service 102f81
    struct sockaddr_in sin, server;
Packit Service 102f81
    char ip[MAX_ADDR_LEN];
Packit Service 102f81
Packit Service 102f81
    if (!hostname || !hostname[0] || port == 0)
Packit Service 102f81
        return -1;
Packit Service 102f81
Packit Service 102f81
    memset(&sin, 0, sizeof(struct sockaddr_in));
Packit Service 102f81
    memset(&server, 0, sizeof(struct sockaddr_in));
Packit Service 102f81
Packit Service 102f81
    if (!resolver_getip(hostname, ip, MAX_ADDR_LEN))
Packit Service 102f81
    {
Packit Service 102f81
        sock_close (sock);
Packit Service 102f81
        return -1;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    if (inet_aton(ip, (struct in_addr *)&sin.sin_addr) == 0)
Packit Service 102f81
    {
Packit Service 102f81
        sock_close(sock);
Packit Service 102f81
        return -1;
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    memcpy(&server.sin_addr, &sin.sin_addr, sizeof(struct sockaddr_in));
Packit Service 102f81
Packit Service 102f81
    server.sin_family = AF_INET;
Packit Service 102f81
    server.sin_port = htons(port);
Packit Service 102f81
Packit Service 102f81
    return connect(sock, (struct sockaddr *)&server, sizeof(server));
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_connect_non_blocking (const char *hostname, const unsigned port)
Packit Service 102f81
{
Packit Service 102f81
    int sock;
Packit Service 102f81
Packit Service 102f81
    sock = socket(AF_INET, SOCK_STREAM, 0);
Packit Service 102f81
    if (sock == -1)
Packit Service 102f81
        return -1;
Packit Service 102f81
Packit Service 102f81
    sock_set_blocking (sock, SOCK_NONBLOCK);
Packit Service 102f81
    sock_try_connection (sock, hostname, port);
Packit Service 102f81
    
Packit Service 102f81
    return sock;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
Packit Service 102f81
{
Packit Service 102f81
    int sock;
Packit Service 102f81
Packit Service 102f81
    sock = socket(AF_INET, SOCK_STREAM, 0);
Packit Service 102f81
    if (sock == -1)
Packit Service 102f81
        return -1;
Packit Service 102f81
Packit Service 102f81
    if (timeout)
Packit Service 102f81
    {
Packit Service 102f81
        sock_set_blocking (sock, SOCK_NONBLOCK);
Packit Service 102f81
        if (sock_try_connection (sock, hostname, port) < 0)
Packit Service 102f81
        {
Packit Service 102f81
            int ret = sock_connected (sock, timeout);
Packit Service 102f81
            if (ret <= 0)
Packit Service 102f81
            {
Packit Service 102f81
                sock_close (sock);
Packit Service 102f81
                return SOCK_ERROR;
Packit Service 102f81
            }
Packit Service 102f81
        }
Packit Service 102f81
        sock_set_blocking(sock, SOCK_BLOCK);
Packit Service 102f81
    }
Packit Service 102f81
    else
Packit Service 102f81
    {
Packit Service 102f81
        if (sock_try_connection (sock, hostname, port) < 0)
Packit Service 102f81
        {
Packit Service 102f81
            sock_close (sock);
Packit Service 102f81
            sock = SOCK_ERROR;
Packit Service 102f81
        }
Packit Service 102f81
    }
Packit Service 102f81
    return sock;
Packit Service 102f81
}
Packit Service 102f81
#endif
Packit Service 102f81
Packit Service 102f81
Packit Service 102f81
/* sock_get_server_socket
Packit Service 102f81
**
Packit Service 102f81
** create a socket for incoming requests on a specified port and
Packit Service 102f81
** interface.  if interface is null, listen on all interfaces.
Packit Service 102f81
** returns the socket, or SOCK_ERROR on failure
Packit Service 102f81
*/
Packit Service 102f81
sock_t sock_get_server_socket(const int port, char *sinterface)
Packit Service 102f81
{
Packit Service 102f81
#ifdef HAVE_INET_PTON
Packit Service 102f81
    struct sockaddr_storage sa;
Packit Service 102f81
#else    
Packit Service 102f81
    struct sockaddr_in sa;
Packit Service 102f81
#endif
Packit Service 102f81
    int family, len, error, opt;
Packit Service 102f81
    sock_t sock;
Packit Service 102f81
    char ip[MAX_ADDR_LEN];
Packit Service 102f81
Packit Service 102f81
    if (port < 0)
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
    /* defaults */
Packit Service 102f81
    memset(&sa, 0, sizeof(sa));
Packit Service 102f81
    family = AF_INET;
Packit Service 102f81
    len = sizeof(struct sockaddr_in);
Packit Service 102f81
Packit Service 102f81
    /* set the interface to bind to if specified */
Packit Service 102f81
    if (sinterface != NULL) {
Packit Service 102f81
        if (!resolver_getip(sinterface, ip, sizeof (ip)))
Packit Service 102f81
            return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
#ifdef HAVE_INET_PTON
Packit Service 102f81
        if (inet_pton(AF_INET, ip, &((struct sockaddr_in*)&sa)->sin_addr) > 0) {
Packit Service 102f81
            ((struct sockaddr_in*)&sa)->sin_family = AF_INET;
Packit Service 102f81
            ((struct sockaddr_in*)&sa)->sin_port = htons(port);
Packit Service 102f81
        } else if (inet_pton(AF_INET6, ip, 
Packit Service 102f81
                    &((struct sockaddr_in6*)&sa)->sin6_addr) > 0) {
Packit Service 102f81
            family = AF_INET6;
Packit Service 102f81
            len = sizeof (struct sockaddr_in6);
Packit Service 102f81
            ((struct sockaddr_in6*)&sa)->sin6_family = AF_INET6;
Packit Service 102f81
            ((struct sockaddr_in6*)&sa)->sin6_port = htons(port);
Packit Service 102f81
        } else {
Packit Service 102f81
            return SOCK_ERROR;
Packit Service 102f81
        }
Packit Service 102f81
#else
Packit Service 102f81
        if (!inet_aton(ip, &sa.sin_addr)) {
Packit Service 102f81
            return SOCK_ERROR;
Packit Service 102f81
        } else {
Packit Service 102f81
            sa.sin_family = AF_INET;
Packit Service 102f81
            sa.sin_port = htons(port);
Packit Service 102f81
        }
Packit Service 102f81
#endif
Packit Service 102f81
    } else {
Packit Service 102f81
        ((struct sockaddr_in*)&sa)->sin_addr.s_addr = INADDR_ANY;
Packit Service 102f81
        ((struct sockaddr_in*)&sa)->sin_family = AF_INET;
Packit Service 102f81
        ((struct sockaddr_in*)&sa)->sin_port = htons(port);
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    /* get a socket */
Packit Service 102f81
    sock = socket(family, SOCK_STREAM, 0);
Packit Service 102f81
    if (sock == -1)
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
    /* reuse it if we can */
Packit Service 102f81
    opt = 1;
Packit Service 102f81
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(int));
Packit Service 102f81
    
Packit Service 102f81
    /* bind socket to port */
Packit Service 102f81
    error = bind(sock, (struct sockaddr *)&sa, len);
Packit Service 102f81
    if (error == -1)
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
    return sock;
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_listen(sock_t serversock, int backlog)
Packit Service 102f81
{
Packit Service 102f81
    if (!sock_valid_socket(serversock))
Packit Service 102f81
        return 0;
Packit Service 102f81
Packit Service 102f81
    if (backlog <= 0)
Packit Service 102f81
        backlog = 10;
Packit Service 102f81
Packit Service 102f81
    return (listen(serversock, backlog) == 0);
Packit Service 102f81
}
Packit Service 102f81
Packit Service 102f81
int sock_accept(sock_t serversock, char *ip, int len)
Packit Service 102f81
{
Packit Service 102f81
#ifdef HAVE_INET_PTON
Packit Service 102f81
    struct sockaddr_storage sa;
Packit Service 102f81
#else    
Packit Service 102f81
    struct sockaddr_in sa;
Packit Service 102f81
#endif
Packit Service 102f81
    int ret;
Packit Service 102f81
    socklen_t slen;
Packit Service 102f81
Packit Service 102f81
    if (!sock_valid_socket(serversock))
Packit Service 102f81
        return SOCK_ERROR;
Packit Service 102f81
Packit Service 102f81
    slen = sizeof(sa);
Packit Service 102f81
    ret = accept(serversock, (struct sockaddr *)&sa, &slen);
Packit Service 102f81
Packit Service 102f81
    if (ret >= 0 && ip != NULL) {
Packit Service 102f81
#ifdef HAVE_INET_PTON
Packit Service 102f81
        if(((struct sockaddr_in *)&sa)->sin_family == AF_INET) 
Packit Service 102f81
            inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr,
Packit Service 102f81
                    ip, len);
Packit Service 102f81
        else if(((struct sockaddr_in6 *)&sa)->sin6_family == AF_INET6) 
Packit Service 102f81
            inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, 
Packit Service 102f81
                    ip, len);
Packit Service 102f81
        else {
Packit Service 102f81
            strncpy(ip, "ERROR", len-1);
Packit Service 102f81
            ip[len-1] = 0;
Packit Service 102f81
        }
Packit Service 102f81
#else
Packit Service 102f81
        /* inet_ntoa is not reentrant, we should protect this */
Packit Service 102f81
        strncpy(ip, inet_ntoa(sa.sin_addr), len);
Packit Service 102f81
#endif
Packit Service 102f81
        sock_set_nolinger(ret);
Packit Service 102f81
        sock_set_keepalive(ret);
Packit Service 102f81
    }
Packit Service 102f81
Packit Service 102f81
    return ret;
Packit Service 102f81
}
Packit Service 102f81