src/nexus.c
changeset 1 cf0e1bb6bcab
parent 0 317e5bc59627
child 2 a834f0559939
--- a/src/nexus.c	Sun Feb 22 02:00:34 2009 +0200
+++ b/src/nexus.c	Sun Feb 22 03:57:44 2009 +0200
@@ -1,56 +1,19 @@
 
 #include <stdlib.h>
-#include <sys/types.h>
-#include <sys/socket.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <netdb.h>
 #include <string.h>
 #include <event.h>
 #include <assert.h>
 
 #include <err.h>
 
+#include "sock.h"
+
 #define CONNECT_HOST "irc.fixme.fi"
 #define CONNECT_SERV "6667"
 #define LINE_LENGTH 512
 
-/*
- * Simple getaddrinfo() connect
- */
-int sock_connect (const char *host, const char *service) {
-    struct addrinfo hints, *res, *r;
-    int _err, sock = -1;
-    
-    // hints
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = AF_UNSPEC;
-    hints.ai_socktype = SOCK_STREAM;
-
-    // resolve
-    if ((_err = getaddrinfo(host, service, &hints, &res)))
-        errx(1, "getaddrinfo: %s", gai_strerror(_err));
-
-    // use
-    for (r = res; r; r = r->ai_next) {
-        // XXX: wrong
-        if ((sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) < 0)
-            err(1, "socket");
-
-        if (connect(sock, r->ai_addr, r->ai_addrlen))
-            err(1, "connect");
-
-        break;
-    }
-    
-    // ensure we got some valid socket
-    if (sock < 0)
-        errx(1, "no valid socket");
-    
-    // ok
-    return sock;
-}
-
 struct recvline_state {
     size_t tail_offset;
     size_t tail_len;
@@ -95,7 +58,7 @@
 /*
  * Receive one line of data into the buffer of the given length
  */
-int recvline (int sock, char *buf, size_t len, struct recvline_state *ctx) {
+int recvline (struct sock_stream *sock, char *buf, size_t len, struct recvline_state *ctx) {
     size_t recv_offset = 0, peek_offset = 0, next_offset = 0;
     int ret;
 
@@ -122,7 +85,7 @@
         assert(recv_offset < len);
         
         // otherwise, read more data
-        if ((ret = read(sock, buf + recv_offset, len - recv_offset)) < 0)
+        if ((ret = sock_stream_read(sock, buf + recv_offset, len - recv_offset)) < 0)
             err(1, "read");
 
         // EOF?
@@ -142,7 +105,7 @@
 }
 
 int main (int argc, char **argv) {
-    int sock;
+    struct sock_stream *sock;
     char line_buf[LINE_LENGTH + 1];
     struct recvline_state recvline_ctx;
 
@@ -150,7 +113,7 @@
     memset(&recvline_ctx, 0, sizeof(recvline_ctx));
     
     // over-simplified connect
-    sock = sock_connect(CONNECT_HOST, CONNECT_SERV);
+    sock = sock_tcp_connect(CONNECT_HOST, CONNECT_SERV);
 
     // read lines and dump them out
     do {