Radcli library 1.5.3
A simple radius library
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 as a double
69 *
70 * @return Get monotonic time seconds since some fixed start) expressed as
71 * double-precision floating point number.
72 -*/
73double rc_getmtime(void)
74{
75#ifdef HAVE_CLOCK_GETTIME
76 struct timespec timespec;
77
78 if (clock_gettime(CLOCK_MONOTONIC, &timespec) != 0)
79 return -1;
80
81 return timespec.tv_sec + ((double)timespec.tv_nsec) / 1000000000.0;
82#else
83 struct timeval timev;
84
85 if (gettimeofday(&timev, NULL) == -1)
86 return -1;
87
88 return timev.tv_sec + ((double)timev.tv_usec) / 1000000.0;
89#endif
90}
91
106char *
108{
109 static char buf[15];
110 static unsigned short int cnt = 0;
111 snprintf (buf, sizeof(buf), "%08lX%04X%02hX",
112 (unsigned long int) time (NULL),
113 (unsigned int) getpid (),
114 cnt & 0xFF);
115 cnt++;
116 return buf;
117}
118/*
119 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
120 *
121 * Permission to use, copy, modify, and distribute this software for any
122 * purpose with or without fee is hereby granted, provided that the above
123 * copyright notice and this permission notice appear in all copies.
124 *
125 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
126 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
127 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
129 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
130 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
131 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
132 *
133 * Copyright 2006 The FreeRADIUS server project
134 */
135
136#ifdef RC_NEED_STRLCPY
137
138/*-
139 * Copy src to string dst of size siz. At most siz-1 characters
140 * will be copied. Always NUL terminates (unless siz == 0).
141 * Returns strlen(src); if retval >= siz, truncation occurred.
142 -*/
143size_t
144rc_strlcpy(char *dst, char const *src, size_t siz)
145{
146 char *d = dst;
147 char const *s = src;
148 size_t n = siz;
149
150 /* Copy as many bytes as will fit */
151 if (n != 0 && --n != 0) {
152 do {
153 if ((*d++ = *s++) == 0)
154 break;
155 } while (--n != 0);
156 }
157
158 /* Not enough room in dst, add NUL and traverse rest of src */
159 if (n == 0) {
160 if (siz != 0)
161 *d = '\0'; /* NUL-terminate dst */
162 while (*s++)
163 ;
164 }
165
166 return(s - src - 1); /* count does not include NUL */
167}
168
169#endif /* RC_NEED_STRLCPY */
170
178int rc_set_netns(const char *net_namespace, int *prev_ns_handle)
179{
180 int rc = 0;
181#ifdef __linux__
182 rc = -1;
183 static const char* crt_nsnet = "/proc/self/ns/net";
184 int sock_ns_fd = -1;
185 char sock_nsnet[NSNET_SZ];
186
187 if (NULL == net_namespace) {
188 rc_log(LOG_ERR, "Namespace not provided");
189 return rc;
190 }
191 if (NULL == prev_ns_handle) {
192 rc_log(LOG_ERR, "NULL NS handle for: %s", net_namespace);
193 return rc;
194 }
195 *prev_ns_handle = -1;
196 snprintf(sock_nsnet, NSNET_SZ, "/var/run/netns/%s", net_namespace);
197 do {
198 *prev_ns_handle = open(crt_nsnet, O_RDONLY);
199 if (*prev_ns_handle < 0) {
200 rc_log(LOG_ERR, "Cannot open %s errno=%s(%d)", crt_nsnet, strerror (errno), errno);
201 break;
202 }
203 sock_ns_fd = open(sock_nsnet, O_RDONLY);
204 if (sock_ns_fd < 0) {
205 rc_log(LOG_ERR, "Cannot open %s errno=%s(%d)", sock_nsnet, strerror (errno), errno);
206 break;
207 }
208 if (setns(sock_ns_fd, CLONE_NEWNET) < 0) {
209 rc_log(LOG_ERR, "'setns' set failed for %s errno=%s(%d)", sock_nsnet,
210 strerror(errno), errno);
211 break;
212 }
213 rc = 0;
214 }while (0);
215 if (sock_ns_fd >= 0) {
216 (void)close(sock_ns_fd);
217 }
218 if (rc != 0) {
219 if (*prev_ns_handle >=0 ) {
220 (void)close(*prev_ns_handle);
221 *prev_ns_handle = -1;
222 }
223 }
224#else
225 rc_log(LOG_ERR, "Not a Linux system. No operation performed");
226#endif
227 return rc;
228}
229
237int rc_reset_netns(int *prev_ns_handle)
238{
239 int rc = 0;
240#ifdef __linux__
241 if (NULL == prev_ns_handle) {
242 rc_log(LOG_ERR, "NULL NS handle");
243 return -1;
244 }
245 if (setns(*prev_ns_handle, CLONE_NEWNET) < 0) {
246 rc_log(LOG_ERR, "'setns' - reset failed errno=%s(%d)", strerror (errno), errno);
247 }
248 if (close(*prev_ns_handle) != 0) {
249 rc_log(LOG_ERR, "Close error fd=%d errno=%s(%d)", *prev_ns_handle, strerror (errno), errno);
250 rc = -1;
251 }
252 *prev_ns_handle = -1;
253#else
254 rc_log(LOG_ERR, "Not a Linux system. No operation performed");
255#endif
256 return rc;
257}
Public API of the radcli library.
char * rc_mksid(void) _RADCLI_GCC_ATTR_DEPRECATED
Generate a "unique" session-ID string.
Definition util.c:107