File: socksvr.c

package info (click to toggle)
libhsync 0.5.7-1.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,060 kB
  • ctags: 543
  • sloc: sh: 7,944; ansic: 5,413; makefile: 154
file content (282 lines) | stat: -rw-r--r-- 6,987 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*				       	-*- c-file-style: "bsd" -*-
 * rproxy -- dynamic caching and delta update in HTTP
 * $Id: socksvr.c,v 1.9 2000/08/06 12:50:36 mbp Exp $
 * 
 * Copyright (C) 2000 by Martin Pool <mbp@humbug.org.au>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * socksvr -- Accept connections on a TCP socket, and pass them
 * through to a server process as stdin/stdout.
 *
 * This is kind of like inetd and many other programs.  Why recreate
 * it?  A few reasons, all coming from the fact that we want to use
 * this in the libhsync automatic test suite.
 *
 * Firstly, we can't count on any particular TCP port being available
 * for use on any server, and we don't want to trouble the user to
 * choose one.  The kernel will assign a port for us if we want one,
 * and we write it out to stdout, so that the caller can see it and
 * send clients to it.
 *
 * Secondly, we don't want to require any human intervention, such as
 * inserting commands into /etc/inetd.conf.
 *
 * Thirdly, we don't want to require tools such as netcat that are
 * only available on some distributions.
 *
 * XXX: This is not secure!  Only use it for testing.  Never run a
 * shell.
 */

#include "includes.h"

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

#include <netinet/in.h>
#include <netinet/tcp.h>

#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>

static void
show_usage(void)
{
    printf("Usage: socksvr OPTIONS COMMAND ...\n"
           "Bind and listen on a port; invoke COMMAND on each connection\n"
           "Options:\n"
           "  -1    serve just a single client, then exit\n"
           "  -D    turn on trace, if enabled in library\n");
}


static int
bind_any_socket(int *psock, int *pport)
{
    struct sockaddr_in addr;
    int             sock;
#ifdef HAVE_SOCKLEN_T
    socklen_t           len;
#else
    size_t		len;
#endif

    /* Create a socket */
    sock = socket(PF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        _hs_error("create socket failed: %s", strerror(errno));
	return -1;
    }

    /* Get an address.  Any address is fine. */
    hs_bzero((char *) &addr, sizeof addr);
#ifdef HAVE_SOCK_SIN_LEN 
    addr.sin_len = sizeof(addr);
#endif 
    addr.sin_port = 0;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sock, (struct sockaddr *) &addr, sizeof addr) < 0) {
        _hs_error("bind socket failed: %s", strerror(errno));
	return (-1);
    }

    /* where are we? */
    len = sizeof addr;
    if (getsockname(sock, (struct sockaddr *) &addr, &len) < 0) {
        _hs_error("getsockname failed: %s", strerror(errno));
        return -1;
    }

    _hs_trace("got socket on port %d", ntohs(addr.sin_port));

    *pport = ntohs(addr.sin_port);
    *psock = sock;

    /* Listen for connections. */
    if (listen(sock, 10) < 0) {
        _hs_error("listen failed: %s", strerror(errno));
        return -1;
    }

    return 1;
}


static int
accept_connection(int sock)
{
    struct sockaddr_in     client;
#ifdef HAVE_SOCKLEN_T
    socklen_t           len;
#else
    size_t len;
#endif
    int                 newsock;

    len = sizeof client;

    if ((newsock = accept(sock, (struct sockaddr *) &client, &len)) < 0) {
        _hs_error("accept failed: %s", strerror(errno));
        return -1;
    }

    return newsock;
}


static int
child_main(int newsock, char **argv)
{
    if ((dup2(newsock, STDIN_FILENO) < 0)
        || (dup2(newsock, STDOUT_FILENO) < 0)) {
        _hs_error("dup2 in child failed: %s", strerror(errno));
        return 1;
    }
           
    if (close(newsock) < 0) {
        _hs_error("close spare child socket: %s", strerror(errno));
        return -1;
    }


    if ((execvp(argv[0], argv)) < 0) {
        _hs_error("exec in child failed: %s", strerror(errno));
        return 1;
    }

    return 0;                   /* prob unreachable */
}


static int
fork_and_serve(int srvr_sock, int newsock, char **argv)
{
    int pid;

    pid = fork();
    if (pid < 0) {
        _hs_error("error forking child: %s", strerror(errno));
        return -1;
    } else if (pid == 0) {
        if (close(srvr_sock) < 0) {
            _hs_error("close listen socket: %s", strerror(errno));
            return -1;
        }
        exit(child_main(newsock, argv));
    } else {
        _hs_trace("forked child %d", pid);
        close(newsock);
        return 0;
    }
}


static void
sigchld_handler(int UNUSED(signum))
{
    int pid, status, serrno;
    serrno = errno;
    while (1) {
        pid = waitpid((pid_t) -1, &status, WNOHANG);
        if (pid < 0 && errno == ECHILD)
            break;
        else if (pid < 0) {
            _hs_error("waitpid failed: %s", strerror(errno));
            break;
        }
        else if (pid == 0)
            break;
        _hs_trace("child %d exited with status %d", pid, status);
    }
    errno = serrno;
}





int
main(int argc, char **argv)
{
    int                 sock;
    int                 port;
    int                 newsock;
    int                 c;
    int                 single = 0;

    		/* may turn it on later */
    while ((c = getopt(argc, argv, "1D")) != -1) {
	switch (c) {
	case '?':
	case ':':
	    return -1;
        case '1':
            single = 1;
            break;
    	case 'D':
	    if (!hs_supports_trace()) {
		_hs_error("library does not support trace");
	    }
	    hs_trace_set_level(LOG_DEBUG);
	    break;
        default:
            abort();
	}
    }

    argc -= optind;
    argv += optind;
    if (argc <= 0) {
        show_usage();
        return 1;
    }

    signal(SIGCHLD, sigchld_handler);

    if (bind_any_socket(&sock, &port) < 0) {
        return 1;
    }

    /* print the port to stdout so it can be used by scripts. */
    printf("%d\n", port);
    fflush(stdout);
    
    while (1) {
        if ((newsock = accept_connection(sock)) < 0)
            return 1;
        if (single) {
            if (child_main(newsock, argv))
                return 1;
            else
                return 0;
        } else {
            if ((fork_and_serve(sock, newsock, argv)) < 0)
                return 1;
        }
    }

    return 0;                   /* probably unreached */
}