mirror of
https://github.com/yrutschle/sslh.git
synced 2025-04-13 15:47:15 +03:00
abstract listening sockets so we have protocol information alongside the socket
This commit is contained in:
parent
ebeabb6c18
commit
c12f7a1ade
13
common.c
13
common.c
@ -70,7 +70,7 @@ void check_res_dump(CR_ACTION act, int res, struct addrinfo *addr, char* syscall
|
||||
}
|
||||
}
|
||||
|
||||
int get_fd_sockets(int *sockfd[])
|
||||
int get_fd_sockets(struct listen_endpoint *sockfd[])
|
||||
{
|
||||
int sd = 0;
|
||||
|
||||
@ -85,7 +85,8 @@ int get_fd_sockets(int *sockfd[])
|
||||
*sockfd = malloc(sd * sizeof(*sockfd[0]));
|
||||
CHECK_ALLOC(*sockfd, "malloc");
|
||||
for (i = 0; i < sd; i++) {
|
||||
(*sockfd)[i] = SD_LISTEN_FDS_START + i;
|
||||
(*sockfd)[i].socketfd = SD_LISTEN_FDS_START + i;
|
||||
(*sockfd)[i].type = SOCK_STREAM;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -157,11 +158,10 @@ int listen_single_addr(struct addrinfo* addr, int keepalive, int udp)
|
||||
}
|
||||
|
||||
/* Starts listening sockets on specified addresses.
|
||||
* OUT: *sockfd[] pointer to newly-allocated array of file descriptors
|
||||
* OUT: *sockfd[] pointer to newly-allocated array of listen_endpoint objects
|
||||
* Returns number of addresses bound
|
||||
* Bound file descriptors are returned in newly-allocated *sockfd pointer
|
||||
*/
|
||||
int start_listen_sockets(int *sockfd[])
|
||||
int start_listen_sockets(struct listen_endpoint *sockfd[])
|
||||
{
|
||||
struct addrinfo *addr, *start_addr;
|
||||
char buf[NI_MAXHOST];
|
||||
@ -189,7 +189,8 @@ int start_listen_sockets(int *sockfd[])
|
||||
|
||||
num_addr++;
|
||||
*sockfd = realloc(*sockfd, num_addr * sizeof(*sockfd));
|
||||
(*sockfd)[num_addr-1] = listen_single_addr(addr, keepalive, udp);
|
||||
(*sockfd)[num_addr-1].socketfd = listen_single_addr(addr, keepalive, udp);
|
||||
(*sockfd)[num_addr-1].type = udp ? SOCK_DGRAM : SOCK_STREAM;
|
||||
if (cfg.verbose)
|
||||
fprintf(stderr, "\t%s\t[%s]\n", sprintaddr(buf, sizeof(buf), addr),
|
||||
cfg.listen[i].keepalive ? "keepalive" : "");
|
||||
|
9
common.h
9
common.h
@ -103,6 +103,11 @@ struct connection {
|
||||
struct queue q[2];
|
||||
};
|
||||
|
||||
struct listen_endpoint {
|
||||
int socketfd; /* file descriptor of listening socket */
|
||||
int type; /* SOCK_DGRAM | SOCK_STREAM */
|
||||
};
|
||||
|
||||
#define FD_CNXCLOSED 0
|
||||
#define FD_NODATA -1
|
||||
#define FD_STALLED -2
|
||||
@ -133,7 +138,7 @@ void log_message(int type, const char* msg, ...);
|
||||
void dump_connection(struct connection *cnx);
|
||||
int resolve_split_name(struct addrinfo **out, char* hostname, char* port);
|
||||
|
||||
int start_listen_sockets(int *sockfd[]);
|
||||
int start_listen_sockets(struct listen_endpoint *sockfd[]);
|
||||
|
||||
int defer_write(struct queue *q, void* data, int data_size);
|
||||
int flush_deferred(struct queue *q);
|
||||
@ -146,6 +151,6 @@ extern const char* server_type;
|
||||
/* sslh-fork.c */
|
||||
void start_shoveler(int);
|
||||
|
||||
void main_loop(int *listen_sockets, int num_addr_listen);
|
||||
void main_loop(struct listen_endpoint *listen_sockets, int num_addr_listen);
|
||||
|
||||
#endif
|
||||
|
12
sslh-fork.c
12
sslh-fork.c
@ -145,7 +145,7 @@ void stop_listeners(int sig)
|
||||
}
|
||||
}
|
||||
|
||||
void set_listen_procname(int listen_socket)
|
||||
void set_listen_procname(struct listen_endpoint *listen_socket)
|
||||
{
|
||||
#ifdef LIBBSD
|
||||
int res;
|
||||
@ -155,7 +155,7 @@ void set_listen_procname(int listen_socket)
|
||||
|
||||
addr.ai_addr = (struct sockaddr*)&ss;
|
||||
addr.ai_addrlen = sizeof(ss);
|
||||
res = getsockname(listen_socket, addr.ai_addr, &addr.ai_addrlen);
|
||||
res = getsockname(listen_socket->socketfd, addr.ai_addr, &addr.ai_addrlen);
|
||||
if (res != -1) {
|
||||
sprintaddr(listen_addr, sizeof(listen_addr), &addr);
|
||||
setproctitle("listener %s", listen_addr);
|
||||
@ -163,7 +163,7 @@ void set_listen_procname(int listen_socket)
|
||||
#endif
|
||||
}
|
||||
|
||||
void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
void main_loop(struct listen_endpoint listen_sockets[], int num_addr_listen)
|
||||
{
|
||||
int in_socket, i, res;
|
||||
struct sigaction action;
|
||||
@ -183,10 +183,10 @@ void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
case 0:
|
||||
/* Listening process just accepts a connection, forks, and goes
|
||||
* back to listening */
|
||||
set_listen_procname(listen_sockets[i]);
|
||||
set_listen_procname(&listen_sockets[i]);
|
||||
while (1)
|
||||
{
|
||||
in_socket = accept(listen_sockets[i], 0, 0);
|
||||
in_socket = accept(listen_sockets[i].socketfd, 0, 0);
|
||||
if (cfg.verbose) fprintf(stderr, "accepted fd %d\n", in_socket);
|
||||
|
||||
switch(fork()) {
|
||||
@ -196,7 +196,7 @@ void main_loop(int listen_sockets[], int num_addr_listen)
|
||||
/* In child process */
|
||||
case 0:
|
||||
for (i = 0; i < num_addr_listen; ++i)
|
||||
close(listen_sockets[i]);
|
||||
close(listen_sockets[i].socketfd);
|
||||
start_shoveler(in_socket);
|
||||
exit(0);
|
||||
|
||||
|
@ -201,7 +201,7 @@ int main(int argc, char *argv[], char* envp[])
|
||||
extern char *optarg;
|
||||
extern int optind;
|
||||
int res, num_addr_listen;
|
||||
int *listen_sockets;
|
||||
struct listen_endpoint *listen_sockets;
|
||||
|
||||
#ifdef LIBBSD
|
||||
setproctitle_init(argc, argv, envp);
|
||||
|
6
test.cfg
6
test.cfg
@ -1,7 +1,7 @@
|
||||
# Configuration file for testing (use both by sslh under
|
||||
# test and the test script `t`)
|
||||
|
||||
verbose: 2;
|
||||
verbose: 3;
|
||||
foreground: true;
|
||||
inetd: false;
|
||||
numeric: false;
|
||||
@ -17,8 +17,8 @@ syslog_facility: "auth";
|
||||
listen:
|
||||
(
|
||||
{ host: "localhost"; port: "8080"; keepalive: true; },
|
||||
{ host: "localhost"; port: "8081"; keepalive: true; },
|
||||
{ host: "localhost"; is_udp: true; port: "4443"; }
|
||||
{ host: "localhost"; port: "8081"; keepalive: true; }
|
||||
# { host: "localhost"; is_udp: true; port: "4443"; }
|
||||
);
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user