File: xio-test.c

package info (click to toggle)
socat 2.0.0~beta9-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 3,740 kB
  • sloc: ansic: 30,875; sh: 11,630; makefile: 149
file content (228 lines) | stat: -rw-r--r-- 6,608 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
/* source: xio-test.c */
/* Copyright Gerhard Rieger 2007-2009 */
/* Published under the GNU General Public License V.2, see file COPYING */

/* this file contains the source for an intermediate test address that appends
   '>' to every block transferred from right to left, and '<' in the other
   direction */

#include "xiosysincludes.h"
#include "xioopen.h"

#include "xio-test.h"


#if WITH_TEST

static int xioopen_test(int argc, const char *argv[], struct opt *opts,
				  int xioflags, xiofile_t *xxfd,
				  unsigned groups, int dummy1, int dummy2,
				  int dummy3);
static int xioopen_testuni(int argc, const char *argv[], struct opt *opts,
				  int xioflags, xiofile_t *xxfd,
				  unsigned groups, int dummy1, int dummy2,
				  int dummy3);
static int xioopen_testrev(int argc, const char *argv[], struct opt *opts,
				  int xioflags, xiofile_t *xxfd,
				  unsigned groups, int dummy1, int dummy2,
				  int dummy3);

static const struct xioaddr_inter_desc xiointer_test0ro = { XIOADDR_PROT, "test", 0, XIOBIT_RDONLY, 0/*groups*/, XIOSHUT_UNSPEC, XIOCLOSE_UNSPEC, xioopen_test, 0, 0, 0, XIOBIT_WRONLY HELP("") };
static const struct xioaddr_inter_desc xiointer_test0wo = { XIOADDR_PROT, "test", 0, XIOBIT_WRONLY, 0/*groups*/, XIOSHUT_UNSPEC, XIOCLOSE_UNSPEC, xioopen_test, 0, 0, 0, XIOBIT_RDONLY HELP("") };
static const struct xioaddr_inter_desc xiointer_test0rw = { XIOADDR_PROT, "test", 0, XIOBIT_RDWR,   0/*groups*/, XIOSHUT_UNSPEC, XIOCLOSE_UNSPEC, xioopen_test, 0, 0, 0, XIOBIT_RDWR   HELP("") };

const union xioaddr_desc *xioaddrs_test[] = {
   (union xioaddr_desc *)&xiointer_test0ro,
   (union xioaddr_desc *)&xiointer_test0wo,
   (union xioaddr_desc *)&xiointer_test0rw,
   NULL };


static const struct xioaddr_inter_desc xiointer_testuni = { XIOADDR_PROT, "testuni", 0, XIOBIT_WRONLY, 0/*groups*/, XIOSHUT_CLOSE, XIOCLOSE_NONE, xioopen_testuni, 0, 0, 0, XIOBIT_RDONLY HELP("") };

const union xioaddr_desc *xioaddrs_testuni[] = {
   (union xioaddr_desc *)&xiointer_testuni,
   NULL };


static const struct xioaddr_inter_desc xiointer_testrev = { XIOADDR_PROT, "testrev", 0, XIOBIT_WRONLY, 0/*groups*/, XIOSHUT_CLOSE, XIOCLOSE_NONE, xioopen_testrev, 0, 0, 0, XIOBIT_RDONLY HELP("") };

const union xioaddr_desc *xioaddrs_testrev[] = {
   (union xioaddr_desc *)&xiointer_testrev,
   NULL };

static int xioopen_test(int argc, const char *argv[], struct opt *opts,
				  int xioflags, xiofile_t *xxfd,
				  unsigned groups, int dummy, int dummy2,
				  int dummy3) {
   struct single *xfd = &xxfd->stream;
   int result;

   assert(argc == 1);
   assert(!(xfd->rfd < 0 && xfd->wfd < 0));	/*!!!*/

   applyopts(-1, opts, PH_INIT);
   if (applyopts_single(xfd, opts, PH_INIT) < 0)  return -1;

   Notice("opening TEST");
   xfd->dtype = XIODATA_TEST;
   applyopts(xfd->rfd, opts, PH_ALL);
   if ((result = _xio_openlate(xfd, opts)) < 0)
      return result;
   return 0;
}

