]> Pileus Git - ~andy/lamechat/blob - net.h
Support pre-formatted text.
[~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 /* Networking Callbacks */
22 typedef void (*send_t)(void *data);
23 typedef void (*recv_t)(void *data, char *buf, int len);
24 typedef void (*err_t)(void *data, int errno);
25
26 /* Networking Types */
27 typedef struct crypto_t crypto_t;
28
29 typedef enum {
30         NET_NOVERIFY = 0x01,
31 } encrypt_t;
32
33 typedef enum {
34         NET_CLOSED,
35         NET_CONNECT,
36         NET_READY,
37         NET_ENCRYPT,
38         NET_HANDSHAKE,
39         NET_ENCRYPTED,
40 } nstate_t;
41
42 typedef struct {
43         char     *host;
44         int       port;
45         send_t    send;
46         recv_t    recv;
47         err_t     err;
48         void     *data;
49
50         poll_t    poll;
51         nstate_t  state;
52         crypto_t *crypto;
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, int flags);
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);