Radcli library 2.0.2
A simple radius library -- legacy API reference
Loading...
Searching...
No Matches
util.c
1/*
2 * Copyright (C) 1995,1996,1997 Lars Fenneberg
3 *
4 * Copyright 1992 Livingston Enterprises, Inc.
5 *
6 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
7 * and Merit Network, Inc. All Rights Reserved
8 *
9 * See the file COPYRIGHT for the respective terms and conditions.
10 * If the file is missing contact me at lf@elemental.net
11 * and I'll send you a copy.
12 *
13 */
14
15#define _GNU_SOURCE
16
17#include <sys/time.h>
18
19#include <config.h>
20#include <includes.h>
21#include <radcli/radcli.h>
22#include "util.h"
23
24#define RC_BUFSIZ 1024
25
26#ifdef __linux__
27#include <sched.h>
28#define NSNET_SZ (128)
29#endif
30
31
32static char const * months[] =
33 {
34 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
36 };
37
38/*- Turns printable string into correct tm struct entries
39 *
40 * @param valstr the printable date in 'day month year' format.
41 * @param tm the output struct.
42 * @return 0 on success, -1 if the month name in valstr was not recognized
43 * (tm is left untouched in that case).
44 -*/
45int rc_str2tm (char const *valstr, struct tm *tm)
46{
47 int i;
48
49 /* Get the month */
50 for (i = 0; i < 12; i++)
51 {
52 if (strncmp (months[i], valstr, 3) == 0)
53 break;
54 }
55 if (i == 12)
56 return -1;
57 tm->tm_mon = i;
58
59 /* Get the Day */
60 tm->tm_mday = atoi (&valstr[4]);
61
62 /* Now the year */
63 tm->tm_year = atoi (&valstr[7]) - 1900;
64
65 return 0;
66}
67
68/*- Returns the current monotonic time in milliseconds.
69 *
70 * @return milliseconds since an arbitrary fixed point, or -1 on failure.
71 -*/
72int64_t rc_getmtime_ms(void)
73{
74#ifdef HAVE_CLOCK_GETTIME
75 struct timespec timespec;
76
77 if (clock_gettime(CLOCK_MONOTONIC, &timespec) != 0)
78 return -1;
79
80 return ((int64_t)timespec.tv_sec * 1000) + (timespec.tv_nsec / 1000000);
81#else
82 struct timeval timev;
83
84 if (gettimeofday(&timev, NULL) == -1)
85 return -1;
86
87 return ((int64_t)timev.tv_sec * 1000) + (timev.tv_usec / 1000);
88#endif
89}
90
91/*
92 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
93 *
94 * Permission to use, copy, modify, and distribute this software for any
95 * purpose with or without fee is hereby granted, provided that the above
96 * copyright notice and this permission notice appear in all copies.
97 *
98 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
99 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
100 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
101 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
102 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
103 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
104 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
105 *
106 * Copyright 2006 The FreeRADIUS server project
107 */
108
109#ifdef RC_NEED_STRLCPY
110
111/*-
112 * Copy src to string dst of size siz. At most siz-1 characters
113 * will be copied. Always NUL terminates (unless siz == 0).
114 * Returns strlen(src); if retval >= siz, truncation occurred.
115 -*/
116size_t
117rc_strlcpy(char *dst, char const *src, size_t siz)
118{
119 char *d = dst;
120 char const *s = src;
121 size_t n = siz;
122
123 /* Copy as many bytes as will fit */
124 if (n != 0 && --n != 0) {
125 do {
126 if ((*d++ = *s++) == 0)
127 break;
128 } while (--n != 0);
129 }
130
131 /* Not enough room in dst, add NUL and traverse rest of src */
132 if (n == 0) {
133 if (siz != 0)
134 *d = '\0'; /* NUL-terminate dst */
135 while (*s++)
136 ;
137 }
138
139 return(s - src - 1); /* count does not include NUL */
140}
141
142#endif /* RC_NEED_STRLCPY */
143
144/*- Set the network namespace for the current thread (or process, if
145 * single-threaded).
146 *
147 * @param net_namespace the name of the namespace to switch into.
148 * @param prev_ns_handle set to a handle for the namespace active before
149 * the switch, for a later rc_reset_netns() call.
150 * @return 0 on success, -1 on failure.
151 -*/
152int rc_set_netns(const char *net_namespace, int *prev_ns_handle)
153{
154 int rc = 0;
155#ifdef __linux__
156 rc = -1;
157 static const char* crt_nsnet = "/proc/self/ns/net";
158 int sock_ns_fd = -1;
159 char sock_nsnet[NSNET_SZ];
160
161 if (NULL == net_namespace) {
162 rc_log(LOG_ERR, "Namespace not provided");
163 return rc;
164 }
165 if (NULL == prev_ns_handle) {
166 rc_log(LOG_ERR, "NULL NS handle for: %s", net_namespace);
167 return rc;
168 }
169 *prev_ns_handle = -1;
170 snprintf(sock_nsnet, NSNET_SZ, "/var/run/netns/%s", net_namespace);
171 do {
172 *prev_ns_handle = open(crt_nsnet, O_RDONLY);
173 if (*prev_ns_handle < 0) {
174 rc_log(LOG_ERR, "Cannot open %s errno=%s(%d)", crt_nsnet, strerror (errno), errno);
175 break;
176 }
177 sock_ns_fd = open(sock_nsnet, O_RDONLY);
178 if (sock_ns_fd < 0) {
179 rc_log(LOG_ERR, "Cannot open %s errno=%s(%d)", sock_nsnet, strerror (errno), errno);
180 break;
181 }
182 if (setns(sock_ns_fd, CLONE_NEWNET) < 0) {
183 rc_log(LOG_ERR, "'setns' set failed for %s errno=%s(%d)", sock_nsnet,
184 strerror(errno), errno);
185 break;
186 }
187 rc = 0;
188 }while (0);
189 if (sock_ns_fd >= 0) {
190 (void)close(sock_ns_fd);
191 }
192 if (rc != 0) {
193 if (*prev_ns_handle >=0 ) {
194 (void)close(*prev_ns_handle);
195 *prev_ns_handle = -1;
196 }
197 }
198#else
199 rc_log(LOG_ERR, "Not a Linux system. No operation performed");
200#endif
201 return rc;
202}
203
204/*- Restore the network namespace saved by rc_set_netns(); prev_ns_handle
205 * is invalid after this call.
206 *
207 * @param prev_ns_handle a handle from rc_set_netns().
208 * @return 0 on success, -1 on failure.
209 -*/
210int rc_reset_netns(int *prev_ns_handle)
211{
212 int rc = 0;
213#ifdef __linux__
214 if (NULL == prev_ns_handle) {
215 rc_log(LOG_ERR, "NULL NS handle");
216 return -1;
217 }
218 if (setns(*prev_ns_handle, CLONE_NEWNET) < 0) {
219 rc_log(LOG_ERR, "'setns' - reset failed errno=%s(%d)", strerror (errno), errno);
220 }
221 if (close(*prev_ns_handle) != 0) {
222 rc_log(LOG_ERR, "Close error fd=%d errno=%s(%d)", *prev_ns_handle, strerror (errno), errno);
223 rc = -1;
224 }
225 *prev_ns_handle = -1;
226#else
227 rc_log(LOG_ERR, "Not a Linux system. No operation performed");
228#endif
229 return rc;
230}