-
Notifications
You must be signed in to change notification settings - Fork 2
/
net.c
78 lines (65 loc) · 1.41 KB
/
net.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
/** net.c
* Low-level socket handling, inc. buffered input. */
#include "hewwo.h"
#include <arpa/inet.h>
#include <assert.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int
dial(const char *addr, const char *port)
{
/* Basically just the example from Beej. */
struct addrinfo hints, *servinfo, *it;
int fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(addr, port, &hints, &servinfo) != 0) {
return -1;
}
for (it = servinfo; it; it = it->ai_next) {
fd = socket(it->ai_family, it->ai_socktype, it->ai_protocol);
if (fd < 0) {
continue;
}
if (connect(fd, it->ai_addr, it->ai_addrlen) == -1) {
close(fd);
continue;
}
return fd;
}
return -2;
}
Bufio *
bufio_init(void)
{
return calloc(1, sizeof(Bufio));
}
int
bufio_read(Bufio *bi, int fd, void (*callback)(char *s))
{
assert(memchr(bi->buf, '\r', bi->pos) == NULL);
int toread = sizeof(bi->buf) - bi->pos;
if (toread <= 0) {
// TODO
}
int ret = read(fd, bi->buf + bi->pos, toread);
if (ret <= 0) return ret;
bi->pos += ret;
for (;;) {
char *cr = memchr(bi->buf, '\r', bi->pos);
if (!cr) break;
*cr = '\0';
char *s = bi->buf;
if (*s == '\n') s++;
callback(s);
int len = cr+1 - bi->buf; /* include NUL */
memmove(bi->buf, cr+1, bi->pos - len);
bi->pos -= len;
}
return ret;
}