static int xioopen_testuni(int argc, const char *argv[], struct opt *opts,
				  int xioflags, xiofile_t *xxfd,
				  unsigned groups, int dummy, int dummy2,
				  int dummy3) {
   struct single *xfd = &xxfd->stream;
   int result;

   assert(argc == 1);
   assert(!(xfd->rfd < 0 && xfd->wfd < 0));	/*!!!*/

   applyopts(-1, opts, PH_INIT);
   if (applyopts_single(xfd, opts, PH_INIT) < 0)  return -1;

   Notice("opening TESTUNI");
   xfd->dtype = XIODATA_TESTUNI;
   applyopts(xfd->rfd, opts, PH_ALL);
   if ((result = _xio_openlate(xfd, opts)) < 0)
      return result;
   return 0;
}

static int xioopen_testrev(int argc, const char *argv[], struct opt *opts,
				  int xioflags, xiofile_t *xxfd,
				  unsigned groups, int dummy, int dummy2,
				  int dummy3) {
   struct single *xfd = &xxfd->stream;
   int result;

   assert(argc == 1);
   assert(!(xfd->rfd < 0 && xfd->wfd < 0));	/*!!!*/

   applyopts(-1, opts, PH_INIT);
   if (applyopts_single(xfd, opts, PH_INIT) < 0)  return -1;

   Notice("opening TESTREV");
   xfd->dtype = XIODATA_TESTREV;
   applyopts(xfd->rfd, opts, PH_ALL);
   if ((result = _xio_openlate(xfd, opts)) < 0)
      return result;
   return 0;
}

size_t xioread_test(struct single *sfd, void *buff, size_t bufsiz) {
   int fd = sfd->rfd;
   ssize_t bytes;
   int _errno;

   do {
      bytes = Read(fd, buff, bufsiz-1);
   } while (bytes < 0 && errno == EINTR);
   if (bytes < 0) {
      _errno = errno;
      switch (_errno) {
      case EPIPE: case ECONNRESET:
	 Warn4("read(%d, %p, "F_Zu"): %s",
	       fd, buff, bufsiz-1, strerror(_errno));
	 break;
      default:
	 Error4("read(%d, %p, "F_Zu"): %s",
		fd, buff, bufsiz-1, strerror(_errno));
      }
      return -1;
   }
   if (bytes == 0) {
      return 0;
   }
   ((char *)buff)[bytes] = '<';
   return bytes+1;
}

size_t xiowrite_test(struct single *sfd, const void *buff, size_t bytes) {
   int fd = sfd->wfd;
   void *buff1;
   ssize_t writt;
   int _errno;

   if ((buff1 = Malloc(bytes+1)) == NULL) {
      return -1;
   }
   memcpy(buff1, buff, bytes);
   ((char *)buff1)[bytes] = '>';
   do {
      writt = Write(fd, buff1, bytes+1);
   } while (writt < 0 && errno == EINTR);
   if (writt < 0) {
      _errno = errno;
      switch (_errno) {
      case EPIPE:
      case ECONNRESET:
	 if (sfd->cool_write) {
	    Notice4("write(%d, %p, "F_Zu"): %s",
		    fd, buff1, bytes+1, strerror(_errno));
	    break;
	 }
	 /*PASSTHROUGH*/
      default:
	 Error4("write(%d, %p, "F_Zu"): %s",
		fd, buff1, bytes+1, strerror(_errno));
      }
      errno = _errno;
      free(buff1);
      return -1;
   }
   if ((size_t)writt < bytes) {
      Warn2("write() only wrote "F_Zu" of "F_Zu" bytes",
	    writt, bytes+1);
   }
   free(buff1);
   return writt;
}

size_t xiowrite_testrev(struct single *sfd, const void *buff, size_t bytes) {
   int fd = sfd->wfd;
   void *buff1;
   ssize_t writt;
   int _errno;

   if ((buff1 = Malloc(bytes+1)) == NULL) {
      return -1;
   }
   memcpy(buff1, buff, bytes);
   ((char *)buff1)[bytes] = '<';
   do {
      writt = Write(fd, buff1, bytes+1);
   } while (writt < 0 && errno == EINTR);
   if (writt < 0) {
      _errno = errno;
      switch (_errno) {
      case EPIPE:
      case ECONNRESET:
	 if (sfd->cool_write) {
	    Notice4("write(%d, %p, "F_Zu"): %s",
		    fd, buff1, bytes+1, strerror(_errno));
	    break;
	 }
	 /*PASSTHROUGH*/
      default:
	 Error4("write(%d, %p, "F_Zu"): %s",
		fd, buff1, bytes+1, strerror(_errno));
      }
      errno = _errno;
      free(buff1);
      return -1;
   }
   if ((size_t)writt < bytes) {
      Warn2("write() only wrote "F_Zu" of "F_Zu" bytes",
	    writt, bytes+1);
   }
   free(buff1);
   return writt;
}

#endif /* WITH_TEST */