| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1995-1996 Sam Leffler |
| 4 |
* Copyright (c) 1995-1996 Silicon Graphics, Inc. |
| 5 |
* HylaFAX is a trademark of Silicon Graphics |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 8 |
* its documentation for any purpose is hereby granted without fee, provided |
| 9 |
* that (i) the above copyright notices and this permission notice appear in |
| 10 |
* all copies of the software and related documentation, and (ii) the names of |
| 11 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 12 |
* publicity relating to the software without the specific, prior written |
| 13 |
* permission of Sam Leffler and Silicon Graphics. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 20 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 |
* OF THIS SOFTWARE. |
| 25 |
*/ |
| 26 |
#include "port.h" |
| 27 |
#include "Dispatcher.h" |
| 28 |
#include "InetFaxServer.h" |
| 29 |
#include "Sys.h" |
| 30 |
#include "Socket.h" |
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
#include <netdb.h> |
| 34 |
#include <ctype.h> |
| 35 |
|
| 36 |
extern "C" { |
| 37 |
#include <arpa/inet.h> |
| 38 |
|
| 39 |
#include <netinet/in_systm.h> |
| 40 |
#include <netinet/ip.h> |
| 41 |
} |
| 42 |
|
| 43 |
InetSuperServer::InetSuperServer(const char* p, int bl) |
| 44 |
: SuperServer("INET", bl) |
| 45 |
, port(p) |
| 46 |
{ |
| 47 |
bindaddress = NULL; |
| 48 |
} |
| 49 |
|
| 50 |
InetSuperServer::~InetSuperServer() {} |
| 51 |
|
| 52 |
void |
| 53 |
InetSuperServer::setBindAddress(const char *bindaddr) |
| 54 |
{ |
| 55 |
bindaddress = bindaddr; |
| 56 |
} |
| 57 |
|
| 58 |
bool |
| 59 |
InetSuperServer::startServer(void) |
| 60 |
{ |
| 61 |
Socket::Address addr; |
| 62 |
struct addrinfo hints, *ai; |
| 63 |
memset(&hints, 0, sizeof(hints)); |
| 64 |
hints.ai_family = AF_UNSPEC; |
| 65 |
hints.ai_flags = AI_PASSIVE; |
| 66 |
#ifdef AI_ADDRCONFIG |
| 67 |
hints.ai_flags |= AI_ADDRCONFIG; |
| 68 |
#endif |
| 69 |
hints.ai_socktype = SOCK_STREAM; |
| 70 |
|
| 71 |
struct protoent* pp = getprotobyname(FAX_PROTONAME); |
| 72 |
if (pp) |
| 73 |
hints.ai_protocol = pp->p_proto; |
| 74 |
|
| 75 |
if (getaddrinfo(bindaddress, port, &hints, &ai) == 0) { |
| 76 |
memcpy(&addr, ai->ai_addr, ai->ai_addrlen); |
| 77 |
freeaddrinfo(ai); |
| 78 |
} else { |
| 79 |
logDebug("Couldn't get address information for port \"%s\"", |
| 80 |
(const char*) port); |
| 81 |
/* |
| 82 |
* Somethings broken here, let's pick some conservative defaults |
| 83 |
*/ |
| 84 |
memset(&addr, 0, sizeof(addr)); |
| 85 |
Socket::family(addr) = AF_INET; |
| 86 |
addr.in.sin_port = htons(FAX_DEFPORT); |
| 87 |
} |
| 88 |
|
| 89 |
int s = socket(Socket::family(addr), SOCK_STREAM, 0); |
| 90 |
if (s >= 0) { |
| 91 |
int on = 1; |
| 92 |
if (Socket::setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) >= 0) { |
| 93 |
if (Socket::bind(s, &addr, Socket::socklen(addr)) >= 0) { |
| 94 |
(void) listen(s, getBacklog()); |
| 95 |
Dispatcher::instance().link(s, Dispatcher::ReadMask, this); |
| 96 |
char hostbuf[128]; |
| 97 |
const char* a = inet_ntop(Socket::family(addr), Socket::addr(addr), hostbuf, sizeof(hostbuf)); |
| 98 |
switch (Socket::family(addr)) |
| 99 |
{ |
| 100 |
case AF_INET: |
| 101 |
logInfo("Listening to %s:%d", a ? a : "", ntohs(Socket::port(addr))); |
| 102 |
break; |
| 103 |
case AF_INET6: |
| 104 |
logInfo("Listening to [%s]:%d", a ? a : "", ntohs(Socket::port(addr))); |
| 105 |
break; |
| 106 |
} |
| 107 |
return (true); // success |
| 108 |
} |
| 109 |
} |
| 110 |
Sys::close(s); |
| 111 |
logError("HylaFAX %s: bind (port %u): %m", |
| 112 |
getKind(), ntohs(Socket::port(addr))); |
| 113 |
} else |
| 114 |
logError("HylaFAX %s: socket: %m", getKind()); |
| 115 |
return (false); |
| 116 |
} |
| 117 |
HylaFAXServer* InetSuperServer::newChild(void) { return new InetFaxServer; } |
| 118 |
|
| 119 |
InetFaxServer* InetFaxServer::_instance = NULL; |
| 120 |
|
| 121 |
InetFaxServer::InetFaxServer() |
| 122 |
{ |
| 123 |
usedefault = true; |
| 124 |
swaitmax = 90; // wait at most 90 seconds |
| 125 |
swaitint = 5; // interval between retries |
| 126 |
|
| 127 |
memset(&data_dest, 0, sizeof (data_dest)); |
| 128 |
Socket::family(data_dest) = AF_INET6; // We'll allow AF_INET6 by default. |
| 129 |
|
| 130 |
hostent* hp = Socket::gethostbyname(hostname); |
| 131 |
|
| 132 |
if (hp != NULL) |
| 133 |
{ |
| 134 |
hostname = hp->h_name; |
| 135 |
struct in_addr in; |
| 136 |
memcpy(&in, hp->h_addr, sizeof (in)); |
| 137 |
hostaddr = inet_ntoa(in); |
| 138 |
} |
| 139 |
|
| 140 |
fxAssert(_instance == NULL, |
| 141 |
"Cannot create multiple InetFaxServer instances"); |
| 142 |
_instance = this; |
| 143 |
} |
| 144 |
InetFaxServer::~InetFaxServer() {} |
| 145 |
|
| 146 |
InetFaxServer& InetFaxServer::instance() { return *_instance; } |
| 147 |
|
| 148 |
void |
| 149 |
InetFaxServer::initServer(void) |
| 150 |
{ |
| 151 |
HylaFAXServer::initServer(); |
| 152 |
usedefault = true; |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
InetFaxServer::open(void) |
| 157 |
{ |
| 158 |
setupNetwork(STDIN_FILENO); |
| 159 |
HylaFAXServer::open(); |
| 160 |
if (TRACE(CONNECT)) |
| 161 |
logInfo("HylaFAX INET connection from %s [%s]", |
| 162 |
(const char*) remotehost, (const char*) remoteaddr); |
| 163 |
} |
| 164 |
|
| 165 |
static const char* |
| 166 |
topDomain(const fxStr& h) |
| 167 |
{ |
| 168 |
int dots = 0; |
| 169 |
u_int l = h.length(); |
| 170 |
|
| 171 |
if(l <= 0) return(NULL); // out early |
| 172 |
|
| 173 |
for (;;) { |
| 174 |
l = h.nextR(l, '.'); |
| 175 |
if (l == 0) |
| 176 |
return (&h[0]); // return whole string |
| 177 |
if (++dots == 2) |
| 178 |
return (&h[l]); |
| 179 |
l -=1; //back over the dot |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Check whether host h is in our local domain, |
| 185 |
* defined as sharing the last two components of the domain part, |
| 186 |
* or the entire domain part if the local domain has only one component. |
| 187 |
* If either name is unqualified (contains no '.'), |
| 188 |
* assume that the host is local, as it will be |
| 189 |
* interpreted as such. |
| 190 |
*/ |
| 191 |
bool |
| 192 |
InetFaxServer::isLocalDomain(const fxStr& h) |
| 193 |
{ |
| 194 |
const char* p1 = topDomain(hostname); |
| 195 |
const char* p2 = topDomain(h); |
| 196 |
return (p1 == NULL || p2 == NULL || strcasecmp(p1, p2) == 0); |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* Check host identity returned by getnameinfo to |
| 201 |
* weed out clients trying to spoof us (this is mostly |
| 202 |
* a sanity check; if they have full control of DNS |
| 203 |
* they can still spoof) |
| 204 |
* Look up the name and check that the peer's address |
| 205 |
* corresponds to the host name. |
| 206 |
*/ |
| 207 |
bool |
| 208 |
InetFaxServer::checkHostIdentity(const char*name) |
| 209 |
{ |
| 210 |
struct addrinfo hints, *ai; |
| 211 |
|
| 212 |
memset(&hints, 0, sizeof(hints)); |
| 213 |
hints.ai_flags = AI_CANONNAME; |
| 214 |
hints.ai_socktype = SOCK_STREAM; |
| 215 |
|
| 216 |
if (getaddrinfo(name, NULL, &hints, &ai) == 0) { |
| 217 |
for (struct addrinfo *aip = ai; aip != NULL; aip = aip->ai_next) { |
| 218 |
Socket::Address *addr = (Socket::Address*)aip->ai_addr; |
| 219 |
if (aip->ai_family != Socket::family(peer_addr)) |
| 220 |
continue; |
| 221 |
if ( (aip->ai_family == AF_INET6 && |
| 222 |
memcmp(&addr->in6.sin6_addr, &peer_addr.in6.sin6_addr, sizeof(struct in6_addr)) ==0) |
| 223 |
||(aip->ai_family == AF_INET && |
| 224 |
memcmp(&addr->in.sin_addr, &peer_addr.in.sin_addr, sizeof(struct in_addr)) ==0) |
| 225 |
) { |
| 226 |
freeaddrinfo(ai); |
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
reply(130, "Warning, client address \"%s\" is not listed for host name \"%s\".", |
| 231 |
(const char*) remoteaddr, name); |
| 232 |
freeaddrinfo(ai); |
| 233 |
} else |
| 234 |
reply(130, "Warning, no inverse address mapping for client host name \"%s\".", |
| 235 |
(const char*) name); |
| 236 |
return(false); |
| 237 |
} |
| 238 |
|
| 239 |
void |
| 240 |
InetFaxServer::setupNetwork(int fd) |
| 241 |
{ |
| 242 |
socklen_t addrlen; |
| 243 |
|
| 244 |
addrlen = sizeof (peer_addr); |
| 245 |
if (Socket::getpeername(fd, &peer_addr, &addrlen) < 0) { |
| 246 |
logError("getpeername: %m (incorrect hfaxd invocation?)"); |
| 247 |
dologout(-1); |
| 248 |
} |
| 249 |
addrlen = sizeof (ctrl_addr); |
| 250 |
if (Socket::getsockname(fd, &ctrl_addr, &addrlen) < 0) { |
| 251 |
logError("getsockname: %m"); |
| 252 |
dologout(-1); |
| 253 |
} |
| 254 |
#if defined(IPTOS_LOWDELAY) |
| 255 |
{ int tos = IPTOS_LOWDELAY; |
| 256 |
if (Socket::setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0) |
| 257 |
logWarning("setsockopt (IP_TOS): %m"); |
| 258 |
} |
| 259 |
#endif |
| 260 |
#if defined(SO_LINGER) && !defined(__linux__) |
| 261 |
{ struct linger opt; |
| 262 |
opt.l_onoff = 1; |
| 263 |
opt.l_linger = 60; |
| 264 |
if (Socket::setsockopt(fd, SOL_SOCKET, SO_LINGER, &opt, sizeof (opt)) < 0) |
| 265 |
logWarning("setsockopt (SO_LINGER): %m"); |
| 266 |
} |
| 267 |
#endif |
| 268 |
/* anchor socket to avoid multi-homing problems */ |
| 269 |
data_source = ctrl_addr; |
| 270 |
Socket::port(data_source) = htons(ntohs(Socket::port(ctrl_addr))-1); |
| 271 |
#ifdef F_SETOWN |
| 272 |
if (fcntl(fd, F_SETOWN, getpid()) == -1) |
| 273 |
logError("fcntl (F_SETOWN): %m"); |
| 274 |
#endif |
| 275 |
#if defined(SO_OOBINLINE) |
| 276 |
/* handle urgent data inline */ |
| 277 |
{ int on = 1; |
| 278 |
if (Socket::setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof (on)) < 0) |
| 279 |
logError("setsockopt (SO_OOBINLINE): %m"); |
| 280 |
} |
| 281 |
#endif |
| 282 |
#ifdef SIGURG |
| 283 |
/* |
| 284 |
* XXX This is #ifdef'd for ISC but indicates a problem |
| 285 |
* XXX in the underlying system. Without support for |
| 286 |
* XXX urgent data clients will not be able to abort an |
| 287 |
* XXX ongoing operation such as a file transfer. |
| 288 |
*/ |
| 289 |
signal(SIGURG, fxSIGHANDLER(sigURG)); |
| 290 |
#endif |
| 291 |
signal(SIGPIPE, fxSIGHANDLER(sigPIPE)); |
| 292 |
|
| 293 |
char hostbuf[128]; |
| 294 |
|
| 295 |
remoteaddr = inet_ntop(Socket::family(peer_addr), Socket::addr(peer_addr), hostbuf, sizeof(hostbuf)); |
| 296 |
|
| 297 |
getnameinfo((struct sockaddr*)&peer_addr, Socket::socklen(peer_addr), |
| 298 |
hostbuf, sizeof(hostbuf), NULL, 0, 0); |
| 299 |
|
| 300 |
if (remoteaddr == "0.0.0.0") |
| 301 |
remotehost = "localhost"; |
| 302 |
else if (checkHostIdentity(hostbuf)) |
| 303 |
remotehost = hostbuf; |
| 304 |
else |
| 305 |
remotehost = remoteaddr; |
| 306 |
Dispatcher::instance().link(STDIN_FILENO, Dispatcher::ReadMask, this); |
| 307 |
} |
| 308 |
|
| 309 |
void |
| 310 |
InetFaxServer::handleUrgentData(void) |
| 311 |
{ |
| 312 |
/* |
| 313 |
* Only process the urgent data is we are expecting it. |
| 314 |
* Specifically, if a file transfer, synchronous job |
| 315 |
* wait, or other potentially long-running operation is |
| 316 |
* taking place we do a setjmp and set a flag so that |
| 317 |
* if the client requests an abort operation (ABOR) then |
| 318 |
* we longjmp below to break out of the op. |
| 319 |
*/ |
| 320 |
if ((state & (S_TRANSFER|S_WAITTRIG)) == 0) { |
| 321 |
if (IS(WAITFIFO)) { |
| 322 |
fifoResponse = "User aborted operation"; |
| 323 |
state &= ~S_WAITFIFO; |
| 324 |
} |
| 325 |
return; |
| 326 |
} |
| 327 |
char line[8]; |
| 328 |
if (!getCmdLine(line, sizeof (line))) { |
| 329 |
reply(221, "You could at least say goodbye."); |
| 330 |
dologout(0); |
| 331 |
} else if (strcasecmp(line, "ABOR\n") == 0) { |
| 332 |
/* |
| 333 |
* Abort the operation in progress. Two reply |
| 334 |
* codes are sent on the control channel; one |
| 335 |
* for the outstanding operation and one for the |
| 336 |
* abort command. |
| 337 |
*/ |
| 338 |
reply(426, "Operation aborted. Data connection closed."); |
| 339 |
reply(226, "Abort successful"); |
| 340 |
longjmp(urgcatch, 1); // XXX potential memory leak |
| 341 |
} else if (strcasecmp(line, "STAT\n") == 0) { |
| 342 |
if (file_size != (off_t) -1) |
| 343 |
reply(213, "Status: %lu of %lu bytes transferred", |
| 344 |
byte_count, file_size); |
| 345 |
else |
| 346 |
reply(213, "Status: %lu bytes transferred", byte_count); |
| 347 |
} else |
| 348 |
pushCmdData(line, strlen(line)); |
| 349 |
} |
| 350 |
|
| 351 |
void |
| 352 |
InetFaxServer::sigURG(int) |
| 353 |
{ |
| 354 |
int old_errno = errno; |
| 355 |
InetFaxServer::instance().handleUrgentData(); |
| 356 |
errno = old_errno; |
| 357 |
} |
| 358 |
|
| 359 |
void |
| 360 |
InetFaxServer::lostConnection(void) |
| 361 |
{ |
| 362 |
if (TRACE(PROTOCOL)) |
| 363 |
logDebug("Lost connection to %s [%s]", |
| 364 |
(const char*) remotehost, (const char*) remoteaddr); |
| 365 |
dologout(-1); |
| 366 |
} |
| 367 |
|
| 368 |
void |
| 369 |
InetFaxServer::sigPIPE(int) |
| 370 |
{ |
| 371 |
int old_errno = errno; |
| 372 |
InetFaxServer::instance().lostConnection(); |
| 373 |
errno = old_errno; |
| 374 |
} |
| 375 |
|
| 376 |
static bool |
| 377 |
setupPassiveDataSocket(int pdata, Socket::Address& pasv_addr) |
| 378 |
{ |
| 379 |
socklen_t len = Socket::socklen(pasv_addr); |
| 380 |
return ( |
| 381 |
Socket::bind(pdata, (struct sockaddr*)&pasv_addr, len) >= 0 && |
| 382 |
Socket::getsockname(pdata, (struct sockaddr*)&pasv_addr, &len) >= 0 && |
| 383 |
listen(pdata, 1) >= 0 |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
#define UC(b) (((int) b) & 0xff) |
| 388 |
|
| 389 |
/* |
| 390 |
* Note: a response of 425 is not mentioned as a possible response to the |
| 391 |
* PASV command in RFC959. However, it has been blessed as a legitimate |
| 392 |
* response by Jon Postel in a telephone conversation with Rick Adams on 25 |
| 393 |
* Jan 89. |
| 394 |
*/ |
| 395 |
void |
| 396 |
InetFaxServer::passiveCmd(void) |
| 397 |
{ |
| 398 |
if (tokenBody[0] == 'E') { |
| 399 |
pasv_addr = ctrl_addr; |
| 400 |
logDebug("Extended passive requested for family %d", Socket::family(pasv_addr)); |
| 401 |
pdata = socket(Socket::family(pasv_addr), SOCK_STREAM, 0); |
| 402 |
if (pdata >= 0) { |
| 403 |
Socket::port(pasv_addr) = 0; |
| 404 |
if (!setupPassiveDataSocket(pdata, pasv_addr)) |
| 405 |
(void) Sys::close(pdata), pdata = -1; |
| 406 |
} |
| 407 |
if (pdata >= 0) { |
| 408 |
reply(229, "Entering Extended Passive Mode (|||%u|)", |
| 409 |
ntohs(Socket::port(pasv_addr))); |
| 410 |
} else |
| 411 |
perror_reply(425, "Cannot setup extended passive connection", errno); |
| 412 |
return; |
| 413 |
} |
| 414 |
if (Socket::family(ctrl_addr) != AF_INET) { |
| 415 |
reply(500, "Cannot use PASV with IPv6 connections"); |
| 416 |
return; |
| 417 |
} |
| 418 |
if (pdata < 0) { |
| 419 |
pdata = socket(AF_INET, SOCK_STREAM, 0); |
| 420 |
if (pdata >= 0) { |
| 421 |
pasv_addr = ctrl_addr; |
| 422 |
pasv_addr.in.sin_port = 0; |
| 423 |
if (!setupPassiveDataSocket(pdata, pasv_addr)) |
| 424 |
(void) Sys::close(pdata), pdata = -1; |
| 425 |
} |
| 426 |
} |
| 427 |
if (pdata >= 0) { |
| 428 |
const u_char* a = (const u_char*) &pasv_addr.in.sin_addr; |
| 429 |
const u_char* p = (const u_char*) &pasv_addr.in.sin_port; |
| 430 |
reply(227, "Entering passive mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), |
| 431 |
UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); |
| 432 |
} else |
| 433 |
perror_reply(425, "Cannot setup passive connection", errno); |
| 434 |
} |
| 435 |
|
| 436 |
void |
| 437 |
InetFaxServer::printaddr(FILE* fd, const char* leader, const struct sockaddr_in& sin) |
| 438 |
{ |
| 439 |
const u_char* a = (const u_char *) &sin.sin_addr; |
| 440 |
const u_char* p = (const u_char *) &sin.sin_port; |
| 441 |
fprintf(fd, "%s (%d,%d,%d,%d,%d,%d)\r\n", leader, |
| 442 |
UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); |
| 443 |
} |
| 444 |
|
| 445 |
void |
| 446 |
InetFaxServer::netStatus(FILE* fd) |
| 447 |
{ |
| 448 |
if (data != -1) |
| 449 |
fprintf(fd, " Client data connection open\r\n"); |
| 450 |
else if (pdata != -1) |
| 451 |
printaddr(fd, " In passive mode", pasv_addr); |
| 452 |
else if (!usedefault) |
| 453 |
printaddr(fd, " PORT", data_dest); |
| 454 |
else |
| 455 |
fprintf(fd, " No client data connection\r\n"); |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* Creat a socket for a data transfer. |
| 460 |
*/ |
| 461 |
FILE* |
| 462 |
InetFaxServer::getDataSocket(const char* mode) |
| 463 |
{ |
| 464 |
if (data >= 0) |
| 465 |
return (fdopen(data, mode)); |
| 466 |
int s = socket(Socket::family(data_dest), SOCK_STREAM, 0); |
| 467 |
if (s < 0) |
| 468 |
goto bad; |
| 469 |
{ int on = 1; |
| 470 |
if (Socket::setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) |
| 471 |
goto bad; |
| 472 |
} |
| 473 |
{ int ntry; // XXX for __GNUC__ |
| 474 |
for (ntry = 1;; ntry++) { |
| 475 |
if (Socket::bind(s, &data_source, Socket::socklen(data_source)) >= 0) |
| 476 |
break; |
| 477 |
if (errno != EADDRINUSE || ntry > 10) |
| 478 |
goto bad; |
| 479 |
sleep(ntry); |
| 480 |
} |
| 481 |
} |
| 482 |
#ifdef IPTOS_THROUGHPUT |
| 483 |
{ int on = IPTOS_THROUGHPUT; |
| 484 |
if (Socket::setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof (on)) < 0) |
| 485 |
logWarning("setsockopt (IP_TOS): %m"); |
| 486 |
} |
| 487 |
#endif |
| 488 |
return (fdopen(s, mode)); |
| 489 |
bad: |
| 490 |
(void) Sys::close(s); |
| 491 |
return (NULL); |
| 492 |
} |
| 493 |
|
| 494 |
bool |
| 495 |
InetFaxServer::dataConnect(void) |
| 496 |
{ |
| 497 |
return Socket::connect(data, &data_dest, Socket::socklen(data_dest)) >= 0; |
| 498 |
} |
| 499 |
|
| 500 |
/* |
| 501 |
* Establish a data connection for a file transfer operation. |
| 502 |
*/ |
| 503 |
FILE* |
| 504 |
InetFaxServer::openDataConn(const char* mode, int& code) |
| 505 |
{ |
| 506 |
byte_count = 0; |
| 507 |
if (pdata >= 0) { // passive mode, wait for connection |
| 508 |
struct sockaddr_in from; |
| 509 |
socklen_t fromlen = sizeof (from); |
| 510 |
// XXX don't block, select....???? |
| 511 |
int s = Socket::accept(pdata, (struct sockaddr*) &from, &fromlen); |
| 512 |
if (s < 0) { |
| 513 |
reply(425, "Cannot open data connection."); |
| 514 |
(void) Sys::close(pdata); |
| 515 |
pdata = -1; |
| 516 |
return (NULL); |
| 517 |
} |
| 518 |
// XXX verify peer address??? |
| 519 |
(void) Sys::close(pdata); |
| 520 |
pdata = s; |
| 521 |
#ifdef IPTOS_LOWDELAY |
| 522 |
int tos = IPTOS_LOWDELAY; |
| 523 |
(void) Socket::setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)); |
| 524 |
|
| 525 |
#endif |
| 526 |
code = 150; |
| 527 |
return (fdopen(pdata, mode)); |
| 528 |
} |
| 529 |
if (data >= 0) { |
| 530 |
code = 125; |
| 531 |
usedefault = 1; |
| 532 |
return (fdopen(data, mode)); |
| 533 |
} |
| 534 |
if (usedefault) |
| 535 |
data_dest = peer_addr; |
| 536 |
usedefault = 1; |
| 537 |
FILE* file = getDataSocket(mode); |
| 538 |
if (file == NULL) { |
| 539 |
reply(425, "Cannot create data socket (%s,%d): %s.", |
| 540 |
inet_ntoa(data_source.in.sin_addr), |
| 541 |
ntohs(data_source.in.sin_port), strerror(errno)); |
| 542 |
return (NULL); |
| 543 |
} |
| 544 |
data = fileno(file); |
| 545 |
for (int swait = 0; !dataConnect(); swait += swaitint) { |
| 546 |
if (!(errno == EADDRINUSE || errno == EINTR) || swait >= swaitmax) { |
| 547 |
perror_reply(425, "Cannot build data connection", errno); |
| 548 |
fclose(file); |
| 549 |
data = -1; |
| 550 |
return (NULL); |
| 551 |
} |
| 552 |
sleep(swaitint); |
| 553 |
} |
| 554 |
code = 150; |
| 555 |
return (file); |
| 556 |
} |
| 557 |
|
| 558 |
bool |
| 559 |
InetFaxServer::hostPort() |
| 560 |
{ |
| 561 |
if (tokenBody[0] == 'E') |
| 562 |
{ |
| 563 |
fxStr s; |
| 564 |
if (! STRING(s)) |
| 565 |
{ |
| 566 |
syntaxError("EPRT |family|address|port|"); |
| 567 |
return false; |
| 568 |
} |
| 569 |
/* |
| 570 |
* Minimual length for EPRT is: 9 |
| 571 |
* |X|X::|X| |
| 572 |
*/ |
| 573 |
char c = s[0]; |
| 574 |
if (s.length() > 9 |
| 575 |
&& c == s[0] && (s[1] == '1' || s[1] == '2') && c == s[2] |
| 576 |
&& c == s[s.length()-1]) { |
| 577 |
u_int pos = 3; |
| 578 |
fxStr a = s.token(pos, c); |
| 579 |
logDebug("`-> Got a: %s[%u]", (const char*)a, pos); |
| 580 |
fxStr p = s.token(pos, c); |
| 581 |
logDebug("`-> Got a: %s[%u]", (const char*)p, pos); |
| 582 |
if (pos != s.length() ) |
| 583 |
{ |
| 584 |
logDebug("Parsing EPRT style failed"); |
| 585 |
syntaxError("EPRT |family|address|port|"); |
| 586 |
return false; |
| 587 |
} |
| 588 |
|
| 589 |
struct addrinfo hints, *ai; |
| 590 |
|
| 591 |
memset(&hints, 0, sizeof(hints)); |
| 592 |
#ifdef AI_NUMERICHOST |
| 593 |
hints.ai_flags |= AI_NUMERICHOST; |
| 594 |
#endif |
| 595 |
#ifdef AI_NUMERICSERV |
| 596 |
hints.ai_flags |= AI_NUMERICSERV; |
| 597 |
#endif |
| 598 |
hints.ai_socktype = SOCK_STREAM; |
| 599 |
switch (s[1]) |
| 600 |
{ |
| 601 |
case '1': |
| 602 |
hints.ai_family = AF_INET; |
| 603 |
break; |
| 604 |
case '2': |
| 605 |
hints.ai_family = AF_INET6; |
| 606 |
break; |
| 607 |
default: |
| 608 |
reply(500, "EPRT: Invalid family \"%c\"", s[1]); |
| 609 |
} |
| 610 |
if (getaddrinfo(a, p, &hints, &ai) != 0) { |
| 611 |
reply(500, "EPRT couldn't parse family \"%c\" address \"%s\" port \"%s\"", |
| 612 |
s[1], (const char*)a, (const char*)p); |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
memcpy(&data_dest, ai->ai_addr, ai->ai_addrlen); |
| 617 |
freeaddrinfo(ai); |
| 618 |
return true; |
| 619 |
} |
| 620 |
syntaxError("Couldn't parse extended port"); |
| 621 |
return false; |
| 622 |
} |
| 623 |
|
| 624 |
long a0, a1, a2, a3; |
| 625 |
long p0, p1; |
| 626 |
bool syntaxOK = |
| 627 |
NUMBER(a0) |
| 628 |
&& COMMA() && NUMBER(a1) |
| 629 |
&& COMMA() && NUMBER(a2) |
| 630 |
&& COMMA() && NUMBER(a3) |
| 631 |
&& COMMA() && NUMBER(p0) |
| 632 |
&& COMMA() && NUMBER(p1) |
| 633 |
; |
| 634 |
if (syntaxOK) { |
| 635 |
data_dest.in.sin_family = AF_INET; |
| 636 |
u_char* a = (u_char*) &data_dest.in.sin_addr; |
| 637 |
u_char* p = (u_char*) &data_dest.in.sin_port; |
| 638 |
a[0] = UC(a0); a[1] = UC(a1); a[2] = UC(a2); a[3] = UC(a3); |
| 639 |
p[0] = UC(p0); p[1] = UC(p1); |
| 640 |
return (true); |
| 641 |
} |
| 642 |
|
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
void |
| 647 |
InetFaxServer::portCmd(Token t) |
| 648 |
{ |
| 649 |
if (t == T_EPRT) |
| 650 |
logcmd(T_EPRT, "|%d|%s|%u|", 1, inet_ntoa(data_dest.in.sin_addr), data_dest.in.sin_port); |
| 651 |
else |
| 652 |
logcmd(T_PORT, "%s;%u", inet_ntoa(data_dest.in.sin_addr), data_dest.in.sin_port); |
| 653 |
usedefault = false; |
| 654 |
if (pdata >= 0) |
| 655 |
(void) Sys::close(pdata), pdata = -1; |
| 656 |
|
| 657 |
if (t == T_EPRT) |
| 658 |
ack(200, cmdToken(T_EPRT)); |
| 659 |
else |
| 660 |
ack(200, cmdToken(T_PORT)); |
| 661 |
} |