summaryrefslogtreecommitdiff
path: root/shared/utils.h
blob: 2bf7aaed1c97cae8c6e2b98f3670912b3b4fc1dc (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
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef fooutilshfoo
#define fooutilshfoo

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <linux/if_packet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <dirent.h>

extern unsigned debug_mask;

#define _unused_       __attribute__((__unused__))
#define _pure_	       __attribute__((__pure__))
#define _const_	       __attribute__((__const__))
#define _alloc_(...)   __attribute__((__alloc_size__(__VA_ARGS__)))
#define _malloc_       __attribute__((__malloc__))
#define _printf_(a, b) __attribute__((__format__(printf, a, b)))
#define _alignas_(x)   __attribute__((__aligned__(__alignof(x))))
#define _big_endian_   __attribute__((packed, scalar_storage_order("big-endian")))

#if __GNUC__ >= 7
#define _fallthrough_ __attribute__((__fallthrough__))
#else
#define _fallthrough_
#endif

#ifndef _noreturn_
#if __STDC_VERSION__ >= 201112L
#define _noreturn_ _Noreturn
#else
#define _noreturn_ __attribute__((__noreturn__))
#endif
#endif

#define STRLEN(x) (sizeof("" x "") - 1)

#include "list.h"
#include "debug.h"
#include "external.h"
#include "config.h"
#include "ansi-colors.h"

/* Length of longest DNS name = 253 + trailing dot */
#define FQDN_STR_LEN 254

/* Length of longest port string */
#define PORT_STR_LEN STRLEN("65535")

/* Length of longest address family string */
#define AF_STR_LEN STRLEN("AF_INETX")

/* Length of longest addrstr, format = "AF_INETX <IPADDR> <PORT> */
#define ADDRSTRLEN (AF_STR_LEN + 1 + INET6_ADDRSTRLEN + 1 + PORT_STR_LEN + 1)

struct saddr {
	union {
		struct sockaddr_storage st;
		struct sockaddr_in in4;
		struct sockaddr_in6 in6;
		struct sockaddr_ll ll;
	};
	socklen_t addrlen;
	char addrstr[ADDRSTRLEN];
	struct list_head list;
};

int open_subdir(int dfd, const char *subdir, bool nofail);

int open_xdg_dir(const char *envname, const char *altpath, bool nofail,
		 char **rpath);

static inline int open_xdg_data_dir(bool nofail, char **rpath)
{
	return open_xdg_dir("XDG_DATA_HOME", ".local/share", nofail, rpath);
}

static inline int open_xdg_cfg_dir(bool nofail, char **rpath)
{
	return open_xdg_dir("XDG_CONFIG_HOME", ".config", nofail, rpath);
}

DIR *__open_dir(const char *user_override,
		int (*base_dir)(bool nofail, char **rpath),
		const char *fallback, char **rpath);

static inline DIR *open_cfg_dir(const char *user_override, char **rpath)
{
	return __open_dir(user_override, open_xdg_cfg_dir, DEFAULT_CFG_DIR,
			  rpath);
}

static inline DIR *open_data_dir(const char *user_override, char **rpath)
{
	return __open_dir(user_override, open_xdg_data_dir, DEFAULT_DATA_DIR,
			  rpath);
}

void enable_colors();

void free_password(char **password);

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

char *saddr_addr(struct saddr *saddr, char *buf, size_t len);

uint16_t saddr_port(struct saddr *saddr);

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

void saddr_set_ipv6(struct saddr *saddr, const struct in6_addr *ip,
		    in_port_t port);

void saddr_any_to_loopback(struct saddr *saddr);

void saddr_set_addrstr(struct saddr *saddr);

int strtou16_strict(const char *str, uint16_t *result);

char *xsprintf(size_t *rlen, const char *fmt, ...) _printf_(2, 3);

char *xstrcat(const char *a, ...);

static inline bool empty_str(const char *str)
{
	if (!str || str[0] == '\0')
		return true;
	else
		return false;
}

static inline bool streq(const char *a, const char *b)
{
	return strcmp(a, b) == 0;
}

static inline bool strcaseeq(const char *a, const char *b)
{
	return strcasecmp(a, b) == 0;
}

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define chtobe32(x) __bswap_constant_32(x)
#else
#define chtobe32(x) (x)
#endif

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define cinet_addr(a, b, c, d)                                                 \
	((uint32_t)((a) << 0 | (b) << 8 | (c) << 16 | (d) << 24))
#else
#define cinet_addr(a, b, c, d)                                                 \
	((uint32_t)((a) << 24 | (b) << 16 | (c) << 8 | (d) << 0))
#endif

#define PIPE_RD 0
#define PIPE_WR 1

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))

#define _cleanup_(x) __attribute__((cleanup(x)))

static inline void closep(int *fd)
{
	if (fd && *fd >= 0)
		close(*fd);
}
#define _cleanup_close_ _cleanup_(closep)

static inline void freep(void *p) {
	xfree(*(void**)p);
}
#define _cleanup_free_ _cleanup_(freep)

static inline void fclosep(FILE **f) {
	if (f && *f)
		fclose(*f);
}
#define _cleanup_fclose_ _cleanup_(fclosep)

static inline void closedirp(DIR **d) {
	if (d && *d)
		closedir(*d);
}
#define _cleanup_closedir_ _cleanup_(closedirp)

#endif