-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocktalk.c
257 lines (215 loc) · 6.31 KB
/
socktalk.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
#include <stdarg.h> // for va_arg, etc.
#include <code64.h> // for encoding username and password
#include "socktalk.h"
int walk_status_reply(const char *str, int *status, const char** line, int *line_len)
{
int i_status = 0;
// Initialize outputs, which will also be used as progress flags.
*status = 0;
*line = NULL;
*line_len = 0;
const char *ptr = str;
while (*ptr && *ptr != '\r')
{
if (*status==0)
{
i_status *= 10;
i_status += *ptr - '0';
if (i_status > 99)
*status = i_status;
}
else if (*line == NULL && *ptr!=' ' && *ptr!='-')
*line = ptr;
++ptr;
}
if (! *ptr)
{
fprintf(stderr, "Unexpected end-of-string while parsing status reply.\n");
return -1;
/* return ptr - str; */
}
else if (*ptr == '\r')
{
*line_len = ptr - *line;
if (*++ptr == '\n')
// Move pointer to character just after \n:
++ptr;
else
fprintf(stderr, "Unexpected %c following a '\\r'.\n", *ptr);
}
return ptr - str;
}
void dump_status_reply(const char *buffer, int buffer_len)
{
const char *ptr = buffer;
const char *end = buffer + buffer_len;
int advance_chars;
// walk_status_reply() output parameter variables
int status;
const char *line;
int line_len;
while (ptr < end && *ptr)
{
advance_chars = walk_status_reply(buffer, &status, &line, &line_len);
switch(advance_chars)
{
case -1:
fprintf(stderr, "Error processing replys from \"%s\"\n", buffer);
case 0:
ptr = end; // Flag to break outer loop
break;
default:
printf("%d : %.*s.\n", status, line_len, line);
ptr += advance_chars;
break;
}
}
}
int log_status_reply_errors(const char *buffer, int buffer_len)
{
int errors = 0;
const char *ptr = buffer;
const char *end = buffer + buffer_len;
int advance_chars;
// walk_status_reply() output parameter variables
int status;
const char *line;
int line_len;
while (ptr < end && *ptr)
{
advance_chars = walk_status_reply(buffer, &status, &line, &line_len);
switch(advance_chars)
{
case -1:
fprintf(stderr, "Error processing replys from \"%s\"\n", buffer);
case 0:
ptr = end; // Flag to break outer loop
break;
default:
if (status >= 400)
{
printf("Error (%d) : %.*s.\n", status, line_len, line);
++errors;
}
ptr += advance_chars;
break;
}
}
return errors;
}
size_t stk_sock_talker(const struct _stalker* talker, const void *data, int data_len)
{
return send(talker->socket_handle, (void*)data, data_len, 0);
}
size_t stk_ssl_talker(const struct _stalker* talker, const void *data, int data_len)
{
return SSL_write(talker->ssl_handle, data, data_len);
}
size_t stk_sock_reader(const struct _stalker* talker, void *buffer, int buff_len)
{
return recv(talker->socket_handle, buffer, buff_len, 0);
}
size_t stk_ssl_reader(const struct _stalker* talker, void *buffer, int buff_len)
{
return SSL_read(talker->ssl_handle, buffer, buff_len);
}
void init_ssl_talker(struct _stalker* talker, SSL* ssl)
{
memset(talker, 0, sizeof(struct _stalker));
talker->ssl_handle = (void*)ssl;
talker->writer = stk_ssl_talker;
talker->reader = stk_ssl_reader;
}
void init_sock_talker(struct _stalker* talker, int socket)
{
memset(talker, 0, sizeof(struct _stalker));
talker->socket_handle = socket;
talker->writer = stk_sock_talker;
talker->reader = stk_sock_reader;
}
size_t stk_vsend_line(const struct _stalker* talker, va_list args)
{
size_t bytes_sent, total_bytes = 0;
size_t bite_len;
va_list args_copy;
va_copy(args_copy, args);
const char *bite = va_arg(args_copy, const char*);
while (bite)
{
bite_len = strlen(bite);
total_bytes += bytes_sent = (*talker->writer)(talker, bite, bite_len);
if (bytes_sent != bite_len)
fprintf(stderr, "Socket talker failed to write complete contents of string.\n");
bite = va_arg(args_copy, const char*);
}
va_end(args_copy);
total_bytes += bytes_sent = (*talker->writer)(talker, "\r\n", 2);
return total_bytes;
}
/**
* @brief Write string of const char* arguments to socket or ssl socket, finish with "\r\n";
*
* This is a variable-argument function, with the _talker argument first,
* followed by const char* arguments, terminated with a NULL argument to
* indicate the end of the list.
*
* No spaces will be added between argument strings, but a final "\r\n"
* will be sent upon encountering the terminating NULL.
*/
size_t stk_send_line(const struct _stalker* talker, ...)
{
size_t bytes_sent, total_bytes = 0;
size_t bite_len;
va_list ap;
va_start(ap, talker);
const char *bite = va_arg(ap, const char*);
while (bite)
{
bite_len = strlen(bite);
total_bytes += bytes_sent = (*talker->writer)(talker, bite, bite_len);
if (bytes_sent != bite_len)
fprintf(stderr, "Socket talker failed to write complete contents of string.\n");
bite = va_arg(ap, const char*);
}
va_end(ap);
total_bytes += bytes_sent = (*talker->writer)(talker, "\r\n", 2);
return total_bytes;
}
/**
* @brief Read from server using current communication protocol. Add \0 to end, if room.
*/
size_t stk_recv_line(const struct _stalker* talker, void* buffer, int buff_len)
{
size_t bytes_read = (*talker->reader)(talker, buffer, buff_len);
if (bytes_read+1 < buff_len)
((char*)buffer)[bytes_read] = '\0';
return bytes_read;
}
int stk_send_recv_line(const struct _stalker *talker, ...)
{
char buffer[1000];
va_list args;
va_start(args, talker);
stk_vsend_line(talker, args);
va_end(args);
stk_recv_line(talker, buffer, sizeof(buffer));
return 0 == log_status_reply_errors(buffer, sizeof(buffer));
}
int seek_status_message(const struct _status_line* sl, const char *value)
{
while (sl)
{
if ( 0 == strcasecmp(sl->message, value))
return 1;
sl = sl->next;
}
return 0;
}
void show_status_chain(const Status_Line *sl)
{
while (sl)
{
printf("%d : \"[44;1m%s[m\"\n", sl->status, sl->message);
sl = sl->next;
}
}