summaryrefslogtreecommitdiff
path: root/shared/utils.c
blob: 6474e300edb46332dfa5d4c6bbc5db6020bdad57 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <inttypes.h>

#include "utils.h"

unsigned debug_mask = 0;

const char *ansi_red = "";
const char *ansi_green = "";
const char *ansi_yellow = "";
const char *ansi_blue = "";
const char *ansi_magenta = "";
const char *ansi_grey = "";
const char *ansi_normal = "";

void enable_colors()
{
	ansi_red = ANSI_RED;
	ansi_green = ANSI_GREEN;
	ansi_yellow = ANSI_YELLOW;
	ansi_blue = ANSI_BLUE;
	ansi_magenta = ANSI_MAGENTA;
	ansi_grey = ANSI_GREY;
	ansi_normal = ANSI_NORMAL;
}

void free_password(char **password)
{
	if (!password || !*password)
		return;

	explicit_bzero(*password, strlen(*password));
	xfree(*password);
	*password = NULL;
}

void socket_set_low_latency(int sfd, bool keepalive, bool iptos, bool nodelay)
{
	int option;

	assert_return(sfd >= 0);

	/* Probably not necessary, but can't hurt */
	if (keepalive) {
		option = true;
		if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &option,
			       sizeof(option)) < 0)
			error("setsockopt: %m");
	}

	/* Doubtful if it has much effect, but can't hurt */
	if (iptos) {
		option = IPTOS_LOWDELAY;
		if (setsockopt(sfd, IPPROTO_IP, IP_TOS, &option,
			       sizeof(option)) < 0)
			error("setsockopt: %m");
	}

	/* Nagle's algorithm is a poor fit for gaming */
	if (nodelay) {
		option = true;
		if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &option,
			       sizeof(option)) < 0)
			error("setsockopt: %m");
	}
}

uint16_t saddr_port(struct saddr *saddr)
{
	// assert_return(saddr, 0);

	switch (saddr->st.ss_family) {
	case AF_INET:
		return ntohs(saddr->in4.sin_port);
	case AF_INET6:
		return ntohs(saddr->in6.sin6_port);
	default:
		return 0;
	}
}

char *saddr_addr(struct saddr *saddr, char *buf, size_t len)
{
	// assert_return(saddr && buf && len > 0, NULL);

	switch (saddr->st.ss_family) {
	case AF_INET:
		if (inet_ntop(saddr->in4.sin_family, &saddr->in4.sin_addr, buf,
			      len))
			return buf;
		break;
	case AF_INET6:
		if (inet_ntop(saddr->in6.sin6_family, &saddr->in6.sin6_addr,
			      buf, len))
			return buf;
		break;
	default:
		break;
	}

	snprintf(buf, len, "<unknown>");
	return buf;
}

void saddr_set_ipv4(struct saddr *saddr, in_addr_t ip, in_port_t port)
{
	// assert_return(saddr);

	memset(&saddr->in4, 0, sizeof(saddr->in4));
	saddr->in4.sin_family = AF_INET;
	saddr->in4.sin_port = port;
	saddr->in4.sin_addr.s_addr = ip;
	saddr->addrlen = sizeof(saddr->in4);
	saddr_set_addrstr(saddr);
}

void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip,
		    in_port_t port)
{
	// assert_return(saddr && ip);

	memset(&saddr->in6, 0, sizeof(saddr->in6));
	saddr->in6.sin6_family = AF_INET6;
	saddr->in6.sin6_port = port;
	if (ip)
		saddr->in6.sin6_addr = *ip;
	saddr->addrlen = sizeof(saddr->in6);
	saddr_set_addrstr(saddr);
}

void saddr_set_addrstr(struct saddr *saddr)
{
	// assert_return(saddr);

	char abuf[ADDRSTRLEN];

	switch (saddr->st.ss_family) {
	case AF_INET:
		snprintf(saddr->addrstr, sizeof(saddr->addrstr),
			 "AF_INET4 %s %" PRIu16,
			 saddr_addr(saddr, abuf, sizeof(abuf)),
			 saddr_port(saddr));
		break;
	case AF_INET6:
		snprintf(saddr->addrstr, sizeof(saddr->addrstr),
			 "AF_INET6 %s %" PRIu16,
			 saddr_addr(saddr, abuf, sizeof(abuf)),
			 saddr_port(saddr));
		break;
	default:
		snprintf(saddr->addrstr, sizeof(saddr->addrstr), "AF_UNKNOWN");
		break;
	}
}

int strtou16_strict(const char *str, uint16_t *result)
{
	char *end;
	long val;

	if (empty_str(str))
		return -EINVAL;

	errno = 0;
	val = strtol(str, &end, 10);

	if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
		return -EINVAL;

	if (errno != 0 && val == 0)
		return -EINVAL;

	if (end == str)
		return -EINVAL;

	if (*end != '\0')
		return -EINVAL;

	if (val < 1 || val > UINT16_MAX)
		return -EINVAL;

	if (result)
		*result = val;
	return 0;
}