Skip to content

Commit

Permalink
healtcheck: upd_check supports ppv2
Browse files Browse the repository at this point in the history
Signed-off-by: ywc689 <ywc689@163.com>
  • Loading branch information
ywc689 committed Dec 8, 2023
1 parent 2fd48ed commit 4a95793
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
16 changes: 6 additions & 10 deletions tools/keepalived/keepalived/check/check_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1551,26 +1551,22 @@ static int http_send_proxy_protocol(thread_ref_t thread)

unsigned int len;
char *ppbuf;
const char ppv2_local_cmd[] = {
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51,
0x55, 0x49, 0x54, 0x0A, 0x20, 0x00, 0x00, 0x00,
};

assert(vs->proxy_protocol >= PROXY_PROTOCOL_DISABLE
&& vs->proxy_protocol < PROXY_PROTOCOL_MAX);

ppbuf = (char *) MALLOC(16);
ppbuf = (char *) MALLOC(PROXY_PROTOCOL_CHECK_MAX_LEN);
if (!ppbuf) {
return -1;
}
memset(ppbuf, 0, 16);
memset(ppbuf, 0, PROXY_PROTOCOL_CHECK_MAX_LEN);

if (PROXY_PROTOCOL_V1 == vs->proxy_protocol) {
len = 15;
sprintf(ppbuf, "%s", "PROXY UNKNOWN\r\n");
len = PROXY_PROTOCOL_CHECK_V1_LEN;
sprintf(ppbuf, "%s", PROXY_PROTOCOL_CHECK_V1);
} else if (PROXY_PROTOCOL_V2 == vs->proxy_protocol) {
len = 16;
memcpy(ppbuf, ppv2_local_cmd, 16);
len = PROXY_PROTOCOL_CHECK_V2_LEN;
memcpy(ppbuf, PROXY_PROTOCOL_CHECK_V2, PROXY_PROTOCOL_CHECK_V2_LEN);
}

if (send(thread->u.f.fd, ppbuf, len, 0) != len) {
Expand Down
29 changes: 27 additions & 2 deletions tools/keepalived/keepalived/check/check_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ udp_connect_thread(thread_ref_t thread)
checker_t *checker = THREAD_ARG(thread);
udp_check_t *udp_check = CHECKER_ARG(checker);
conn_opts_t *co = checker->co;
uint8_t *payload;
uint16_t payload_len;
bool new_payload;
int fd;
int status;

Expand All @@ -334,22 +337,44 @@ udp_connect_thread(thread_ref_t thread)
return 0;
}

if (PROXY_PROTOCOL_V2 == checker->vs->proxy_protocol) {
payload_len = udp_check->payload_len + PROXY_PROTOCOL_CHECK_V2_LEN;
if (!udp_check->payload_len)
payload_len += 16;
payload = MALLOC(payload_len);
if (!payload)
return 1;
new_payload = true;
memcpy(payload, PROXY_PROTOCOL_CHECK_V2, PROXY_PROTOCOL_CHECK_V2_LEN);
if (udp_check->payload_len > 0)
memcpy(payload + PROXY_PROTOCOL_CHECK_V2_LEN, udp_check->payload, udp_check->payload_len);
else
set_buf(payload + PROXY_PROTOCOL_CHECK_V2_LEN, 16);
} else {
payload_len = udp_check->payload_len;
payload = udp_check->payload;
new_payload = false;
}

if ((fd = socket(co->dst.ss_family, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_UDP)) == -1) {
log_message(LOG_INFO, "UDP connect fail to create socket. Rescheduling.");
thread_add_timer(thread->master, udp_connect_thread, checker,
checker->delay_loop);

if (new_payload)
FREE(payload);
return 0;
}

status = udp_bind_connect(fd, co, udp_check->payload, udp_check->payload_len);
status = udp_bind_connect(fd, co, payload, payload_len);

/* handle udp connection status & register check worker thread */
if (udp_check_state(fd, status, thread, udp_check_thread, co->connection_to)) {
close(fd);
udp_epilog(thread, false);
}

if (new_payload)
FREE(payload);
return 0;
}

Expand Down
10 changes: 10 additions & 0 deletions tools/keepalived/keepalived/include/check_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
/* Daemon dynamic data structure definition */
#define KEEPALIVED_DEFAULT_DELAY (60 * TIMER_HZ)
#define TNLKINDSIZ 16

#define PROXY_PROTOCOL_CHECK_V1 "PROXY UNKNOWN\r\n"
static const char PROXY_PROTOCOL_CHECK_V2[] = {
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51,
0x55, 0x49, 0x54, 0x0A, 0x20, 0x00, 0x00, 0x00,
};
#define PROXY_PROTOCOL_CHECK_V1_LEN 15
#define PROXY_PROTOCOL_CHECK_V2_LEN 16
#define PROXY_PROTOCOL_CHECK_MAX_LEN 16

/* SSL specific data */
typedef struct _ssl_data {
int enable;
Expand Down

0 comments on commit 4a95793

Please sign in to comment.