| 1 |
/* |
| 2 |
* tclIOSock.c -- |
| 3 |
* |
| 4 |
* Common routines used by all socket based channel types. |
| 5 |
* |
| 6 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 7 |
* |
| 8 |
* See the file "license.terms" for information on usage and redistribution of |
| 9 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "tclInt.h" |
| 13 |
|
| 14 |
#if defined(_WIN32) |
| 15 |
/* On Windows, we need to do proper Unicode->UTF-8 conversion. */ |
| 16 |
|
| 17 |
typedef struct ThreadSpecificData { |
| 18 |
int initialized; |
| 19 |
Tcl_DString errorMsg; /* UTF-8 encoded error-message */ |
| 20 |
} ThreadSpecificData; |
| 21 |
static Tcl_ThreadDataKey dataKey; |
| 22 |
|
| 23 |
#undef gai_strerror |
| 24 |
static const char *gai_strerror(int code) { |
| 25 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 26 |
|
| 27 |
if (tsdPtr->initialized) { |
| 28 |
Tcl_DStringFree(&tsdPtr->errorMsg); |
| 29 |
} else { |
| 30 |
tsdPtr->initialized = 1; |
| 31 |
} |
| 32 |
Tcl_WinTCharToUtf((TCHAR *)gai_strerrorW(code), -1, &tsdPtr->errorMsg); |
| 33 |
return Tcl_DStringValue(&tsdPtr->errorMsg); |
| 34 |
} |
| 35 |
#endif |
| 36 |
|
| 37 |
/* |
| 38 |
*--------------------------------------------------------------------------- |
| 39 |
* |
| 40 |
* TclSockGetPort -- |
| 41 |
* |
| 42 |
* Maps from a string, which could be a service name, to a port. Used by |
| 43 |
* socket creation code to get port numbers and resolve registered |
| 44 |
* service names to port numbers. |
| 45 |
* |
| 46 |
* Results: |
| 47 |
* A standard Tcl result. On success, the port number is returned in |
| 48 |
* portPtr. On failure, an error message is left in the interp's result. |
| 49 |
* |
| 50 |
* Side effects: |
| 51 |
* None. |
| 52 |
* |
| 53 |
*--------------------------------------------------------------------------- |
| 54 |
*/ |
| 55 |
|
| 56 |
int |
| 57 |
TclSockGetPort( |
| 58 |
Tcl_Interp *interp, |
| 59 |
const char *string, /* Integer or service name */ |
| 60 |
const char *proto, /* "tcp" or "udp", typically */ |
| 61 |
int *portPtr) /* Return port number */ |
| 62 |
{ |
| 63 |
struct servent *sp; /* Protocol info for named services */ |
| 64 |
Tcl_DString ds; |
| 65 |
const char *native; |
| 66 |
|
| 67 |
if (Tcl_GetInt(NULL, string, portPtr) != TCL_OK) { |
| 68 |
/* |
| 69 |
* Don't bother translating 'proto' to native. |
| 70 |
*/ |
| 71 |
|
| 72 |
native = Tcl_UtfToExternalDString(NULL, string, -1, &ds); |
| 73 |
sp = getservbyname(native, proto); /* INTL: Native. */ |
| 74 |
Tcl_DStringFree(&ds); |
| 75 |
if (sp != NULL) { |
| 76 |
*portPtr = ntohs((unsigned short) sp->s_port); |
| 77 |
return TCL_OK; |
| 78 |
} |
| 79 |
} |
| 80 |
if (Tcl_GetInt(interp, string, portPtr) != TCL_OK) { |
| 81 |
return TCL_ERROR; |
| 82 |
} |
| 83 |
if (*portPtr > 0xFFFF) { |
| 84 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 85 |
"couldn't open socket: port number too high", -1)); |
| 86 |
return TCL_ERROR; |
| 87 |
} |
| 88 |
return TCL_OK; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
*---------------------------------------------------------------------- |
| 93 |
* |
| 94 |
* TclSockMinimumBuffers -- |
| 95 |
* |
| 96 |
* Ensure minimum buffer sizes (non zero). |
| 97 |
* |
| 98 |
* Results: |
| 99 |
* A standard Tcl result. |
| 100 |
* |
| 101 |
* Side effects: |
| 102 |
* Sets SO_SNDBUF and SO_RCVBUF sizes. |
| 103 |
* |
| 104 |
*---------------------------------------------------------------------- |
| 105 |
*/ |
| 106 |
|
| 107 |
#if !defined(_WIN32) && !defined(__CYGWIN__) |
| 108 |
# define SOCKET int |
| 109 |
#endif |
| 110 |
|
| 111 |
int |
| 112 |
TclSockMinimumBuffers( |
| 113 |
void *sock, /* Socket file descriptor */ |
| 114 |
int size) /* Minimum buffer size */ |
| 115 |
{ |
| 116 |
int current; |
| 117 |
socklen_t len; |
| 118 |
|
| 119 |
len = sizeof(int); |
| 120 |
getsockopt((SOCKET)(size_t) sock, SOL_SOCKET, SO_SNDBUF, |
| 121 |
(char *) ¤t, &len); |
| 122 |
if (current < size) { |
| 123 |
len = sizeof(int); |
| 124 |
setsockopt((SOCKET)(size_t) sock, SOL_SOCKET, SO_SNDBUF, |
| 125 |
(char *) &size, len); |
| 126 |
} |
| 127 |
len = sizeof(int); |
| 128 |
getsockopt((SOCKET)(size_t) sock, SOL_SOCKET, SO_RCVBUF, |
| 129 |
(char *) ¤t, &len); |
| 130 |
if (current < size) { |
| 131 |
len = sizeof(int); |
| 132 |
setsockopt((SOCKET)(size_t) sock, SOL_SOCKET, SO_RCVBUF, |
| 133 |
(char *) &size, len); |
| 134 |
} |
| 135 |
return TCL_OK; |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
*---------------------------------------------------------------------- |
| 140 |
* |
| 141 |
* TclCreateSocketAddress -- |
| 142 |
* |
| 143 |
* This function initializes a sockaddr structure for a host and port. |
| 144 |
* |
| 145 |
* Results: |
| 146 |
* 1 if the host was valid, 0 if the host could not be converted to an IP |
| 147 |
* address. |
| 148 |
* |
| 149 |
* Side effects: |
| 150 |
* Fills in the *sockaddrPtr structure. |
| 151 |
* |
| 152 |
*---------------------------------------------------------------------- |
| 153 |
*/ |
| 154 |
|
| 155 |
int |
| 156 |
TclCreateSocketAddress( |
| 157 |
Tcl_Interp *interp, /* Interpreter for querying |
| 158 |
* the desired socket family */ |
| 159 |
struct addrinfo **addrlist, /* Socket address list */ |
| 160 |
const char *host, /* Host. NULL implies INADDR_ANY */ |
| 161 |
int port, /* Port number */ |
| 162 |
int willBind, /* Is this an address to bind() to or |
| 163 |
* to connect() to? */ |
| 164 |
const char **errorMsgPtr) /* Place to store the error message |
| 165 |
* detail, if available. */ |
| 166 |
{ |
| 167 |
struct addrinfo hints; |
| 168 |
struct addrinfo *p; |
| 169 |
struct addrinfo *v4head = NULL, *v4ptr = NULL; |
| 170 |
struct addrinfo *v6head = NULL, *v6ptr = NULL; |
| 171 |
char *native = NULL, portbuf[TCL_INTEGER_SPACE], *portstring; |
| 172 |
const char *family = NULL; |
| 173 |
Tcl_DString ds; |
| 174 |
int result; |
| 175 |
|
| 176 |
if (host != NULL) { |
| 177 |
native = Tcl_UtfToExternalDString(NULL, host, -1, &ds); |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* Workaround for OSX's apparent inability to resolve "localhost", "0" |
| 182 |
* when the loopback device is the only available network interface. |
| 183 |
*/ |
| 184 |
if (host != NULL && port == 0) { |
| 185 |
portstring = NULL; |
| 186 |
} else { |
| 187 |
TclFormatInt(portbuf, port); |
| 188 |
portstring = portbuf; |
| 189 |
} |
| 190 |
|
| 191 |
(void) memset(&hints, 0, sizeof(hints)); |
| 192 |
hints.ai_family = AF_UNSPEC; |
| 193 |
|
| 194 |
/* |
| 195 |
* Magic variable to enforce a certain address family - to be superseded |
| 196 |
* by a TIP that adds explicit switches to [socket] |
| 197 |
*/ |
| 198 |
|
| 199 |
if (interp != NULL) { |
| 200 |
family = Tcl_GetVar(interp, "::tcl::unsupported::socketAF", 0); |
| 201 |
if (family != NULL) { |
| 202 |
if (strcmp(family, "inet") == 0) { |
| 203 |
hints.ai_family = AF_INET; |
| 204 |
} else if (strcmp(family, "inet6") == 0) { |
| 205 |
hints.ai_family = AF_INET6; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
hints.ai_socktype = SOCK_STREAM; |
| 211 |
|
| 212 |
#if 0 |
| 213 |
/* |
| 214 |
* We found some problems when using AI_ADDRCONFIG, e.g. on systems that |
| 215 |
* have no networking besides the loopback interface and want to resolve |
| 216 |
* localhost. See [Bugs 3385024, 3382419, 3382431]. As the advantage of |
| 217 |
* using AI_ADDRCONFIG in situations where it works, is probably low, |
| 218 |
* we'll leave it out for now. After all, it is just an optimisation. |
| 219 |
* |
| 220 |
* Missing on: OpenBSD, NetBSD. |
| 221 |
* Causes failure when used on AIX 5.1 and HP-UX |
| 222 |
*/ |
| 223 |
|
| 224 |
#if defined(AI_ADDRCONFIG) && !defined(_AIX) && !defined(__hpux) |
| 225 |
hints.ai_flags |= AI_ADDRCONFIG; |
| 226 |
#endif /* AI_ADDRCONFIG && !_AIX && !__hpux */ |
| 227 |
#endif /* 0 */ |
| 228 |
|
| 229 |
if (willBind) { |
| 230 |
hints.ai_flags |= AI_PASSIVE; |
| 231 |
} |
| 232 |
|
| 233 |
result = getaddrinfo(native, portstring, &hints, addrlist); |
| 234 |
|
| 235 |
if (host != NULL) { |
| 236 |
Tcl_DStringFree(&ds); |
| 237 |
} |
| 238 |
|
| 239 |
if (result != 0) { |
| 240 |
*errorMsgPtr = |
| 241 |
#ifdef EAI_SYSTEM /* Doesn't exist on Windows */ |
| 242 |
(result == EAI_SYSTEM) ? Tcl_PosixError(interp) : |
| 243 |
#endif /* EAI_SYSTEM */ |
| 244 |
gai_strerror(result); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* Put IPv4 addresses before IPv6 addresses to maximize backwards |
| 250 |
* compatibility of [fconfigure -sockname] output. |
| 251 |
* |
| 252 |
* There might be more elegant/efficient ways to do this. |
| 253 |
*/ |
| 254 |
if (willBind) { |
| 255 |
for (p = *addrlist; p != NULL; p = p->ai_next) { |
| 256 |
if (p->ai_family == AF_INET) { |
| 257 |
if (v4head == NULL) { |
| 258 |
v4head = p; |
| 259 |
} else { |
| 260 |
v4ptr->ai_next = p; |
| 261 |
} |
| 262 |
v4ptr = p; |
| 263 |
} else { |
| 264 |
if (v6head == NULL) { |
| 265 |
v6head = p; |
| 266 |
} else { |
| 267 |
v6ptr->ai_next = p; |
| 268 |
} |
| 269 |
v6ptr = p; |
| 270 |
} |
| 271 |
} |
| 272 |
*addrlist = NULL; |
| 273 |
if (v6head != NULL) { |
| 274 |
*addrlist = v6head; |
| 275 |
v6ptr->ai_next = NULL; |
| 276 |
} |
| 277 |
if (v4head != NULL) { |
| 278 |
v4ptr->ai_next = *addrlist; |
| 279 |
*addrlist = v4head; |
| 280 |
} |
| 281 |
} |
| 282 |
return 1; |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* Local Variables: |
| 287 |
* mode: c |
| 288 |
* c-basic-offset: 4 |
| 289 |
* fill-column: 78 |
| 290 |
* End: |
| 291 |
*/ |