0
|
1 |
|
|
2 |
#include <stdlib.h>
|
|
3 |
#include <sys/types.h>
|
|
4 |
#include <sys/socket.h>
|
|
5 |
#include <unistd.h>
|
|
6 |
#include <stdio.h>
|
|
7 |
#include <netdb.h>
|
|
8 |
#include <string.h>
|
|
9 |
#include <event.h>
|
|
10 |
#include <assert.h>
|
|
11 |
|
|
12 |
#include <err.h>
|
|
13 |
|
|
14 |
#define CONNECT_HOST "irc.fixme.fi"
|
|
15 |
#define CONNECT_SERV "6667"
|
|
16 |
#define LINE_LENGTH 512
|
|
17 |
|
|
18 |
/*
|
|
19 |
* Simple getaddrinfo() connect
|
|
20 |
*/
|
|
21 |
int sock_connect (const char *host, const char *service) {
|
|
22 |
struct addrinfo hints, *res, *r;
|
|
23 |
int _err, sock = -1;
|
|
24 |
|
|
25 |
// hints
|
|
26 |
memset(&hints, 0, sizeof(hints));
|
|
27 |
hints.ai_family = AF_UNSPEC;
|
|
28 |
hints.ai_socktype = SOCK_STREAM;
|
|
29 |
|
|
30 |
// resolve
|
|
31 |
if ((_err = getaddrinfo(host, service, &hints, &res)))
|
|
32 |
errx(1, "getaddrinfo: %s", gai_strerror(_err));
|
|
33 |
|
|
34 |
// use
|
|
35 |
for (r = res; r; r = r->ai_next) {
|
|
36 |
// XXX: wrong
|
|
37 |
if ((sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) < 0)
|
|
38 |
err(1, "socket");
|
|
39 |
|
|
40 |
if (connect(sock, r->ai_addr, r->ai_addrlen))
|
|
41 |
err(1, "connect");
|
|
42 |
|
|
43 |
break;
|
|
44 |
}
|
|
45 |
|
|
46 |
// ensure we got some valid socket
|
|
47 |
if (sock < 0)
|
|
48 |
errx(1, "no valid socket");
|
|
49 |
|
|
50 |
// ok
|
|
51 |
return sock;
|
|
52 |
}
|
|
53 |
|
|
54 |
struct recvline_state {
|
|
55 |
size_t tail_offset;
|
|
56 |
size_t tail_len;
|
|
57 |
};
|
|
58 |
|
|
59 |
/*
|
|
60 |
* This looks for a full '\r\n' terminated line at the beginning of the given buffer. If found, the \r\n will be
|
|
61 |
* replaced with a '\0', and the offset to the beginning of the next line returned. If not found, zero is returned
|
|
62 |
* (which is never a valid next-line offset).
|
|
63 |
*
|
|
64 |
* The given \a hint is an hint as to the offset at which to start scanning, used for incremental invocations of this
|
|
65 |
* on the same buffer.
|
|
66 |
*
|
|
67 |
*/
|
|
68 |
int _parse_line (char *buf, size_t len, size_t *hint) {
|
|
69 |
int i;
|
|
70 |
|
|
71 |
// empty buffer -> nothing
|
|
72 |
if (len == 0)
|
|
73 |
return 0;
|
|
74 |
|
|
75 |
// look for terminating '\r\n' sequence
|
|
76 |
for (i = *hint; i < len - 1; i++) {
|
|
77 |
// match this + next char
|
|
78 |
if (buf[i] == '\r' && buf[i + 1] == '\n')
|
|
79 |
break;
|
|
80 |
}
|
|
81 |
|
|
82 |
// incomplete?
|
|
83 |
if (i >= len - 1) {
|
|
84 |
*hint = len - 1;
|
|
85 |
return 0;
|
|
86 |
}
|
|
87 |
|
|
88 |
// mangle the newline off
|
|
89 |
buf[i] = '\0';
|
|
90 |
|
|
91 |
// return offset to next line
|
|
92 |
return i + 2;
|
|
93 |
}
|
|
94 |
|
|
95 |
/*
|
|
96 |
* Receive one line of data into the buffer of the given length
|
|
97 |
*/
|
|
98 |
int recvline (int sock, char *buf, size_t len, struct recvline_state *ctx) {
|
|
99 |
size_t recv_offset = 0, peek_offset = 0, next_offset = 0;
|
|
100 |
int ret;
|
|
101 |
|
|
102 |
// adjust offset from previous data
|
|
103 |
recv_offset = ctx->tail_len;
|
|
104 |
|
|
105 |
// move trailing data from previous line to front of buffer
|
|
106 |
if (ctx->tail_offset) {
|
|
107 |
// move to front
|
|
108 |
memmove(buf, buf + ctx->tail_offset, ctx->tail_len);
|
|
109 |
|
|
110 |
// reset
|
|
111 |
ctx->tail_offset = 0;
|
|
112 |
ctx->tail_len = 0;
|
|
113 |
}
|
|
114 |
|
|
115 |
// readline loop
|
|
116 |
do {
|
|
117 |
// parse any line at the beginning of the buffer
|
|
118 |
if ((next_offset = _parse_line(buf, recv_offset, &peek_offset)) > 0)
|
|
119 |
break;
|
|
120 |
|
|
121 |
// ensure there's enough space for it
|
|
122 |
assert(recv_offset < len);
|
|
123 |
|
|
124 |
// otherwise, read more data
|
|
125 |
if ((ret = read(sock, buf + recv_offset, len - recv_offset)) < 0)
|
|
126 |
err(1, "read");
|
|
127 |
|
|
128 |
// EOF?
|
|
129 |
if (ret == 0)
|
|
130 |
errx(1, "read: EOF");
|
|
131 |
|
|
132 |
// update recv_offset
|
|
133 |
recv_offset += ret;
|
|
134 |
} while (1);
|
|
135 |
|
|
136 |
// update state for next call
|
|
137 |
ctx->tail_offset = next_offset;
|
|
138 |
ctx->tail_len = recv_offset - next_offset;
|
|
139 |
|
|
140 |
// ok
|
|
141 |
return 0;
|
|
142 |
}
|
|
143 |
|
|
144 |
int main (int argc, char **argv) {
|
|
145 |
int sock;
|
|
146 |
char line_buf[LINE_LENGTH + 1];
|
|
147 |
struct recvline_state recvline_ctx;
|
|
148 |
|
|
149 |
// initialize
|
|
150 |
memset(&recvline_ctx, 0, sizeof(recvline_ctx));
|
|
151 |
|
|
152 |
// over-simplified connect
|
|
153 |
sock = sock_connect(CONNECT_HOST, CONNECT_SERV);
|
|
154 |
|
|
155 |
// read lines and dump them out
|
|
156 |
do {
|
|
157 |
// recv
|
|
158 |
recvline(sock, line_buf, sizeof(line_buf), &recvline_ctx);
|
|
159 |
|
|
160 |
// printf
|
|
161 |
printf("<<< %s\n", line_buf);
|
|
162 |
|
|
163 |
} while (1);
|
|
164 |
|
|
165 |
// ok
|
|
166 |
return 0;
|
|
167 |
}
|
|
168 |
|