1 /*
2     This file is part of libmicrospdy
3     Copyright Copyright (C) 2012 Andrey Uzunov
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file microspdy/internal.h
21  * @brief  internal functions and macros for the framing layer
22  * @author Andrey Uzunov
23  */
24 
25 #ifndef INTERNAL_H_H
26 #define INTERNAL_H_H
27 
28 #include "platform.h"
29 #include "microspdy.h"
30 
31 /**
32  * size of read buffers for each connection
33  * must be at least the size of SPDY_MAX_SUPPORTED_FRAME_SIZE
34  */
35 #define SPDYF_BUFFER_SIZE 8192
36 
37 /**
38  * initial size of window for each stream (this is for the data
39  * within data frames that can be handled)
40  */
41 #define SPDYF_INITIAL_WINDOW_SIZE 65536
42 
43 /**
44  * number of frames written to the socket at once. After X frames
45  * everything should be run again. In this way the application can
46  * response to more important requests while a big file is still
47  * being transmitted to the client
48  */
49 #define SPDYF_NUM_SENT_FRAMES_AT_ONCE 10
50 
51 
52 /**
53  * Handler for fatal errors.
54  */
55 extern SPDY_PanicCallback spdyf_panic;
56 
57 
58 /**
59  * Closure argument for "mhd_panic".
60  */
61 extern void *spdyf_panic_cls;
62 
63 
64 /**
65  * Trigger 'panic' action based on fatal errors.
66  *
67  * @param msg error message (const char *)
68  */
69 #define SPDYF_PANIC(msg) \
70 	spdyf_panic (spdyf_panic_cls, __FILE__, __LINE__, msg)
71 
72 
73 /**
74  * Asserts the validity of an expression.
75  *
76  * @param expr (bool)
77  * @param msg message to print on error (const char *)
78  */
79 #define SPDYF_ASSERT(expr, msg) \
80 	if(!(expr)){\
81 		SPDYF_PANIC(msg);\
82 		abort();\
83 	}
84 
85 
86 /**
87  * Convert 24 bit integer from host byte order to network byte order.
88  *
89  * @param n input value (int32_t)
90  * @return converted value (uint32_t)
91  */
92 #if HAVE_BIG_ENDIAN
93 #define HTON24(n) n
94 #else
95 #define HTON24(n) (((((uint32_t)(n) & 0xFF)) << 16)\
96 	| (((uint32_t)(n) & 0xFF00))\
97 	| ((((uint32_t)(n) & 0xFF0000)) >> 16))
98 #endif
99 
100 
101 /**
102  * Convert 24 bit integer from network byte order to host byte order.
103  *
104  * @param n input value (int32_t)
105  * @return converted value (uint32_t)
106  */
107 #if HAVE_BIG_ENDIAN
108 #define NTOH24(n) n
109 #else
110 #define NTOH24(n) (((((uint32_t)(n) & 0xFF)) << 16)\
111 	| (((uint32_t)(n) & 0xFF00))\
112 	| ((((uint32_t)(n) & 0xFF0000)) >> 16))
113 #endif
114 
115 
116 /**
117  * Convert 31 bit integer from network byte order to host byte order.
118  *
119  * @param n input value (int32_t)
120  * @return converted value (uint32_t)
121  */
122 #if HAVE_BIG_ENDIAN
123 #define NTOH31(n) n
124 #else
125 #define NTOH31(n) (((((uint32_t)(n) & 0x7F)) << 24) | \
126                   ((((uint32_t)(n) & 0xFF00)) << 8) | \
127                   ((((uint32_t)(n) & 0xFF0000)) >> 8) | \
128                   ((((uint32_t)(n) & 0xFF000000)) >> 24))
129 #endif
130 
131 
132 /**
133  * Convert 31 bit integer from host byte order to network byte order.
134  *
135  * @param n input value (int32_t)
136  * @return converted value (uint32_t)
137  */
138 #if HAVE_BIG_ENDIAN
139 #define HTON31(n) n
140 #else
141 #define HTON31(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
142                   ((((uint32_t)(n) & 0xFF00)) << 8) | \
143                   ((((uint32_t)(n) & 0xFF0000)) >> 8) | \
144                   ((((uint32_t)(n) & 0x7F000000)) >> 24))
145 #endif
146 
147 
148 /**
149  * Print formatted debug value.
150  *
151  * @param fmt format (const char *)
152  * @param ... args for format
153  */
154 #define SPDYF_DEBUG(fmt, ...) do { \
155 	fprintf (stdout, "%s\n%u: ",__FILE__, __LINE__);\
156 	fprintf(stdout,fmt,##__VA_ARGS__);\
157 	fprintf(stdout,"\n");\
158 	fflush(stdout); } while (0)
159 
160 
161 /**
162  * Print stream for debuging.
163  *
164  * @param strm (void *)
165  * @param size (int)
166  */
167 #define SPDYF_PRINT_STREAM(strm, size) do { \
168 	int ___i;\
169 	for(___i=0;___i<size;___i++){\
170 		fprintf(stdout,"%x ",*((uint8_t *) strm + ___i));\
171 		fflush(stdout);\
172 	}\
173 	fprintf(stdout,"\n");\
174 	} while (0)
175 
176 
177 /**
178  * Print message and raise SIGINT for debug purposes.
179  *
180  * @param msg message (const char *)
181  */
182 #define SPDYF_SIGINT(msg) do { \
183 	fprintf(stdout,"%i : %s\n", __LINE__,__FILE__);\
184 	fprintf(stdout,msg);\
185 	fprintf(stdout,"\n");\
186 	fflush(stdout);\
187 	raise(SIGINT); } while (0)
188 
189 
190 /**
191  * Returns monotonic time, to be used for session timeouts.
192  *
193  * @return time in milliseconds
194  */
195 unsigned long long
196 SPDYF_monotonic_time(void);
197 
198 #endif
199