-
Notifications
You must be signed in to change notification settings - Fork 0
/
real_socket_helper.c
204 lines (170 loc) · 5.25 KB
/
real_socket_helper.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
/* Filename: real_socket_wrapper.c */
#include <assert.h>
#include <errno.h> /* errno */
#include <stdarg.h>
#include <stdio.h> /* vsnprintf */
#include <stdlib.h> /* malloc, realloc */
#include <string.h> /* memset, strlen */
#include "real_socket_helper.h"
ssize_t real_read_once( int fd, void* buf, unsigned n ) {
ssize_t nread;
char* chbuf;
assert( fd >= 0 && "Bad fd in read_once" );
/* loop until 1 or more bytes have been read from fd, or an EOF or error occurs */
chbuf = (char*)buf;
while( 1 ) {
if( (nread = read(fd, chbuf, n)) < 0 ) {
if( errno == EINTR )
continue; /* interrupt: no bytes read: try again */
else
return -1; /* problem with read: error! */
}
else if( nread == 0 )
return 0; /* EOF */
else
return nread;
}
}
ssize_t real_readn( int fd, void* buf, unsigned n ) {
size_t nleft;
ssize_t nread;
char* chbuf;
assert( fd >= 0 && "Bad fd in readn" );
/* loop until n bytes have been read from fd, or an EOF or error occurs */
chbuf = (char*)buf;
nleft = n;
while( nleft > 0 ) {
if( (nread = read(fd, chbuf, nleft)) < 0 ) {
if( errno == EINTR )
continue; /* interrupt: no bytes read: try again */
else
return -1; /* problem with read: error! */
}
else if( nread == 0 )
break; /* EOF */
nleft -= nread;
chbuf += nread;
}
return (n - nleft);
}
int real_read_search( int fd, const char* needle, search_state_t* state, int max_reads ) {
int first_loop;
int found_needle;
int needle_len;
ssize_t nread;
char* offset;
size_t amount;
int j;
assert( fd >= 0 && "Bad fd in read_search" );
first_loop = 1;
found_needle = 0;
needle_len = strlen( needle );
assert( needle_len > 0 );
nread = 0;
do {
/* check the buffer first in case needle is already there */
if( !first_loop ) {
if( search_state_grow_if_full( state ) == 2 )
return -2; /* buffer full and cannot grow */
/* try to read as much as we can */
offset = state->chbuf + state->used;
amount = state->capacity - state->used;
if( (nread = read( fd, offset, amount )) < 0 ) {
if( errno == EINTR )
continue; /* interrupt: no bytes read: try again */
else
return -1; /* problem with read: error! */
}
else if( nread == 0 )
return 1; /* EOF */
}
/* check for the needle in the newly read data */
while( state->search_offset+needle_len <= state->used+nread ) {
found_needle = 1;
for( j=0; j<needle_len; j++ ) {
if( state->chbuf[state->search_offset+j] != needle[j] ) {
found_needle = 0;
break; /* nope, must match all chars */
}
}
if( found_needle ) {
state->needle_offset = state->search_offset++;
break;
}
state->search_offset++;
}
state->used += nread;
first_loop = 0;
if( max_reads && !found_needle ) {
if( nread )
max_reads -= 1;
if( max_reads == 0 )
return -3;
}
}
while( !found_needle );
return 0; /* success */
}
int real_writen( int fd, const void* buf, unsigned n ) {
size_t nleft;
ssize_t nwritten;
char* chbuf;
assert( fd >= 0 && "Bad fd in writen" );
/* loop until n bytes have been written into fd, or an error occurs */
chbuf = (char*)buf;
nleft = n;
while( nleft > 0 ) {
if( (nwritten = write(fd, chbuf, nleft)) <= 0 ) {
if( nwritten < 0 && errno == EINTR )
continue; /* interrupt: no bytes written: try again */
else
return -1; /* problem with write: error! */
}
nleft -= nwritten;
chbuf += nwritten;
}
return 0; /* indicates success */
}
int real_writenf( int fd, const char* format, ... ) {
int actual;
char buf[WRITEN_MAX_LEN];
va_list args;
va_start( args, format );
actual = vsnprintf( buf, WRITEN_MAX_LEN, format, args );
va_end( args );
#ifdef _PRINT_TO_STDERR_NOT_SOCKET_
fprintf( stderr, buf );
return 0;
#endif
if( actual < WRITEN_MAX_LEN )
return real_writen( fd, &buf, actual ); /* doesn't incl. the trailing NUL */
else {
errno = ERANGE;
return -1;
}
}
int real_writenstr( int fd, const char* str ) {
unsigned len;
#ifdef _PRINT_TO_STDERR_NOT_SOCKET_
fprintf( stderr, str );
return 0;
#endif
len = strlen( str );
return real_writen( fd, str, len );
}
int real_writenstrs( int fd, int num_args, ... ) {
const char* str;
int ret;
va_list args;
va_start( args, num_args );
ret = 0;
while( ret==0 && num_args-- > 0 ) {
str = va_arg(args, const char*);
ret = real_writenstr( fd, str );
}
va_end( args );
return ret;
}
void real_close( int fd ) {
close( fd );
}