-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.c
108 lines (89 loc) · 2.53 KB
/
util.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
/*********************************************************************
* Copyright 2017-2018 Network Device Education Foundation, Inc. ("NetDEF")
*
* This program 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 2 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* util.c: miscelaneous functions that doesn't fit other files.
*
* Authors
* -------
* Rafael Zalamena <[email protected]>
*/
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "bfd.h"
size_t strxcpy(char *dst, const char *src, size_t len)
{
size_t srclen, tlen;
srclen = strlen(src);
tlen = (srclen < len) ? srclen : len;
memcpy(dst, src, tlen);
dst[tlen] = 0;
return srclen;
}
const char *satostr(struct sockaddr_any *sa)
{
#define INETSTR_BUFCOUNT 8
static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
static int bufidx = 0;
struct sockaddr_in *sin = &sa->sa_sin;
struct sockaddr_in6 *sin6;
bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
strxcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
buf[bufidx][0] = 0;
switch (sin->sin_family) {
case AF_INET:
inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
sizeof(buf[bufidx]));
break;
case AF_INET6:
sin6 = &sa->sa_sin6;
inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
sizeof(buf[bufidx]));
break;
}
return buf[bufidx];
}
int strtosa(const char *addr, struct sockaddr_any *sa)
{
memset(sa, 0, sizeof(*sa));
if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
sa->sa_sin.sin_family = AF_INET;
return 0;
}
if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
sa->sa_sin6.sin6_family = AF_INET6;
return 0;
}
return -1;
}
time_t get_monotime(struct timeval *tv)
{
struct timespec ts;
int r;
r = clock_gettime(CLOCK_MONOTONIC, &ts);
if (r != 0) {
memset(tv, 0, sizeof(*tv));
return 0;
}
if (tv) {
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec / 1000;
}
return ts.tv_sec;
}