This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iop.c
244 lines (221 loc) · 4.65 KB
/
iop.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
236
237
238
239
240
241
242
243
244
/*
* rocks/iop.c
*
* Interoperability of rocks with ordinary sockets.
*
* Copyright (C) 2001 Victor Zandy
* See COPYING for distribution terms.
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include "rs.h"
#include "log.h"
#define ROCKIDLEN 16
static char rockid[ROCKIDLEN] = "IROCKYOUROCKMAN";
/* return 1 if peer is enhanced; return 0 if not or if there was an error */
static int
client_probe(rs_t rs)
{
char buf[ROCKIDLEN];
int rv;
ring_t ring;
fd_set fds;
struct timeval tv;
int ret = 0;
int n;
shutdown(rs->sd, 1);
ring = rs_new_ring(ROCKIDLEN);
if (!ring)
return 0;
while (1) {
FD_ZERO(&fds);
FD_SET(rs->sd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 500000;
n = select(rs->sd+1, &fds, NULL, NULL, &tv);
if (0 > n && errno == EINTR)
continue;
if (0 > n) {
rs_log("edp: rock <%p> client probe error", rs);
break;
}
if (0 == n) {
rs_log("edp: rock <%p> client probe timeout", rs);
break;
}
rv = read(rs->sd, buf, sizeof(buf));
if (0 > rv && errno == EINTR)
continue;
if (!rv || (0 > rv && errno == ECONNRESET)) {
/* server closed or reset connection */
char *p = rs_ring_data(ring);
int n = rs_ring_nbytes(ring);
if (n < ROCKIDLEN)
break;
p += n - ROCKIDLEN;
if (!strncmp(p, rockid, ROCKIDLEN))
ret = 1;
break;
}
rs_push_ring(ring, buf, rv);
}
close(rs->sd);
rs_free_ring(ring);
return ret;
}
static unsigned long
room(ring_t ring)
{
return rs_ring_size(ring) - rs_ring_nbytes(ring);
}
static ring_t
double_ring(ring_t ring)
{
ring_t r;
unsigned long sz;
sz = 2 * rs_ring_size(ring);
r = rs_new_ring(sz);
if (!r)
return NULL;
rs_push_ring(r, rs_ring_data(ring), rs_ring_nbytes(ring));
rs_free_ring(ring);
return r;
}
static int
scan_for_rockid(rs_t rs)
{
ring_t spill, stage;
char buf[ROCKIDLEN];
int rv, over;
stage = rs_new_ring(ROCKIDLEN);
spill = rs_new_ring(2048); /* FIXME: 2048 is arbitrary */
if (!stage || !spill)
goto err;
while (1) {
rv = read(rs->sd, buf, sizeof(buf));
if (0 > rv && errno != EINTR)
goto err;
if (0 == rv)
goto err;
/* if stage does not have room for the new data,
move the difference from stage to the spill */
over = rv - room(stage);
if (over > 0 && over > room(spill))
if (!(spill = double_ring(spill)))
goto err;
if (over > 0) {
rs_push_ring(spill, rs_ring_data(stage), over);
rs_pop_ring(stage, over);
}
/* stage buf */
rs_push_ring(stage, buf, rv);
/* check for announcement */
if (rs_ring_nbytes(stage) == ROCKIDLEN
&& !strncmp(rs_ring_data(stage), rockid, ROCKIDLEN))
break; /* found it */
}
rs_free_ring(stage);
if (rs_ring_nbytes(spill) > 0)
rs->edpspill = spill;
else
rs_free_ring(spill);
return 0;
err:
if (stage)
rs_free_ring(stage);
if (spill)
rs_free_ring(spill);
return -1;
}
/* returns:
-1 error - no connection
0 peer is not a rock
1 peer is a rock
*/
int
rs_iop_connect(rs_t rs)
{
int td;
int isrock;
isrock = client_probe(rs); /* closes rs->sd */
#if 0
if (isrock)
rs_log("edp: rock <%p> peer is a rock", rs);
else
rs_log("edp: rock <%p> peer is not a rock", rs);
#endif
td = socket(AF_INET, SOCK_STREAM, 0);
if (0 > td) {
rserrno = errno;
return -1;
}
if (td != rs->sd) {
if (0 > dup2(td, rs->sd)) {
rserrno = errno;
return -1;
}
close(td);
}
/* reconnect */
if (0 > connect(rs->sd, (struct sockaddr *)&rs->sa_peer,
sizeof(struct sockaddr_in))) {
rserrno = errno;
return -1;
}
if (!isrock)
return 0;
/* send the announcement */
if (0 > rs_xwrite(rs->sd, rockid, ROCKIDLEN))
return -1;
/* buffer and scan for the reply */
if (0 > scan_for_rockid(rs))
return -1;
return isrock;
}
int
rs_iopsrv(rs_t rs, char *z, int len, edp_result_t *result)
{
int l;
int rv;
char buf[ROCKIDLEN];
/* any which way, we're not coming back here */
rs->state = RS_NOTCONNECTED;
*result = EDP_NOTROCK;
l = MIN(len, ROCKIDLEN);
rv = read(rs->sd, buf, l);
if (0 > rv)
return -1;
if (0 == rv) {
*result = EDP_PROBE;
if (0 > rs_xwrite(rs->sd, rockid, ROCKIDLEN))
return -1;
rs->state = RS_EDP; /* FIXME: good idea? */
return 0;
}
if (!strncmp(rockid, buf, rv)) {
if (rv != ROCKIDLEN) {
/* get the rest */
rv = rs_xread(rs->sd, &buf[rv], ROCKIDLEN-rv, 0);
if (0 >= rv)
return -1;
if (strncmp(rockid, buf, ROCKIDLEN))
/* FIXME: we should tolerate this */
return -1;
}
*result = EDP_ROCK;
if (0 > rs_xwrite(rs->sd, rockid, ROCKIDLEN))
return -1;
if (0 > rs_init_connection(rs))
return -1;
return 0;
}
rs_fallback(rs);
memcpy(z, buf, rv);
return rv;
}