[go: up one dir, main page]

File: xioopen.c

package info (click to toggle)
socat 1.4.3.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,192 kB
  • ctags: 3,709
  • sloc: ansic: 19,348; sh: 3,435; makefile: 154
file content (410 lines) | stat: -rwxr-xr-x 10,489 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* $Id: xioopen.c,v 1.104 2005/08/18 19:59:25 gerhard Exp $ */
/* Copyright Gerhard Rieger 2001-2005 */
/* Published under the GNU General Public License V.2, see file COPYING */

/* this is the source file of the extended open function */

#include "xiosysincludes.h"

#include "xioopen.h"
#include "xiomodes.h"


static xiofile_t *xioallocfd(void);

const struct addrname addressnames[] = {
#if 1
#if WITH_STDIO
   { "-",		&addr_stdio },
#endif
#if WITH_CREAT
   { "creat",	&addr_creat },
   { "create",	&addr_creat },
#endif
#if WITH_EXEC
   { "exec",		&addr_exec },
#endif
#if WITH_FDNUM
   { "fd",		&addr_fd },
#endif
#if WITH_PIPE
   { "fifo",		&addr_pipe },
#endif
#if WITH_FILE
   { "file",		&addr_open },
#endif
#if WITH_GOPEN
   { "gopen",	&addr_gopen },
#endif
#if WITH_IP4 && WITH_TCP
   { "inet",		&addr_tcp4_connect },
#endif
#if WITH_IP4 && WITH_TCP && WITH_LISTEN
   { "inet-l",	&addr_tcp4_listen },
   { "inet-listen",	&addr_tcp4_listen },
#endif
#if WITH_IP6 && WITH_TCP
   { "inet6",	&addr_tcp6_connect },
#endif
#if WITH_IP6 && WITH_TCP && WITH_LISTEN
   { "inet6-l",	&addr_tcp6_listen },
   { "inet6-listen",	&addr_tcp6_listen },
#endif
#if WITH_IP4 && WITH_RAWIP
   { "ip",		&addr_rawip4 },
   { "ip4",		&addr_rawip4 },
#endif
#if WITH_IP6 && WITH_RAWIP
   { "ip6",		&addr_rawip6 },
#endif
#if WITH_UNIX
   { "local",	&addr_unix_connect },
#endif
#if WITH_FILE
   { "open",		&addr_open },
#endif
#if WITH_OPENSSL
   { "openssl",		&addr_openssl },
   { "openssl-connect",		&addr_openssl },
#if WITH_LISTEN
   { "openssl-listen",		&addr_openssl_listen },
#endif
#endif
#if WITH_PIPE
   { "pipe",		&addr_pipe },
#endif
#if WITH_PROXY
   { "proxy",		&addr_proxy_connect },
   { "proxy-connect",	&addr_proxy_connect },
#endif
#if WITH_PTY
   { "pty",		&addr_pty },
#endif
#if WITH_READLINE
   { "readline",	&addr_readline },
#endif
#if WITH_SOCKS4
   { "socks",	&addr_socks4_connect },
   { "socks4",	&addr_socks4_connect },
#endif
#if WITH_SOCKS4A
   { "socks4a",	&addr_socks4a_connect },
#endif
#if WITH_OPENSSL
   { "ssl",		&addr_openssl },
#if WITH_LISTEN
   { "ssl-l",		&addr_openssl_listen },
#endif
#endif
#if WITH_STDIO
   { "stderr",	&addr_stderr },
   { "stdin",	&addr_stdin },
   { "stdio",	&addr_stdio },
   { "stdout",	&addr_stdout },
#endif
#if WITH_SYSTEM
   { "system",	&addr_system },
#endif
#if WITH_IP4 && WITH_TCP
   { "tcp",		&addr_tcp4_connect },
#endif
#if WITH_IP4 && WITH_TCP && WITH_LISTEN
   { "tcp-l",	&addr_tcp4_listen },
   { "tcp-listen",	&addr_tcp4_listen },
#endif
#if WITH_IP4 && WITH_TCP
   { "tcp4",		&addr_tcp4_connect },
#endif
#if WITH_IP4 && WITH_TCP && WITH_LISTEN
   { "tcp4-l",	&addr_tcp4_listen },
   { "tcp4-listen",	&addr_tcp4_listen },
#endif
#if WITH_IP6 && WITH_TCP
   { "tcp6",		&addr_tcp6_connect },
#endif
#if WITH_IP6 && WITH_TCP && WITH_LISTEN
   { "tcp6-l",	&addr_tcp6_listen },
   { "tcp6-listen",	&addr_tcp6_listen },
#endif
#if WITH_IP4 && WITH_UDP
   { "udp",		&addr_udp4_connect },
#endif
#if WITH_IP4 && WITH_UDP && WITH_LISTEN
   { "udp-l",	&addr_udp4_listen },
   { "udp-listen",	&addr_udp4_listen },
#endif
#if WITH_IP4 && WITH_UDP
   { "udp4",		&addr_udp4_connect },
#endif
#if WITH_IP4 && WITH_UDP && WITH_LISTEN
   { "udp4-l",	&addr_udp4_listen },
   { "udp4-listen",	&addr_udp4_listen },
#endif
#if WITH_IP6 && WITH_UDP
   { "udp6",		&addr_udp6_connect },
#endif
#if WITH_IP6 && WITH_UDP && WITH_LISTEN
   { "udp6-l",	&addr_udp6_listen },
   { "udp6-listen",	&addr_udp6_listen },
#endif
#if WITH_UNIX
   { "unix",		&addr_unix_connect },
   { "unix-connect",	&addr_unix_connect },
#endif
#if WITH_UNIX && WITH_LISTEN
   { "unix-l",	&addr_unix_listen },
   { "unix-listen",	&addr_unix_listen },
#endif
#else /* !0 */
#  if WITH_INTEGRATE
#    include "xiointegrate.c"
#  else
#    include "xioaddrtab.c"
#  endif
#endif /* !0 */
   { NULL }	/* end marker */
} ;


