1 /*-
2  * Copyright (c) 2019 Felix Weinrank
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef __PROGRAMS_HELPER_H__
29 #define __PROGRAMS_HELPER_H__
30 
31 #ifndef _WIN32
32 #define SCTP_PACKED __attribute__((packed))
33 #else
34 #pragma pack (push, 1)
35 #define SCTP_PACKED
36 #endif
37 
38 struct sctp_chunk_header {
39 	uint8_t chunk_type;	/* chunk type */
40 	uint8_t chunk_flags;	/* chunk flags */
41 	uint16_t chunk_length;	/* chunk length */
42 	/* optional params follow */
43 } SCTP_PACKED;
44 
45 struct sctp_init_chunk {
46 	struct sctp_chunk_header ch;
47 	uint32_t initiate_tag;	/* initiate tag */
48 	uint32_t a_rwnd;	/* a_rwnd */
49 	uint16_t num_outbound_streams;	/* OS */
50 	uint16_t num_inbound_streams;	/* MIS */
51 	uint32_t initial_tsn;	/* I-TSN */
52 	/* optional param's follow */
53 } SCTP_PACKED;
54 
55 #ifdef _WIN32
56 #pragma pack(pop)
57 #endif
58 
59 #undef SCTP_PACKED
60 
61 void
62 handle_notification(union sctp_notification *notif, size_t n);
63 #ifndef timersub
64 #define timersub(tvp, uvp, vvp)                                   \
65 	do {                                                      \
66 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;    \
67 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
68 		if ((vvp)->tv_usec < 0) {                         \
69 			(vvp)->tv_sec--;                          \
70 			(vvp)->tv_usec += 1000000;                \
71 		}                                                 \
72 	} while (0)
73 #endif
74 
75 void
76 debug_printf_runtime(void);
77 
78 void
79 debug_printf_stack(const char *format, ...);
80 
81 #define debug_printf(...)                       \
82 	do {                                        \
83 		fprintf(stderr, "[P]");                 \
84 		debug_printf_runtime();                 \
85 		fprintf(stderr, __VA_ARGS__);           \
86 	} while (0)
87 #endif /* __PROGRAMS_HELPER_H__ */
88