forked from yrutschle/sslh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyprotocol.c
More file actions
147 lines (115 loc) · 4.03 KB
/
proxyprotocol.c
File metadata and controls
147 lines (115 loc) · 4.03 KB
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
/*
# proxyprotocol: Support for HAProxy's proxyprotocol
#
# Copyright (C) 2025 Yves Rutschle
#
# 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 2 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.
#
# The full text for the General Public License is here:
# http://www.gnu.org/licenses/gpl.html
*/
#include "config.h"
#ifdef HAVE_PROXYPROTOCOL
#include <proxy_protocol.h>
#include "common.h"
#include "log.h"
/* Converts socket family to libproxyprotocol family */
static int family_to_pp(int af_family)
{
switch (af_family) {
case AF_INET:
return ADDR_FAMILY_INET;
case AF_INET6:
return ADDR_FAMILY_INET6;
case AF_UNIX:
return ADDR_FAMILY_UNIX;
default:
print_message(msg_int_error, "Unknown internal socket family %d\n", af_family);
return -1;
}
}
typedef char libpp_addr[108]; /* This is hardcoded in libproxyprotocol/proxy_protocol.h */
/* Fills *addr, *host and *serv with the connection information corresponding
* to fd. *host is the IP address as string and *serv is the service (port)
* */
static int get_peer_info(int fd, struct addrinfo* addr, libpp_addr* host, uint16_t* serv)
{
char serv_str[NI_MAXSERV];
int res;
res = getpeername(fd, addr->ai_addr, &addr->ai_addrlen);
CHECK_RES_RETURN(res, "getpeername", -1);
res = getnameinfo(addr->ai_addr, addr->ai_addrlen,
(char*)host, sizeof(*host),
serv_str, sizeof(serv_str),
NI_NUMERICHOST | NI_NUMERICSERV );
CHECK_RES_RETURN(res, "getnameinfo", -1);
*serv = atoi(serv_str);
return 0;
}
/* Fills *addr, *host and *serv with the LOCAL connection information corresponding
* to fd. *host is the IP address as string and *serv is the service (port)
* */
static int get_local_sock_info(int fd, struct addrinfo* addr, libpp_addr* host, uint16_t* serv)
{
char serv_str[NI_MAXSERV];
int res;
// addr->ai_addr should point to a suitable sockaddr
// addr->ai_addrlen should be initialized to its size
res = getsockname(fd, addr->ai_addr, &addr->ai_addrlen);
CHECK_RES_RETURN(res, "getsockname", -1);
res = getnameinfo(addr->ai_addr, addr->ai_addrlen,
(char*)host, sizeof(*host),
serv_str, sizeof(serv_str),
NI_NUMERICHOST | NI_NUMERICSERV);
if (res != 0) {
print_message(msg_system_error, "getnameinfo for local sock: %s\n", gai_strerror(res));
return -1;
}
*serv = atoi(serv_str);
return 0;
}
int pp_write_header(int pp_version, struct connection* cnx)
{
pp_info_t pp_info_in_v1 = {
.transport_protocol = TRANSPORT_PROTOCOL_STREAM,
};
uint16_t pp1_hdr_len;
int32_t error;
struct sockaddr_storage ss;
struct addrinfo addr;
int res;
addr.ai_addr = (struct sockaddr*)&ss;
addr.ai_addrlen = sizeof(ss);
res = get_peer_info(cnx->q[0].fd,
&addr,
&pp_info_in_v1.src_addr,
&pp_info_in_v1.src_port);
if (res == -1) return -1;
pp_info_in_v1.address_family = family_to_pp(addr.ai_addr->sa_family);
res = get_local_sock_info(cnx->q[0].fd,
&addr,
&pp_info_in_v1.dst_addr,
&pp_info_in_v1.dst_port
);
if (res == -1) return -1;
uint8_t *pp1_hdr = pp_create_hdr(pp_version, &pp_info_in_v1, &pp1_hdr_len, &error);
if (!pp1_hdr) {
print_message(msg_system_error, "pp_create_hrd:%d:%s\n", error, pp_strerror(error));
return -1;
}
defer_write_before(&cnx->q[1], pp1_hdr, pp1_hdr_len);
pp_info_clear(&pp_info_in_v1);
free(pp1_hdr);
return 0;
}
#endif /* HAVE_PROXYPROTOCOL */