xref: /freebsd/contrib/ntp/include/recvbuff.h (revision 7d8f797b725e3efc0a4256554654780df83c456c)
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 1000.
43  */
44 #define	RX_BUFF_SIZE	1000		/* hail Mary */
45 
46 
47 typedef struct recvbuf recvbuf_t;
48 
49 struct recvbuf {
50 	recvbuf_t *	link;	/* next in list */
51 	union {
52 		sockaddr_u	X_recv_srcadr;
53 		caddr_t		X_recv_srcclock;
54 		struct peer *	X_recv_peer;
55 	} X_from_where;
56 #define recv_srcadr		X_from_where.X_recv_srcadr
57 #define	recv_srcclock		X_from_where.X_recv_srcclock
58 #define recv_peer		X_from_where.X_recv_peer
59 #ifndef HAVE_IO_COMPLETION_PORT
60 	sockaddr_u	srcadr;		/* where packet came from */
61 #else
62 	int		recv_srcadr_len;/* filled in on completion */
63 #endif
64 	endpt *		dstadr;		/* address pkt arrived on */
65 	SOCKET		fd;		/* fd on which it was received */
66 	int		msg_flags;	/* Flags received about the packet */
67 	l_fp		recv_time;	/* time of arrival */
68 	void		(*receiver)(struct recvbuf *); /* callback */
69 	int		recv_length;	/* number of octets received */
70 	union {
71 		struct pkt	X_recv_pkt;
72 		u_char		X_recv_buffer[RX_BUFF_SIZE];
73 	} recv_space;
74 #define	recv_pkt		recv_space.X_recv_pkt
75 #define	recv_buffer		recv_space.X_recv_buffer
76 	int used;		/* reference count */
77 };
78 
79 extern	void	init_recvbuff(int);
80 
81 /* freerecvbuf - make a single recvbuf available for reuse
82  */
83 extern	void	freerecvbuf(struct recvbuf *);
84 
85 /*  Get a free buffer (typically used so an async
86  *  read can directly place data into the buffer
87  *
88  *  The buffer is removed from the free list. Make sure
89  *  you put it back with freerecvbuf() or
90  */
91 
92 /* signal safe - no malloc */
93 extern	struct recvbuf *get_free_recv_buffer(void);
94 /* signal unsafe - may malloc */
95 extern	struct recvbuf *get_free_recv_buffer_alloc(void);
96 
97 /*   Add a buffer to the full list
98  */
99 extern	void	add_full_recv_buffer(struct recvbuf *);
100 
101 /* number of recvbufs on freelist */
102 extern u_long free_recvbuffs(void);
103 extern u_long full_recvbuffs(void);
104 extern u_long total_recvbuffs(void);
105 extern u_long lowater_additions(void);
106 
107 /*  Returns the next buffer in the full list.
108  *
109  */
110 extern	struct recvbuf *get_full_recv_buffer(void);
111 
112 /*
113  * purge_recv_buffers_for_fd() - purges any previously-received input
114  *				 from a given file descriptor.
115  */
116 extern	void purge_recv_buffers_for_fd(SOCKET);
117 
118 /*
119  * Checks to see if there are buffers to process
120  */
121 extern isc_boolean_t has_full_recv_buffer(void);
122 
123 #endif	/* RECVBUFF_H */
124