aboutsummaryrefslogtreecommitdiff
path: root/src/microspdy/internal.h
blob: a7aec14f92ae6a505f929689bc321ecb38bc5adb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
    This file is part of libmicrospdy
    Copyright (C) 2012 Andrey Uzunov

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file structures.h
 * @brief  internal functions and macros for the framing layer
 * @author Andrey Uzunov
 */

#ifndef INTERNAL_H_H
#define INTERNAL_H_H

#include "platform.h"
#include "microspdy.h"

/**
 * size of read buffers for each connection
 * must be at least the size of SPDY_MAX_SUPPORTED_FRAME_SIZE
 */
#define SPDYF_BUFFER_SIZE 8192

/**
 * initial size of window for each stream (this is for the data
 * within data frames that can be handled)
 */
#define SPDYF_INITIAL_WINDOW_SIZE 65536

/**
 * number of frames written to the socket at once. After X frames
 * everything should be run again. In this way the application can
 * response to more important requests while a big file is still
 * being transmitted to the client
 */
#define SPDYF_NUM_SENT_FRAMES_AT_ONCE 10


/**
 * Handler for fatal errors.
 */
extern SPDY_PanicCallback spdyf_panic;


/**
 * Closure argument for "mhd_panic".
 */
extern void *spdyf_panic_cls;


/**
 * Trigger 'panic' action based on fatal errors.
 * 
 * @param msg error message (const char *)
 */
#define SPDYF_PANIC(msg) \
	spdyf_panic (spdyf_panic_cls, __FILE__, __LINE__, msg)


/**
 * Asserts the validity of an expression.
 *
 * @param expression (bool)
 * @param msg message to print on error (const char *)
 */
#define SPDYF_ASSERT(expr,msg) \
	if(!(expr)){\
		SPDYF_PANIC(msg);\
		abort();\
	}


/**
 * Convert 24 bit integer from host byte order to network byte order.
 *
 * @param n input value (int32_t)
 * @return converted value (uint32_t)
 */
#if HAVE_BIG_ENDIAN
#define HTON24(n) n
#else
#define HTON24(n) (((((uint32_t)(n) & 0xFF)) << 16)\
	| (((uint32_t)(n) & 0xFF00))\
	| ((((uint32_t)(n) & 0xFF0000)) >> 16))
#endif


/**
 * Convert 24 bit integer from network byte order to host byte order.
 *
 * @param n input value (int32_t)
 * @return converted value (uint32_t)
 */	
#if HAVE_BIG_ENDIAN
#define NTOH24(n) n
#else
#define NTOH24(n) (((((uint32_t)(n) & 0xFF)) << 16)\
	| (((uint32_t)(n) & 0xFF00))\
	| ((((uint32_t)(n) & 0xFF0000)) >> 16))
#endif


/**
 * Convert 31 bit integer from network byte order to host byte order.
 *
 * @param n input value (int32_t)
 * @return converted value (uint32_t)
 */
#if HAVE_BIG_ENDIAN
#define NTOH31(n) n
#else
#define NTOH31(n) (((((uint32_t)(n) & 0x7F)) << 24) | \
                  ((((uint32_t)(n) & 0xFF00)) << 8) | \
                  ((((uint32_t)(n) & 0xFF0000)) >> 8) | \
                  ((((uint32_t)(n) & 0xFF000000)) >> 24))
#endif


/**
 * Convert 31 bit integer from host byte order to network byte order.
 *
 * @param n input value (int32_t)
 * @return converted value (uint32_t)
 */
#if HAVE_BIG_ENDIAN
#define HTON31(n) n
#else
#define HTON31(n) (((((uint32_t)(n) & 0xFF)) << 24) | \
                  ((((uint32_t)(n) & 0xFF00)) << 8) | \
                  ((((uint32_t)(n) & 0xFF0000)) >> 8) | \
                  ((((uint32_t)(n) & 0x7F000000)) >> 24))
#endif


/**
 * Print formatted debug value.
 *
 * @param fmt format (const char *)
 * @param ... args for format
 */
#define SPDYF_DEBUG(fmt, ...) do { \
	fprintf (stdout, "%s\n%u: ",__FILE__, __LINE__);\
	fprintf(stdout,fmt,##__VA_ARGS__);\
	fprintf(stdout,"\n");\
	fflush(stdout); } while (0)


/**
 * Print stream for debuging.
 *
 * @param strm (void *)
 * @param size (int)
 */
#define SPDYF_PRINT_STREAM(strm, size) do { \
	int ___i;\
	for(___i=0;___i<size;___i++){\
		fprintf(stdout,"%x ",*((uint8_t *) strm + ___i));\
		fflush(stdout);\
	}\
	fprintf(stdout,"\n");\
	} while (0)


/**
 * Print message and raise SIGINT for debug purposes.
 *
 * @param msg message (const char *)
 */
#define SPDYF_SIGINT(msg) do { \
	fprintf(stdout,"%i : %s\n", __LINE__,__FILE__);\
	fprintf(stdout,msg);\
	fprintf(stdout,"\n");\
	fflush(stdout);\
	raise(SIGINT); } while (0)


/**
 * Returns monotonic time, to be used for session timeouts.
 *
 * @return time in milliseconds
 */
unsigned long long
SPDYF_monotonic_time(void);

#endif