#include #include #include #include #include #include #if _WIN32 #include #else #include #endif #include #include "socket.h" #include "bufio.h" #include "lookup.h" static void main_gets_catch(struct bufio *io, void *line, size_t len, enum bufio_errno err, void *s) { static char buf[1024]; warn("err=%d (%s)", err, bufio_errlist[err]); assert(err == BUFIO_ESUCCESS || err == BUFIO_EEOF); if (len > 0) warnx("read %d bytes: %.*s", (int)len, (int)(len - 1), (char *)line); if (err == BUFIO_EEOF) { bufio_close(io); socket_close(s); warnx("got EOF"); return; } bufio_gets(io, buf, sizeof buf, &main_gets_catch, s, &(struct timeval){ 2, 0 }); return; } /* main_gets_catch() */ static void main_write_catch(struct bufio *io, void *request, size_t len, enum bufio_errno err, void *s) { static char buf[1024]; assert(err == BUFIO_ESUCCESS); assert(len == strlen(request)); bufio_gets(io, buf, sizeof buf, &main_gets_catch, s, &(struct timeval){ 2, 0 }); return; } /* main_write_catch() */ static void main_connect_catch(struct socket *s, enum socket_errno so_errno, void *arg) { static const char request[] = "GET / HTTP/1.0\n\n"; struct bufio *io; enum bufio_errno io_errno; assert(so_errno == SOCKET_ESUCCESS); warnx("caught connect to google.com"); assert(io = bufio_open(0, 0, &io_errno)); bufio_set_source(io, socket_to_source(s)); bufio_set_sink(io, socket_to_sink(s)); bufio_writen(io, request, sizeof request - 1, &main_write_catch, s, &(struct timeval){ 2, 0 }); return /* void */; } /* main_connect_catch() */ int main(void) { struct socket *s; struct socket_name so_name; enum socket_errno so_errno; #if _WIN32 WSADATA wsaData; #endif #if _WIN32 if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData)) err(EXIT_FAILURE, "WSAStartup"); #endif event_init(); #if _WIN32 socket_name_init_host(&so_name, "google.com", "80", LOOKUP_IN_A|LOOKUP_IN_AAAA, 0); #else socket_name_init_host(&so_name, "google.com", "http", LOOKUP_IN_A|LOOKUP_IN_AAAA, 0); #endif if (0 == (s = socket_open(&so_name, 0, 0, 0, &so_errno))) err(EXIT_FAILURE, "socket_open: %s", socket_strerror(so_errno)); socket_connect(s, &main_connect_catch, s, 0); event_loop(0); return 0; }