2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* Windows networking abstraction.
|
2001-01-07 22:16:16 +03:00
|
|
|
*
|
|
|
|
* Due to this clean abstraction it was possible
|
|
|
|
* to easily implement IPv6 support :)
|
|
|
|
*
|
|
|
|
* IPv6 patch 1 (27 October 2000) Jeroen Massar <jeroen@unfix.org>
|
|
|
|
* - Preliminary hacked IPv6 support.
|
|
|
|
* - Connecting to IPv6 address (eg fec0:4242:4242:100:2d0:b7ff:fe8f:5d42) works.
|
|
|
|
* - Connecting to IPv6 hostname (eg heaven.ipv6.unfix.org) works.
|
|
|
|
* - Compiles as either IPv4 or IPv6.
|
|
|
|
*
|
|
|
|
* IPv6 patch 2 (29 October 2000) Jeroen Massar <jeroen@unfix.org>
|
|
|
|
* - When compiled as IPv6 it also allows connecting to IPv4 hosts.
|
|
|
|
* - Added some more documentation.
|
|
|
|
*
|
|
|
|
* IPv6 patch 3 (18 November 2000) Jeroen Massar <jeroen@unfix.org>
|
|
|
|
* - It now supports dynamically loading the IPv6 resolver dll's.
|
|
|
|
* This way we should be able to distribute one (1) binary
|
|
|
|
* which supports both IPv4 and IPv6.
|
|
|
|
* - getaddrinfo() and getnameinfo() are loaded dynamicaly if possible.
|
|
|
|
* - in6addr_any is defined in this file so we don't need to link to wship6.lib
|
|
|
|
* - The patch is now more unified so that we can still
|
|
|
|
* remove all IPv6 support by undef'ing IPV6.
|
|
|
|
* But where it fallsback to IPv4 it uses the IPv4 code which is already in place...
|
|
|
|
* - Canonical name resolving works.
|
|
|
|
*
|
|
|
|
* IPv6 patch 4 (07 January 2001) Jeroen Massar <jeroen@unfix.org>
|
|
|
|
* - patch against CVS of today, will be submitted to the bugs list
|
|
|
|
* as a 'cvs diff -u' on Simon's request...
|
|
|
|
*
|
2000-10-23 15:55:11 +04:00
|
|
|
*/
|
|
|
|
|
2001-01-07 22:16:16 +03:00
|
|
|
/*
|
|
|
|
* Define IPV6 to have IPv6 on-the-fly-loading support.
|
|
|
|
* This means that one doesn't have to have an IPv6 stack to use it.
|
|
|
|
* But if an IPv6 stack is found it is used with a fallback to IPv4.
|
|
|
|
*/
|
|
|
|
/* #define IPV6 1 */
|
|
|
|
|
|
|
|
#ifdef IPV6
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <tpipv6.h>
|
|
|
|
#else
|
2000-10-23 15:55:11 +04:00
|
|
|
#include <winsock.h>
|
2001-01-07 22:16:16 +03:00
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
2000-10-23 15:55:11 +04:00
|
|
|
#include <stdio.h>
|
2000-10-24 14:47:49 +04:00
|
|
|
#include <stdlib.h>
|
2001-08-25 21:09:23 +04:00
|
|
|
#include <assert.h>
|
2000-10-23 15:55:11 +04:00
|
|
|
|
2001-03-13 13:22:45 +03:00
|
|
|
#define DEFINE_PLUG_METHOD_MACROS
|
2000-10-23 15:55:11 +04:00
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "tree234.h"
|
|
|
|
|
2002-12-18 14:39:25 +03:00
|
|
|
#define ipv4_is_loopback(addr) \
|
|
|
|
((ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L)
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
struct Socket_tag {
|
2003-01-14 21:43:45 +03:00
|
|
|
const struct socket_function_table *fn;
|
2001-03-13 13:22:45 +03:00
|
|
|
/* the above variable absolutely *must* be the first in this structure */
|
2000-10-23 15:55:11 +04:00
|
|
|
char *error;
|
|
|
|
SOCKET s;
|
2001-03-13 13:22:45 +03:00
|
|
|
Plug plug;
|
2000-10-23 15:55:11 +04:00
|
|
|
void *private_ptr;
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain output_data;
|
2001-09-08 02:39:01 +04:00
|
|
|
int connected;
|
2000-10-23 15:55:11 +04:00
|
|
|
int writable;
|
2001-08-25 21:09:23 +04:00
|
|
|
int frozen; /* this causes readability notifications to be ignored */
|
|
|
|
int frozen_readable; /* this means we missed at least one readability
|
|
|
|
* notification while we were frozen */
|
2001-10-12 23:32:13 +04:00
|
|
|
int localhost_only; /* for listening sockets */
|
2001-08-25 21:09:23 +04:00
|
|
|
char oobdata[1];
|
2001-01-29 17:49:21 +03:00
|
|
|
int sending_oob;
|
2001-02-01 17:11:04 +03:00
|
|
|
int oobinline;
|
2001-10-28 12:57:47 +03:00
|
|
|
int pending_error; /* in case send() returns error */
|
2000-10-23 15:55:11 +04:00
|
|
|
};
|
|
|
|
|
2001-03-13 13:22:45 +03:00
|
|
|
/*
|
|
|
|
* We used to typedef struct Socket_tag *Socket.
|
|
|
|
*
|
|
|
|
* Since we have made the networking abstraction slightly more
|
|
|
|
* abstract, Socket no longer means a tcp socket (it could mean
|
|
|
|
* an ssl socket). So now we must use Actual_Socket when we know
|
|
|
|
* we are talking about a tcp socket.
|
|
|
|
*/
|
|
|
|
typedef struct Socket_tag *Actual_Socket;
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
struct SockAddr_tag {
|
|
|
|
char *error;
|
2002-12-18 19:23:11 +03:00
|
|
|
/*
|
|
|
|
* Which address family this address belongs to. AF_INET for
|
|
|
|
* IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
|
|
|
|
* resolution has not been done and a simple host name is held
|
|
|
|
* in this SockAddr structure.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
int family;
|
2001-01-07 22:16:16 +03:00
|
|
|
unsigned long address; /* Address IPv4 style. */
|
|
|
|
#ifdef IPV6
|
|
|
|
struct addrinfo *ai; /* Address IPv6 style. */
|
|
|
|
#endif
|
2002-12-18 19:23:11 +03:00
|
|
|
char hostname[512]; /* Store an unresolved host name. */
|
2000-10-23 15:55:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static tree234 *sktree;
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
static int cmpfortree(void *av, void *bv)
|
|
|
|
{
|
|
|
|
Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
|
|
|
|
unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s;
|
|
|
|
if (as < bs)
|
|
|
|
return -1;
|
|
|
|
if (as > bs)
|
|
|
|
return +1;
|
2000-10-23 15:55:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
static int cmpforsearch(void *av, void *bv)
|
|
|
|
{
|
|
|
|
Actual_Socket b = (Actual_Socket) bv;
|
|
|
|
unsigned long as = (unsigned long) av, bs = (unsigned long) b->s;
|
|
|
|
if (as < bs)
|
|
|
|
return -1;
|
|
|
|
if (as > bs)
|
|
|
|
return +1;
|
2000-10-23 15:55:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
void sk_init(void)
|
|
|
|
{
|
2000-10-23 15:55:11 +04:00
|
|
|
sktree = newtree234(cmpfortree);
|
|
|
|
}
|
|
|
|
|
2002-03-06 23:13:22 +03:00
|
|
|
void sk_cleanup(void)
|
|
|
|
{
|
|
|
|
Actual_Socket s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sktree) {
|
|
|
|
for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
|
|
|
|
closesocket(s->s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
char *winsock_error_string(int error)
|
|
|
|
{
|
2000-12-18 12:20:26 +03:00
|
|
|
switch (error) {
|
2001-05-06 18:35:20 +04:00
|
|
|
case WSAEACCES:
|
|
|
|
return "Network error: Permission denied";
|
|
|
|
case WSAEADDRINUSE:
|
|
|
|
return "Network error: Address already in use";
|
|
|
|
case WSAEADDRNOTAVAIL:
|
|
|
|
return "Network error: Cannot assign requested address";
|
|
|
|
case WSAEAFNOSUPPORT:
|
|
|
|
return
|
|
|
|
"Network error: Address family not supported by protocol family";
|
|
|
|
case WSAEALREADY:
|
|
|
|
return "Network error: Operation already in progress";
|
|
|
|
case WSAECONNABORTED:
|
|
|
|
return "Network error: Software caused connection abort";
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
return "Network error: Connection refused";
|
|
|
|
case WSAECONNRESET:
|
|
|
|
return "Network error: Connection reset by peer";
|
|
|
|
case WSAEDESTADDRREQ:
|
|
|
|
return "Network error: Destination address required";
|
|
|
|
case WSAEFAULT:
|
|
|
|
return "Network error: Bad address";
|
|
|
|
case WSAEHOSTDOWN:
|
|
|
|
return "Network error: Host is down";
|
|
|
|
case WSAEHOSTUNREACH:
|
|
|
|
return "Network error: No route to host";
|
|
|
|
case WSAEINPROGRESS:
|
|
|
|
return "Network error: Operation now in progress";
|
|
|
|
case WSAEINTR:
|
|
|
|
return "Network error: Interrupted function call";
|
|
|
|
case WSAEINVAL:
|
|
|
|
return "Network error: Invalid argument";
|
|
|
|
case WSAEISCONN:
|
|
|
|
return "Network error: Socket is already connected";
|
|
|
|
case WSAEMFILE:
|
|
|
|
return "Network error: Too many open files";
|
|
|
|
case WSAEMSGSIZE:
|
|
|
|
return "Network error: Message too long";
|
|
|
|
case WSAENETDOWN:
|
|
|
|
return "Network error: Network is down";
|
|
|
|
case WSAENETRESET:
|
|
|
|
return "Network error: Network dropped connection on reset";
|
|
|
|
case WSAENETUNREACH:
|
|
|
|
return "Network error: Network is unreachable";
|
|
|
|
case WSAENOBUFS:
|
|
|
|
return "Network error: No buffer space available";
|
|
|
|
case WSAENOPROTOOPT:
|
|
|
|
return "Network error: Bad protocol option";
|
|
|
|
case WSAENOTCONN:
|
|
|
|
return "Network error: Socket is not connected";
|
|
|
|
case WSAENOTSOCK:
|
|
|
|
return "Network error: Socket operation on non-socket";
|
|
|
|
case WSAEOPNOTSUPP:
|
|
|
|
return "Network error: Operation not supported";
|
|
|
|
case WSAEPFNOSUPPORT:
|
|
|
|
return "Network error: Protocol family not supported";
|
|
|
|
case WSAEPROCLIM:
|
|
|
|
return "Network error: Too many processes";
|
|
|
|
case WSAEPROTONOSUPPORT:
|
|
|
|
return "Network error: Protocol not supported";
|
|
|
|
case WSAEPROTOTYPE:
|
|
|
|
return "Network error: Protocol wrong type for socket";
|
|
|
|
case WSAESHUTDOWN:
|
|
|
|
return "Network error: Cannot send after socket shutdown";
|
|
|
|
case WSAESOCKTNOSUPPORT:
|
|
|
|
return "Network error: Socket type not supported";
|
|
|
|
case WSAETIMEDOUT:
|
|
|
|
return "Network error: Connection timed out";
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
return "Network error: Resource temporarily unavailable";
|
|
|
|
case WSAEDISCON:
|
|
|
|
return "Network error: Graceful shutdown in progress";
|
|
|
|
default:
|
|
|
|
return "Unknown network error";
|
2000-12-18 12:20:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-12 18:26:10 +03:00
|
|
|
SockAddr sk_namelookup(const char *host, char **canonicalname)
|
2001-01-07 22:16:16 +03:00
|
|
|
{
|
2003-03-29 19:14:26 +03:00
|
|
|
SockAddr ret = snew(struct SockAddr_tag);
|
2000-10-23 15:55:11 +04:00
|
|
|
unsigned long a;
|
2001-01-07 22:16:16 +03:00
|
|
|
struct hostent *h = NULL;
|
2001-05-09 18:01:15 +04:00
|
|
|
char realhost[8192];
|
2000-10-23 15:55:11 +04:00
|
|
|
|
2001-01-07 22:16:16 +03:00
|
|
|
/* Clear the structure and default to IPv4. */
|
|
|
|
memset(ret, 0, sizeof(struct SockAddr_tag));
|
|
|
|
ret->family = 0; /* We set this one when we have resolved the host. */
|
2001-05-09 18:01:15 +04:00
|
|
|
*realhost = '\0';
|
2001-01-07 22:16:16 +03:00
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
|
2001-01-07 22:16:16 +03:00
|
|
|
#ifdef IPV6
|
|
|
|
|
|
|
|
/* Try to get the getaddrinfo() function from wship6.dll */
|
|
|
|
/* This way one doesn't need to have IPv6 dll's to use PuTTY and
|
|
|
|
* it will fallback to IPv4. */
|
2001-05-06 18:35:20 +04:00
|
|
|
typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
|
|
|
|
const char *servname,
|
|
|
|
const struct addrinfo *
|
|
|
|
hints,
|
|
|
|
struct addrinfo ** res);
|
2001-01-07 22:16:16 +03:00
|
|
|
FGETADDRINFO fGetAddrInfo = NULL;
|
|
|
|
|
|
|
|
HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll");
|
|
|
|
if (dllWSHIP6)
|
2001-05-06 18:35:20 +04:00
|
|
|
fGetAddrInfo = (FGETADDRINFO) GetProcAddress(dllWSHIP6,
|
|
|
|
"getaddrinfo");
|
2001-01-07 22:16:16 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use fGetAddrInfo when it's available (which usually also
|
|
|
|
* means IPv6 is installed...)
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
if (fGetAddrInfo) {
|
2001-01-07 22:16:16 +03:00
|
|
|
/*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */
|
|
|
|
if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0)
|
|
|
|
ret->family = ret->ai->ai_family;
|
2001-05-06 18:35:20 +04:00
|
|
|
} else
|
2001-01-07 22:16:16 +03:00
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-01-07 22:16:16 +03:00
|
|
|
/*
|
|
|
|
* Otherwise use the IPv4-only gethostbyname...
|
|
|
|
* (NOTE: we don't use gethostbyname as a
|
|
|
|
* fallback!)
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
if (ret->family == 0) {
|
|
|
|
/*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
|
2001-05-13 18:02:28 +04:00
|
|
|
if ( (h = gethostbyname(host)) )
|
2001-05-06 18:35:20 +04:00
|
|
|
ret->family = AF_INET;
|
|
|
|
}
|
2001-01-07 22:16:16 +03:00
|
|
|
}
|
|
|
|
/*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
if (ret->family == 0) {
|
2000-10-23 15:55:11 +04:00
|
|
|
DWORD err = WSAGetLastError();
|
2001-01-07 22:16:16 +03:00
|
|
|
ret->error = (err == WSAENETDOWN ? "Network is down" :
|
2001-05-06 18:35:20 +04:00
|
|
|
err ==
|
|
|
|
WSAHOST_NOT_FOUND ? "Host does not exist" : err
|
|
|
|
== WSATRY_AGAIN ? "Host not found" :
|
2001-01-07 22:16:16 +03:00
|
|
|
#ifdef IPV6
|
|
|
|
fGetAddrInfo ? "getaddrinfo: unknown error" :
|
|
|
|
#endif
|
|
|
|
"gethostbyname: unknown error");
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
2001-05-06 18:35:20 +04:00
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR) & lpMsgBuf, 0, NULL);
|
|
|
|
/*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */
|
2001-01-07 22:16:16 +03:00
|
|
|
/* Free the buffer. */
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
}
|
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
} else {
|
2001-01-07 22:16:16 +03:00
|
|
|
ret->error = NULL;
|
|
|
|
|
|
|
|
#ifdef IPV6
|
|
|
|
/* If we got an address info use that... */
|
2001-05-06 18:35:20 +04:00
|
|
|
if (ret->ai) {
|
|
|
|
typedef int (CALLBACK * FGETNAMEINFO)
|
|
|
|
(const struct sockaddr FAR * sa, socklen_t salen,
|
|
|
|
char FAR * host, size_t hostlen, char FAR * serv,
|
|
|
|
size_t servlen, int flags);
|
2001-01-07 22:16:16 +03:00
|
|
|
FGETNAMEINFO fGetNameInfo = NULL;
|
|
|
|
|
|
|
|
/* Are we in IPv4 fallback mode? */
|
|
|
|
/* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
|
|
|
|
if (ret->family == AF_INET)
|
2001-05-06 18:35:20 +04:00
|
|
|
memcpy(&a,
|
|
|
|
(char *) &((SOCKADDR_IN *) ret->ai->
|
|
|
|
ai_addr)->sin_addr, sizeof(a));
|
2001-01-07 22:16:16 +03:00
|
|
|
|
|
|
|
/* Now let's find that canonicalname... */
|
2001-05-06 18:35:20 +04:00
|
|
|
if ((dllWSHIP6)
|
|
|
|
&& (fGetNameInfo =
|
|
|
|
(FGETNAMEINFO) GetProcAddress(dllWSHIP6,
|
|
|
|
"getnameinfo"))) {
|
|
|
|
if (fGetNameInfo
|
|
|
|
((struct sockaddr *) ret->ai->ai_addr,
|
|
|
|
ret->family ==
|
|
|
|
AF_INET ? sizeof(SOCKADDR_IN) :
|
2001-05-09 18:01:15 +04:00
|
|
|
sizeof(SOCKADDR_IN6), realhost,
|
|
|
|
sizeof(realhost), NULL, 0, 0) != 0) {
|
|
|
|
strncpy(realhost, host, sizeof(realhost));
|
2001-01-07 22:16:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* We used the IPv4-only gethostbyname()... */
|
|
|
|
else
|
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-01-07 22:16:16 +03:00
|
|
|
memcpy(&a, h->h_addr, sizeof(a));
|
|
|
|
/* This way we are always sure the h->h_name is valid :) */
|
2001-05-09 18:01:15 +04:00
|
|
|
strncpy(realhost, h->h_name, sizeof(realhost));
|
2001-01-07 22:16:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef IPV6
|
|
|
|
FreeLibrary(dllWSHIP6);
|
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
} else {
|
2001-04-28 15:24:45 +04:00
|
|
|
/*
|
|
|
|
* This must be a numeric IPv4 address because it caused a
|
|
|
|
* success return from inet_addr.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
ret->family = AF_INET;
|
2001-05-09 18:01:15 +04:00
|
|
|
strncpy(realhost, host, sizeof(realhost));
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
ret->address = ntohl(a);
|
2001-05-09 18:01:15 +04:00
|
|
|
realhost[lenof(realhost)-1] = '\0';
|
2003-03-29 19:14:26 +03:00
|
|
|
*canonicalname = snewn(1+strlen(realhost), char);
|
2001-05-09 18:01:15 +04:00
|
|
|
strcpy(*canonicalname, realhost);
|
2000-10-23 15:55:11 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-01-12 18:26:10 +03:00
|
|
|
SockAddr sk_nonamelookup(const char *host)
|
2002-12-18 19:23:11 +03:00
|
|
|
{
|
2003-03-29 19:14:26 +03:00
|
|
|
SockAddr ret = snew(struct SockAddr_tag);
|
2003-01-02 13:07:50 +03:00
|
|
|
ret->error = NULL;
|
2002-12-18 19:23:11 +03:00
|
|
|
ret->family = AF_UNSPEC;
|
|
|
|
strncpy(ret->hostname, host, lenof(ret->hostname));
|
|
|
|
ret->hostname[lenof(ret->hostname)-1] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-09-08 02:39:01 +04:00
|
|
|
void sk_getaddr(SockAddr addr, char *buf, int buflen)
|
|
|
|
{
|
|
|
|
#ifdef IPV6
|
2002-12-18 19:23:11 +03:00
|
|
|
if (addr->family == AF_INET6) {
|
|
|
|
FIXME; /* I don't know how to get a text form of an IPv6 address. */
|
|
|
|
} else
|
2001-09-08 02:39:01 +04:00
|
|
|
#endif
|
2002-12-18 19:23:11 +03:00
|
|
|
if (addr->family == AF_INET) {
|
2001-09-08 02:39:01 +04:00
|
|
|
struct in_addr a;
|
|
|
|
a.s_addr = htonl(addr->address);
|
|
|
|
strncpy(buf, inet_ntoa(a), buflen);
|
2002-12-18 19:23:11 +03:00
|
|
|
buf[buflen-1] = '\0';
|
2001-09-08 02:39:01 +04:00
|
|
|
} else {
|
2002-12-18 19:23:11 +03:00
|
|
|
assert(addr->family == AF_UNSPEC);
|
|
|
|
strncpy(buf, addr->hostname, buflen);
|
|
|
|
buf[buflen-1] = '\0';
|
2001-09-08 02:39:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-18 15:18:54 +03:00
|
|
|
int sk_hostname_is_local(char *name)
|
|
|
|
{
|
|
|
|
return !strcmp(name, "localhost");
|
|
|
|
}
|
|
|
|
|
|
|
|
int sk_address_is_local(SockAddr addr)
|
|
|
|
{
|
|
|
|
#ifdef IPV6
|
2002-12-18 19:23:11 +03:00
|
|
|
if (addr->family == AF_INET6) {
|
|
|
|
FIXME; /* someone who can compile for IPV6 had better do this bit */
|
|
|
|
} else
|
2002-12-18 15:18:54 +03:00
|
|
|
#endif
|
2002-12-18 19:23:11 +03:00
|
|
|
if (addr->family == AF_INET) {
|
2002-12-18 15:18:54 +03:00
|
|
|
struct in_addr a;
|
|
|
|
a.s_addr = htonl(addr->address);
|
|
|
|
return ipv4_is_loopback(a);
|
|
|
|
} else {
|
2002-12-18 19:23:11 +03:00
|
|
|
assert(addr->family == AF_UNSPEC);
|
|
|
|
return 0; /* we don't know; assume not */
|
2002-12-18 15:18:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-27 19:01:18 +04:00
|
|
|
int sk_addrtype(SockAddr addr)
|
|
|
|
{
|
2002-12-18 19:23:11 +03:00
|
|
|
return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
|
|
|
|
#ifdef IPV6
|
|
|
|
addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
|
|
|
|
#endif
|
|
|
|
ADDRTYPE_NAME);
|
2002-04-27 19:01:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void sk_addrcopy(SockAddr addr, char *buf)
|
|
|
|
{
|
2002-12-18 19:23:11 +03:00
|
|
|
assert(addr->family != AF_UNSPEC);
|
2002-04-27 19:01:18 +04:00
|
|
|
#ifdef IPV6
|
2002-12-18 19:23:11 +03:00
|
|
|
if (addr->family == AF_INET6) {
|
|
|
|
memcpy(buf, (char*) addr->ai, 16);
|
|
|
|
} else
|
2002-04-27 19:01:18 +04:00
|
|
|
#endif
|
2002-12-18 19:23:11 +03:00
|
|
|
if (addr->family == AF_INET) {
|
2002-04-27 19:01:18 +04:00
|
|
|
struct in_addr a;
|
|
|
|
a.s_addr = htonl(addr->address);
|
2002-09-20 21:54:17 +04:00
|
|
|
memcpy(buf, (char*) &a.s_addr, 4);
|
2002-04-27 19:01:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
void sk_addr_free(SockAddr addr)
|
|
|
|
{
|
2000-10-23 15:55:11 +04:00
|
|
|
sfree(addr);
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
static Plug sk_tcp_plug(Socket sock, Plug p)
|
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
|
|
|
Plug ret = s->plug;
|
2001-05-06 18:35:20 +04:00
|
|
|
if (p)
|
|
|
|
s->plug = p;
|
2001-03-13 13:22:45 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sk_tcp_flush(Socket s)
|
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
/*
|
|
|
|
* We send data to the socket as soon as we can anyway,
|
|
|
|
* so we don't need to do anything here. :-)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2001-05-13 18:02:28 +04:00
|
|
|
static void sk_tcp_close(Socket s);
|
2003-01-10 21:33:35 +03:00
|
|
|
static int sk_tcp_write(Socket s, const char *data, int len);
|
|
|
|
static int sk_tcp_write_oob(Socket s, const char *data, int len);
|
2002-03-23 20:47:21 +03:00
|
|
|
static void sk_tcp_set_private_ptr(Socket s, void *ptr);
|
|
|
|
static void *sk_tcp_get_private_ptr(Socket s);
|
|
|
|
static void sk_tcp_set_frozen(Socket s, int is_frozen);
|
2001-05-13 18:02:28 +04:00
|
|
|
static char *sk_tcp_socket_error(Socket s);
|
2001-03-13 13:22:45 +03:00
|
|
|
|
2001-08-09 00:44:35 +04:00
|
|
|
extern char *do_select(SOCKET skt, int startup);
|
|
|
|
|
|
|
|
Socket sk_register(void *sock, Plug plug)
|
|
|
|
{
|
2003-01-14 21:43:45 +03:00
|
|
|
static const struct socket_function_table fn_table = {
|
2001-08-09 00:44:35 +04:00
|
|
|
sk_tcp_plug,
|
|
|
|
sk_tcp_close,
|
|
|
|
sk_tcp_write,
|
|
|
|
sk_tcp_write_oob,
|
|
|
|
sk_tcp_flush,
|
2002-03-23 20:47:21 +03:00
|
|
|
sk_tcp_set_private_ptr,
|
|
|
|
sk_tcp_get_private_ptr,
|
|
|
|
sk_tcp_set_frozen,
|
2001-08-09 00:44:35 +04:00
|
|
|
sk_tcp_socket_error
|
|
|
|
};
|
|
|
|
|
|
|
|
DWORD err;
|
|
|
|
char *errstr;
|
|
|
|
Actual_Socket ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create Socket structure.
|
|
|
|
*/
|
2003-03-29 19:14:26 +03:00
|
|
|
ret = snew(struct Socket_tag);
|
2001-08-09 00:44:35 +04:00
|
|
|
ret->fn = &fn_table;
|
|
|
|
ret->error = NULL;
|
|
|
|
ret->plug = plug;
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain_init(&ret->output_data);
|
2001-08-09 00:44:35 +04:00
|
|
|
ret->writable = 1; /* to start with */
|
|
|
|
ret->sending_oob = 0;
|
|
|
|
ret->frozen = 1;
|
2001-08-25 21:09:23 +04:00
|
|
|
ret->frozen_readable = 0;
|
2001-10-12 23:32:13 +04:00
|
|
|
ret->localhost_only = 0; /* unused, but best init anyway */
|
2001-10-28 12:57:47 +03:00
|
|
|
ret->pending_error = 0;
|
2001-08-09 00:44:35 +04:00
|
|
|
|
|
|
|
ret->s = (SOCKET)sock;
|
|
|
|
|
|
|
|
if (ret->s == INVALID_SOCKET) {
|
|
|
|
err = WSAGetLastError();
|
|
|
|
ret->error = winsock_error_string(err);
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->oobinline = 0;
|
|
|
|
|
|
|
|
/* Set up a select mechanism. This could be an AsyncSelect on a
|
|
|
|
* window, or an EventSelect on an event object. */
|
|
|
|
errstr = do_select(ret->s, 1);
|
|
|
|
if (errstr) {
|
|
|
|
ret->error = errstr;
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
add234(sktree, ret);
|
|
|
|
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
2001-02-01 17:11:04 +03:00
|
|
|
Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
|
2001-11-30 00:47:11 +03:00
|
|
|
int nodelay, Plug plug)
|
2001-03-13 13:22:45 +03:00
|
|
|
{
|
2003-01-14 21:43:45 +03:00
|
|
|
static const struct socket_function_table fn_table = {
|
2001-03-13 13:22:45 +03:00
|
|
|
sk_tcp_plug,
|
|
|
|
sk_tcp_close,
|
|
|
|
sk_tcp_write,
|
|
|
|
sk_tcp_write_oob,
|
|
|
|
sk_tcp_flush,
|
2002-03-23 20:47:21 +03:00
|
|
|
sk_tcp_set_private_ptr,
|
|
|
|
sk_tcp_get_private_ptr,
|
|
|
|
sk_tcp_set_frozen,
|
2001-03-13 13:22:45 +03:00
|
|
|
sk_tcp_socket_error
|
|
|
|
};
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
SOCKET s;
|
2001-01-07 22:16:16 +03:00
|
|
|
#ifdef IPV6
|
|
|
|
SOCKADDR_IN6 a6;
|
|
|
|
#endif
|
2000-10-23 15:55:11 +04:00
|
|
|
SOCKADDR_IN a;
|
|
|
|
DWORD err;
|
|
|
|
char *errstr;
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket ret;
|
2001-01-19 13:10:37 +03:00
|
|
|
short localport;
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create Socket structure.
|
|
|
|
*/
|
2003-03-29 19:14:26 +03:00
|
|
|
ret = snew(struct Socket_tag);
|
2001-03-13 13:22:45 +03:00
|
|
|
ret->fn = &fn_table;
|
2000-10-23 15:55:11 +04:00
|
|
|
ret->error = NULL;
|
2001-03-13 13:22:45 +03:00
|
|
|
ret->plug = plug;
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain_init(&ret->output_data);
|
2001-09-08 02:39:01 +04:00
|
|
|
ret->connected = 0; /* to start with */
|
|
|
|
ret->writable = 0; /* to start with */
|
2000-10-23 16:31:54 +04:00
|
|
|
ret->sending_oob = 0;
|
2001-08-09 00:44:35 +04:00
|
|
|
ret->frozen = 0;
|
2001-08-25 21:09:23 +04:00
|
|
|
ret->frozen_readable = 0;
|
2001-10-12 23:32:13 +04:00
|
|
|
ret->localhost_only = 0; /* unused, but best init anyway */
|
2001-10-28 12:57:47 +03:00
|
|
|
ret->pending_error = 0;
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
2002-12-18 19:23:11 +03:00
|
|
|
assert(addr->family != AF_UNSPEC);
|
2001-01-07 22:16:16 +03:00
|
|
|
s = socket(addr->family, SOCK_STREAM, 0);
|
2000-10-23 15:55:11 +04:00
|
|
|
ret->s = s;
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
err = WSAGetLastError();
|
2001-05-06 18:35:20 +04:00
|
|
|
ret->error = winsock_error_string(err);
|
2001-03-13 13:22:45 +03:00
|
|
|
return (Socket) ret;
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
2001-02-01 17:11:04 +03:00
|
|
|
|
|
|
|
ret->oobinline = oobinline;
|
|
|
|
if (oobinline) {
|
2001-01-29 17:49:21 +03:00
|
|
|
BOOL b = TRUE;
|
2001-05-06 18:35:20 +04:00
|
|
|
setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
|
2001-01-29 17:49:21 +03:00
|
|
|
}
|
2000-10-23 15:55:11 +04:00
|
|
|
|
2001-11-30 00:47:11 +03:00
|
|
|
if (nodelay) {
|
|
|
|
BOOL b = TRUE;
|
|
|
|
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
|
|
|
|
}
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* Bind to local address.
|
|
|
|
*/
|
2001-01-19 13:10:37 +03:00
|
|
|
if (privport)
|
2001-05-06 18:35:20 +04:00
|
|
|
localport = 1023; /* count from 1023 downwards */
|
2001-01-07 22:16:16 +03:00
|
|
|
else
|
2001-05-06 18:35:20 +04:00
|
|
|
localport = 0; /* just use port 0 (ie winsock picks) */
|
2001-01-19 13:10:37 +03:00
|
|
|
|
|
|
|
/* Loop round trying to bind */
|
|
|
|
while (1) {
|
2001-05-06 18:35:20 +04:00
|
|
|
int retcode;
|
2001-01-19 13:10:37 +03:00
|
|
|
|
|
|
|
#ifdef IPV6
|
2001-05-06 18:35:20 +04:00
|
|
|
if (addr->family == AF_INET6) {
|
|
|
|
memset(&a6, 0, sizeof(a6));
|
|
|
|
a6.sin6_family = AF_INET6;
|
|
|
|
/*a6.sin6_addr = in6addr_any; *//* == 0 */
|
|
|
|
a6.sin6_port = htons(localport);
|
|
|
|
} else
|
2001-01-07 22:16:16 +03:00
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
|
|
|
a.sin_family = AF_INET;
|
|
|
|
a.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
a.sin_port = htons(localport);
|
|
|
|
}
|
2001-01-07 22:16:16 +03:00
|
|
|
#ifdef IPV6
|
2001-05-06 18:35:20 +04:00
|
|
|
retcode = bind(s, (addr->family == AF_INET6 ?
|
|
|
|
(struct sockaddr *) &a6 :
|
|
|
|
(struct sockaddr *) &a),
|
|
|
|
(addr->family ==
|
|
|
|
AF_INET6 ? sizeof(a6) : sizeof(a)));
|
2001-01-07 22:16:16 +03:00
|
|
|
#else
|
2001-05-06 18:35:20 +04:00
|
|
|
retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
|
2001-01-07 22:16:16 +03:00
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
if (retcode != SOCKET_ERROR) {
|
|
|
|
err = 0;
|
|
|
|
break; /* done */
|
|
|
|
} else {
|
|
|
|
err = WSAGetLastError();
|
|
|
|
if (err != WSAEADDRINUSE) /* failed, for a bad reason */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localport == 0)
|
|
|
|
break; /* we're only looping once */
|
|
|
|
localport--;
|
|
|
|
if (localport == 0)
|
|
|
|
break; /* we might have got to the end */
|
2001-01-19 13:10:37 +03:00
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
if (err) {
|
2001-01-07 22:16:16 +03:00
|
|
|
ret->error = winsock_error_string(err);
|
2001-03-13 13:22:45 +03:00
|
|
|
return (Socket) ret;
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to remote address.
|
|
|
|
*/
|
2001-01-07 22:16:16 +03:00
|
|
|
#ifdef IPV6
|
2001-05-06 18:35:20 +04:00
|
|
|
if (addr->family == AF_INET6) {
|
|
|
|
memset(&a, 0, sizeof(a));
|
2001-01-07 22:16:16 +03:00
|
|
|
a6.sin6_family = AF_INET6;
|
2001-05-06 18:35:20 +04:00
|
|
|
a6.sin6_port = htons((short) port);
|
|
|
|
a6.sin6_addr =
|
|
|
|
((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
|
|
|
|
} else
|
2001-01-07 22:16:16 +03:00
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-01-07 22:16:16 +03:00
|
|
|
a.sin_family = AF_INET;
|
|
|
|
a.sin_addr.s_addr = htonl(addr->address);
|
2001-05-06 18:35:20 +04:00
|
|
|
a.sin_port = htons((short) port);
|
2001-01-07 22:16:16 +03:00
|
|
|
}
|
2001-09-08 02:39:01 +04:00
|
|
|
|
|
|
|
/* Set up a select mechanism. This could be an AsyncSelect on a
|
|
|
|
* window, or an EventSelect on an event object. */
|
|
|
|
errstr = do_select(s, 1);
|
|
|
|
if (errstr) {
|
|
|
|
ret->error = errstr;
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
if ((
|
|
|
|
#ifdef IPV6
|
|
|
|
connect(s, ((addr->family == AF_INET6) ?
|
|
|
|
(struct sockaddr *) &a6 : (struct sockaddr *) &a),
|
|
|
|
(addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
|
2001-01-07 22:16:16 +03:00
|
|
|
#else
|
2001-05-06 18:35:20 +04:00
|
|
|
connect(s, (struct sockaddr *) &a, sizeof(a))
|
2001-01-07 22:16:16 +03:00
|
|
|
#endif
|
2001-05-06 18:35:20 +04:00
|
|
|
) == SOCKET_ERROR) {
|
2000-10-23 15:55:11 +04:00
|
|
|
err = WSAGetLastError();
|
2001-09-08 02:39:01 +04:00
|
|
|
/*
|
|
|
|
* We expect a potential EWOULDBLOCK here, because the
|
|
|
|
* chances are the front end has done a select for
|
|
|
|
* FD_CONNECT, so that connect() will complete
|
|
|
|
* asynchronously.
|
|
|
|
*/
|
|
|
|
if ( err != WSAEWOULDBLOCK ) {
|
|
|
|
ret->error = winsock_error_string(err);
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we _don't_ get EWOULDBLOCK, the connect has completed
|
|
|
|
* and we should set the socket as writable.
|
|
|
|
*/
|
|
|
|
ret->writable = 1;
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
add234(sktree, ret);
|
|
|
|
|
2001-03-13 13:22:45 +03:00
|
|
|
return (Socket) ret;
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
2002-12-18 14:39:25 +03:00
|
|
|
Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only)
|
2001-08-09 00:44:35 +04:00
|
|
|
{
|
2003-01-14 21:43:45 +03:00
|
|
|
static const struct socket_function_table fn_table = {
|
2001-08-09 00:44:35 +04:00
|
|
|
sk_tcp_plug,
|
|
|
|
sk_tcp_close,
|
|
|
|
sk_tcp_write,
|
|
|
|
sk_tcp_write_oob,
|
|
|
|
sk_tcp_flush,
|
2002-03-23 20:47:21 +03:00
|
|
|
sk_tcp_set_private_ptr,
|
|
|
|
sk_tcp_get_private_ptr,
|
|
|
|
sk_tcp_set_frozen,
|
2001-08-09 00:44:35 +04:00
|
|
|
sk_tcp_socket_error
|
|
|
|
};
|
|
|
|
|
|
|
|
SOCKET s;
|
|
|
|
#ifdef IPV6
|
|
|
|
SOCKADDR_IN6 a6;
|
|
|
|
#endif
|
|
|
|
SOCKADDR_IN a;
|
|
|
|
DWORD err;
|
|
|
|
char *errstr;
|
|
|
|
Actual_Socket ret;
|
|
|
|
int retcode;
|
|
|
|
int on = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create Socket structure.
|
|
|
|
*/
|
2003-03-29 19:14:26 +03:00
|
|
|
ret = snew(struct Socket_tag);
|
2001-08-09 00:44:35 +04:00
|
|
|
ret->fn = &fn_table;
|
|
|
|
ret->error = NULL;
|
|
|
|
ret->plug = plug;
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain_init(&ret->output_data);
|
2001-08-09 00:44:35 +04:00
|
|
|
ret->writable = 0; /* to start with */
|
|
|
|
ret->sending_oob = 0;
|
|
|
|
ret->frozen = 0;
|
2001-08-25 21:09:23 +04:00
|
|
|
ret->frozen_readable = 0;
|
2001-10-12 23:32:13 +04:00
|
|
|
ret->localhost_only = local_host_only;
|
2001-10-28 12:57:47 +03:00
|
|
|
ret->pending_error = 0;
|
2001-08-09 00:44:35 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
|
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
ret->s = s;
|
|
|
|
|
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
err = WSAGetLastError();
|
|
|
|
ret->error = winsock_error_string(err);
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->oobinline = 0;
|
|
|
|
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
|
|
|
|
|
|
|
|
#ifdef IPV6
|
|
|
|
if (addr->family == AF_INET6) {
|
|
|
|
memset(&a6, 0, sizeof(a6));
|
|
|
|
a6.sin6_family = AF_INET6;
|
2002-12-18 14:39:25 +03:00
|
|
|
/* FIXME: srcaddr is ignored for IPv6, because I (SGT) don't
|
|
|
|
* know how to do it. :-) */
|
2001-08-27 19:59:37 +04:00
|
|
|
if (local_host_only)
|
|
|
|
a6.sin6_addr = in6addr_loopback;
|
|
|
|
else
|
|
|
|
a6.sin6_addr = in6addr_any;
|
2001-08-09 00:44:35 +04:00
|
|
|
a6.sin6_port = htons(port);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2002-12-18 14:39:25 +03:00
|
|
|
int got_addr = 0;
|
2001-08-09 00:44:35 +04:00
|
|
|
a.sin_family = AF_INET;
|
2002-12-18 14:39:25 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind to source address. First try an explicitly
|
|
|
|
* specified one...
|
|
|
|
*/
|
|
|
|
if (srcaddr) {
|
|
|
|
a.sin_addr.s_addr = inet_addr(srcaddr);
|
|
|
|
if (a.sin_addr.s_addr != INADDR_NONE) {
|
|
|
|
/* Override localhost_only with specified listen addr. */
|
|
|
|
ret->localhost_only = ipv4_is_loopback(a.sin_addr);
|
|
|
|
got_addr = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ... and failing that, go with one of the standard ones.
|
|
|
|
*/
|
|
|
|
if (!got_addr) {
|
|
|
|
if (local_host_only)
|
|
|
|
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
else
|
|
|
|
a.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
}
|
|
|
|
|
2001-08-09 00:44:35 +04:00
|
|
|
a.sin_port = htons((short)port);
|
|
|
|
}
|
|
|
|
#ifdef IPV6
|
|
|
|
retcode = bind(s, (addr->family == AF_INET6 ?
|
|
|
|
(struct sockaddr *) &a6 :
|
|
|
|
(struct sockaddr *) &a),
|
|
|
|
(addr->family ==
|
|
|
|
AF_INET6 ? sizeof(a6) : sizeof(a)));
|
|
|
|
#else
|
|
|
|
retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
|
|
|
|
#endif
|
|
|
|
if (retcode != SOCKET_ERROR) {
|
|
|
|
err = 0;
|
|
|
|
} else {
|
|
|
|
err = WSAGetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
ret->error = winsock_error_string(err);
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (listen(s, SOMAXCONN) == SOCKET_ERROR) {
|
|
|
|
closesocket(s);
|
|
|
|
ret->error = winsock_error_string(err);
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up a select mechanism. This could be an AsyncSelect on a
|
|
|
|
* window, or an EventSelect on an event object. */
|
|
|
|
errstr = do_select(s, 1);
|
|
|
|
if (errstr) {
|
|
|
|
ret->error = errstr;
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
add234(sktree, ret);
|
|
|
|
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
static void sk_tcp_close(Socket sock)
|
|
|
|
{
|
2001-01-22 14:34:52 +03:00
|
|
|
extern char *do_select(SOCKET skt, int startup);
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
2001-01-22 14:34:52 +03:00
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
del234(sktree, s);
|
|
|
|
do_select(s->s, 0);
|
|
|
|
closesocket(s->s);
|
2000-12-12 13:33:13 +03:00
|
|
|
sfree(s);
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The function which tries to send on a socket once it's deemed
|
|
|
|
* writable.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
void try_send(Actual_Socket s)
|
|
|
|
{
|
2001-08-25 21:09:23 +04:00
|
|
|
while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
|
2000-10-23 15:55:11 +04:00
|
|
|
int nsent;
|
|
|
|
DWORD err;
|
2001-08-25 21:09:23 +04:00
|
|
|
void *data;
|
2001-05-06 18:35:20 +04:00
|
|
|
int len, urgentflag;
|
|
|
|
|
|
|
|
if (s->sending_oob) {
|
|
|
|
urgentflag = MSG_OOB;
|
|
|
|
len = s->sending_oob;
|
2001-08-25 21:09:23 +04:00
|
|
|
data = &s->oobdata;
|
2001-05-06 18:35:20 +04:00
|
|
|
} else {
|
|
|
|
urgentflag = 0;
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain_prefix(&s->output_data, &data, &len);
|
2001-05-06 18:35:20 +04:00
|
|
|
}
|
2001-08-25 21:09:23 +04:00
|
|
|
nsent = send(s->s, data, len, urgentflag);
|
2001-05-06 18:35:20 +04:00
|
|
|
noise_ultralight(nsent);
|
2000-10-23 15:55:11 +04:00
|
|
|
if (nsent <= 0) {
|
|
|
|
err = (nsent < 0 ? WSAGetLastError() : 0);
|
2001-09-06 02:04:19 +04:00
|
|
|
if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
|
2001-03-15 16:25:48 +03:00
|
|
|
/*
|
|
|
|
* Perfectly normal: we've sent all we can for the moment.
|
|
|
|
*
|
2001-09-06 02:04:19 +04:00
|
|
|
* (Some WinSock send() implementations can return
|
|
|
|
* <0 but leave no sensible error indication -
|
|
|
|
* WSAGetLastError() is called but returns zero or
|
|
|
|
* a small number - so we check that case and treat
|
|
|
|
* it just like WSAEWOULDBLOCK.)
|
2001-03-15 16:25:48 +03:00
|
|
|
*/
|
2000-10-23 15:55:11 +04:00
|
|
|
s->writable = FALSE;
|
2001-05-06 18:35:20 +04:00
|
|
|
return;
|
2000-10-23 15:55:11 +04:00
|
|
|
} else if (nsent == 0 ||
|
2001-05-06 18:35:20 +04:00
|
|
|
err == WSAECONNABORTED || err == WSAECONNRESET) {
|
|
|
|
/*
|
2001-10-28 12:57:47 +03:00
|
|
|
* If send() returns CONNABORTED or CONNRESET, we
|
|
|
|
* unfortunately can't just call plug_closing(),
|
|
|
|
* because it's quite likely that we're currently
|
|
|
|
* _in_ a call from the code we'd be calling back
|
|
|
|
* to, so we'd have to make half the SSH code
|
|
|
|
* reentrant. Instead we flag a pending error on
|
|
|
|
* the socket, to be dealt with (by calling
|
|
|
|
* plug_closing()) at some suitable future moment.
|
2001-05-06 18:35:20 +04:00
|
|
|
*/
|
2001-10-28 12:57:47 +03:00
|
|
|
s->pending_error = err;
|
|
|
|
return;
|
2000-10-23 15:55:11 +04:00
|
|
|
} else {
|
2002-10-26 16:58:13 +04:00
|
|
|
/* We're inside the Windows frontend here, so we know
|
|
|
|
* that the frontend handle is unnecessary. */
|
|
|
|
logevent(NULL, winsock_error_string(err));
|
2002-03-23 21:04:27 +03:00
|
|
|
fatalbox("%s", winsock_error_string(err));
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
} else {
|
2001-08-25 21:09:23 +04:00
|
|
|
if (s->sending_oob) {
|
|
|
|
if (nsent < len) {
|
|
|
|
memmove(s->oobdata, s->oobdata+nsent, len-nsent);
|
|
|
|
s->sending_oob = len - nsent;
|
|
|
|
} else {
|
|
|
|
s->sending_oob = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bufchain_consume(&s->output_data, nsent);
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-10 21:33:35 +03:00
|
|
|
static int sk_tcp_write(Socket sock, const char *buf, int len)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* Add the data to the buffer list on the socket.
|
|
|
|
*/
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain_add(&s->output_data, buf, len);
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now try sending from the start of the buffer list.
|
|
|
|
*/
|
|
|
|
if (s->writable)
|
|
|
|
try_send(s);
|
2001-08-25 21:09:23 +04:00
|
|
|
|
|
|
|
return bufchain_size(&s->output_data);
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
2003-01-10 21:33:35 +03:00
|
|
|
static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* Replace the buffer list on the socket with the data.
|
|
|
|
*/
|
2001-08-25 21:09:23 +04:00
|
|
|
bufchain_clear(&s->output_data);
|
|
|
|
assert(len <= sizeof(s->oobdata));
|
|
|
|
memcpy(s->oobdata, buf, len);
|
2000-10-23 15:55:11 +04:00
|
|
|
s->sending_oob = len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now try sending from the start of the buffer list.
|
|
|
|
*/
|
|
|
|
if (s->writable)
|
|
|
|
try_send(s);
|
2001-08-25 21:09:23 +04:00
|
|
|
|
|
|
|
return s->sending_oob;
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
int select_result(WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2001-01-29 18:10:56 +03:00
|
|
|
int ret, open;
|
2000-10-23 15:55:11 +04:00
|
|
|
DWORD err;
|
2001-04-16 16:25:03 +04:00
|
|
|
char buf[20480]; /* nice big buffer for plenty of speed */
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s;
|
2000-10-24 14:47:49 +04:00
|
|
|
u_long atmark;
|
2000-10-23 15:55:11 +04:00
|
|
|
|
|
|
|
/* wParam is the socket itself */
|
2002-12-08 14:00:47 +03:00
|
|
|
|
2003-01-09 22:26:12 +03:00
|
|
|
if (wParam == 0)
|
|
|
|
return 1; /* boggle */
|
2002-12-08 14:00:47 +03:00
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
s = find234(sktree, (void *) wParam, cmpforsearch);
|
2000-10-23 15:55:11 +04:00
|
|
|
if (!s)
|
|
|
|
return 1; /* boggle */
|
|
|
|
|
|
|
|
if ((err = WSAGETSELECTERROR(lParam)) != 0) {
|
2001-05-06 18:35:20 +04:00
|
|
|
/*
|
|
|
|
* An error has occurred on this socket. Pass it to the
|
|
|
|
* plug.
|
|
|
|
*/
|
|
|
|
return plug_closing(s->plug, winsock_error_string(err), err, 0);
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
2000-10-23 19:20:05 +04:00
|
|
|
noise_ultralight(lParam);
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
switch (WSAGETSELECTEVENT(lParam)) {
|
2001-09-08 02:39:01 +04:00
|
|
|
case FD_CONNECT:
|
|
|
|
s->connected = s->writable = 1;
|
|
|
|
break;
|
2000-10-23 15:55:11 +04:00
|
|
|
case FD_READ:
|
2001-08-09 00:44:35 +04:00
|
|
|
/* In the case the socket is still frozen, we don't even bother */
|
2001-08-25 21:09:23 +04:00
|
|
|
if (s->frozen) {
|
|
|
|
s->frozen_readable = 1;
|
2001-08-09 00:44:35 +04:00
|
|
|
break;
|
2001-08-25 21:09:23 +04:00
|
|
|
}
|
2001-08-09 00:44:35 +04:00
|
|
|
|
2001-05-06 18:35:20 +04:00
|
|
|
/*
|
|
|
|
* We have received data on the socket. For an oobinline
|
|
|
|
* socket, this might be data _before_ an urgent pointer,
|
|
|
|
* in which case we send it to the back end with type==1
|
|
|
|
* (data prior to urgent).
|
|
|
|
*/
|
|
|
|
if (s->oobinline) {
|
|
|
|
atmark = 1;
|
|
|
|
ioctlsocket(s->s, SIOCATMARK, &atmark);
|
|
|
|
/*
|
|
|
|
* Avoid checking the return value from ioctlsocket(),
|
|
|
|
* on the grounds that some WinSock wrappers don't
|
|
|
|
* support it. If it does nothing, we get atmark==1,
|
|
|
|
* which is equivalent to `no OOB pending', so the
|
|
|
|
* effect will be to non-OOB-ify any OOB data.
|
|
|
|
*/
|
|
|
|
} else
|
|
|
|
atmark = 1;
|
2001-02-01 17:11:04 +03:00
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
ret = recv(s->s, buf, sizeof(buf), 0);
|
2001-05-06 18:35:20 +04:00
|
|
|
noise_ultralight(ret);
|
2000-10-23 15:55:11 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
err = WSAGetLastError();
|
|
|
|
if (err == WSAEWOULDBLOCK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2001-05-06 18:35:20 +04:00
|
|
|
return plug_closing(s->plug, winsock_error_string(err), err,
|
|
|
|
0);
|
2001-03-13 13:22:45 +03:00
|
|
|
} else if (0 == ret) {
|
2001-05-06 18:35:20 +04:00
|
|
|
return plug_closing(s->plug, NULL, 0, 0);
|
2000-10-23 15:55:11 +04:00
|
|
|
} else {
|
2001-05-06 18:35:20 +04:00
|
|
|
return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FD_OOB:
|
2001-05-06 18:35:20 +04:00
|
|
|
/*
|
|
|
|
* This will only happen on a non-oobinline socket. It
|
|
|
|
* indicates that we can immediately perform an OOB read
|
|
|
|
* and get back OOB data, which we will send to the back
|
|
|
|
* end with type==2 (urgent data).
|
|
|
|
*/
|
|
|
|
ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
|
|
|
|
noise_ultralight(ret);
|
|
|
|
if (ret <= 0) {
|
2002-03-23 21:04:27 +03:00
|
|
|
char *str = (ret == 0 ? "Internal networking trouble" :
|
|
|
|
winsock_error_string(WSAGetLastError()));
|
2002-10-26 16:58:13 +04:00
|
|
|
/* We're inside the Windows frontend here, so we know
|
|
|
|
* that the frontend handle is unnecessary. */
|
|
|
|
logevent(NULL, str);
|
2002-03-23 21:04:27 +03:00
|
|
|
fatalbox("%s", str);
|
2001-05-06 18:35:20 +04:00
|
|
|
} else {
|
|
|
|
return plug_receive(s->plug, 2, buf, ret);
|
|
|
|
}
|
|
|
|
break;
|
2000-10-23 15:55:11 +04:00
|
|
|
case FD_WRITE:
|
2001-08-25 21:09:23 +04:00
|
|
|
{
|
|
|
|
int bufsize_before, bufsize_after;
|
|
|
|
s->writable = 1;
|
|
|
|
bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
|
|
|
|
try_send(s);
|
|
|
|
bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
|
|
|
|
if (bufsize_after < bufsize_before)
|
|
|
|
plug_sent(s->plug, bufsize_after);
|
|
|
|
}
|
2000-10-23 15:55:11 +04:00
|
|
|
break;
|
|
|
|
case FD_CLOSE:
|
2001-01-29 18:10:56 +03:00
|
|
|
/* Signal a close on the socket. First read any outstanding data. */
|
2001-05-06 18:35:20 +04:00
|
|
|
open = 1;
|
|
|
|
do {
|
|
|
|
ret = recv(s->s, buf, sizeof(buf), 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
err = WSAGetLastError();
|
|
|
|
if (err == WSAEWOULDBLOCK)
|
|
|
|
break;
|
|
|
|
return plug_closing(s->plug, winsock_error_string(err),
|
|
|
|
err, 0);
|
|
|
|
} else {
|
|
|
|
if (ret)
|
|
|
|
open &= plug_receive(s->plug, 0, buf, ret);
|
|
|
|
else
|
|
|
|
open &= plug_closing(s->plug, NULL, 0, 0);
|
2001-03-13 13:22:45 +03:00
|
|
|
}
|
2001-01-29 18:10:56 +03:00
|
|
|
} while (ret > 0);
|
2001-05-06 18:35:20 +04:00
|
|
|
return open;
|
2001-08-09 00:44:35 +04:00
|
|
|
case FD_ACCEPT:
|
|
|
|
{
|
2001-10-12 23:32:13 +04:00
|
|
|
struct sockaddr_in isa;
|
|
|
|
int addrlen = sizeof(struct sockaddr_in);
|
2001-08-27 19:59:37 +04:00
|
|
|
SOCKET t; /* socket of connection */
|
|
|
|
|
2001-10-12 23:32:13 +04:00
|
|
|
memset(&isa, 0, sizeof(struct sockaddr_in));
|
2001-08-27 19:59:37 +04:00
|
|
|
err = 0;
|
2001-12-11 21:47:35 +03:00
|
|
|
t = accept(s->s,(struct sockaddr *)&isa,&addrlen);
|
2001-08-27 19:59:37 +04:00
|
|
|
if (t == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
err = WSAGetLastError();
|
|
|
|
if (err == WSATRY_AGAIN)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-12-18 14:39:25 +03:00
|
|
|
if (s->localhost_only && !ipv4_is_loopback(isa.sin_addr)) {
|
2001-10-12 23:32:13 +04:00
|
|
|
closesocket(t); /* dodgy WinSock let nonlocal through */
|
|
|
|
} else if (plug_accepting(s->plug, (void*)t)) {
|
2001-08-27 19:59:37 +04:00
|
|
|
closesocket(t); /* denied or error */
|
|
|
|
}
|
2001-08-09 00:44:35 +04:00
|
|
|
}
|
2000-10-23 15:55:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-10-28 12:57:47 +03:00
|
|
|
/*
|
|
|
|
* Deal with socket errors detected in try_send().
|
|
|
|
*/
|
|
|
|
void net_pending_errors(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Actual_Socket s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This might be a fiddly business, because it's just possible
|
|
|
|
* that handling a pending error on one socket might cause
|
|
|
|
* others to be closed. (I can't think of any reason this might
|
|
|
|
* happen in current SSH implementation, but to maintain
|
|
|
|
* generality of this network layer I'll assume the worst.)
|
|
|
|
*
|
|
|
|
* So what we'll do is search the socket list for _one_ socket
|
|
|
|
* with a pending error, and then handle it, and then search
|
|
|
|
* the list again _from the beginning_. Repeat until we make a
|
|
|
|
* pass with no socket errors present. That way we are
|
|
|
|
* protected against the socket list changing under our feet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do {
|
|
|
|
for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
|
|
|
|
if (s->pending_error) {
|
|
|
|
/*
|
|
|
|
* An error has occurred on this socket. Pass it to the
|
|
|
|
* plug.
|
|
|
|
*/
|
|
|
|
plug_closing(s->plug,
|
|
|
|
winsock_error_string(s->pending_error),
|
|
|
|
s->pending_error, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (s);
|
|
|
|
}
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* Each socket abstraction contains a `void *' private field in
|
|
|
|
* which the client can keep state.
|
|
|
|
*/
|
2002-03-23 20:47:21 +03:00
|
|
|
static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
2000-10-23 15:55:11 +04:00
|
|
|
s->private_ptr = ptr;
|
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
|
2002-03-23 20:47:21 +03:00
|
|
|
static void *sk_tcp_get_private_ptr(Socket sock)
|
2001-05-06 18:35:20 +04:00
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
2000-10-23 15:55:11 +04:00
|
|
|
return s->private_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Special error values are returned from sk_namelookup and sk_new
|
|
|
|
* if there's a problem. These functions extract an error message,
|
|
|
|
* or return NULL if there's no problem.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
char *sk_addr_error(SockAddr addr)
|
|
|
|
{
|
2000-10-23 15:55:11 +04:00
|
|
|
return addr->error;
|
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
static char *sk_tcp_socket_error(Socket sock)
|
|
|
|
{
|
2001-03-13 13:22:45 +03:00
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
2000-10-23 15:55:11 +04:00
|
|
|
return s->error;
|
|
|
|
}
|
|
|
|
|
2002-03-23 20:47:21 +03:00
|
|
|
static void sk_tcp_set_frozen(Socket sock, int is_frozen)
|
2001-08-09 00:44:35 +04:00
|
|
|
{
|
|
|
|
Actual_Socket s = (Actual_Socket) sock;
|
2001-08-25 21:09:23 +04:00
|
|
|
if (s->frozen == is_frozen)
|
|
|
|
return;
|
2001-08-09 00:44:35 +04:00
|
|
|
s->frozen = is_frozen;
|
2001-08-25 21:09:23 +04:00
|
|
|
if (!is_frozen && s->frozen_readable) {
|
2001-08-09 00:44:35 +04:00
|
|
|
char c;
|
|
|
|
recv(s->s, &c, 1, MSG_PEEK);
|
|
|
|
}
|
2001-08-25 21:09:23 +04:00
|
|
|
s->frozen_readable = 0;
|
2001-08-09 00:44:35 +04:00
|
|
|
}
|
|
|
|
|
2000-10-23 15:55:11 +04:00
|
|
|
/*
|
|
|
|
* For Plink: enumerate all sockets currently active.
|
|
|
|
*/
|
2001-05-06 18:35:20 +04:00
|
|
|
SOCKET first_socket(int *state)
|
|
|
|
{
|
2001-04-16 21:18:24 +04:00
|
|
|
Actual_Socket s;
|
|
|
|
*state = 0;
|
|
|
|
s = index234(sktree, (*state)++);
|
2000-10-23 15:55:11 +04:00
|
|
|
return s ? s->s : INVALID_SOCKET;
|
|
|
|
}
|
2001-05-06 18:35:20 +04:00
|
|
|
|
|
|
|
SOCKET next_socket(int *state)
|
|
|
|
{
|
2001-04-16 21:18:24 +04:00
|
|
|
Actual_Socket s = index234(sktree, (*state)++);
|
2000-10-23 15:55:11 +04:00
|
|
|
return s ? s->s : INVALID_SOCKET;
|
|
|
|
}
|
2002-10-30 20:57:31 +03:00
|
|
|
|
|
|
|
int net_service_lookup(char *service)
|
|
|
|
{
|
|
|
|
struct servent *se;
|
|
|
|
se = getservbyname(service, NULL);
|
|
|
|
if (se != NULL)
|
|
|
|
return ntohs(se->s_port);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|