]>
Commit | Line | Data |
---|---|---|
18f8bd28 CP |
1 | /* |
2 | Easy async socket library with HELIX encryption and authentication | |
3 | Copyright (C) 2004 Chris Porter. | |
4 | */ | |
5 | ||
6 | #ifndef __esockets_H | |
7 | #define __esockets_H | |
8 | ||
9 | #include "../lib/helix.h" | |
10 | ||
11 | #define ESOCKET_UNIX_DOMAIN ESOCKET_LISTENING | |
12 | #define ESOCKET_UNIX_DOMAIN_CONNECTED ESOCKET_INCOMING | |
13 | #define ESOCKET_TCP_LISTENING ESOCKET_LISTENING | |
14 | #define ESOCKET_INCOMING_TCP ESOCKET_INCOMING | |
15 | #define ESOCKET_OUTGOING_TCP ESOCKET_OUTGOING | |
16 | #define ESOCKET_OUTGOING_TCP_CONNECTED ESOCKET_OUTGOING_CONNECTED | |
17 | ||
18 | #define ESOCKET_LISTENING 1 | |
19 | #define ESOCKET_OUTGOING 2 | |
20 | #define ESOCKET_INCOMING 3 | |
21 | #define ESOCKET_OUTGOING_CONNECTED 4 | |
22 | ||
23 | #define ST_CONNECTING 1 | |
24 | #define ST_CONNECTED 2 | |
25 | #define ST_LISTENING 3 | |
26 | #define ST_DISCONNECT 4 | |
27 | #define ST_BLANK 5 | |
28 | ||
29 | #define BLANK_TOKEN 0 | |
30 | ||
31 | #define BUF_CONT -1 | |
32 | #define BUF_OVERFLOW -2 | |
33 | ||
34 | #define MAX_BUFSIZE 50000 | |
35 | ||
36 | #define USED_MAC_LEN 3 | |
37 | ||
38 | typedef unsigned short packet_t; | |
39 | ||
40 | #define MAX_BINARY_LINE_SIZE MAX_BUFSIZE | |
41 | #define MAX_ASCII_LINE_SIZE MAX_BINARY_LINE_SIZE - sizeof(packet_t) - USED_MAC_LEN | |
42 | ||
43 | #define MAX_OUT_QUEUE_SIZE 1500 | |
44 | ||
45 | struct buffer; | |
46 | struct esocket; | |
47 | ||
48 | typedef int (*parse_event)(struct esocket *sock); | |
49 | ||
50 | typedef int (*line_event)(struct esocket *sock, char *newline); | |
51 | ||
52 | typedef struct esocket_in_buffer { | |
53 | char data[MAX_BUFSIZE]; | |
54 | char *writepos; | |
55 | char *curpos; | |
56 | char *startpos; | |
57 | unsigned short buffer_size; | |
58 | parse_event on_parse; | |
59 | packet_t packet_length; | |
60 | } in_buffer; | |
61 | ||
62 | typedef struct esocket_packet { | |
63 | char *line; | |
64 | packet_t size; | |
65 | struct esocket_packet *next; | |
66 | } esocket_packet; | |
67 | ||
68 | typedef struct esocket_out_buffer { | |
69 | struct esocket_packet *head; | |
70 | struct esocket_packet *end; | |
71 | unsigned short count; | |
72 | } out_buffer; | |
73 | ||
74 | typedef void (*esocket_event)(struct esocket *socket); | |
75 | ||
76 | typedef struct esocket_events { | |
77 | esocket_event on_connect; | |
78 | esocket_event on_accept; | |
79 | esocket_event on_disconnect; | |
80 | line_event on_line; | |
81 | } esocket_events; | |
82 | ||
83 | typedef struct esocket { | |
84 | int fd; | |
85 | struct esocket_in_buffer in; | |
86 | struct esocket_out_buffer out; | |
87 | char socket_type; | |
88 | char socket_status; | |
89 | struct esocket_events events; | |
90 | struct esocket *next; | |
91 | unsigned short token; | |
92 | void *tag; | |
93 | helix_ctx keysend; | |
94 | helix_ctx keyreceive; | |
95 | } esocket; | |
96 | ||
97 | struct esocket *esocket_add(int fd, char socket_type, struct esocket_events *events, unsigned short token); | |
98 | struct esocket *find_esocket_from_fd(int fd); | |
99 | void esocket_poll_event(int fd, short events); | |
100 | void esocket_disconnect(struct esocket *active); | |
101 | int buffer_parse_ascii(struct esocket *sock); | |
102 | int esocket_read(struct esocket *sock); | |
103 | int esocket_write(struct esocket *sock, char *buffer, int bytes); | |
104 | int esocket_write_line(struct esocket *sock, char *format, ...); | |
105 | unsigned short esocket_token(void); | |
106 | struct esocket *find_esocket_from_fd(int fd); | |
107 | void esocket_clean_by_token(unsigned short token); | |
108 | void switch_buffer_mode(struct esocket *sock, char *key, unsigned char *ournonce, unsigned char *theirnonce); | |
109 | void esocket_disconnect_when_complete(struct esocket *active); | |
110 | int esocket_raw_write(struct esocket *sock, char *buffer, int bytes); | |
111 | ||
112 | #endif | |
113 | ||
114 |