1 /*
2 Copyright (c) 2003-2006 by Juliusz Chroboczek
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 /* request->operation */
24 #define IO_READ 0
25 #define IO_WRITE 1
26 #define IO_MASK 0xFF
27 /* Do not initiate operation now -- wait for the poll loop. */
28 #define IO_NOTNOW 0x100
29 /* Call the progress handler once if no data arrives immediately. */
30 #define IO_IMMEDIATE 0x200
31 /* Emit a chunk length before every write operation */
32 #define IO_CHUNKED 0x400
33 /* Emit a zero-length chunk at the end if chunked */
34 #define IO_END 0x800
35 
36 /* Internal -- header is really buf3 */
37 #define IO_BUF3 0x1000
38 /* Internal -- header is really buf_location */
39 #define IO_BUF_LOCATION 0x2000
40 
41 typedef struct _StreamRequest {
42     short operation;
43     short fd;
44     int offset;
45     int len;
46     int len2;
47     union {
48         struct {
49             int hlen;
50             char *header;
51         } h;
52         struct {
53             int len3;
54             char *buf3;
55         } b;
56         struct {
57             char **buf_location;
58         } l;
59     } u;
60     char *buf;
61     char *buf2;
62     int (*handler)(int, FdEventHandlerPtr, struct _StreamRequest*);
63     void *data;
64 } StreamRequestRec, *StreamRequestPtr;
65 
66 typedef struct _ConnectRequest {
67     int fd;
68     int af;
69     struct _Atom *addr;
70     int firstindex;
71     int index;
72     int port;
73     int (*handler)(int, FdEventHandlerPtr, struct _ConnectRequest*);
74     void *data;
75 } ConnectRequestRec, *ConnectRequestPtr;
76 
77 typedef struct _AcceptRequest {
78     int fd;
79     int (*handler)(int, FdEventHandlerPtr, struct _AcceptRequest*);
80     void *data;
81 } AcceptRequestRec, *AcceptRequestPtr;
82 
83 void preinitIo();
84 void initIo();
85 
86 FdEventHandlerPtr
87 do_stream(int operation, int fd, int offset, char *buf, int len,
88           int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
89           void *data);
90 
91 FdEventHandlerPtr
92 do_stream_h(int operation, int fd, int offset,
93             char *header, int hlen, char *buf, int len,
94             int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
95             void *data);
96 
97 FdEventHandlerPtr
98 do_stream_2(int operation, int fd, int offset,
99             char *buf, int len, char *buf2, int len2,
100             int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
101             void *data);
102 
103 FdEventHandlerPtr
104 do_stream_3(int operation, int fd, int offset,
105             char *buf, int len, char *buf2, int len2, char *buf3, int len3,
106             int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
107             void *data);
108 
109 FdEventHandlerPtr
110 do_stream_buf(int operation, int fd, int offset, char **buf_location, int len,
111               int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
112               void *data);
113 
114 FdEventHandlerPtr
115 schedule_stream(int operation, int fd, int offset,
116                 char *header, int hlen,
117                 char *buf, int len, char *buf2, int len2, char *buf3, int len3,
118                 char **buf_location,
119                 int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
120                 void *data);
121 
122 int do_scheduled_stream(int, FdEventHandlerPtr);
123 int streamRequestDone(StreamRequestPtr);
124 
125 FdEventHandlerPtr
126 do_connect(struct _Atom *addr, int index, int port,
127            int (*handler)(int, FdEventHandlerPtr, ConnectRequestPtr),
128            void *data);
129 
130 int do_scheduled_connect(int, FdEventHandlerPtr event);
131 
132 FdEventHandlerPtr
133 do_accept(int fd,
134           int (*handler)(int, FdEventHandlerPtr, AcceptRequestPtr),
135           void* data);
136 
137 FdEventHandlerPtr
138 schedule_accept(int fd,
139                 int (*handler)(int, FdEventHandlerPtr, AcceptRequestPtr),
140                 void* data);
141 
142 int do_scheduled_accept(int, FdEventHandlerPtr event);
143 
144 FdEventHandlerPtr
145 create_listener(char *address, int port,
146                 int (*handler)(int, FdEventHandlerPtr, AcceptRequestPtr),
147                 void *data);
148 int setNonblocking(int fd, int nonblocking);
149 int setNodelay(int fd, int nodelay);
150 int setV6only(int fd, int v6only);
151 int lingeringClose(int fd);
152 
153 typedef struct _NetAddress {
154     int prefix;
155     int af;
156     unsigned char data[16];
157 } NetAddressRec, *NetAddressPtr;
158 
159 NetAddressPtr parseNetAddress(AtomListPtr list);
160 int netAddressMatch(int fd, NetAddressPtr list) ATTRIBUTE ((pure));
161 
162