2001-09-09 22:24:39 -04:00
|
|
|
/* sock.c
|
|
|
|
* - General Socket Functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999 the icecast team
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 <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>
|
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
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#define EINPROGRESS WSAEINPROGRESS
|
|
|
|
#define ENOTSOCK WSAENOTSOCK
|
2002-11-30 21:11:07 -05:00
|
|
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
#define EALREADY WSAEALREADY
|
2002-11-23 10:55:27 -05:00
|
|
|
#define socklen_t int
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sock.h"
|
|
|
|
#include "resolver.h"
|
|
|
|
|
|
|
|
/* sock_initialize
|
|
|
|
**
|
|
|
|
** initializes the socket library. you must call this
|
|
|
|
** before using the library!
|
|
|
|
*/
|
|
|
|
void sock_initialize(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsad;
|
|
|
|
WSAStartup(0x0101, &wsad);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
resolver_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_shutdown
|
|
|
|
**
|
|
|
|
** shutdown the socket library. remember to call this when you're
|
|
|
|
** through using the lib
|
|
|
|
*/
|
|
|
|
void sock_shutdown(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_get_localip
|
|
|
|
**
|
|
|
|
** 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
|
|
|
|
*/
|
|
|
|
char *sock_get_localip(char *buff, int len)
|
|
|
|
{
|
|
|
|
char temp[1024];
|
|
|
|
|
|
|
|
if (gethostname(temp, 1024) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (resolver_getip(temp, buff, len))
|
|
|
|
return buff;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_error
|
|
|
|
**
|
|
|
|
** returns the last socket error
|
|
|
|
*/
|
|
|
|
int sock_error(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return WSAGetLastError();
|
|
|
|
#else
|
|
|
|
return errno;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_recoverable
|
|
|
|
**
|
|
|
|
** determines if the socket error is recoverable
|
|
|
|
** in terms of non blocking sockets
|
|
|
|
*/
|
|
|
|
int sock_recoverable(int error)
|
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
return (error == 0 || error == EAGAIN || error == EINTR ||
|
|
|
|
error == EINPROGRESS || error == EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_stalled (int error)
|
|
|
|
{
|
|
|
|
return error == EAGAIN || error == EINPROGRESS || error == EWOULDBLOCK ||
|
|
|
|
error == EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_success (int error)
|
|
|
|
{
|
|
|
|
return error == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_connect_pending (int error)
|
|
|
|
{
|
|
|
|
return error == EINPROGRESS || error == EALREADY;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_valid_socket
|
|
|
|
**
|
|
|
|
** determines if a sock_t represents a valid socket
|
|
|
|
*/
|
|
|
|
int sock_valid_socket(sock_t sock)
|
|
|
|
{
|
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);
|
|
|
|
ret = getsockopt(sock, SOL_SOCKET, SO_TYPE, &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
|
|
|
}
|
|
|
|
|
|
|
|
/* inet_aton
|
|
|
|
**
|
|
|
|
** turns an ascii ip address into a binary representation
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
int inet_aton(const char *s, struct in_addr *a)
|
|
|
|
{
|
|
|
|
int lsb, b2, b3, msb;
|
|
|
|
|
|
|
|
if (sscanf(s, "%d.%d.%d.%d", &lsb, &b2, &b3, &msb) < 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
a->s_addr = inet_addr(s);
|
|
|
|
|
|
|
|
return (a->s_addr != INADDR_NONE);
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
/* sock_set_blocking
|
|
|
|
**
|
|
|
|
** set the sock blocking or nonblocking
|
|
|
|
** SOCK_BLOCK for blocking
|
|
|
|
** SOCK_NONBLOCK for nonblocking
|
|
|
|
*/
|
|
|
|
int sock_set_blocking(sock_t sock, const int block)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int varblock = block;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((!sock_valid_socket(sock)) || (block < 0) || (block > 1))
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
return ioctlsocket(sock, FIONBIO, &varblock);
|
|
|
|
#else
|
|
|
|
return fcntl(sock, F_SETFL, (block == SOCK_BLOCK) ? 0 : O_NONBLOCK);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-03-22 16:44:29 -05:00
|
|
|
int sock_set_nolinger(sock_t sock)
|
|
|
|
{
|
|
|
|
struct linger lin = { 0, 0 };
|
2002-11-22 08:02:51 -05:00
|
|
|
return setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&lin,
|
|
|
|
sizeof(struct linger));
|
2002-03-22 16:44:29 -05:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:08:00 -05:00
|
|
|
int sock_set_nodelay(sock_t sock)
|
|
|
|
{
|
|
|
|
int nodelay = 1;
|
|
|
|
|
2003-01-18 21:46:06 -05:00
|
|
|
return setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)nodelay,
|
2003-01-18 02:08:00 -05:00
|
|
|
sizeof(int));
|
|
|
|
}
|
|
|
|
|
2002-03-22 16:44:29 -05:00
|
|
|
int sock_set_keepalive(sock_t sock)
|
|
|
|
{
|
|
|
|
int keepalive = 1;
|
2002-11-22 08:02:51 -05:00
|
|
|
return setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive,
|
|
|
|
sizeof(int));
|
2002-03-22 16:44:29 -05:00
|
|
|
}
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
/* sock_close
|
|
|
|
**
|
|
|
|
** close the socket
|
|
|
|
*/
|
|
|
|
int sock_close(sock_t sock)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return closesocket(sock);
|
|
|
|
#else
|
|
|
|
return close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
/* sock_writev
|
|
|
|
*
|
|
|
|
* write multiple buffers at once, return bytes actually written
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_WRITEV
|
|
|
|
|
|
|
|
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count)
|
|
|
|
{
|
|
|
|
return writev (sock, iov, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
ssize_t sock_writev (int sock, const struct iovec *iov, const size_t count)
|
|
|
|
{
|
|
|
|
int i = count, accum = 0, ret;
|
|
|
|
const struct iovec *v = iov;
|
|
|
|
|
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
if (v->iov_base && v->iov_len)
|
|
|
|
{
|
|
|
|
ret = sock_write_bytes (sock, v->iov_base, v->iov_len);
|
|
|
|
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
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
/* sock_write_bytes
|
|
|
|
**
|
|
|
|
** 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
|
|
|
*/
|
2002-11-22 08:02:51 -05:00
|
|
|
int sock_write_bytes(sock_t sock, const void *buff, const size_t len)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
/* sanity check */
|
|
|
|
if (!buff) {
|
|
|
|
return SOCK_ERROR;
|
|
|
|
} else if (len <= 0) {
|
|
|
|
return SOCK_ERROR;
|
2002-07-31 11:00:12 -04:00
|
|
|
} /*else if (!sock_valid_socket(sock)) {
|
2001-09-09 22:24:39 -04:00
|
|
|
return SOCK_ERROR;
|
2002-07-31 11:00:12 -04:00
|
|
|
} */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-03-22 16:24:57 -05:00
|
|
|
return send(sock, buff, len, 0);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_write_string
|
|
|
|
**
|
|
|
|
** writes a string to a socket
|
|
|
|
** this function always blocks even if the socket is nonblocking
|
|
|
|
*/
|
|
|
|
int sock_write_string(sock_t sock, const char *buff)
|
|
|
|
{
|
|
|
|
return (sock_write_bytes(sock, buff, strlen(buff)) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_write
|
|
|
|
**
|
|
|
|
** write a formatted string to the socket
|
|
|
|
** this function will always block, even if the socket is nonblocking
|
|
|
|
** will truncate the string if it's greater than 1024 chars.
|
|
|
|
*/
|
|
|
|
int sock_write(sock_t sock, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buff[1024];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buff, 1024, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return sock_write_bytes(sock, buff, strlen(buff));
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_read_bytes(sock_t sock, char *buff, const int len)
|
|
|
|
{
|
|
|
|
|
2002-07-31 11:00:12 -04:00
|
|
|
/*if (!sock_valid_socket(sock)) return 0; */
|
2001-09-09 22:24:39 -04:00
|
|
|
if (!buff) return 0;
|
|
|
|
if (len <= 0) return 0;
|
|
|
|
|
|
|
|
return recv(sock, buff, len, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sock_read_line
|
|
|
|
**
|
|
|
|
** 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
|
|
|
|
*/
|
|
|
|
int sock_read_line(sock_t sock, char *buff, const int len)
|
|
|
|
{
|
|
|
|
char c = '\0';
|
|
|
|
int read_bytes, pos;
|
|
|
|
|
2002-07-31 11:00:12 -04:00
|
|
|
/*if (!sock_valid_socket(sock)) {
|
2001-09-09 22:24:39 -04:00
|
|
|
return 0;
|
2002-07-31 11:00:12 -04:00
|
|
|
} else*/ if (!buff) {
|
2001-09-09 22:24:39 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
/* see if a connection can be written to
|
|
|
|
** return -1 unable to check
|
|
|
|
** return 0 for not yet
|
|
|
|
** return 1 for ok
|
2001-09-09 22:24:39 -04:00
|
|
|
*/
|
2002-11-22 08:02:51 -05:00
|
|
|
int sock_connected (int sock, unsigned timeout)
|
|
|
|
{
|
|
|
|
fd_set wfds;
|
|
|
|
int val = SOCK_ERROR;
|
|
|
|
socklen_t size = sizeof val;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(sock, &wfds);
|
|
|
|
|
|
|
|
switch (select(sock + 1, NULL, &wfds, NULL, &tv))
|
|
|
|
{
|
|
|
|
case 0: return SOCK_TIMEOUT;
|
|
|
|
default: if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &val, &size) < 0)
|
|
|
|
val = SOCK_ERROR;
|
|
|
|
case -1: return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
|
|
|
|
int sock_connect_non_blocking (const char *hostname, const unsigned port)
|
|
|
|
{
|
|
|
|
int sock = SOCK_ERROR;
|
|
|
|
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))
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
ai = head;
|
|
|
|
while (ai)
|
|
|
|
{
|
|
|
|
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol))
|
|
|
|
> -1)
|
|
|
|
{
|
|
|
|
sock_set_blocking (sock, SOCK_NONBLOCK);
|
|
|
|
if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
|
|
|
|
!sock_connect_pending(sock_error()))
|
|
|
|
{
|
|
|
|
sock_close (sock);
|
|
|
|
sock = SOCK_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ai = ai->ai_next;
|
|
|
|
}
|
|
|
|
if (head) freeaddrinfo (head);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
|
|
|
|
{
|
2002-11-22 08:02:51 -05:00
|
|
|
int sock = SOCK_ERROR;
|
|
|
|
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))
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
ai = head;
|
|
|
|
while (ai)
|
|
|
|
{
|
|
|
|
if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol))
|
|
|
|
> -1)
|
|
|
|
{
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
sock_set_blocking (sock, SOCK_NONBLOCK);
|
|
|
|
if (connect (sock, ai->ai_addr, ai->ai_addrlen) < 0)
|
|
|
|
{
|
|
|
|
int ret = sock_connected (sock, timeout);
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
sock_close (sock);
|
|
|
|
sock = SOCK_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sock_set_blocking(sock, SOCK_BLOCK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (connect (sock, ai->ai_addr, ai->ai_addrlen) < 0)
|
|
|
|
{
|
|
|
|
sock_close (sock);
|
|
|
|
sock = SOCK_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ai = ai->ai_next;
|
|
|
|
}
|
|
|
|
if (head) freeaddrinfo (head);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
#else
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
int sock_try_connection (int sock, const char *hostname, const unsigned port)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
|
2003-02-13 06:29:07 -05:00
|
|
|
if (!resolver_getip(hostname, ip, MAX_ADDR_LEN))
|
2002-11-22 08:02:51 -05:00
|
|
|
{
|
|
|
|
sock_close (sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inet_aton(ip, (struct in_addr *)&sin.sin_addr) == 0)
|
|
|
|
{
|
|
|
|
sock_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&server.sin_addr, &sin.sin_addr, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(port);
|
|
|
|
|
|
|
|
return connect(sock, (struct sockaddr *)&server, sizeof(server));
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_connect_non_blocking (const char *hostname, const unsigned port)
|
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sock_set_blocking (sock, SOCK_NONBLOCK);
|
|
|
|
sock_try_connection (sock, hostname, port);
|
|
|
|
|
|
|
|
return sock;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-11-22 08:02:51 -05:00
|
|
|
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout)
|
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
sock_set_blocking (sock, SOCK_NONBLOCK);
|
|
|
|
if (sock_try_connection (sock, hostname, port) < 0)
|
|
|
|
{
|
|
|
|
int ret = sock_connected (sock, timeout);
|
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
sock_close (sock);
|
|
|
|
return SOCK_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sock_set_blocking(sock, SOCK_BLOCK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sock_try_connection (sock, hostname, port) < 0)
|
|
|
|
{
|
|
|
|
sock_close (sock);
|
|
|
|
sock = SOCK_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
/* sock_get_server_socket
|
|
|
|
**
|
|
|
|
** create a socket for incoming requests on a specified port and
|
|
|
|
** interface. if interface is null, listen on all interfaces.
|
|
|
|
** returns the socket, or SOCK_ERROR on failure
|
|
|
|
*/
|
2001-10-20 01:57:28 -04:00
|
|
|
sock_t sock_get_server_socket(const int port, char *sinterface)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
#else
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
#endif
|
|
|
|
int sa_family, sa_len, error, opt;
|
|
|
|
sock_t sock;
|
2003-02-13 06:29:07 -05:00
|
|
|
char ip[MAX_ADDR_LEN];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
/* defaults */
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa_family = AF_INET;
|
2002-03-22 16:09:36 -05:00
|
|
|
sa_len = sizeof(struct sockaddr_in);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* set the interface to bind to if specified */
|
2001-10-20 01:57:28 -04:00
|
|
|
if (sinterface != NULL) {
|
|
|
|
if (!resolver_getip(sinterface, ip, sizeof (ip)))
|
2001-09-09 22:24:39 -04:00
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (inet_pton(AF_INET, ip, &((struct sockaddr_in*)&sa)->sin_addr) > 0) {
|
|
|
|
((struct sockaddr_in*)&sa)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in*)&sa)->sin_port = htons(port);
|
2002-11-22 08:02:51 -05:00
|
|
|
} else if (inet_pton(AF_INET6, ip,
|
|
|
|
&((struct sockaddr_in6*)&sa)->sin6_addr) > 0) {
|
2001-09-09 22:24:39 -04:00
|
|
|
sa_family = AF_INET6;
|
|
|
|
sa_len = sizeof (struct sockaddr_in6);
|
|
|
|
((struct sockaddr_in6*)&sa)->sin6_family = AF_INET6;
|
|
|
|
((struct sockaddr_in6*)&sa)->sin6_port = htons(port);
|
2002-03-22 16:09:36 -05:00
|
|
|
} else {
|
2001-09-09 22:24:39 -04:00
|
|
|
return SOCK_ERROR;
|
2002-03-22 16:09:36 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2002-03-22 16:09:36 -05:00
|
|
|
if (!inet_aton(ip, &sa.sin_addr)) {
|
2001-09-09 22:24:39 -04:00
|
|
|
return SOCK_ERROR;
|
2002-03-22 16:09:36 -05:00
|
|
|
} else {
|
2001-09-09 22:24:39 -04:00
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(port);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
((struct sockaddr_in*)&sa)->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
((struct sockaddr_in*)&sa)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in*)&sa)->sin_port = htons(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a socket */
|
|
|
|
sock = socket(sa_family, SOCK_STREAM, 0);
|
|
|
|
if (sock == -1)
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
/* reuse it if we can */
|
|
|
|
opt = 1;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(int));
|
|
|
|
|
|
|
|
/* bind socket to port */
|
|
|
|
error = bind(sock, (struct sockaddr *)&sa, sa_len);
|
|
|
|
if (error == -1)
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_listen(sock_t serversock, int backlog)
|
|
|
|
{
|
|
|
|
if (!sock_valid_socket(serversock))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (backlog <= 0)
|
|
|
|
backlog = 10;
|
|
|
|
|
|
|
|
return (listen(serversock, backlog) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock_accept(sock_t serversock, char *ip, int len)
|
|
|
|
{
|
2003-02-13 06:29:07 -05:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_storage sa;
|
|
|
|
#else
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
int ret;
|
2002-11-22 08:02:51 -05:00
|
|
|
socklen_t slen;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
if (!sock_valid_socket(serversock))
|
|
|
|
return SOCK_ERROR;
|
|
|
|
|
2003-02-13 06:29:07 -05:00
|
|
|
slen = sizeof(sa);
|
|
|
|
ret = accept(serversock, (struct sockaddr *)&sa, &slen);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-03-22 16:44:29 -05:00
|
|
|
if (ret >= 0 && ip != NULL) {
|
2003-02-06 08:10:48 -05:00
|
|
|
/* inet_ntoa is not reentrant, we should protect this */
|
2003-02-13 06:29:07 -05:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if(inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr,
|
|
|
|
ip, len) <= 0)
|
|
|
|
{
|
|
|
|
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr,
|
|
|
|
ip, len);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
strncpy(ip, inet_ntoa(sa.sin_addr), len);
|
|
|
|
#endif
|
2002-03-22 16:44:29 -05:00
|
|
|
sock_set_nolinger(ret);
|
|
|
|
sock_set_keepalive(ret);
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2003-02-13 06:29:07 -05:00
|
|
|
|