-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paths2f.c
235 lines (209 loc) · 5.39 KB
/
s2f.c
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
/*
* This is the vstt Sourcecode - http://wendzel.de
*
* Copyright (c) 2006,2016 Steffen Wendzel, www.wendzel.de
* - All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include "main.h"
int sockfd;
int connfd;
socklen_t size;
struct sockaddr_in sa;
int port;
int i_am_a_server;
int yup;
void
usage()
{
extern char *__progname;
fprintf(stderr, "usage: %s [-s] [-i input-fifo] "
"[-o output-fifo] -p port\n", __progname);
exit(1);
}
void
do_connect()
{
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err(1, "socket()");
bzero(&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
if (i_am_a_server) {
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
&yup, sizeof(yup)) != 0)
err(1, "setsockopt(SO_REUSEADDR)");
sa.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr *) &sa,
sizeof(struct sockaddr_in)) < 0)
err(1, "bind()");
/* do not use multiple connections, only accept ONE at
* a time to make sure we recv()/send() only the right
* data.
*/
if (listen(sockfd, 1) < 0)
err(1, "listen()");
size = sizeof(struct sockaddr_in);
if ((connfd=accept(sockfd, (struct sockaddr *) &sa,
&size)) < 0)
err(1, "accept()");
printf("s2f ready.\n");
} else {
if (!inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr))
err(1, "inet_pton");
while (connect(sockfd, (struct sockaddr *) &sa,
sizeof(struct sockaddr_in)) == -1) {
perror("connect");
close(sockfd);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err(1, "socket()");
sleep(1);
}
printf("connected.\n");
/* play it on this way */
connfd = sockfd;
}
}
int
main(int argc, char *argv[])
{
char *input;
char *output;
int ch;
int iffd, offd;
struct sockaddr name;
socklen_t namelen;
fd_set fds;
char buf[16*1024] = { '\0' };
int len, slen;
int peak;
#ifdef DEBUG
int incoming_pkts = 0;
int sent_pkts = 0;
#endif
input = output = NULL;
port = i_am_a_server = 0;
yup = 1;
namelen = sizeof(name);
while ((ch = getopt(argc, argv, "hi:o:p:s")) != -1) {
switch (ch) {
case 'i':
if (!(input = (char *) calloc(strlen(optarg)+1,
sizeof(char))))
err(1, "calloc()");
strncpy(input, optarg, strlen(optarg));
break;
case 'o':
if (!(output = (char *)
calloc(strlen(optarg)+1, sizeof(char))))
err(1, "calloc()");
strncpy(output, optarg, strlen(optarg));
break;
case 'p':
port = atoi(optarg);
break;
case 's':
i_am_a_server = 1;
break;
case 'h':
/* FALLTROUGH */
default:
usage();
/* NOTREACHED */
}
}
if (input == NULL)
input = RECVFPEER_FIFO;
if (output == NULL)
output = SEND2PEER_FIFO;
if (port == 0)
usage();
/* fifo setup */
if ((iffd = open(input, O_RDWR)) == -1)
err(1, "open(input)");
if ((offd = open(output, O_RDWR)) == -1)
err(1, "open(output)");
/* network setup */
do_connect();
/* select setup */
FD_ZERO(&fds);
/* mainloop */
do {
if (getpeername(connfd, &name, &namelen) == -1) {
if (errno == ENOTCONN || errno == EBADF || errno == ENOTSOCK) {
/* socket closed || not connected
* => re-connect */
printf("s2f: connection closed. re-connecting.\n");
do_connect();
} else {
err(1, "s2f: getpeername");
}
}
FD_SET(connfd, &fds);
FD_SET(iffd, &fds);
peak = (connfd > iffd ? connfd : iffd);
if (select(peak+1, &fds, NULL, NULL, NULL) == -1) {
if (errno == EINTR)
continue;
else
err(1, "select");
}
if (FD_ISSET(iffd, &fds)) {
// read the buf
len = read(iffd, buf, sizeof(buf)-1);
if (len < 0)
err(1, "read");
// send the buf
if (len) {
if (!(slen = send(connfd, buf, len, 0)))
err(1, "send");
#ifdef DEBUG
printf("recv: %i: len=%i , slen=%i\n",
++incoming_pkts, len, slen);
FFLUSH
#endif
}
bzero(buf, len);
}
if (FD_ISSET(connfd, &fds)) {
// read from socket
len = recv(connfd, buf, sizeof(buf)-1, 0);
if (len < 0)
err(1, "recv");
// write the buf
if (len) {
if (!(slen = write(offd, buf, len)))
err(1, "write");
#ifdef DEBUG
printf("send: %i: len=%i\n",
++sent_pkts, len);
FFLUSH
#endif
}
bzero(buf, len);
}
} while (1);
return 0;
}