0
0
mirror of https://github.com/darkk/redsocks.git synced 2025-08-25 11:15:30 +00:00
redsocks/utils.h
Bin Jin dcbdcae943 Libevent2 fix: bad default line-extracting strategy
Libevent 2 deprecates evbuffer_readline() function, where any sequence
of any number of carriage return and linefeed characters is treated like
end of line, and introduces better evbuffer_readln(), where termination
format can be explicitly set to: \n, [\r]\n, \r\n or already mentioned
old behaviour.

Change past evbuffer_readline() calls to new
redsocks_evbuffer_readline() function. If libevent 2 is present, use
there evbuffer_readln() with eol_style set to an optional carriage
return, followed by a linefeed (EVBUFFER_EOL_CRLF) instead of obsolete
evbuffer_readln().

  Important note:
Consuming all CR and LF characters in one go (behaviour of
evbuffer_readline(), nowadays aliasing to evbuffer_readln() with
eol_style set to EVBUFFER_EOL_ANY) hangs up parsing of HTTP request
header ending with "\r\n\r\n", because it misses the empty line.

Conflicts (resolved):

	utils.c
	utils.h
2011-11-27 03:54:23 +04:00

55 lines
2.0 KiB
C

#ifndef UTILS_H_SAT_FEB__2_02_24_05_2008
#define UTILS_H_SAT_FEB__2_02_24_05_2008
#include <stddef.h>
#include <time.h>
#include <event.h>
struct sockaddr_in;
#define SIZEOF_ARRAY(arr) (sizeof(arr) / sizeof(arr[0]))
#define FOREACH(ptr, array) for (ptr = array; ptr < array + SIZEOF_ARRAY(array); ptr++)
#define FOREACH_REV(ptr, array) for (ptr = array + SIZEOF_ARRAY(array) - 1; ptr >= array; ptr--)
#define UNUSED(x) ((void)(x))
#if defined __GNUC__
#define PACKED __attribute__((packed))
#else
#error Unknown compiler, modify utils.h for it
#endif
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
time_t redsocks_time(time_t *t);
char *redsocks_evbuffer_readline(struct evbuffer *buf);
struct bufferevent* red_connect_relay(struct sockaddr_in *addr, evbuffercb writecb, everrorcb errorcb, void *cbarg);
int red_socket_geterrno(struct bufferevent *buffev);
int red_is_socket_connected_ok(struct bufferevent *buffev);
int red_recv_udp_pkt(int fd, char *buf, size_t buflen, struct sockaddr_in *inaddr);
int fcntl_nonblock(int fd);
#define event_fmt_str "%s|%s|%s|%s|%s|0x%x"
#define event_fmt(what) \
(what) & EVBUFFER_READ ? "EVBUFFER_READ" : "0", \
(what) & EVBUFFER_WRITE ? "EVBUFFER_WRITE" : "0", \
(what) & EVBUFFER_EOF ? "EVBUFFER_EOF" : "0", \
(what) & EVBUFFER_ERROR ? "EVBUFFER_ERROR" : "0", \
(what) & EVBUFFER_TIMEOUT ? "EVBUFFER_TIMEOUT" : "0", \
(what) & ~(EVBUFFER_READ|EVBUFFER_WRITE|EVBUFFER_EOF|EVBUFFER_ERROR|EVBUFFER_TIMEOUT)
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
/* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */
#endif /* UTILS_H_SAT_FEB__2_02_24_05_2008 */