File: UnixFaxServer.c%2B%2B

package info (click to toggle)
hylafax 1%3A4.1.1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,400 kB
  • ctags: 7,270
  • sloc: sh: 15,895; ansic: 12,661; makefile: 1,439; cpp: 850
file content (212 lines) | stat: -rw-r--r-- 5,647 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*	$Id: UnixFaxServer.c++,v 1.2 1999/06/13 07:41:16 robert Exp $ */
/*
 * Copyright (c) 1995-1996 Sam Leffler
 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
 * HylaFAX is a trademark of Silicon Graphics
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */
/*
 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 *
 * This stuff does not work; it is here as a placeholder in
 * case it becomes worthwhile to add UNIX domain socket support.
 *
 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 */
#if CONFIG_UNIXTRANSPORT
#include "Dispatcher.h"
#include "UnixFaxServer.h"
#include "Sys.h"
#include "Socket.h"
#include "config.h"

UnixSuperServer::UnixSuperServer(const char* f, int bl)
    : SuperServer("UNIX", bl)
    , fileName(f)
{}
UnixSuperServer::~UnixSuperServer()
{
    if (fileName != "")
	Sys::unlink(fileName);
}

bool
UnixSuperServer::startServer(void)
{
    int s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s >= 0) {
	struct sockaddr_un Sun;
	Sun.sun_family = AF_UNIX;
	strncpy(Sun.sun_path, fileName, sizeof (Sun.sun_path));
	if (Socket::bind(s, &Sun, sizeof (Sun)) >= 0) {
#ifdef HAS_FCHMOD
	    (void) fchmod(s, 0622);
#else
	    (void) Sys::chmod(fileName, 0622);
#endif
	    (void) listen(s, getBacklog());
	    Dispatcher::instance().link(s, Dispatcher::ReadMask, this);
	    return (true);
	}
	Sys::close(s);
	logError("%s HylaFAX: bind (port %s): %m",
	    getKind(), (const char*) fileName);
	fileName = "";				// don't try to unlink file
    } else
	logError("%s HylaFAX: socket: %m", getKind());
    return (false);
}
HylaFAXServer* UnixSuperServer::newChild(void) { return new UnixFaxServer; }

UnixFaxServer::UnixFaxServer()
{
    usedefault = true;
}
UnixFaxServer::~UnixFaxServer() {}

void
UnixFaxServer::initServer(void)
{
    HylaFAXServer::initServer();
    usedefault = true;
}

void
UnixFaxServer::open(void)
{
    remotehost = hostname;		// always on same machine
    remoteaddr = "local-UNIX";		// XXX
    Dispatcher::instance().link(STDIN_FILENO, Dispatcher::ReadMask, this);
    HylaFAXServer::open();
}

void
UnixFaxServer::passiveCmd(void)
{
    perror_reply(425, "Cannot open passive connection "
	"(makes no sense with UNIX domain sockets)", errno);
}

void
UnixFaxServer::netStatus(FILE* fd)
{
    if (data != -1)
        fprintf(fd, "    Client data connection open\r\n");
    else
        fprintf(fd, "    No client data connection\r\n");
}

/*
 * Creat a socket for a data transfer.
 */
FILE*
UnixFaxServer::getDataSocket(const char* mode)
{
    if (data >= 0)
        return (fdopen(data, mode));
    int s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s >= 0) {
	/* anchor socket to avoid multi-homing problems */
	data_source.sun_family = AF_UNIX;
	strcpy(data_source.sun_path, ctrl_addr.sun_path);
	if (bind(s, (struct sockaddr*) &data_source, sizeof (data_source)) >= 0) {
	    return (fdopen(s, mode));
	}
    }
    (void) Sys::close(s);
    return (NULL);
}

bool
UnixFaxServer::dataConnect(void)
{
    return Socket::connect(data, &data_dest,sizeof (data_dest)) >= 0;
}

/*
 * Establish a data connection for a file transfer operation.
 */
FILE*
UnixFaxServer::openDataConn(const char* mode, int& code)
{
    byte_count = 0;
    if (pdata >= 0) {
        struct sockaddr_un from;
        int fromlen = sizeof(from);
        int s = accept(pdata, (struct sockaddr*) &from, &fromlen);
        if (s < 0) {
            reply(425, "Cannot open data connection.");
            (void) Sys::close(pdata);
            pdata = -1;
            return (NULL);
        }
        (void) Sys::close(pdata);
        pdata = s;
	code = 150;
        return (fdopen(pdata, mode));
    }
    if (data >= 0) {
	code = 125;
        usedefault = 1;
        return (fdopen(data, mode));
    }
    if (usedefault)
        data_dest = peer_addr;
    usedefault = 1;
    FILE* file = getDataSocket(mode);
    if (file == NULL) {
        reply(425, "Cannot create data socket (%s): %s.",
	      data_source.sun_path, strerror(errno));
	return (NULL);
    }
    data = fileno(file);
    if (!dataConnect()) {
	perror_reply(425, "Cannot build data connection", errno);
	fclose(file);
	data = -1;
	return (NULL);
    }
    code = 150;
    return (file);
}

bool
UnixFaxServer::hostPort()
{
    fxStr s;
    if (pathname(s)) {
	data_dest.sun_family = AF_UNIX;
	strncpy(data_dest.sun_path, s, sizeof (data_dest.sun_path));
	return (true);
    } else
	return (false);
}

void
UnixFaxServer::portCmd(void)
{
    logcmd(T_PORT, "%s", data_dest.sun_path);
    usedefault = false;
    if (pdata >= 0)
	(void) Sys::close(pdata), pdata = -1;
    reply(200, "PORT command successful.");
}
#endif