diff libnnnet.h @ 412:3e64acb433e8

Split libnnchat into libnnet and libnnutil
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 06:38:26 +0300
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnnnet.h	Thu May 24 06:38:26 2012 +0300
@@ -0,0 +1,132 @@
+/*
+ * NNChat - Custom chat client for NewbieNudes.com chatrooms
+ * Written by Matti 'ccr' Hämäläinen
+ * (C) Copyright 2008-2012 Tecnic Software productions (TNSP)
+ */
+#ifndef LIBNNCHAT_H
+#define LIBNNCHAT_H
+
+#include <stdio.h>
+#include <unistd.h>
+#include "th_types.h"
+#include "th_string.h"
+
+#ifdef __WIN32
+#include <windows.h>
+#include <winsock.h>
+typedef uint16_t in_port_t;
+typedef uint32_t in_addr_t;
+#else
+#include <sys/select.h>
+#include <sys/socket.h>
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#include <arpa/inet.h>
+#include <netdb.h>
+#endif
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+
+
+#define NN_CONNBUF_SIZE   (64 * 1024)
+#define NN_DELAY_USEC     (15 * 1000)
+
+
+enum
+{
+    NN_CONN_UNINIT = 0,
+    NN_CONN_PROXY_NEG,
+    NN_CONN_OPEN,
+    NN_CONN_CLOSED
+};
+
+enum
+{
+    NN_PROXY_NONE = 0,
+    NN_PROXY_SOCKS4,
+    NN_PROXY_SOCKS4A,
+
+    NN_PROXY_LAST
+};
+
+enum
+{
+    SOCKS_CMD_CONNECT = 1,
+    SOCKS_CMD_BIND = 2
+};
+
+struct nn_socks_t
+{
+    uint8_t version;
+    uint8_t command;
+    in_port_t port;
+    in_addr_t addr;
+} __attribute__((__packed__));
+
+struct nn_socks_res_t
+{
+    uint8_t nb;
+    uint8_t result;
+    in_port_t port;
+    in_addr_t addr;
+} __attribute__((__packed__));
+
+typedef struct _nn_conn_t
+{
+    struct
+    {
+        char *host;
+        struct hostent *hst;
+        int type;
+        int port;
+        struct in_addr addr;
+    } proxy;
+
+    char *host;
+    struct hostent *hst;
+    int port;
+
+    int socket;
+    struct in_addr addr;
+    fd_set sockfds;
+
+    void (*errfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
+    void (*msgfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
+
+    int err;
+    int status;
+
+    char buf[NN_CONNBUF_SIZE + 16];
+    char *ptr, *in_ptr;
+    ssize_t got_bytes, total_bytes;
+} nn_conn_t;
+
+
+const char *nn_get_errstr(int err);
+BOOL        nn_network_init();
+void        nn_network_close(void);
+
+struct hostent *nn_resolve_host(nn_conn_t *conn, const char *name);
+nn_conn_t * nn_conn_new(
+    void (*errfunc)(nn_conn_t *conn, const char *fmt, va_list ap),
+    void (*msgfunc)(nn_conn_t *conn, const char *fmt, va_list ap));
+
+int         nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host);
+int         nn_conn_open(nn_conn_t *conn, const int port, const char *host);
+void        nn_conn_close(nn_conn_t *);
+void        nn_conn_reset(nn_conn_t *);
+int         nn_conn_pull(nn_conn_t *);
+BOOL        nn_conn_send_buf(nn_conn_t *, const char *buf, const size_t len);
+BOOL        nn_conn_send_msg(nn_conn_t *, const char *user, const char *fmt, ...);
+BOOL        nn_conn_check(nn_conn_t *);
+
+
+BOOL        nn_conn_buf_check(nn_conn_t *conn, size_t n);
+BOOL        nn_conn_buf_skip(nn_conn_t *conn, size_t n);
+int         nn_conn_buf_strncmp(nn_conn_t *conn, const char *str, const size_t n);
+int         nn_conn_buf_strcmp(nn_conn_t *conn, const char *str);
+char *      nn_conn_buf_strstr(nn_conn_t *conn, const char *str);
+
+#endif