2003-06-03 14:29:23 -04:00
|
|
|
/* -*- c-basic-offset: 4; -*- */
|
2003-07-13 22:14:59 -04:00
|
|
|
/* sock.c: General Socket Functions
|
2001-09-09 22:24:39 -04:00
|
|
|
*
|
2018-10-12 06:28:09 -04:00
|
|
|
* Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
|
|
|
|
* Brendan Cully <brendan@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>,
|
|
|
|
* Jack Moffitt <jack@icecast.org>,
|
|
|
|
* Ed "oddsock" Zaleski <oddsock@xiph.org>,
|
|
|
|
* Copyright (C) 2012-2018 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
2001-09-09 22:24:39 -04:00
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2001-09-09 22:24:39 -04:00
|
|
|
*
|
2014-12-05 04:31:08 -05:00
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2003-07-13 22:14:59 -04:00
|
|
|
*
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
|
|
|
|
2003-03-08 11:05:38 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2001-10-20 01:57:28 -04:00
|
|
|
#include <errno.h>
|
2008-02-04 23:21:12 -05:00
|
|
|
#ifdef HAVE_POLL
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2003-06-05 15:18:00 -04:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
2003-06-05 15:02:36 -04:00
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2003-01-18 02:08:00 -05:00
|
|
|
#include <netinet/tcp.h>
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#else
|
2001-10-20 01:57:28 -04:00
|
|
|
#include <winsock2.h>
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2018-11-02 07:20:04 -04:00
|
|
|
#include <igloo/sock.h>
|
|
|
|
#include <igloo/resolver.h>
|
2018-11-01 07:23:07 -04:00
|
|
|
#include "../src/private.h"
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2007-08-08 23:19:24 -04:00
|
|
|
/* for older C libraries */
|
|
|
|
#ifndef AI_NUMERICSERV
|
2007-08-09 19:51:26 -04:00
|
|
|
# define AI_NUMERICSERV 0
|
|
|
|
#endif
|
|
|
|
#ifndef AI_ADDRCONFIG
|
|
|
|
# define AI_ADDRCONFIG 0
|
2007-08-08 23:19:24 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_initialize
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** initializes the socket library. you must call this
|
|
|
|
** before using the library!
|
|
|
|
*/
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_sock_initialize(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
WSADATA wsad;
|
|
|
|
WSAStartup(0x0101, &wsad);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_resolver_initialize();
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_shutdown
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** shutdown the socket library. remember to call this when you're
|
|
|
|
** through using the lib
|
|
|
|
*/
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_sock_shutdown(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
WSACleanup();
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
2003-07-01 14:49:07 -04:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_resolver_shutdown();
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_get_localip
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** gets the local ip address for the machine
|
|
|
|
** the ip it returns *should* be on the internet.
|
|
|
|
** in any case, it's as close as we can hope to get
|
|
|
|
** unless someone has better ideas on how to do this
|
|
|
|
*/
|
2018-10-12 06:55:24 -04:00
|
|
|
char *igloo_sock_get_localip(char *buff, int len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
char temp[1024];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-06-05 13:09:12 -04:00
|
|
|
if (gethostname(temp, sizeof(temp)) != 0)
|
2003-03-14 21:10:19 -05:00
|
|
|
return NULL;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
if (igloo_resolver_getip(temp, buff, len))
|
2003-03-14 21:10:19 -05:00
|
|
|
return buff;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return NULL;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_error
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** returns the last socket error
|
|
|
|
*/
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_sock_error(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
return WSAGetLastError();
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
return errno;
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
void igloo_sock_set_error(int val)
|
2004-02-12 16:28:35 -05:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSASetLastError (val);
|
|
|
|
#else
|
|
|
|
errno = val;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_recoverable
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** determines if the socket error is recoverable
|
|
|
|
** in terms of non blocking sockets
|
|
|
|
*/
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_sock_recoverable(int error)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2004-02-12 16:28:35 -05:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
|
|
|
case EINPROGRESS:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
2014-12-26 14:50:47 -05:00
|
|
|
#if defined (WSAEWOULDBLOCK) && WSAEWOULDBLOCK != EWOULDBLOCK
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
#if defined (WSAEINPROGRESS) && WSAEINPROGRESS != EINPROGRESS
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
#endif
|
2004-02-12 16:28:35 -05:00
|
|
|
#ifdef ERESTART
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
int igloo_sock_stalled (int error)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2004-02-12 16:28:35 -05:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
case EINPROGRESS:
|
|
|
|
case EALREADY:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
2014-12-26 14:50:47 -05:00
|
|
|
#if defined (WSAEWOULDBLOCK) && WSAEWOULDBLOCK != EWOULDBLOCK
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
#if defined (WSAEINPROGRESS) && WSAEINPROGRESS != EINPROGRESS
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
#endif
|
2004-02-12 16:28:35 -05:00
|
|
|
#ifdef ERESTART
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-06 21:32:42 -04:00
|
|
|
static int sock_connect_pending (int error)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
return error == EINPROGRESS || error == EALREADY;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_valid_socket
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
2018-10-12 07:27:17 -04:00
|
|
|
** determines if a igloo_sock_t represents a valid socket
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_valid_socket(igloo_sock_t sock)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
int ret;
|
|
|
|
int optval;
|
|
|
|
socklen_t optlen;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
optlen = sizeof(int);
|
2003-10-19 23:08:46 -04:00
|
|
|
/* apparently on windows getsockopt.optval is a char * */
|
|
|
|
ret = getsockopt(sock, SOL_SOCKET, SO_TYPE, (void*) &optval, &optlen);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
return (ret == 0);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 18:17:13 -04:00
|
|
|
|
|
|
|
/* determines if the passed socket is still connected */
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_active (igloo_sock_t sock)
|
2007-08-21 18:17:13 -04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
l = recv (sock, &c, 1, MSG_PEEK);
|
|
|
|
if (l == 0)
|
|
|
|
return 0;
|
2018-10-12 07:27:17 -04:00
|
|
|
if (l == igloo_SOCK_ERROR && igloo_sock_recoverable (igloo_sock_error()))
|
2007-08-21 18:17:13 -04:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
/* inet_aton
|
|
|
|
**
|
|
|
|
** turns an ascii ip address into a binary representation
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
int inet_aton(const char *s, struct in_addr *a)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int lsb, b2, b3, msb;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (sscanf(s, "%d.%d.%d.%d", &lsb, &b2, &b3, &msb) < 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
a->s_addr = inet_addr(s);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return (a->s_addr != INADDR_NONE);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_set_blocking
|
2009-01-07 21:47:44 -05:00
|
|
|
*
|
|
|
|
* set the sock blocking or nonblocking
|
|
|
|
* 1 for blocking
|
|
|
|
* 0 for nonblocking
|
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_set_blocking(igloo_sock_t sock, int block)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-10-19 23:08:46 -04:00
|
|
|
#ifdef __MINGW32__
|
2009-03-14 12:05:12 -04:00
|
|
|
u_long varblock = 1;
|
2003-10-19 23:08:46 -04:00
|
|
|
#else
|
2009-03-14 12:05:12 -04:00
|
|
|
int varblock = 1;
|
2003-10-19 23:08:46 -04:00
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
if ((!igloo_sock_valid_socket(sock)) || (block < 0) || (block > 1))
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2009-03-14 12:05:12 -04:00
|
|
|
if (block) varblock = 0;
|
2003-03-14 21:10:19 -05:00
|
|
|
return ioctlsocket(sock, FIONBIO, &varblock);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2009-01-07 21:47:44 -05:00
|
|
|
return fcntl(sock, F_SETFL, (block) ? 0 : O_NONBLOCK);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_set_nolinger(igloo_sock_t sock)
|
2002-03-22 16:44:29 -05:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
struct linger lin = { 0, 0 };
|
|
|
|
return setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&lin,
|
2002-11-22 08:02:51 -05:00
|
|
|
sizeof(struct linger));
|
2002-03-22 16:44:29 -05:00
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_set_nodelay(igloo_sock_t sock)
|
2003-01-18 02:08:00 -05:00
|
|
|
{
|
|
|
|
int nodelay = 1;
|
|
|
|
|
2003-04-08 08:09:57 -04:00
|
|
|
return setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&nodelay,
|
2003-01-18 02:08:00 -05:00
|
|
|
sizeof(int));
|
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_set_keepalive(igloo_sock_t sock)
|
2002-03-22 16:44:29 -05:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int keepalive = 1;
|
|
|
|
return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive,
|
2002-11-22 08:02:51 -05:00
|
|
|
sizeof(int));
|
2002-03-22 16:44:29 -05:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_close
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** close the socket
|
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_close(igloo_sock_t sock)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
return closesocket(sock);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
return close(sock);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_writev
|
2002-11-22 08:02:51 -05:00
|
|
|
*
|
|
|
|
* write multiple buffers at once, return bytes actually written
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_WRITEV
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
ssize_t igloo_sock_writev (igloo_sock_t sock, const struct iovec *iov, size_t count)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
return writev (sock, iov, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
ssize_t igloo_sock_writev (igloo_sock_t sock, const struct iovec *iov, size_t count)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
int i = count, accum = 0, ret;
|
|
|
|
const struct iovec *v = iov;
|
|
|
|
|
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
if (v->iov_base && v->iov_len)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
ret = igloo_sock_write_bytes (sock, v->iov_base, v->iov_len);
|
2002-11-22 08:02:51 -05:00
|
|
|
if (ret == -1 && accum==0)
|
|
|
|
return -1;
|
|
|
|
if (ret == -1)
|
|
|
|
ret = 0;
|
|
|
|
accum += ret;
|
|
|
|
if (ret < (int)v->iov_len)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
v++;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
return accum;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_write_bytes
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** write bytes to the socket
|
2002-03-22 16:24:57 -05:00
|
|
|
** this function will _NOT_ block
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_write_bytes(igloo_sock_t sock, const void *buff, size_t len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
/* sanity check */
|
|
|
|
if (!buff) {
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2003-03-14 21:10:19 -05:00
|
|
|
} else if (len <= 0) {
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2018-10-12 06:55:24 -04:00
|
|
|
} /*else if (!igloo_sock_valid_socket(sock)) {
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2003-03-14 21:10:19 -05:00
|
|
|
} */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return send(sock, buff, len, 0);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_write_string
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** writes a string to a socket
|
2003-05-29 08:18:04 -04:00
|
|
|
** This function must only be called with a blocking socket.
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_write_string(igloo_sock_t sock, const char *buff)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
return (igloo_sock_write_bytes(sock, buff, strlen(buff)) > 0);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_write
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** write a formatted string to the socket
|
2003-05-29 08:18:04 -04:00
|
|
|
** this function must only be called with a blocking socket.
|
2001-09-09 22:24:39 -04:00
|
|
|
** will truncate the string if it's greater than 1024 chars.
|
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_write(igloo_sock_t sock, const char *fmt, ...)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-06-05 20:05:19 -04:00
|
|
|
int rc;
|
2003-03-14 21:10:19 -05:00
|
|
|
va_list ap;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-06-05 20:05:19 -04:00
|
|
|
va_start (ap, fmt);
|
2018-10-12 06:55:24 -04:00
|
|
|
rc = igloo_sock_write_fmt (sock, fmt, ap);
|
2003-06-05 20:05:19 -04:00
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return rc;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2004-10-06 10:07:55 -04:00
|
|
|
#ifdef HAVE_OLD_VSNPRINTF
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_write_fmt(igloo_sock_t sock, const char *fmt, va_list ap)
|
2004-10-06 10:07:55 -04:00
|
|
|
{
|
|
|
|
va_list ap_local;
|
|
|
|
unsigned int len = 1024;
|
|
|
|
char *buff = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* don't go infinite, but stop at some huge limit */
|
|
|
|
while (len < 2*1024*1024)
|
|
|
|
{
|
|
|
|
char *tmp = realloc (buff, len);
|
|
|
|
ret = -1;
|
|
|
|
if (tmp == NULL)
|
|
|
|
break;
|
|
|
|
buff = tmp;
|
|
|
|
va_copy (ap_local, ap);
|
|
|
|
ret = vsnprintf (buff, len, fmt, ap_local);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
ret = igloo_sock_write_bytes (sock, buff, ret);
|
2004-10-06 10:07:55 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
len += 8192;
|
|
|
|
}
|
|
|
|
free (buff);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_write_fmt(igloo_sock_t sock, const char *fmt, va_list ap)
|
2003-03-07 09:57:36 -05:00
|
|
|
{
|
2003-06-05 20:05:19 -04:00
|
|
|
char buffer [1024], *buff = buffer;
|
|
|
|
int len;
|
2018-10-12 07:27:17 -04:00
|
|
|
int rc = igloo_SOCK_ERROR;
|
2003-06-05 20:05:19 -04:00
|
|
|
va_list ap_retry;
|
|
|
|
|
|
|
|
va_copy (ap_retry, ap);
|
2003-03-07 09:57:36 -05:00
|
|
|
|
2003-06-05 20:05:19 -04:00
|
|
|
len = vsnprintf (buff, sizeof (buffer), fmt, ap);
|
2003-03-07 09:57:36 -05:00
|
|
|
|
2003-06-05 20:05:19 -04:00
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if ((size_t)len < sizeof (buffer)) /* common case */
|
2018-10-12 06:55:24 -04:00
|
|
|
rc = igloo_sock_write_bytes(sock, buff, (size_t)len);
|
2003-06-05 20:05:19 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* truncated */
|
|
|
|
buff = malloc (++len);
|
|
|
|
if (buff)
|
|
|
|
{
|
|
|
|
len = vsnprintf (buff, len, fmt, ap_retry);
|
|
|
|
if (len > 0)
|
2018-10-12 06:55:24 -04:00
|
|
|
rc = igloo_sock_write_bytes (sock, buff, len);
|
2003-06-05 20:05:19 -04:00
|
|
|
free (buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-06 07:19:50 -04:00
|
|
|
va_end (ap_retry);
|
2003-06-05 20:05:19 -04:00
|
|
|
|
|
|
|
return rc;
|
2003-03-07 09:57:36 -05:00
|
|
|
}
|
2004-10-06 10:07:55 -04:00
|
|
|
#endif
|
2003-03-07 09:57:36 -05:00
|
|
|
|
2003-06-05 20:05:19 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_read_bytes(igloo_sock_t sock, char *buff, size_t len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/*if (!igloo_sock_valid_socket(sock)) return 0; */
|
2003-03-14 21:10:19 -05:00
|
|
|
if (!buff) return 0;
|
|
|
|
if (len <= 0) return 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return recv(sock, buff, len, 0);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_read_line
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** Read one line of at max len bytes from sock into buff.
|
|
|
|
** If ok, return 1 and nullterminate buff. Otherwize return 0.
|
|
|
|
** Terminating \n is not put into the buffer.
|
|
|
|
**
|
|
|
|
** this function will probably not work on sockets in nonblocking mode
|
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_read_line(igloo_sock_t sock, char *buff, const int len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
char c = '\0';
|
|
|
|
int read_bytes, pos;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/*if (!igloo_sock_valid_socket(sock)) {
|
2003-03-14 21:10:19 -05:00
|
|
|
return 0;
|
|
|
|
} else*/ if (!buff) {
|
|
|
|
return 0;
|
|
|
|
} else if (len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
read_bytes = recv(sock, &c, 1, 0);
|
|
|
|
|
|
|
|
if (read_bytes < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((c != '\n') && (pos < len) && (read_bytes == 1)) {
|
|
|
|
if (c != '\r')
|
|
|
|
buff[pos++] = c;
|
|
|
|
read_bytes = recv(sock, &c, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_bytes == 1) {
|
|
|
|
buff[pos] = '\0';
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2004-07-26 12:03:52 -04:00
|
|
|
/* see if a connection has been established. If timeout is < 0 then wait
|
|
|
|
* indefinitely, else wait for the stated number of seconds.
|
2018-10-12 07:27:17 -04:00
|
|
|
* return igloo_SOCK_TIMEOUT for timeout
|
|
|
|
* return igloo_SOCK_ERROR for failure
|
2004-02-17 09:17:57 -05:00
|
|
|
* return 0 for try again, interrupted
|
|
|
|
* return 1 for ok
|
|
|
|
*/
|
2008-02-04 23:21:12 -05:00
|
|
|
#ifdef HAVE_POLL
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_connected (igloo_sock_t sock, int timeout)
|
2008-02-04 23:21:12 -05:00
|
|
|
{
|
|
|
|
struct pollfd check;
|
2018-10-12 07:27:17 -04:00
|
|
|
int val = igloo_SOCK_ERROR;
|
2009-07-06 10:28:16 -04:00
|
|
|
socklen_t size = sizeof val;
|
2008-02-04 23:21:12 -05:00
|
|
|
|
|
|
|
check.fd = sock;
|
|
|
|
check.events = POLLOUT;
|
|
|
|
switch (poll (&check, 1, timeout*1000))
|
|
|
|
{
|
2018-10-12 07:27:17 -04:00
|
|
|
case 0: return igloo_SOCK_TIMEOUT;
|
2009-07-06 10:28:16 -04:00
|
|
|
default:
|
|
|
|
/* on windows getsockopt.val is defined as char* */
|
|
|
|
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*) &val, &size) == 0)
|
|
|
|
{
|
|
|
|
if (val == 0)
|
|
|
|
return 1;
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_error (val);
|
2009-07-06 10:28:16 -04:00
|
|
|
}
|
|
|
|
/* fall through */
|
2008-02-04 23:21:12 -05:00
|
|
|
case -1:
|
2018-10-12 06:55:24 -04:00
|
|
|
if (igloo_sock_recoverable (igloo_sock_error()))
|
2008-02-04 23:21:12 -05:00
|
|
|
return 0;
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2008-02-04 23:21:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_connected (igloo_sock_t sock, int timeout)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
fd_set wfds;
|
2018-10-12 07:27:17 -04:00
|
|
|
int val = igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
socklen_t size = sizeof val;
|
2004-02-12 16:28:35 -05:00
|
|
|
struct timeval tv, *timeval = NULL;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
2004-07-26 12:03:52 -04:00
|
|
|
/* make a timeout of <0 be indefinite */
|
|
|
|
if (timeout >= 0)
|
2004-02-12 16:28:35 -05:00
|
|
|
{
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
timeval = &tv;
|
|
|
|
}
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(sock, &wfds);
|
|
|
|
|
2004-02-12 16:28:35 -05:00
|
|
|
switch (select(sock + 1, NULL, &wfds, NULL, timeval))
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2003-10-19 23:08:46 -04:00
|
|
|
case 0:
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_TIMEOUT;
|
2003-10-19 23:08:46 -04:00
|
|
|
default:
|
2004-02-12 16:28:35 -05:00
|
|
|
/* on windows getsockopt.val is defined as char* */
|
|
|
|
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*) &val, &size) == 0)
|
|
|
|
{
|
|
|
|
if (val == 0)
|
|
|
|
return 1;
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_error (val);
|
2004-02-12 16:28:35 -05:00
|
|
|
}
|
2004-02-17 09:17:57 -05:00
|
|
|
/* fall through */
|
2003-10-19 23:08:46 -04:00
|
|
|
case -1:
|
2018-10-12 06:55:24 -04:00
|
|
|
if (igloo_sock_recoverable (igloo_sock_error()))
|
2004-02-12 16:28:35 -05:00
|
|
|
return 0;
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
}
|
2008-02-04 23:21:12 -05:00
|
|
|
#endif
|
2002-11-22 08:02:51 -05:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_connect_wto (const char *hostname, int port, int timeout)
|
2009-01-07 21:47:44 -05:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
return igloo_sock_connect_wto_bind(hostname, port, NULL, timeout);
|
2009-01-07 21:47:44 -05:00
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_connect_non_blocking (const char *hostname, unsigned port)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2018-10-12 07:27:17 -04:00
|
|
|
int sock = igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
struct addrinfo *ai, *head, hints;
|
|
|
|
char service[8];
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof (hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
snprintf (service, sizeof (service), "%u", port);
|
|
|
|
|
|
|
|
if (getaddrinfo (hostname, service, &hints, &head))
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
ai = head;
|
|
|
|
while (ai)
|
|
|
|
{
|
|
|
|
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol))
|
|
|
|
> -1)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_blocking (sock, 0);
|
2002-11-22 08:02:51 -05:00
|
|
|
if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
|
2018-10-12 06:55:24 -04:00
|
|
|
!sock_connect_pending(igloo_sock_error()))
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2018-10-12 07:27:17 -04:00
|
|
|
sock = igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ai = ai->ai_next;
|
|
|
|
}
|
|
|
|
if (head) freeaddrinfo (head);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2004-07-26 12:03:52 -04:00
|
|
|
/* issue a connect, but return after the timeout (seconds) is reached. If
|
|
|
|
* timeout is 0 or less then we will wait until the OS gives up on the connect
|
|
|
|
* The socket is returned
|
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t sock = igloo_SOCK_ERROR;
|
2009-01-07 21:47:44 -05:00
|
|
|
struct addrinfo *ai, *head, *b_head=NULL, hints;
|
2002-11-22 08:02:51 -05:00
|
|
|
char service[8];
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof (hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
snprintf (service, sizeof (service), "%u", port);
|
|
|
|
|
|
|
|
if (getaddrinfo (hostname, service, &hints, &head))
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
ai = head;
|
|
|
|
while (ai)
|
|
|
|
{
|
2004-02-12 16:28:35 -05:00
|
|
|
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2004-07-26 12:03:52 -04:00
|
|
|
if (timeout > 0)
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_blocking (sock, 0);
|
2009-01-07 21:47:44 -05:00
|
|
|
|
|
|
|
if (bnd)
|
|
|
|
{
|
|
|
|
struct addrinfo b_hints;
|
|
|
|
memset (&b_hints, 0, sizeof(b_hints));
|
|
|
|
b_hints.ai_family = ai->ai_family;
|
|
|
|
b_hints.ai_socktype = ai->ai_socktype;
|
|
|
|
b_hints.ai_protocol = ai->ai_protocol;
|
|
|
|
if (getaddrinfo (bnd, NULL, &b_hints, &b_head) ||
|
|
|
|
bind (sock, b_head->ai_addr, b_head->ai_addrlen) < 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2018-10-12 07:27:17 -04:00
|
|
|
sock = igloo_SOCK_ERROR;
|
2009-01-07 21:47:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-02-12 16:28:35 -05:00
|
|
|
|
|
|
|
if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* loop as the connect maybe async */
|
2018-10-12 07:27:17 -04:00
|
|
|
while (sock != igloo_SOCK_ERROR)
|
2004-02-12 16:28:35 -05:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
if (igloo_sock_recoverable (igloo_sock_error()))
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
int connected = igloo_sock_connected (sock, timeout);
|
2004-07-26 12:03:52 -04:00
|
|
|
if (connected == 0) /* try again, interrupted */
|
|
|
|
continue;
|
|
|
|
if (connected == 1) /* connected */
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2004-07-26 12:03:52 -04:00
|
|
|
if (timeout >= 0)
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_blocking(sock, 1);
|
2003-06-18 07:05:49 -04:00
|
|
|
break;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2018-10-12 07:27:17 -04:00
|
|
|
sock = igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
2018-10-12 07:27:17 -04:00
|
|
|
if (sock != igloo_SOCK_ERROR)
|
2004-02-12 16:28:35 -05:00
|
|
|
break;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
ai = ai->ai_next;
|
|
|
|
}
|
2009-01-07 21:47:44 -05:00
|
|
|
if (b_head)
|
|
|
|
freeaddrinfo (b_head);
|
|
|
|
freeaddrinfo (head);
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2007-08-08 23:19:24 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_get_server_socket (int port, const char *sinterface)
|
2007-08-08 23:19:24 -04:00
|
|
|
{
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
struct addrinfo hints, *res, *ai;
|
|
|
|
char service [10];
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
if (port < 0)
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2007-08-08 23:19:24 -04:00
|
|
|
|
|
|
|
memset (&sa, 0, sizeof(sa));
|
|
|
|
memset (&hints, 0, sizeof(hints));
|
|
|
|
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG | AI_NUMERICSERV | AI_NUMERICHOST;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
snprintf (service, sizeof (service), "%d", port);
|
|
|
|
|
|
|
|
if (getaddrinfo (sinterface, service, &hints, &res))
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2007-08-08 23:19:24 -04:00
|
|
|
ai = res;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int on = 1;
|
|
|
|
sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
|
|
|
if (sock < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on));
|
2007-12-20 11:49:02 -05:00
|
|
|
on = 0;
|
2008-02-27 21:16:23 -05:00
|
|
|
#ifdef IPV6_V6ONLY
|
2007-12-20 11:49:02 -05:00
|
|
|
setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof on);
|
2008-02-27 21:16:23 -05:00
|
|
|
#endif
|
2007-08-08 23:19:24 -04:00
|
|
|
|
|
|
|
if (bind (sock, ai->ai_addr, ai->ai_addrlen) < 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2007-08-08 23:19:24 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
freeaddrinfo (res);
|
|
|
|
return sock;
|
|
|
|
|
|
|
|
} while ((ai = ai->ai_next));
|
|
|
|
|
|
|
|
freeaddrinfo (res);
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2007-08-08 23:19:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
#else
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2007-08-08 23:19:24 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int sock_try_connection (igloo_sock_t sock, const char *hostname, unsigned int port)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin, server;
|
2003-02-13 06:29:07 -05:00
|
|
|
char ip[MAX_ADDR_LEN];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
if (!hostname || !hostname[0] || port == 0)
|
|
|
|
return -1;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
|
|
memset(&server, 0, sizeof(struct sockaddr_in));
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
if (!igloo_resolver_getip(hostname, ip, MAX_ADDR_LEN))
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2002-11-22 08:02:51 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inet_aton(ip, (struct in_addr *)&sin.sin_addr) == 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close(sock);
|
2002-11-22 08:02:51 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&server.sin_addr, &sin.sin_addr, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
server.sin_family = AF_INET;
|
2009-01-07 21:47:44 -05:00
|
|
|
server.sin_port = htons((short)port);
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
return connect(sock, (struct sockaddr *)&server, sizeof(server));
|
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_connect_non_blocking (const char *hostname, unsigned port)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t sock;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
2018-10-12 07:27:17 -04:00
|
|
|
if (sock == igloo_SOCK_ERROR)
|
|
|
|
return igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_blocking (sock, 0);
|
2002-11-22 08:02:51 -05:00
|
|
|
sock_try_connection (sock, hostname, port);
|
|
|
|
|
|
|
|
return sock;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t sock;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
2018-10-12 07:27:17 -04:00
|
|
|
if (sock == igloo_SOCK_ERROR)
|
|
|
|
return igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
|
2009-01-07 21:47:44 -05:00
|
|
|
if (bnd)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
|
|
|
|
if (inet_aton (bnd, &sa.sin_addr) == 0 ||
|
|
|
|
bind (sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2009-01-07 21:47:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
if (timeout)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_blocking (sock, 0);
|
2002-11-22 08:02:51 -05:00
|
|
|
if (sock_try_connection (sock, hostname, port) < 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
int ret = igloo_sock_connected (sock, timeout);
|
2002-11-22 08:02:51 -05:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_blocking(sock, 1);
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sock_try_connection (sock, hostname, port) < 0)
|
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_close (sock);
|
2018-10-12 07:27:17 -04:00
|
|
|
sock = igloo_SOCK_ERROR;
|
2002-11-22 08:02:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
/* igloo_sock_get_server_socket
|
2001-09-09 22:24:39 -04:00
|
|
|
**
|
|
|
|
** create a socket for incoming requests on a specified port and
|
|
|
|
** interface. if interface is null, listen on all interfaces.
|
2018-10-12 07:27:17 -04:00
|
|
|
** returns the socket, or igloo_SOCK_ERROR on failure
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_get_server_socket(int port, const char *sinterface)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
struct sockaddr_in sa;
|
2007-08-08 23:19:24 -04:00
|
|
|
int error, opt;
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t sock;
|
2003-03-14 21:10:19 -05:00
|
|
|
char ip[MAX_ADDR_LEN];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (port < 0)
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* defaults */
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* set the interface to bind to if specified */
|
|
|
|
if (sinterface != NULL) {
|
2018-10-12 06:55:24 -04:00
|
|
|
if (!igloo_resolver_getip(sinterface, ip, sizeof (ip)))
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (!inet_aton(ip, &sa.sin_addr)) {
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2003-03-14 21:10:19 -05:00
|
|
|
} else {
|
|
|
|
sa.sin_family = AF_INET;
|
2009-01-07 21:47:44 -05:00
|
|
|
sa.sin_port = htons((short)port);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
} else {
|
2007-08-08 23:19:24 -04:00
|
|
|
sa.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
sa.sin_family = AF_INET;
|
2009-01-07 21:47:44 -05:00
|
|
|
sa.sin_port = htons((short)port);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* get a socket */
|
2007-08-08 23:19:24 -04:00
|
|
|
sock = socket (AF_INET, SOCK_STREAM, 0);
|
2003-03-14 21:10:19 -05:00
|
|
|
if (sock == -1)
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* reuse it if we can */
|
|
|
|
opt = 1;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(int));
|
2007-08-08 23:19:24 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* bind socket to port */
|
2007-08-08 23:19:24 -04:00
|
|
|
error = bind(sock, (struct sockaddr *)&sa, sizeof (struct sockaddr_in));
|
2003-03-14 21:10:19 -05:00
|
|
|
if (error == -1)
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return sock;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2007-08-08 23:19:24 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
void igloo_sock_set_send_buffer (igloo_sock_t sock, int win_size)
|
2009-03-14 12:05:12 -04:00
|
|
|
{
|
|
|
|
setsockopt (sock, SOL_SOCKET, SO_SNDBUF, (char *) &win_size, sizeof(win_size));
|
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
int igloo_sock_listen(igloo_sock_t serversock, int backlog)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2018-10-12 06:55:24 -04:00
|
|
|
if (!igloo_sock_valid_socket(serversock))
|
2003-03-14 21:10:19 -05:00
|
|
|
return 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (backlog <= 0)
|
|
|
|
backlog = 10;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return (listen(serversock, backlog) == 0);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t igloo_sock_accept(igloo_sock_t serversock, char *ip, size_t len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2007-08-08 23:19:24 -04:00
|
|
|
#ifdef HAVE_GETNAMEINFO
|
2003-03-14 21:10:19 -05:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
#else
|
|
|
|
struct sockaddr_in sa;
|
2003-02-13 06:29:07 -05:00
|
|
|
#endif
|
2018-10-12 07:27:17 -04:00
|
|
|
igloo_sock_t ret;
|
2003-03-14 21:10:19 -05:00
|
|
|
socklen_t slen;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 06:55:24 -04:00
|
|
|
if (ip == NULL || len == 0 || !igloo_sock_valid_socket(serversock))
|
2018-10-12 07:27:17 -04:00
|
|
|
return igloo_SOCK_ERROR;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
slen = sizeof(sa);
|
|
|
|
ret = accept(serversock, (struct sockaddr *)&sa, &slen);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2018-10-12 07:27:17 -04:00
|
|
|
if (ret != igloo_SOCK_ERROR)
|
2007-08-08 23:19:24 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETNAMEINFO
|
|
|
|
if (getnameinfo ((struct sockaddr *)&sa, slen, ip, len, NULL, 0, NI_NUMERICHOST))
|
|
|
|
snprintf (ip, len, "unknown");
|
2003-02-13 06:29:07 -05:00
|
|
|
#else
|
2003-02-14 06:14:55 -05:00
|
|
|
/* inet_ntoa is not reentrant, we should protect this */
|
2003-03-14 21:10:19 -05:00
|
|
|
strncpy(ip, inet_ntoa(sa.sin_addr), len);
|
2003-02-13 06:29:07 -05:00
|
|
|
#endif
|
2018-10-12 06:55:24 -04:00
|
|
|
igloo_sock_set_nolinger(ret);
|
|
|
|
igloo_sock_set_keepalive(ret);
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return ret;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
2003-02-13 06:29:07 -05:00
|
|
|
|