]>
Commit | Line | Data |
---|---|---|
18f8bd28 | 1 | /* |
d4897ed9 CP |
2 | Easy async socket library |
3 | Copyright (C) 2004-2007 Chris Porter. | |
18f8bd28 CP |
4 | */ |
5 | ||
6 | #ifndef __esockets_H | |
7 | #define __esockets_H | |
8 | ||
d4897ed9 CP |
9 | #include "../lib/rijndael.h" |
10 | #include "../lib/sha2.h" | |
11 | #include "library.h" | |
9be751b7 | 12 | #include <sys/types.h> |
632cc7de | 13 | #include <ctype.h> |
18f8bd28 CP |
14 | |
15 | #define ESOCKET_UNIX_DOMAIN ESOCKET_LISTENING | |
16 | #define ESOCKET_UNIX_DOMAIN_CONNECTED ESOCKET_INCOMING | |
17 | #define ESOCKET_TCP_LISTENING ESOCKET_LISTENING | |
18 | #define ESOCKET_INCOMING_TCP ESOCKET_INCOMING | |
19 | #define ESOCKET_OUTGOING_TCP ESOCKET_OUTGOING | |
20 | #define ESOCKET_OUTGOING_TCP_CONNECTED ESOCKET_OUTGOING_CONNECTED | |
21 | ||
22 | #define ESOCKET_LISTENING 1 | |
23 | #define ESOCKET_OUTGOING 2 | |
24 | #define ESOCKET_INCOMING 3 | |
25 | #define ESOCKET_OUTGOING_CONNECTED 4 | |
26 | ||
27 | #define ST_CONNECTING 1 | |
28 | #define ST_CONNECTED 2 | |
29 | #define ST_LISTENING 3 | |
30 | #define ST_DISCONNECT 4 | |
31 | #define ST_BLANK 5 | |
32 | ||
33 | #define BLANK_TOKEN 0 | |
34 | ||
35 | #define BUF_CONT -1 | |
36 | #define BUF_OVERFLOW -2 | |
5757e1ef | 37 | #define BUF_ERROR -3 |
d4897ed9 CP |
38 | #define BUF_RESET -4 |
39 | ||
40 | #define PARSE_ASCII 0 | |
41 | #define PARSE_CRYPTO 1 | |
18f8bd28 CP |
42 | |
43 | #define MAX_BUFSIZE 50000 | |
44 | ||
d4897ed9 | 45 | #define USED_MAC_LEN 16 |
18f8bd28 CP |
46 | |
47 | typedef unsigned short packet_t; | |
48 | ||
49 | #define MAX_BINARY_LINE_SIZE MAX_BUFSIZE | |
d4897ed9 | 50 | #define MAX_ASCII_LINE_SIZE MAX_BINARY_LINE_SIZE - 10 - USED_MAC_LEN |
18f8bd28 | 51 | |
701500af | 52 | #define MAX_OUT_QUEUE_SIZE 5000 |
18f8bd28 CP |
53 | |
54 | struct buffer; | |
55 | struct esocket; | |
56 | ||
57 | typedef int (*parse_event)(struct esocket *sock); | |
58 | ||
59 | typedef int (*line_event)(struct esocket *sock, char *newline); | |
60 | ||
61 | typedef struct esocket_in_buffer { | |
d4897ed9 CP |
62 | char *data; |
63 | int size; | |
64 | short mode; | |
65 | unsigned char *cryptobuf; | |
66 | int cryptobufsize; | |
67 | short mac; | |
18f8bd28 CP |
68 | } in_buffer; |
69 | ||
70 | typedef struct esocket_packet { | |
71 | char *line; | |
72 | packet_t size; | |
a690fb9f | 73 | packet_t startpos; |
18f8bd28 CP |
74 | struct esocket_packet *next; |
75 | } esocket_packet; | |
76 | ||
77 | typedef struct esocket_out_buffer { | |
78 | struct esocket_packet *head; | |
79 | struct esocket_packet *end; | |
80 | unsigned short count; | |
81 | } out_buffer; | |
82 | ||
83 | typedef void (*esocket_event)(struct esocket *socket); | |
84 | ||
85 | typedef struct esocket_events { | |
86 | esocket_event on_connect; | |
87 | esocket_event on_accept; | |
88 | esocket_event on_disconnect; | |
89 | line_event on_line; | |
90 | } esocket_events; | |
91 | ||
92 | typedef struct esocket { | |
93 | int fd; | |
94 | struct esocket_in_buffer in; | |
95 | struct esocket_out_buffer out; | |
96 | char socket_type; | |
97 | char socket_status; | |
98 | struct esocket_events events; | |
99 | struct esocket *next; | |
100 | unsigned short token; | |
101 | void *tag; | |
d4897ed9 CP |
102 | |
103 | unsigned char clientkey[32]; | |
104 | unsigned char serverkey[32]; | |
105 | u_int64_t clientseqno; | |
106 | u_int64_t serverseqno; | |
107 | hmacsha256 clienthmac; | |
108 | rijndaelcbc *clientcrypto; | |
109 | rijndaelcbc *servercrypto; | |
18f8bd28 CP |
110 | } esocket; |
111 | ||
112 | struct esocket *esocket_add(int fd, char socket_type, struct esocket_events *events, unsigned short token); | |
113 | struct esocket *find_esocket_from_fd(int fd); | |
114 | void esocket_poll_event(int fd, short events); | |
115 | void esocket_disconnect(struct esocket *active); | |
116 | int buffer_parse_ascii(struct esocket *sock); | |
117 | int esocket_read(struct esocket *sock); | |
118 | int esocket_write(struct esocket *sock, char *buffer, int bytes); | |
119 | int esocket_write_line(struct esocket *sock, char *format, ...); | |
120 | unsigned short esocket_token(void); | |
121 | struct esocket *find_esocket_from_fd(int fd); | |
122 | void esocket_clean_by_token(unsigned short token); | |
d4897ed9 | 123 | void switch_buffer_mode(struct esocket *sock, unsigned char *serverkey, unsigned char *serveriv, unsigned char *clientkey, unsigned char *clientiv); |
18f8bd28 CP |
124 | void esocket_disconnect_when_complete(struct esocket *active); |
125 | int esocket_raw_write(struct esocket *sock, char *buffer, int bytes); | |
126 | ||
632cc7de CP |
127 | #ifndef BYTE_ORDER |
128 | #ifndef _BYTE_ORDER | |
129 | #ifndef __BYTE_ORDER | |
130 | #error No byte order defined! | |
131 | #else | |
132 | #define BYTE_ORDER __BYTE_ORDER | |
133 | #endif | |
134 | #else | |
135 | #define BYTE_ORDER _BYTE_ORDER | |
136 | #endif | |
137 | #endif | |
138 | ||
60deb130 CP |
139 | #ifndef htonq |
140 | #if BYTE_ORDER == BIG_ENDIAN | |
141 | #define htonq(x) x | |
142 | #define ntohq(x) x | |
143 | #else | |
144 | #define htonq(x) ntohq(x) | |
145 | #define ntohq(x) (((u_int64_t)htonl((x)>>32))|(((u_int64_t)htonl(x))<<32)) | |
146 | #endif | |
147 | #endif | |
148 | ||
18f8bd28 CP |
149 | #endif |
150 | ||
151 |