Debian Codegraph @

hfaxd/UnixFaxServer.c++

raw
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 /*
27 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
28 *
29 * This stuff does not work; it is here as a placeholder in
30 * case it becomes worthwhile to add UNIX domain socket support.
31 *
32 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
33 */
34 #if CONFIG_UNIXTRANSPORT
35 #include "Dispatcher.h"
36 #include "UnixFaxServer.h"
37 #include "Sys.h"
38 #include "Socket.h"
39 #include "config.h"
40
41 UnixSuperServer::UnixSuperServer(const char* f, int bl)
42 : SuperServer("UNIX", bl)
43 , fileName(f)
44 {}
45 UnixSuperServer::~UnixSuperServer()
46 {
47 if (fileName != "")
48 Sys::unlink(fileName);
49 }
50
51 bool
52 UnixSuperServer::startServer(void)
53 {
54 int s = socket(AF_UNIX, SOCK_STREAM, 0);
55 if (s >= 0) {
56 struct sockaddr_un Sun;
57 Sun.sun_family = AF_UNIX;
58 strncpy(Sun.sun_path, fileName, sizeof (Sun.sun_path));
59 if (Socket::bind(s, &Sun, sizeof (Sun)) >= 0) {
60 #ifdef HAS_FCHMOD
61 (void) fchmod(s, 0622);
62 #else
63 (void) Sys::chmod(fileName, 0622);
64 #endif
65 (void) listen(s, getBacklog());
66 Dispatcher::instance().link(s, Dispatcher::ReadMask, this);
67 return (true);
68 }
69 Sys::close(s);
70 logError("%s HylaFAX: bind (port %s): %m",
71 getKind(), (const char*) fileName);
72 fileName = ""; // don't try to unlink file
73 } else
74 logError("%s HylaFAX: socket: %m", getKind());
75 return (false);
76 }
77 HylaFAXServer* UnixSuperServer::newChild(void) { return new UnixFaxServer; }
78
79 UnixFaxServer::UnixFaxServer()
80 {
81 usedefault = true;
82 }
83 UnixFaxServer::~UnixFaxServer() {}
84
85 void
86 UnixFaxServer::initServer(void)
87 {
88 HylaFAXServer::initServer();
89 usedefault = true;
90 }
91
92 void
93 UnixFaxServer::open(void)
94 {
95 remotehost = hostname; // always on same machine
96 remoteaddr = "local-UNIX"; // XXX
97 Dispatcher::instance().link(STDIN_FILENO, Dispatcher::ReadMask, this);
98 HylaFAXServer::open();
99 }
100
101 void
102 UnixFaxServer::passiveCmd(void)
103 {
104 perror_reply(425, "Cannot open passive connection "
105 "(makes no sense with UNIX domain sockets)", errno);
106 }
107
108 void
109 UnixFaxServer::netStatus(FILE* fd)
110 {
111 if (data != -1)
112 fprintf(fd, " Client data connection open\r\n");
113 else
114 fprintf(fd, " No client data connection\r\n");
115 }
116
117 /*
118 * Creat a socket for a data transfer.
119 */
120 FILE*
121 UnixFaxServer::getDataSocket(const char* mode)
122 {
123 if (data >= 0)
124 return (fdopen(data, mode));
125 int s = socket(AF_UNIX, SOCK_STREAM, 0);
126 if (s >= 0) {
127 /* anchor socket to avoid multi-homing problems */
128 data_source.sun_family = AF_UNIX;
129 strcpy(data_source.sun_path, ctrl_addr.sun_path);
130 if (bind(s, (struct sockaddr*) &data_source, sizeof (data_source)) >= 0) {
131 return (fdopen(s, mode));
132 }
133 }
134 (void) Sys::close(s);
135 return (NULL);
136 }
137
138 bool
139 UnixFaxServer::dataConnect(void)
140 {
141 return Socket::connect(data, &data_dest,sizeof (data_dest)) >= 0;
142 }
143
144 /*
145 * Establish a data connection for a file transfer operation.
146 */
147 FILE*
148 UnixFaxServer::openDataConn(const char* mode, int& code)
149 {
150 byte_count = 0;
151 if (pdata >= 0) {
152 struct sockaddr_un from;
153 int fromlen = sizeof(from);
154 int s = accept(pdata, (struct sockaddr*) &from, &fromlen);
155 if (s < 0) {
156 reply(425, "Cannot open data connection.");
157 (void) Sys::close(pdata);
158 pdata = -1;
159 return (NULL);
160 }
161 (void) Sys::close(pdata);
162 pdata = s;
163 code = 150;
164 return (fdopen(pdata, mode));
165 }
166 if (data >= 0) {
167 code = 125;
168 usedefault = 1;
169 return (fdopen(data, mode));
170 }
171 if (usedefault)
172 data_dest = peer_addr;
173 usedefault = 1;
174 FILE* file = getDataSocket(mode);
175 if (file == NULL) {
176 reply(425, "Cannot create data socket (%s): %s.",
177 data_source.sun_path, strerror(errno));
178 return (NULL);
179 }
180 data = fileno(file);
181 if (!dataConnect()) {
182 perror_reply(425, "Cannot build data connection", errno);
183 fclose(file);
184 data = -1;
185 return (NULL);
186 }
187 code = 150;
188 return (file);
189 }
190
191 bool
192 UnixFaxServer::hostPort()
193 {
194 fxStr s;
195 if (pathname(s)) {
196 data_dest.sun_family = AF_UNIX;
197 strncpy(data_dest.sun_path, s, sizeof (data_dest.sun_path));
198 return (true);
199 } else
200 return (false);
201 }
202
203 void
204 UnixFaxServer::portCmd(void)
205 {
206 logcmd(T_PORT, "%s", data_dest.sun_path);
207 usedefault = false;
208 if (pdata >= 0)
209 (void) Sys::close(pdata), pdata = -1;
210 reply(200, "PORT command successful.");
211 }
212 #endif