2013-07-10 17:15:38 -04:00
|
|
|
#ifndef __COMMON_H_
|
|
|
|
#define __COMMON_H_
|
|
|
|
|
2013-07-26 13:42:22 -04:00
|
|
|
/* FD_SETSIZE is 64 on Cygwin, which is really low. Just redefining it is
|
|
|
|
* enough for the macros to adapt (http://support.microsoft.com/kb/111855)
|
|
|
|
*/
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#define FD_SETSIZE 4096
|
|
|
|
#endif
|
2013-07-10 17:15:38 -04:00
|
|
|
|
2013-07-10 17:12:42 -04:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <time.h>
|
2013-07-10 17:14:15 -04:00
|
|
|
#include <getopt.h>
|
2013-08-06 08:29:56 -04:00
|
|
|
#include "version.h"
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
#define CHECK_RES_DIE(res, str) \
|
|
|
|
if (res == -1) { \
|
|
|
|
perror(str); \
|
|
|
|
exit(1); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_RES_RETURN(res, str) \
|
|
|
|
if (res == -1) { \
|
2013-07-21 07:46:45 -04:00
|
|
|
log_message(LOG_CRIT, "%s:%d:%s\n", str, errno, strerror(errno)); \
|
2013-07-10 17:12:42 -04:00
|
|
|
return res; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
#define TRACE fprintf(stderr, "%s:%d\n", __FILE__, __LINE__);
|
|
|
|
#else
|
|
|
|
#define TRACE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum connection_state {
|
|
|
|
ST_PROBING=1, /* Waiting for timeout to find where to forward */
|
|
|
|
ST_SHOVELING /* Connexion is established */
|
|
|
|
};
|
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
/* this is used to pass protocols through the command-line parameter parsing */
|
|
|
|
#define PROT_SHIFT 1000 /* protocol options will be 1000, 1001, etc */
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
/* A 'queue' is composed of a file descriptor (which can be read from or
|
|
|
|
* written to), and a queue for defered write data */
|
|
|
|
struct queue {
|
|
|
|
int fd;
|
|
|
|
void *begin_defered_data;
|
|
|
|
void *defered_data;
|
|
|
|
int defered_data_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct connection {
|
|
|
|
enum connection_state state;
|
|
|
|
time_t probe_timeout;
|
2013-09-23 18:30:34 -04:00
|
|
|
struct proto *proto;
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
/* q[0]: queue for external connection (client);
|
|
|
|
* q[1]: queue for internal connection (httpd or sshd);
|
|
|
|
* */
|
|
|
|
struct queue q[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FD_CNXCLOSED 0
|
|
|
|
#define FD_NODATA -1
|
|
|
|
#define FD_STALLED -2
|
|
|
|
|
|
|
|
|
|
|
|
/* common.c */
|
|
|
|
void init_cnx(struct connection *cnx);
|
2013-07-21 07:46:45 -04:00
|
|
|
int connect_addr(struct addrinfo *addr, int fd_from, const char* cnx_name);
|
2013-07-10 17:12:42 -04:00
|
|
|
int fd2fd(struct queue *target, struct queue *from);
|
2013-07-10 17:14:15 -04:00
|
|
|
char* sprintaddr(char* buf, size_t size, struct addrinfo *a);
|
|
|
|
void resolve_name(struct addrinfo **out, char* fullname);
|
2013-07-10 17:12:42 -04:00
|
|
|
void log_connection(struct connection *cnx);
|
2013-07-10 17:15:38 -04:00
|
|
|
int check_access_rights(int in_socket, const char* service);
|
2013-07-10 17:12:42 -04:00
|
|
|
void setup_signals(void);
|
2013-07-10 17:15:38 -04:00
|
|
|
void setup_syslog(const char* bin_name);
|
|
|
|
void drop_privileges(const char* user_name);
|
|
|
|
void write_pid_file(const char* pidfile);
|
2013-07-10 17:12:42 -04:00
|
|
|
void log_message(int type, char* msg, ...);
|
|
|
|
void dump_connection(struct connection *cnx);
|
2013-07-10 17:15:38 -04:00
|
|
|
int resolve_split_name(struct addrinfo **out, const char* hostname, const char* port);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
2013-07-10 17:14:15 -04:00
|
|
|
int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list);
|
2013-07-10 17:12:42 -04:00
|
|
|
|
|
|
|
int defer_write(struct queue *q, void* data, int data_size);
|
|
|
|
int flush_defered(struct queue *q);
|
|
|
|
|
2013-07-21 07:46:45 -04:00
|
|
|
extern int probing_timeout, verbose, inetd, foreground,
|
|
|
|
background, transparent, numeric;
|
2013-07-10 17:14:15 -04:00
|
|
|
extern struct sockaddr_storage addr_ssl, addr_ssh, addr_openvpn;
|
|
|
|
extern struct addrinfo *addr_listen;
|
2013-07-10 17:12:42 -04:00
|
|
|
extern const char* USAGE_STRING;
|
2013-07-10 17:19:33 -04:00
|
|
|
extern const char* user_name, *pid_file;
|
2013-07-10 17:12:42 -04:00
|
|
|
extern const char* server_type;
|
|
|
|
|
|
|
|
/* sslh-fork.c */
|
|
|
|
void start_shoveler(int);
|
|
|
|
|
2013-07-10 17:13:32 -04:00
|
|
|
void main_loop(int *listen_sockets, int num_addr_listen);
|
2013-07-10 17:15:38 -04:00
|
|
|
|
|
|
|
#endif
|