]> Pileus Git - ~andy/lamechat/blob - net.h
Use net for IRC
[~andy/lamechat] / net.h
1 /*
2  * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /* Networking Constants */
19 #define NET_BUFFER 1024
20
21 /* OpenSSL Types */
22 typedef struct ssl_ctx_st SSL_CTX;
23 typedef struct ssl_st     SSL;
24 typedef struct bio_st     BIO;
25
26 /* Networking Callbacks */
27 typedef void (*recv_t)(void *data, char *buf, int len);
28 typedef void (*err_t)(void *data, int errno);
29
30 /* Networking Types */
31 typedef enum {
32         NET_CLOSED,
33         NET_CONNECT,
34         NET_READY,
35         NET_HANDSHAKE,
36         NET_ENCRYPTED,
37 } nstate_t;
38
39 typedef struct {
40         char    *host;
41         int      port;
42         recv_t   recv;
43         err_t    err;
44         void    *data;
45
46         SSL_CTX *ctx;
47         SSL     *ssl;
48         BIO     *in;
49         BIO     *out;
50
51         poll_t   poll;
52         nstate_t state;
53
54         char     out_buf[NET_BUFFER];
55         int      out_pos;
56         int      out_len;
57 } net_t;
58
59 /* Networking functions */
60 const char *get_hostname(void);
61
62 /* Connection functions */
63 void net_init(void);
64 void net_open(net_t *net, const char *host, int port);
65 void net_encrypt(net_t *net);
66 int  net_send(net_t *net, const char *buf, int len);
67 int  net_print(net_t *net, const char *fmt, ...);
68 void net_close(net_t *net);