xref: /freebsd/contrib/ntp/include/recvbuff.h (revision 499fe48de8938d4c7b0a91e20eb6c16db9d55633)
1 #ifndef RECVBUFF_H
2 #define RECVBUFF_H
3 
4 #include "ntp.h"
5 #include "ntp_net.h"
6 #include "ntp_lists.h"
7 
8 #include <isc/result.h>
9 
10 /*
11  * recvbuf memory management
12  */
13 #define RECV_INIT	10	/* 10 buffers initially */
14 #define RECV_LOWAT	3	/* when we're down to three buffers get more */
15 #define RECV_INC	5	/* get 5 more at a time */
16 #define RECV_TOOMANY	40	/* this is way too many buffers */
17 
18 #if defined HAVE_IO_COMPLETION_PORT
19 # include "ntp_iocompletionport.h"
20 # include "ntp_timer.h"
21 
22 # define RECV_BLOCK_IO()	EnterCriticalSection(&RecvCritSection)
23 # define RECV_UNBLOCK_IO()	LeaveCriticalSection(&RecvCritSection)
24 
25 /*  Return the event which is set when items are added to the full list
26  */
27 extern HANDLE	get_recv_buff_event(void);
28 #else
29 # define RECV_BLOCK_IO()
30 # define RECV_UNBLOCK_IO()
31 #endif
32 
33 
34 /*
35  * Format of a recvbuf.  These are used by the asynchronous receive
36  * routine to store incoming packets and related information.
37  */
38 
39 /*
40  *  the maximum length NTP packet contains the NTP header, one Autokey
41  *  request, one Autokey response and the MAC. Assuming certificates don't
42  *  get too big, the maximum packet length is set arbitrarily at 1200.
43  *  (was 1000, but that bumps on 2048 RSA keys)
44  */
45 #define	RX_BUFF_SIZE	1200		/* hail Mary */
46 
47 
48 typedef struct recvbuf recvbuf_t;
49 
50 struct recvbuf {
51 	recvbuf_t *	link;	/* next in list */
52 	union {
53 		sockaddr_u	X_recv_srcadr;
54 		caddr_t		X_recv_srcclock;
55 		struct peer *	X_recv_peer;
56 	} X_from_where;
57 #define recv_srcadr		X_from_where.X_recv_srcadr
58 #define	recv_srcclock		X_from_where.X_recv_srcclock
59 #define recv_peer		X_from_where.X_recv_peer
60 #ifndef HAVE_IO_COMPLETION_PORT
61 	sockaddr_u	srcadr;		/* where packet came from */
62 #else
63 	int		recv_srcadr_len;/* filled in on completion */
64 #endif
65 	endpt *		dstadr;		/* address pkt arrived on */
66 	SOCKET		fd;		/* fd on which it was received */
67 	int		msg_flags;	/* Flags received about the packet */
68 	l_fp		recv_time;	/* time of arrival */
69 	void		(*receiver)(struct recvbuf *); /* callback */
70 	int		recv_length;	/* number of octets received */
71 	union {
72 		struct pkt	X_recv_pkt;
73 		u_char		X_recv_buffer[RX_BUFF_SIZE];
74 	} recv_space;
75 #define	recv_pkt		recv_space.X_recv_pkt
76 #define	recv_buffer		recv_space.X_recv_buffer
77 	int used;		/* reference count */
78 };
79 
80 extern	void	init_recvbuff(int);
81 
82 /* freerecvbuf - make a single recvbuf available for reuse
83  */
84 extern	void	freerecvbuf(struct recvbuf *);
85 
86 /*  Get a free buffer (typically used so an async
87  *  read can directly place data into the buffer
88  *
89  *  The buffer is removed from the free list. Make sure
90  *  you put it back with freerecvbuf() or
91  */
92 
93 /* signal safe - no malloc */
94 extern	struct recvbuf *get_free_recv_buffer(void);
95 /* signal unsafe - may malloc, never returs NULL */
96 extern	struct recvbuf *get_free_recv_buffer_alloc(void);
97 
98 /*   Add a buffer to the full list
99  */
100 extern	void	add_full_recv_buffer(struct recvbuf *);
101 
102 /* number of recvbufs on freelist */
103 extern u_long free_recvbuffs(void);
104 extern u_long full_recvbuffs(void);
105 extern u_long total_recvbuffs(void);
106 extern u_long lowater_additions(void);
107 
108 /*  Returns the next buffer in the full list.
109  *
110  */
111 extern	struct recvbuf *get_full_recv_buffer(void);
112 
113 /*
114  * purge_recv_buffers_for_fd() - purges any previously-received input
115  *				 from a given file descriptor.
116  */
117 extern	void purge_recv_buffers_for_fd(int);
118 
119 /*
120  * Checks to see if there are buffers to process
121  */
122 extern isc_boolean_t has_full_recv_buffer(void);
123 
124 #endif	/* RECVBUFF_H */
125