Repository navigation
Expand file tree
/
Copy pathnetwork.c
More file actions
149 lines (120 loc) · 6.14 KB
/
Copy pathnetwork.c
File metadata and controls
149 lines (120 loc) · 6.14 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
148
149
/* ------------------------------------------------------------------------------------------------------------------ */
/* TS-Warp - Transparent proxy server and traffic wrapper */
/* ------------------------------------------------------------------------------------------------------------------ */
/*
* Copyright (c) 2021-2026, Mikhail Zakharov <zmey20000@yahoo.com>
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* -- Network functions --------------------------------------------------------------------------------------------- */
#include <stdlib.h>
#include <string.h>
#include "network.h"
#include "logfile.h"
#include "pidfile.h"
#include "utility.h"
/* ------------------------------------------------------------------------------------------------------------------ */
int connect_desnation(struct sockaddr dest) {
/* Establish TCP connetion with a det address */
int sock;
if ((sock = socket(dest.sa_family, SOCK_STREAM, 0)) < 0) {
printl(LOG_CRIT, "Error creating a socket for the destination address");
return sock;
}
#if (WITH_TCP_NODELAY)
int tpc_ndelay = 1;
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &tpc_ndelay, sizeof(int)) == -1)
printl(LOG_WARN, "Error setting TCP_NODELAY socket option for outgoing connections");
#endif
int keepalive = 1;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(int)) == -1)
printl(LOG_WARN, "Error setting SO_KEEPALIVE socket option for outgoing connections");
#if !defined(__APPLE__) && !defined(__OpenBSD__)
int keepidle = TCP_KEEPIDLE_S;
if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepidle, sizeof(int)))
printl(LOG_WARN, "Error setting TCP_KEEPIDLE socket option for outgoing connections");
#endif
/* Timeout for a new not yet established connections on FreeBSD/Darwin,
or a number of retries on Linux, or nothing on OpenBSD */
#if defined(__FreeBSD__)
int keepinit = TCP_KEEPINIT_S;
if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINIT, &keepinit, sizeof(int)) == -1)
printl(LOG_WARN, "Error setting TCP_KEEPINIT socket option for outgoing connections");
#endif
#if defined(__APPLE__)
int keepinit = TCP_CONNECTIONTIMEOUT_S;
if (setsockopt(sock, IPPROTO_TCP, TCP_CONNECTIONTIMEOUT, &keepinit, sizeof(int)) == -1)
printl(LOG_WARN, "Error setting TCP_CONNECTIONTIMEOUT socket option for outgoing connections");
#endif
#if defined(linux)
int syncnt = TCP_SYNCNT_N;
if (setsockopt(sock, IPPROTO_TCP, TCP_SYNCNT, &syncnt, sizeof(int)) == -1)
printl(LOG_WARN, "Error setting TCP_SYNCNT socket option for outgoing connections");
#endif
printl(LOG_VERB, "Socket to connect with destination address created");
if ((connect(sock, &dest, sizeof dest)) < 0) {
printl(LOG_CRIT, "Unable to connect with destination address");
return -1;
}
return sock;
}
/* ------------------------------------------------------------------------------------------------------------------ */
char *inet2str(struct sockaddr_storage *ai_addr, char *str_addr) {
/* inet_ntop() wrapper. If str_add is NULL, memory is auto-allocated,
don't forget to free it after usage! */
char buf[INET_ADDRPORTSTRLEN];
if (!str_addr) str_addr = (char *)malloc(INET_ADDRPORTSTRLEN);
memset(str_addr, 0, INET_ADDRPORTSTRLEN);
memset(&buf, 0, INET_ADDRPORTSTRLEN);
switch (ai_addr->ss_family) {
case AF_INET:
inet_ntop(AF_INET, &SIN4_ADDR(*ai_addr), buf, INET_ADDRSTRLEN);
sprintf(str_addr, "%s:%d", buf, ntohs(SIN4_PORT(*ai_addr)));
break;
case AF_INET6:
inet_ntop(AF_INET6, &SIN6_ADDR(*ai_addr), buf, INET6_ADDRSTRLEN);
sprintf(str_addr, "%s:%d", buf, ntohs(SIN6_PORT(*ai_addr)));
break;
case AF_UNSPEC:
printl(LOG_VERB, "Address is not set");
return NULL;
break;
default:
printl(LOG_WARN, "Unrecognized address family: %d", ai_addr->ss_family);
return NULL;
}
return str_addr;
}
/* ------------------------------------------------------------------------------------------------------------------ */
struct sockaddr_storage str2inet(char *str_addr, char *str_port) {
struct addrinfo hints, *res = NULL;
struct sockaddr_storage a_ret;
int ret;
memset(&a_ret, 0, sizeof(struct sockaddr_storage));
SA_FAMILY(a_ret) = AF_INET;
S4_ADDR(a_ret) = INADDR_NONE;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((ret = getaddrinfo(str_addr, str_port, &hints, &res)) > 0)
printl(LOG_CRIT, "Error resolving address [%s]:[%s]: [%s]", str_addr, str_port, gai_strerror(ret));
else
memmove(&a_ret, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
return a_ret;
}