-
Notifications
You must be signed in to change notification settings - Fork 1
/
bsocket.c
292 lines (247 loc) · 6.34 KB
/
bsocket.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
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
/* bsocket.c - buffer socket functions
* Copyright (C) 1988-2016 Sean MacLennan <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#ifndef WIN32
#include <sys/uio.h>
#endif
#include "buff.h"
#define MAX_IOVS 16
#ifdef WIN32
/* These aren't optimal... but they should work */
struct iovec {
void *iov_base;
size_t iov_len;
};
int readv(int fd, const struct iovec *iov, int iovcnt)
{
int i, n, n_read = 0;
for (i = 0; i < iovcnt; ++i, ++iov)
if (iov->iov_len) {
n = read(fd, iov->iov_base, iov->iov_len);
if (n < 0)
return n;
else if (n == 0)
return n_read;
n_read += n;
}
return n_read;
}
int writev(int fd, const struct iovec *iov, int iovcnt)
{
int i, n, n_wrote = 0;
for (i = 0; i < iovcnt; ++i, ++iov)
if (iov->iov_len) {
n = write(fd, iov->iov_base, iov->iov_len);
if (n < 0)
return n;
else if (n == 0)
return n_wrote;
n_wrote += n;
}
return n_wrote;
}
#endif
/* These can be used with files... but where written to use with
* sockets. */
/** Read from a file descriptor using readv. Simple version; only
* reads at most two pages. Can be used for file but meant for
* sockets.
*/
int bread(struct buff *buff, int fd)
{
int n, n_read, left;
struct page *npage;
struct iovec iovs[2];
if (buff->curchar != curplen(buff)) {
/* Move data after point into new page */
if (!pagesplit(buff, buff->curchar))
return -ENOMEM;
}
left = PGSIZE - curplen(buff);
/* try to fill current page */
if (left) {
iovs[0].iov_base = buff->curcptr;
iovs[0].iov_len = left;
} else
iovs[0].iov_len = 0;
/* plus a new page */
npage = newpage(buff->curpage);
if (!npage)
return -ENOMEM;
iovs[1].iov_base = npage->pdata;
iovs[1].iov_len = PGSIZE;
n = n_read = readv(fd, iovs, 2);
if (n <= 0) {
freepage(buff, npage);
return n;
}
if (left) {
int wrote = MIN(left, n);
buff->curcptr += wrote;
buff->curchar += wrote;
curplen(buff) += wrote;
n -= wrote;
}
if (n > 0) {
makecur(buff, npage, n);
curplen(buff) = n;
} else
freepage(buff, npage);
return n_read;
}
/** Write to a file descriptor using writev. Can be used for files but
* meant for sockets. Leaves the point at the end of the write.
*/
int bwrite(struct buff *buff, int fd, unsigned size)
{
struct iovec iovs[MAX_IOVS];
struct page *pg;
int i, n, amount, did = 0;
do {
unsigned have = curplen(buff) - buff->curchar;
iovs[0].iov_base = buff->curcptr;
iovs[0].iov_len = MIN(have, size);
size -= iovs[0].iov_len;
amount = iovs[0].iov_len;
for (pg = buff->curpage->nextp, i = 1;
i < MAX_IOVS && size > 0 && pg;
++i, pg = pg->nextp) {
iovs[i].iov_base = pg->pdata;
iovs[i].iov_len = MIN(pg->plen, size);
size -= iovs[i].iov_len;
amount += iovs[i].iov_len;
}
do
n = writev(fd, iovs, i);
while (n < 0 && errno == EINTR);
if (n > 0) {
bmove(buff, n);
did += n;
}
} while (n == amount && size > 0);
return did;
}
/* Some bulk insert routines that are handy with sockets. */
/* You must guarantee we are at the end of the page */
static int bappendpage(struct buff *buff, const Byte *data, int size)
{
int appended = 0;
/* Fill the current page */
int n, left = PGSIZE - curplen(buff);
if (left > 0) {
n = MIN(left, size);
memcpy(buff->curcptr, data, n);
buff->curcptr += n;
buff->curchar += n;
curplen(buff) += n;
size -= n;
data += n;
appended += n;
undo_add(buff, n);
}
/* Put the rest in new pages */
while (size > 0) {
struct page *npage = newpage(buff->curpage);
if (!npage)
return appended;
makecur(buff, npage, 0);
n = MIN(PGSIZE, size);
memcpy(buff->curcptr, data, n);
curplen(buff) = n;
makeoffset(buff, n);
undo_add(buff, n);
size -= n;
data += n;
appended += n;
}
return appended;
}
/** Append data to the end of the buffer. Point is left at the end of
* the buffer. Returns how much data was actually appended.
*/
int bappend(struct buff *buff, const Byte *data, int size)
{
btoend(buff);
return bappendpage(buff, data, size);
}
/* Simple version to start.
* Can use size / PGSIZE + 1 + 1 pages.
*/
/** Insert data at the current point. Point is left at the end of the
* inserted data. Returns how much data was actually inserted.
*/
int bindata(struct buff *buff, Byte *data, unsigned size)
{
struct page *npage;
unsigned n, copied = 0;
/* If we can append... use append */
if (buff->curchar == curplen(buff))
return bappendpage(buff, data, size);
n = PGSIZE - curplen(buff);
if (n >= size) { /* fits in this page */
struct mark *m;
n = curplen(buff) - buff->curchar;
memmove(buff->curcptr + size, buff->curcptr, n);
memcpy(buff->curcptr, data, size);
foreach_global_pagemark(buff, m, buff->curpage)
if (m->moffset >= buff->curchar)
m->moffset += size;
foreach_pagemark(buff, m, buff->curpage)
if (m->moffset >= buff->curchar)
m->moffset += size;
buff->curcptr += size;
buff->curchar += size;
curplen(buff) += size;
undo_add(buff, size);
return size;
}
n = curplen(buff) - buff->curchar;
if (n > 0) {
if (!pagesplit(buff, buff->curchar))
return copied;
/* Copy as much as possible to the end of this page */
n = MIN(PGSIZE - buff->curchar, size);
memcpy(buff->curcptr, data, n);
data += n;
size -= n;
undo_add(buff, n);
copied += n;
buff->curcptr += n;
buff->curchar += n;
curplen(buff) = buff->curchar;
}
while (size > 0) {
if (!(npage = newpage(buff->curpage)))
break;
n = MIN(PGSIZE, size);
memcpy(npage->pdata, data, n);
data += n;
size -= n;
undo_add(buff, n);
copied += n;
makecur(buff, npage, n);
curplen(buff) = n;
}
return copied;
}