-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrepro.c
264 lines (218 loc) · 5.31 KB
/
mrepro.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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>
#include "mrepro.h"
#include <netdb.h>
#include <arpa/inet.h>
#include <syslog.h>
#include <stdarg.h>
#define ERR_NO 1
extern int daemon_fl;
int Socket(int domain, int type, int protocol) {
int sock_fd = socket(domain, type, protocol);
if(sock_fd < 0) {
my_print(LOG_ALERT, "Socket initialization failed.\n");
exit(ERR_NO);
}
return sock_fd;
}
ssize_t Sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen) {
int sent;
sent = sendto(sockfd, buf, len, flags, dest_addr, addrlen);
if(sent < 0) {
errx(ERR_NO, "Error while sending datagram.");
}
return sent;
}
ssize_t Recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen) {
ssize_t msg_len;
msg_len = recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
if(msg_len < 0) {
my_print(LOG_ALERT, "Error while receiving datagram.\n");
exit(ERR_NO);
}
return msg_len;
}
int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
int result = bind(sockfd, addr, addrlen);
if(result == -1) {
my_print(LOG_ALERT, "Binding error: %d\n", errno);
exit(ERR_NO);
}
return result;
}
int Connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
int result = connect(sockfd, addr, addrlen);
if(result == -1) {
errx(ERR_NO, "Connect error.");
}
return result;
}
int Listen(int sockfd, int backlog) {
int result = listen(sockfd, backlog);
if(result == -1) {
my_print(LOG_ALERT, "Listen error.\n");
exit(ERR_NO);
}
return result;
}
int Accept(int socket, struct sockaddr *cliaddr, socklen_t *addrlen) {
int newfd = accept(socket, cliaddr, addrlen);
if(newfd == -1) {
my_print(LOG_ALERT, "Accept error.\n");
exit(ERR_NO);
}
return newfd;
}
int Getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res) {
int result = getaddrinfo(node, service, hints, res);
if(result != 0) {
my_print(LOG_ALERT, "%s\n", gai_strerror(result));
exit(ERR_NO);
}
return result;
}
int Inet_pton(int af, const char *src, void *dst) {
int result = inet_pton(af, src, dst);
if(result != 1) {
errx(ERR_NO, "%s is not a valid IP address.", src);
}
return result;
}
const char *Inet_ntop(int af, const void *src, char *dst, socklen_t size) {
const char *result = inet_ntop(af, src, dst, size);
if(!result) {
my_print(LOG_ALERT, "Invalid representation of IP address.\n");
exit(ERR_NO);
}
return result;
}
ssize_t readn(int fd, void *vptr, size_t n) {
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while(nleft > 0) {
if((nread = read(fd, ptr, nleft)) < 0) {
if(errno == EINTR) {
nread = 0;
} else {
errx(ERR_NO, "Error while sending data.");
}
} else if(nread == 0) {
break;
}
nleft -= nread;
ptr += nread;
}
return (n - nleft);
}
ssize_t writen(int fd, const void *vptr, size_t n) {
size_t nleft;
ssize_t nwritten;
const char *ptr;
ptr = vptr;
nleft = n;
while(nleft > 0) {
if((nwritten = write(fd, ptr, nleft)) <= 0) {
if(nwritten < 0 && errno == EINTR) {
nwritten = 0;
} else {
my_print(LOG_ALERT, "Error while sending data.\n");
exit(ERR_NO);
}
}
nleft -= nwritten;
ptr += nwritten;
}
return n;
}
int read_till_newline(void *buffer, int tcp_sock_fd) {
char *ptr;
ptr = buffer;
ssize_t nread;
struct addrinfo hints, *res;
ssize_t bytes_read = 0;
while(1) {
if((nread = read(tcp_sock_fd, ptr, 1)) < 0) {
if(errno == EINTR) {
continue;
} else {
errx(2, "Error while sending data.");
}
}
bytes_read += nread;
ptr += nread;
if(ptr != buffer && *(ptr - 1) == '\n') {
break;
} else if(nread == 0) {
return 0;
}
}
return bytes_read;
}
void my_print(int priority, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
if(daemon_fl) {
vsyslog(priority, fmt, args);
} else {
vfprintf(stderr, fmt, args);
}
va_end(args);
}
void bind_service(int sock_fd, char *serv_name, int socktype) {
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = socktype;
hints.ai_flags = AI_PASSIVE;
Getaddrinfo(NULL, serv_name, &hints, &res);
Bind(sock_fd, (struct sockaddr *)res->ai_addr, res->ai_addrlen);
}
int read_http_req(int conn_fd, void **buf, int buf_len) {
char *ptr;
ptr = *buf;
ssize_t nread;
int offset;
struct addrinfo hints, *res;
ssize_t bytes_read = 0;
while(1) {
if(bytes_read == buf_len) {
buf_len *= 2;
offset = ptr - (char *)*buf;
*buf = (char *)realloc(*buf, buf_len);
ptr = *buf + offset;
}
if((nread = read(conn_fd, ptr, buf_len)) < 0) {
if(errno == EINTR) {
continue;
} else {
errx(2, "Error while sending data.");
}
}
bytes_read += nread;
ptr += nread;
if(ptr > ((char *)*buf + 3)) {
if(*(ptr - 4) == '\r' && *(ptr - 3) == '\n'
&& *(ptr - 2) == '\r' && *(ptr - 1) == '\n') {
*ptr = '\0';
break;
}
}
if(nread == 0) {
return 0;
}
}
return bytes_read;
}