-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c
More file actions
131 lines (114 loc) · 2.91 KB
/
common.c
File metadata and controls
131 lines (114 loc) · 2.91 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
/*
* CHAPI - ChaCha20-based Host Address Protocol over UDP
*
* Copyright (C) 2025 Liu Yu <f78fk@live.com>
*
* This file is part of CHAPI.
*
* CHAPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CHAPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CHAPI. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <syslog.h>
#include "common.h"
#include "chapi-log.h"
int hex_to_bin(const char *hex, unsigned char *bin, size_t bin_len)
{
size_t i;
unsigned int byte;
for (i = 0; i < bin_len; i++) {
sscanf(hex + 2 * i, "%2x", &byte);
bin[i] = (unsigned char)byte;
}
return 0;
}
int is_valid_ipv4(const char *ip)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ip, &(sa.sin_addr));
return result == 1 ? 1 : 0;
}
int send_with_retry(int sock, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen,
int max_retries)
{
if (sock < 0 || !buf || len == 0 || !dest_addr || max_retries <= 0) {
errno = EINVAL;
return -1;
}
int attempt = 0;
ssize_t sent_len = 0;
while (attempt < max_retries) {
sent_len = sendto(sock, buf, len, flags, dest_addr, addrlen);
if (sent_len >= 0) {
return sent_len;
}
switch (errno) {
#if EAGAIN == EWOULDBLOCK
case EAGAIN:
#else
case EAGAIN:
case EWOULDBLOCK:
#endif
case EINTR:
usleep(1000 * (1 << attempt));
LOG_ERR_MSG("sendto retryable error (attempt %d): %s",
attempt + 1, strerror(errno));
break;
default:
LOG_ERR_MSG("sendto fatal error: %s", strerror(errno));
return -1;
}
attempt++;
}
LOG_ERR_MSG("sendto failed after %d attempts", max_retries);
return -1;
}
int load_key(unsigned char *key_out, int *source)
{
const char *path = KEY_FILE_PATH;
FILE *fp = NULL;
char hex[KEY_LEN * 2 + 2] = { 0 };
fp = fopen(path, "r");
if (!fp)
goto fail;
if (!fgets(hex, sizeof(hex), fp))
goto fail;
size_t len = strlen(hex);
while (len > 0 && (hex[len - 1] == '\n' || hex[len - 1] == '\r'))
hex[--len] = '\0';
if (len != KEY_LEN * 2)
goto fail;
for (size_t i = 0; i < len; ++i) {
if (!isxdigit((unsigned char)hex[i]))
goto fail;
}
if (hex_to_bin(hex, key_out, KEY_LEN) != 0)
goto fail;
fclose(fp);
if (source)
*source = KEY_SOURCE_FILE;
return 0;
fail:
if (fp)
fclose(fp);
int ret = hex_to_bin(KEY_HEX, key_out, KEY_LEN);
if (source)
*source = KEY_SOURCE_MACRO;
return ret;
}