/* prepares a xiofile_t record for dual address type:
   sets the tag and allocates memory for the substreams.
   returns 0 on success, or <0 if an error occurred.
*/
int xioopen_makedual(xiofile_t *file) {
   file->tag = XIO_TAG_DUAL;
   file->common.flags = XIO_RDWR;
   if ((file->dual.stream[0] = xioallocfd()) == NULL)
      return -1;
   file->dual.stream[0]->common.flags = XIO_RDONLY;
   if ((file->dual.stream[1] = xioallocfd()) == NULL)
      return -1;
   file->dual.stream[1]->common.flags = XIO_WRONLY;
   return 0;
}

static xiofile_t *xioallocfd(void) {
   xiofile_t *fd;

   if ((fd = Calloc(1, sizeof(xiofile_t))) == NULL) {
      return NULL;
   }
   /* some default values; 0's and NULL's need not be applied (calloc'ed) */
   fd->common.tag       = XIO_TAG_INVALID;
/* fd->common.addr      = NULL; */
   fd->common.flags     = XIO_RDWR;

#if WITH_RETRY
/* fd->stream.retry	= 0; */
/* fd->stream.forever	= false; */
   fd->stream.intervall.tv_sec  = 1;
/* fd->stream.intervall.tv_nsec = 0; */
#endif /* WITH_RETRY */
/* fd->common.ignoreeof = false; */
/* fd->common.eof       = 0; */

   fd->stream.fd        = -1;
   fd->stream.dtype     = DATA_STREAM;
#if WITH_SOCKET
/* fd->stream.salen     = 0; */
#endif /* WITH_SOCKET */
   fd->stream.howtoend  = END_CLOSE;
/* fd->stream.name      = NULL; */
/* fd->stream.para.exec.pid = 0; */
   fd->stream.lineterm  = LINETERM_RAW;

   /*!! support n socks */
   if (!sock[0]) {
      sock[0] = fd;
   } else {
      sock[1] = fd;
   }
   return fd;
}

