| 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 "config.h" |
| 27 |
#include "FaxClient.h" |
| 28 |
#include "UnixTransport.h" |
| 29 |
#include "Sys.h" |
| 30 |
|
| 31 |
#include "NLS.h" |
| 32 |
|
| 33 |
UnixTransport::UnixTransport(FaxClient& c) : Transport(c) {} |
| 34 |
UnixTransport::~UnixTransport() {} |
| 35 |
|
| 36 |
#if CONFIG_UNIXTRANSPORT |
| 37 |
bool |
| 38 |
UnixTransport::isA(const char* address) |
| 39 |
{ |
| 40 |
return Sys::isSocketFile(address); |
| 41 |
} |
| 42 |
|
| 43 |
#include "Socket.h" |
| 44 |
|
| 45 |
extern "C" { |
| 46 |
#include <sys/un.h> |
| 47 |
} |
| 48 |
|
| 49 |
bool |
| 50 |
UnixTransport::callServer(fxStr& emsg) |
| 51 |
{ |
| 52 |
int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 53 |
if (fd < 0) { |
| 54 |
emsg = NLS::TEXT("Can not create socket to connect to server."); |
| 55 |
return (false); |
| 56 |
} |
| 57 |
struct sockaddr_un Sun; |
| 58 |
memset(&Sun, 0, sizeof (Sun)); |
| 59 |
Sun.sun_family = AF_UNIX; |
| 60 |
strncpy(Sun.sun_path, client.getHost(), sizeof (Sun.sun_path)); |
| 61 |
if (client.getVerbose()) |
| 62 |
client.traceServer(NLS::TEXT("connect to server at %s"), |
| 63 |
(const char*) client.getHost()); |
| 64 |
if (Socket::connect(fd, &Sun, sizeof (Sun)) >= 0) { |
| 65 |
client.setCtrlFds(fd, dup(fd)); |
| 66 |
return (true); |
| 67 |
} else { |
| 68 |
emsg = fxStr::format(NLS::TEXT("Can not reach server at Unix domain socket \"%s\"."), |
| 69 |
(const char*) client.getHost()); |
| 70 |
Sys::close(fd), fd = -1; |
| 71 |
return (false); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
bool |
| 76 |
UnixTransport::initDataConn(fxStr&) |
| 77 |
{ |
| 78 |
#ifdef notdef |
| 79 |
struct sockaddr_in data_addr; |
| 80 |
Socket::socklen_t dlen = sizeof (data_addr); |
| 81 |
if (Socket::getsockname(fileno(client.getCtrlFd()), &data_addr, &dlen) < 0) { |
| 82 |
emsg = fxStr::format("getsockname(ctrl): %s", strerror(errno)); |
| 83 |
return (false); |
| 84 |
} |
| 85 |
data_addr.sin_port = 0; // let system allocate port |
| 86 |
fd = socket(AF_INET, SOCK_STREAM, 0); |
| 87 |
if (fd < 0) { |
| 88 |
emsg = fxStr::format("socket: %s", strerror(errno)); |
| 89 |
return (false); |
| 90 |
} |
| 91 |
if (Socket::bind(fd, &data_addr, sizeof (data_addr)) < 0) { |
| 92 |
emsg = fxStr::format("bind: %s", strerror(errno)); |
| 93 |
goto bad; |
| 94 |
} |
| 95 |
dlen = sizeof (data_addr); |
| 96 |
if (Socket::getsockname(fd, &data_addr, &dlen) < 0) { |
| 97 |
emsg = fxStr::format("getsockname: %s", strerror(errno)); |
| 98 |
goto bad; |
| 99 |
} |
| 100 |
if (listen(fd, 1) < 0) { |
| 101 |
emsg = fxStr::format("listen: %s", strerror(errno)); |
| 102 |
goto bad; |
| 103 |
} |
| 104 |
const char* a = (const char*) &data_addr.sin_addr; |
| 105 |
const char* p = (const char*) &data_addr.sin_port; |
| 106 |
#define UC(b) (((int) b) & 0xff) |
| 107 |
if (client.command("PORT %u,%u,%u,%u,%u,%u", |
| 108 |
UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), |
| 109 |
UC(p[0]), UC(p[1])) != COMPLETE) |
| 110 |
return (false); |
| 111 |
#undef UC |
| 112 |
client.setDataFd(fd); |
| 113 |
return (true); |
| 114 |
bad: |
| 115 |
Sys::close(fd), fd = -1; |
| 116 |
#endif |
| 117 |
return (false); |
| 118 |
} |
| 119 |
|
| 120 |
bool |
| 121 |
UnixTransport::openDataConn(fxStr&) |
| 122 |
{ |
| 123 |
#ifdef notdef |
| 124 |
int s = Socket::accept(fileno(client.getDataFd()), NULL, NULL); |
| 125 |
if (s >= 0) { |
| 126 |
client.setDataFd(s); |
| 127 |
return (true); |
| 128 |
} else { |
| 129 |
emsg = fxStr::format("accept: %s", strerror(errno)); |
| 130 |
return (false); |
| 131 |
} |
| 132 |
#else |
| 133 |
return (false); |
| 134 |
#endif |
| 135 |
} |
| 136 |
#else |
| 137 |
void |
| 138 |
Transport::notConfigured(fxStr& emsg) |
| 139 |
{ |
| 140 |
emsg = NLS::TEXT("Sorry, no Unix domain communication support was configured."); |
| 141 |
} |
| 142 |
|
| 143 |
bool UnixTransport::callServer(fxStr& emsg) |
| 144 |
{ notConfigured(emsg); return (false); } |
| 145 |
bool UnixTransport::initDataConn(fxStr& emsg) |
| 146 |
{ notConfigured(emsg); return (false); } |
| 147 |
bool UnixTransport::openDataConn(fxStr& emsg) |
| 148 |
{ notConfigured(emsg); return (false); } |
| 149 |
#endif |