-
Notifications
You must be signed in to change notification settings - Fork 4
/
libkeepalive.c
102 lines (82 loc) · 3.08 KB
/
libkeepalive.c
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
/* Copyright (c) 2019-2023, Michael Santos <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <arpa/inet.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keepalive.h"
void _init(void);
static int (*sys_connect)(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
#pragma GCC diagnostic ignored "-Wpedantic"
int __attribute__((visibility("default")))
connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
#pragma GCC diagnostic warning "-Wpedantic"
static keepalive_t opt = {0};
void _init(void) {
const char *err;
char *env_debug;
char *env_tcp_keepidle;
char *env_tcp_keepcnt;
char *env_tcp_keepintvl;
char *env_tcp_user_timeout;
char *env_tcp_syncnt;
char *env_tcp_maxseg;
char *env_tcp_window_clamp;
env_debug = getenv("LIBKEEPALIVE_DEBUG");
env_tcp_keepidle = getenv("TCP_KEEPIDLE");
env_tcp_keepcnt = getenv("TCP_KEEPCNT");
env_tcp_keepintvl = getenv("TCP_KEEPINTVL");
env_tcp_user_timeout = getenv("TCP_USER_TIMEOUT");
env_tcp_syncnt = getenv("TCP_SYNCNT");
env_tcp_maxseg = getenv("TCP_MAXSEG");
env_tcp_window_clamp = getenv("TCP_WINDOW_CLAMP");
keepalive_init(&opt);
if (env_debug)
opt.debug = 1;
if (env_tcp_keepidle)
opt.tcp_keepidle = atoi(env_tcp_keepidle);
if (env_tcp_keepcnt)
opt.tcp_keepcnt = atoi(env_tcp_keepcnt);
if (env_tcp_keepintvl)
opt.tcp_keepintvl = atoi(env_tcp_keepintvl);
if (env_tcp_user_timeout)
opt.tcp_user_timeout = atoi(env_tcp_user_timeout);
if (env_tcp_syncnt)
opt.tcp_syncnt = atoi(env_tcp_syncnt);
if (env_tcp_maxseg)
opt.tcp_maxseg = atoi(env_tcp_maxseg);
if (env_tcp_window_clamp)
opt.tcp_window_clamp = atoi(env_tcp_window_clamp);
/* TCP_KEEPIDLE + TCP_KEEPINTVL * TCP_KEEPCNT */
if (opt.tcp_user_timeout < 0)
opt.tcp_user_timeout =
(opt.tcp_keepidle + opt.tcp_keepintvl * opt.tcp_keepcnt) * 1000;
#pragma GCC diagnostic ignored "-Wpedantic"
sys_connect = dlsym(RTLD_NEXT, "connect");
#pragma GCC diagnostic warning "-Wpedantic"
err = dlerror();
if (err != NULL)
(void)fprintf(stderr, "libkeepalive:dlsym (connect): %s\n", err);
}
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
int oerrno = errno;
(void)keepalive(sockfd, &opt);
errno = oerrno;
return sys_connect(sockfd, addr, addrlen);
}