]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/util/tube.h
2 * util/tube.h - pipe service
4 * Copyright (c) 2008, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains pipe service functions.
50 #include "util/locks.h"
51 #include "util/winsock_event.h"
55 * Callback from pipe listen function
56 * void mycallback(tube, msg, len, error, user_argument);
57 * if error is true (NETEVENT_*), msg is probably NULL.
59 typedef void tube_callback_t(struct tube
*, uint8_t*, size_t, int, void*);
66 /** pipe end to read from */
68 /** pipe end to write on */
71 /** listen commpoint */
72 struct comm_point
* listen_com
;
73 /** listen callback */
74 tube_callback_t
* listen_cb
;
75 /** listen callback user arg */
77 /** are we currently reading a command, 0 if not, else bytecount */
79 /** size of current read command, may be partially read */
81 /** the current read command content, malloced, can be partially read*/
84 /** background write queue, commpoint to write results back */
85 struct comm_point
* res_com
;
86 /** are we curently writing a result, 0 if not, else bytecount into
87 * the res_list first entry. */
89 /** list of outstanding results to be written back */
90 struct tube_res_list
* res_list
;
92 struct tube_res_list
* res_last
;
94 #else /* USE_WINSOCK */
95 /** listen callback */
96 tube_callback_t
* listen_cb
;
97 /** listen callback user arg */
99 /** the windows sockets event (signaled if items in pipe) */
101 /** winsock event storage when registered with event base */
102 struct event ev_listen
;
104 /** lock on the list of outstanding items */
105 lock_basic_t res_lock
;
106 /** list of outstanding results on pipe */
107 struct tube_res_list
* res_list
;
109 struct tube_res_list
* res_last
;
110 #endif /* USE_WINSOCK */
114 * List of results (arbitrary command serializations) to write back
116 struct tube_res_list
{
118 struct tube_res_list
* next
;
119 /** serialized buffer to write */
121 /** length to write */
127 * @return: new tube struct or NULL on error.
129 struct tube
* tube_create(void);
132 * Delete and destroy a pipe
133 * @param tube: to delete
135 void tube_delete(struct tube
* tube
);
138 * Write length bytes followed by message.
139 * @param tube: the tube to write on.
140 * If that tube is a pipe, its write fd is used as
141 * the socket to write on. Is nonblocking.
142 * Set to blocking by the function,
143 * and back to non-blocking at exit of function.
144 * @param buf: the message.
145 * @param len: length of message.
146 * @param nonblock: if set to true, the first write is nonblocking.
147 * If the first write fails the function returns -1.
148 * If set false, the first write is blocking.
149 * @return: all remainder writes are nonblocking.
150 * return 0 on error, in that case blocking/nonblocking of socket is
152 * return 1 if all OK.
154 int tube_write_msg(struct tube
* tube
, uint8_t* buf
, uint32_t len
,
158 * Read length bytes followed by message.
159 * @param tube: The tube to read on.
160 * If that tube is a pipe, its read fd is used as
161 * the socket to read on. Is nonblocking.
162 * Set to blocking by the function,
163 * and back to non-blocking at exit of function.
164 * @param buf: the message, malloced.
165 * @param len: length of message, returned.
166 * @param nonblock: if set to true, the first read is nonblocking.
167 * If the first read fails the function returns -1.
168 * If set false, the first read is blocking.
169 * @return: all remainder reads are nonblocking.
170 * return 0 on error, in that case blocking/nonblocking of socket is
171 * unknown. On EOF 0 is returned.
172 * return 1 if all OK.
174 int tube_read_msg(struct tube
* tube
, uint8_t** buf
, uint32_t* len
,
178 * Close read part of the pipe.
179 * The tube can no longer be read from.
180 * @param tube: tube to operate on.
182 void tube_close_read(struct tube
* tube
);
185 * Close write part of the pipe.
186 * The tube can no longer be written to.
187 * @param tube: tube to operate on.
189 void tube_close_write(struct tube
* tube
);
192 * See if data is ready for reading on the tube without blocking.
193 * @param tube: tube to check for readable items
194 * @return true if readable items are present. False if not (or error).
195 * true on pipe_closed.
197 int tube_poll(struct tube
* tube
);
200 * Wait for data to be ready for reading on the tube. is blocking.
202 * @param tube: the tube to wait on.
203 * @return: if there was something to read (false on error).
204 * true on pipe_closed.
206 int tube_wait(struct tube
* tube
);
209 * Get FD that is readable when new information arrives.
211 * @return file descriptor.
213 int tube_read_fd(struct tube
* tube
);
216 * Start listening for information over the pipe.
217 * Background registration of a read listener, callback when read completed.
218 * Do not mix with tube_read_msg style direct reads from the pipe.
219 * @param tube: tube to listen on
220 * @param base: what base to register event callback.
221 * @param cb: callback routine.
222 * @param arg: user argument for callback routine.
223 * @return true if successful, false on error.
225 int tube_setup_bg_listen(struct tube
* tube
, struct comm_base
* base
,
226 tube_callback_t
* cb
, void* arg
);
229 * Remove bg listen setup from event base.
230 * @param tube: what tube to cleanup
232 void tube_remove_bg_listen(struct tube
* tube
);
235 * Start background write handler for the pipe.
236 * Do not mix with tube_write_msg style direct writes to the pipe.
237 * @param tube: tube to write on
238 * @param base: what base to register event handler on.
239 * @return true if successful, false on error.
241 int tube_setup_bg_write(struct tube
* tube
, struct comm_base
* base
);
244 * Remove bg write setup from event base.
245 * @param tube: what tube to cleanup
247 void tube_remove_bg_write(struct tube
* tube
);
251 * Append data item to background list of writes.
252 * Mallocs a list entry behind the scenes.
253 * Not locked behind the scenes, call from one thread or lock on outside.
254 * @param tube: what tube to queue on.
255 * @param msg: memory message to send. Is free()d after use.
256 * Put at the end of the to-send queue.
257 * @param len: length of item.
258 * @return 0 on failure (msg freed).
260 int tube_queue_item(struct tube
* tube
, uint8_t* msg
, size_t len
);
262 /** for fptr wlist, callback function */
263 int tube_handle_listen(struct comm_point
* c
, void* arg
, int error
,
264 struct comm_reply
* reply_info
);
266 /** for fptr wlist, callback function */
267 int tube_handle_write(struct comm_point
* c
, void* arg
, int error
,
268 struct comm_reply
* reply_info
);
270 /** for fptr wlist, winsock signal event callback function */
271 void tube_handle_signal(int fd
, short events
, void* arg
);
273 #endif /* UTIL_TUBE_H */