/* parse the argument that specifies a two-directional data stream */
xiofile_t *xioopen(const char *addr,	/* address specification */
		   int xioflags) {
   xiofile_t *fd;
   char *a1, *a2;

   if (xioinitialize() < 0) {
      return NULL;
   }

   if ((a1 = strdup(addr)) == NULL) {
      Error1("strdup("F_Zu"): out of memory", strlen(addr)+1);
      return NULL;
   }

   if (a2 = strstr(a1, xioopts.pipesep)) {
      /* in- and out-pipe are specified separately */
      /* xioflags argument: */
      if ((xioflags&XIO_ACCMODE) != XIO_RDWR) {
	 Error("unidirectional open with dual address");
	 return NULL;
      }
      if ((fd = xioallocfd()) == NULL) {
	 return NULL;
      }
      if (xioopen_makedual(fd) < 0)
	 return NULL;
      *a2 = '\0';
      a2 += strlen(xioopts.pipesep);
      if (xioopensingle(a1, fd->dual.stream[0], XIO_RDONLY|(xioflags&~XIO_ACCMODE&~XIO_MAYEXEC)) != 0)
	 return NULL;
      if (xioopensingle(a2, fd->dual.stream[1], XIO_WRONLY|(xioflags&~XIO_ACCMODE&~XIO_MAYEXEC)) != 0)
	 return NULL;
#if 0
      fd->common.ignoreeof = fd->dual.stream[0]->common.ignoreeof;
#endif
      fd->common.flags = (XIO_RDWR | fd->dual.stream[0]->common.flags&(~XIO_ACCMODE) |
			  fd->dual.stream[1]->common.flags&(~XIO_ACCMODE));

   /* there are some specific addresses that we must handle specially if they 
      occur as single address, because copying of the first fd to the second 
      does not do what we want: STDIO, -, PIPE */
      /* but only for the bidirectional case */
#if WITH_STDIO
   } else if ((xioflags&XIO_ACCMODE) == XIO_RDWR &&
	      !strncasecmp("STDIO", a1, 5) &&
	      (a1[5] == ',' || a1[5] == '\0')) {
      if ((fd = xioallocfd()) == NULL) {
	 return NULL;
      }
      if (xioopen_stdio_bi(a1+5, fd) < 0) {
	 free(fd);
	 return NULL;
      }
#endif /* WITH_STDIO */

#if WITH_STDIO
    } else if ((xioflags&XIO_ACCMODE) == XIO_RDWR &&
	      !strncmp("-", a1, 1) &&
	      (a1[1] == ',' || a1[1] == '\0')) {
      if ((fd = xioallocfd()) == NULL) {
	 return NULL;
      }
      if (xioopen_stdio_bi(a1+1, fd) < 0) {
	 free(fd);
	 return NULL;
      }
#endif /* WITH_STDIO */

#if WITH_PIPE
    } else if ((xioflags&XIO_ACCMODE) == XIO_RDWR &&
	       (!strncasecmp("PIPE", a1, 4) ||
	       !strncasecmp("FIFO", a1, 4) ||
	       !strncasecmp("ECHO", a1, 4)) &&
	      (a1[4] == ',' || a1[4] == '\0')) {
      /*! xioflags argument */
      if ((fd = xioallocfd()) == NULL) {
	 return NULL;
      }
      if (xioopen_fifo_unnamed(a1+4, fd) < 0) {
	 free(fd);
	 return NULL;
      }
#endif /* WITH_PIPE */

   } else {
      if ((fd = xioallocfd()) == NULL) {
	 return NULL;
      }
      /* a "usual" bidirectional stream specification, one address */
      if (xioopensingle(a1, fd, xioflags) != 0) {
	 free(fd);
	 return 0;
      }
      fd->common.tag       = XIO_TAG_RDWR;
#if 0
      /* some extended default values */
      fd->stream.fd        = -1;
      fd->stream.dtype     = DATA_STREAM;
#if WITH_SOCKET
      fd->stream.salen     = 0;
#endif /* WITH_SOCKET */
      fd->stream.howtoend  = END_CLOSE;
      fd->stream.name      = NULL;
      fd->stream.para.exec.pid = 0;
      fd->stream.lineterm  = LINETERM_RAW;
#endif
#if 0
	 if (fd->stream[1].fd < 0) {
	    fd->stream[1] = sock->fd[0];
	    fd->stream[1].fd = Dup(fd->stream[0].fd);
	    /*0 Info2("dup(%d) -> %d", fd->stream[0].fd, fd->stream[1].fd);*/
	    Fcntl_l(fd->fd[1].fd, F_SETFD,
		    Fcntl(fd->fd[0].fd, F_GETFD) ? FD_CLOEXEC : 0);
	 }
#endif
   }

   fd->common.flags     &= (~XIO_ACCMODE);
   fd->common.flags     |= (xioflags & XIO_ACCMODE);

   free(a1);
   return fd;
}


/* xioflags is XIO_RDONLY, XIO_WRONLY, or XIO_RDWR, or'ed with XIO_MAYCHILD etc. */
int xioopensingle(char *addr, union bipipe *fd, int xioflags) {
   struct addrname *ae;
   const struct addrdesc *addrdesc;
   char *colon, *comma, *a1, *akey;
   int result;

   /* identify where address keyword ends */
   colon = strchr(addr, ':');
   comma = strchr(addr, ',');
   if (colon) {
      if (comma) {
	 a1 = Min(colon, comma);
      } else {
	 a1 = colon;
      }
   } else {
      if (comma) {
	 a1 = comma;
      } else {
	 a1 = strchr(addr, '\0');
      }
   }
   if ((akey = Malloc(a1-addr+1)) == NULL)
      return -1;
   strncpy(akey, addr, a1-addr);
   akey[a1-addr] = '\0';

   ae = (struct addrname *)
      keyw((struct wordent *)&addressnames, akey,
	   sizeof(addressnames)/sizeof(struct addrname)-1);
   free(akey);
   if (ae) {
      addrdesc = ae->desc;
      if (*a1) { *a1++ = '\0'; }
   } else {
      char *a2;
#if WITH_FDNUM
      if (isdigit(addr[0]&0xff)) {
	 a1 = addr;
	 addrdesc = &addr_fd;
      } else 
#endif /* WITH_FDNUM */
#if WITH_GOPEN
      if ((a2 = strchr(addr, '/')) && (a2 < a1 || a1 == NULL)) {
	 a1 = addr;
	 addrdesc = &addr_gopen;
      } else
#endif /* WITH_GOPEN */
      {
	 Error1("unknown device/address \"%s\"", addr);
	 return STAT_NORETRY;
      }
   }

   fd->common.tag  = XIO_TAG_RDWR;
   fd->common.addr = addrdesc;
   result = (*addrdesc->func)(a1, xioflags, fd, addrdesc->groups, addrdesc->arg1,
			      addrdesc->arg2, addrdesc->argp1);
   return result;
}