-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
common.c
117 lines (93 loc) · 2.97 KB
/
common.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
srtla - SRT transport proxy with link aggregation
Copyright (C) 2020-2021 BELABOX project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <endian.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include "common.h"
void exit_help() {
print_help();
exit(EXIT_FAILURE);
}
#define ADDR_BUF_SZ 50
char _global_addr_buf[ADDR_BUF_SZ];
const char *print_addr(struct sockaddr *addr) {
struct sockaddr_in *ain = (struct sockaddr_in *)addr;
return inet_ntop(ain->sin_family, &ain->sin_addr, _global_addr_buf, ADDR_BUF_SZ);
}
int port_no(struct sockaddr *addr) {
struct sockaddr_in *ain = (struct sockaddr_in *)addr;
return ntohs(ain->sin_port);
}
int parse_ip(struct sockaddr_in *addr, char *ip_str) {
in_addr_t ip = inet_addr(ip_str);
if (ip == -1) return -1;
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = ip;
return 0;
}
int parse_port(char *port_str) {
int port = strtol(port_str, NULL, 10);
if (port <= 0 || port > 65535) return -2;
return port;
}
int get_seconds(time_t *s) {
struct timespec ts;
int ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
if (ret != 0) return -1;
*s = ts.tv_sec;
return 0;
}
int get_ms(uint64_t *ms) {
struct timespec ts;
int ret = clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
if (ret != 0) return -1;
*ms = ((uint64_t)(ts.tv_sec)) * 1000 + ((uint64_t)(ts.tv_nsec)) / 1000 / 1000;
return 0;
}
int32_t get_srt_sn(void *pkt, int n) {
if (n < 4) return -1;
uint32_t sn = be32toh(*((uint32_t *)pkt));
if ((sn & (1 << 31)) == 0) {
return (int32_t)sn;
}
return -1;
}
uint16_t get_srt_type(void *pkt, int n) {
if (n < 2) return 0;
return be16toh(*((uint16_t *)pkt));
}
int is_srt_ack(void *pkt, int n) {
return get_srt_type(pkt, n) == SRT_TYPE_ACK;
}
int is_srtla_keepalive(void *pkt, int n) {
return get_srt_type(pkt, n) == SRTLA_TYPE_KEEPALIVE;
}
int is_srtla_reg1(void *pkt, int len) {
if (len != SRTLA_TYPE_REG1_LEN) return 0;
return get_srt_type(pkt, len) == SRTLA_TYPE_REG1;
}
int is_srtla_reg2(void *pkt, int len) {
if (len != SRTLA_TYPE_REG2_LEN) return 0;
return get_srt_type(pkt, len) == SRTLA_TYPE_REG2;
}
int is_srtla_reg3(void *pkt, int len) {
if (len != SRTLA_TYPE_REG3_LEN) return 0;
return get_srt_type(pkt, len) == SRTLA_TYPE_REG3;
}