view network.h @ 599:eeea75b8b6f3

Implement proxy user id setting.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 May 2014 16:14:09 +0300
parents f9fb2a96a0c9
children 4bae14092b78
line wrap: on
line source

/*
 * NNChat - Custom chat client for NewbieNudes.com chatrooms
 * Written by Matti 'ccr' Hämäläinen
 * (C) Copyright 2008-2013 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
#define __OBJC_BOOL // A nasty hack
#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


#define NN_CONNBUF_SIZE   (64 * 1024)
#define NN_DELAY_USEC     (15 * 1000)
#define NN_DUMP_BYTES	  16


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;
        char *userid;
    } 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 *msg);
    void (*msgfunc)(struct _nn_conn_t *conn, const char *msg);

    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 *msg),
    void (*msgfunc)(nn_conn_t *conn, const char *msg));

int         nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host, const char *userid);
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 *str);
BOOL        nn_conn_send_msg_v(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);

void        nn_conn_dump_buffer(FILE *f, nn_conn_t *conn);

#endif