Electroneum
netevent.h
Go to the documentation of this file.
1 /*
2  * util/netevent.h - event notification
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
60 #ifndef NET_EVENT_H
61 #define NET_EVENT_H
62 
63 #include "dnscrypt/dnscrypt.h"
64 
65 struct sldns_buffer;
66 struct comm_point;
67 struct comm_reply;
68 struct ub_event_base;
69 
70 /* internal event notification data storage structure. */
71 struct internal_event;
72 struct internal_base;
73 struct internal_timer; /* A sub struct of the comm_timer super struct */
74 
76 typedef int comm_point_callback_type(struct comm_point*, void*, int,
77  struct comm_reply*);
78 
80 #define NETEVENT_NOERROR 0
81 
82 #define NETEVENT_CLOSED -1
83 
84 #define NETEVENT_TIMEOUT -2
85 
86 #define NETEVENT_CAPSFAIL -3
87 
88 #define NETEVENT_DONE -4
89 
91 #define NETEVENT_SLOW_ACCEPT_TIME 2000
92 
96 struct comm_base {
98  struct internal_base* eb;
101  void (*stop_accept)(void*);
104  void (*start_accept)(void*);
106  void* cb_arg;
107 };
108 
112 struct comm_reply {
114  struct comm_point* c;
118  socklen_t addrlen;
120  int srctype;
121  /* DnsCrypt context */
122 #ifdef USE_DNSCRYPT
123  uint8_t client_nonce[crypto_box_HALF_NONCEBYTES];
124  uint8_t nmkey[crypto_box_BEFORENMBYTES];
125  const dnsccert *dnsc_cert;
126  int is_dnscrypted;
127 #endif
128 
129  union {
130 #ifdef IPV6_PKTINFO
131  struct in6_pktinfo v6info;
132 #endif
133 #ifdef IP_PKTINFO
134  struct in_pktinfo v4info;
135 #elif defined(IP_RECVDSTADDR)
136  struct in_addr v4addr;
137 #endif
138  }
140  pktinfo;
142  size_t max_udp_size;
143 };
144 
154 struct comm_point {
156  struct internal_event* ev;
157 
159  int fd;
160 
162  struct timeval* timeout;
163 
166 
167  /* -------- TCP Handler -------- */
176 
177  /* -------- TCP Accept -------- */
188 
189  /* -------- SSL TCP DNS ------- */
191  void* ssl;
193  enum {
204  } ssl_shake_state;
205 
206  /* -------- HTTP ------- */
217  size_t http_stored;
218 
219  /* -------- dnstap ------- */
221  struct dt_env* dtenv;
222 
237  }
239  type;
240 
241  /* ---------- Behaviour ----------- */
244 
248 
255 
258 
261 
262 #ifdef USE_MSG_FASTOPEN
263 
264  int tcp_do_fastopen;
265 #endif
266 
267 #ifdef USE_DNSCRYPT
268 
269  int dnscrypt;
271  struct sldns_buffer* dnscrypt_buffer;
272 #endif
273 
275  int inuse;
276 
303  void *cb_arg;
304 };
305 
309 struct comm_timer {
311  struct internal_timer* ev_timer;
312 
314  void (*callback)(void*);
315 
317  void* cb_arg;
318 };
319 
323 struct comm_signal {
325  struct comm_base* base;
326 
328  struct internal_signal* ev_signal;
329 
331  void (*callback)(int, void*);
332 
334  void* cb_arg;
335 };
336 
343 struct comm_base* comm_base_create(int sigs);
344 
351 struct comm_base* comm_base_create_event(struct ub_event_base* base);
352 
358 void comm_base_delete_no_base(struct comm_base* b);
359 
365 void comm_base_delete(struct comm_base* b);
366 
374 void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
375 
380 void comm_base_dispatch(struct comm_base* b);
381 
386 void comm_base_exit(struct comm_base* b);
387 
397  void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
398 
404 struct ub_event_base* comm_base_internal(struct comm_base* b);
405 
417 struct comm_point* comm_point_create_udp(struct comm_base* base,
418  int fd, struct sldns_buffer* buffer,
419  comm_point_callback_type* callback, void* callback_arg);
420 
434  int fd, struct sldns_buffer* buffer,
435  comm_point_callback_type* callback, void* callback_arg);
436 
454 struct comm_point* comm_point_create_tcp(struct comm_base* base,
455  int fd, int num, size_t bufsize,
456  comm_point_callback_type* callback, void* callback_arg);
457 
466 struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
467  size_t bufsize, comm_point_callback_type* callback, void* callback_arg);
468 
479 struct comm_point* comm_point_create_http_out(struct comm_base* base,
480  size_t bufsize, comm_point_callback_type* callback,
481  void* callback_arg, struct sldns_buffer* temp);
482 
492 struct comm_point* comm_point_create_local(struct comm_base* base,
493  int fd, size_t bufsize,
494  comm_point_callback_type* callback, void* callback_arg);
495 
505 struct comm_point* comm_point_create_raw(struct comm_base* base,
506  int fd, int writing,
507  comm_point_callback_type* callback, void* callback_arg);
508 
513 void comm_point_close(struct comm_point* c);
514 
520 void comm_point_delete(struct comm_point* c);
521 
527 
533 
542 int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
543  struct sockaddr* addr, socklen_t addrlen);
544 
549 void comm_point_stop_listening(struct comm_point* c);
550 
558 void comm_point_start_listening(struct comm_point* c, int newfd, int msec);
559 
566 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
567 
575 size_t comm_point_get_mem(struct comm_point* c);
576 
584 struct comm_timer* comm_timer_create(struct comm_base* base,
585  void (*cb)(void*), void* cb_arg);
586 
591 void comm_timer_disable(struct comm_timer* timer);
592 
598 void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
599 
604 void comm_timer_delete(struct comm_timer* timer);
605 
611 int comm_timer_is_set(struct comm_timer* timer);
612 
618 size_t comm_timer_get_mem(struct comm_timer* timer);
619 
628  void (*callback)(int, void*), void* cb_arg);
629 
637 int comm_signal_bind(struct comm_signal* comsig, int sig);
638 
643 void comm_signal_delete(struct comm_signal* comsig);
644 
655  struct sockaddr_storage* addr, socklen_t* addrlen);
656 
657 /**** internal routines ****/
658 
667 void comm_point_udp_callback(int fd, short event, void* arg);
668 
677 void comm_point_udp_ancil_callback(int fd, short event, void* arg);
678 
687 void comm_point_tcp_accept_callback(int fd, short event, void* arg);
688 
697 void comm_point_tcp_handle_callback(int fd, short event, void* arg);
698 
707 void comm_point_http_handle_callback(int fd, short event, void* arg);
708 
717 void comm_timer_callback(int fd, short event, void* arg);
718 
727 void comm_signal_callback(int fd, short event, void* arg);
728 
737 void comm_point_local_handle_callback(int fd, short event, void* arg);
738 
747 void comm_point_raw_handle_callback(int fd, short event, void* arg);
748 
757 void comm_base_handle_slow_accept(int fd, short event, void* arg);
758 
759 #ifdef USE_WINSOCK
760 
766 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
767 #endif
768 
770 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
771 
772 #endif /* NET_EVENT_H */
struct comm_base * comm_base_create(int sigs)
void comm_point_send_reply(struct comm_reply *repinfo)
struct internal_base * eb
Definition: netevent.h:98
int do_not_close
Definition: netevent.h:243
size_t comm_point_get_mem(struct comm_point *c)
void * cb_arg
Definition: netevent.h:317
struct comm_signal * comm_signal_create(struct comm_base *base, void(*callback)(int, void *), void *cb_arg)
void(* stop_accept)(void *)
Definition: netevent.h:101
struct sldns_buffer * buffer
Definition: netevent.h:165
struct comm_base * comm_base_create_event(struct ub_event_base *base)
void(* callback)(void *)
Definition: netevent.h:314
void comm_point_udp_ancil_callback(int fd, short event, void *arg)
union comm_reply::@43 pktinfo
void comm_base_handle_slow_accept(int fd, short event, void *arg)
int tcp_do_toggle_rw
Definition: netevent.h:254
int tcp_check_nb_connect
Definition: netevent.h:260
int tcp_do_close
Definition: netevent.h:247
void comm_signal_callback(int fd, short event, void *arg)
int tcp_timeout_msec
Definition: netevent.h:257
comm_point_callback_type * callback
Definition: netevent.h:301
void * cb_arg
Definition: netevent.h:303
int http_is_chunked
Definition: netevent.h:213
void comm_point_start_listening(struct comm_point *c, int newfd, int msec)
void comm_point_udp_callback(int fd, short event, void *arg)
struct comm_point * comm_point_create_udp(struct comm_base *base, int fd, struct sldns_buffer *buffer, comm_point_callback_type *callback, void *callback_arg)
int comm_point_callback_type(struct comm_point *, void *, int, struct comm_reply *)
Definition: netevent.h:76
void comm_base_delete(struct comm_base *b)
void * cb_arg
Definition: netevent.h:106
void comm_point_close(struct comm_point *c)
unsigned char uint8_t
Definition: stdint.h:124
struct comm_point * comm_point_create_raw(struct comm_base *base, int fd, int writing, comm_point_callback_type *callback, void *callback_arg)
void * cb_arg
Definition: netevent.h:334
struct comm_point * tcp_free
Definition: netevent.h:187
struct comm_point * comm_point_create_tcp_out(struct comm_base *base, size_t bufsize, comm_point_callback_type *callback, void *callback_arg)
struct comm_point * tcp_parent
Definition: netevent.h:173
void comm_point_tcp_handle_callback(int fd, short event, void *arg)
int comm_point_perform_accept(struct comm_point *c, struct sockaddr_storage *addr, socklen_t *addrlen)
void comm_timer_callback(int fd, short event, void *arg)
int comm_signal_bind(struct comm_signal *comsig, int sig)
void(* start_accept)(void *)
Definition: netevent.h:104
void comm_point_http_handle_callback(int fd, short event, void *arg)
void(* callback)(int, void *)
Definition: netevent.h:331
socklen_t addrlen
Definition: netevent.h:118
int max_tcp_count
Definition: netevent.h:179
int tcp_connect_errno_needs_log(struct sockaddr *addr, socklen_t addrlen)
void comm_base_set_slow_accept_handlers(struct comm_base *b, void(*stop_accept)(void *), void(*start_accept)(void *), void *arg)
size_t comm_timer_get_mem(struct comm_timer *timer)
void comm_base_exit(struct comm_base *b)
struct sldns_buffer * http_temp
Definition: netevent.h:215
void * ssl
Definition: netevent.h:191
void comm_point_delete(struct comm_point *c)
struct internal_signal * ev_signal
Definition: netevent.h:328
void comm_timer_disable(struct comm_timer *timer)
void comm_point_listen_for_rw(struct comm_point *c, int rd, int wr)
size_t http_stored
Definition: netevent.h:217
int http_in_headers
Definition: netevent.h:208
void comm_point_tcp_accept_callback(int fd, short event, void *arg)
struct comm_point * comm_point_create_http_out(struct comm_base *base, size_t bufsize, comm_point_callback_type *callback, void *callback_arg, struct sldns_buffer *temp)
int tcp_is_reading
Definition: netevent.h:169
struct internal_timer * ev_timer
Definition: netevent.h:311
struct comm_point * c
Definition: netevent.h:114
struct comm_base * base
Definition: netevent.h:325
void comm_base_delete_no_base(struct comm_base *b)
enum comm_point::comm_point_type type
struct comm_point ** tcp_handlers
Definition: netevent.h:184
struct sockaddr_storage addr
Definition: netevent.h:116
int comm_timer_is_set(struct comm_timer *timer)
int http_in_chunk_headers
Definition: netevent.h:211
struct comm_point * comm_point_create_local(struct comm_base *base, int fd, size_t bufsize, comm_point_callback_type *callback, void *callback_arg)
struct comm_reply repinfo
Definition: netevent.h:175
void comm_base_timept(struct comm_base *b, time_t **tt, struct timeval **tv)
size_t tcp_byte_count
Definition: netevent.h:171
void comm_point_drop_reply(struct comm_reply *repinfo)
void comm_signal_delete(struct comm_signal *comsig)
struct comm_point * comm_point_create_udp_ancil(struct comm_base *base, int fd, struct sldns_buffer *buffer, comm_point_callback_type *callback, void *callback_arg)
int inuse
Definition: netevent.h:275
enum comm_point::@44 ssl_shake_state
struct internal_event * ev
Definition: netevent.h:156
struct comm_point * comm_point_create_tcp(struct comm_base *base, int fd, int num, size_t bufsize, comm_point_callback_type *callback, void *callback_arg)
void comm_point_local_handle_callback(int fd, short event, void *arg)
int srctype
Definition: netevent.h:120
size_t max_udp_size
Definition: netevent.h:142
struct comm_timer * comm_timer_create(struct comm_base *base, void(*cb)(void *), void *cb_arg)
struct timeval * timeout
Definition: netevent.h:162
void comm_timer_delete(struct comm_timer *timer)
int comm_point_send_udp_msg(struct comm_point *c, struct sldns_buffer *packet, struct sockaddr *addr, socklen_t addrlen)
int cur_tcp_count
Definition: netevent.h:181
void comm_point_stop_listening(struct comm_point *c)
struct dt_env * dtenv
Definition: netevent.h:221
void comm_point_raw_handle_callback(int fd, short event, void *arg)
void comm_base_dispatch(struct comm_base *b)
void comm_timer_set(struct comm_timer *timer, struct timeval *tv)
struct ub_event_base * comm_base_internal(struct comm_base *b)