libnl 1.1.4
Modules | Data Structures | Enumerations | Variables
Utilities

Modules

 Abstract Address
 
 Abstract Data
 

Data Structures

struct  nl_dump_params
 Dumping parameters. More...
 

Enumerations

enum  nl_dump_type {
  NL_DUMP_BRIEF , NL_DUMP_FULL , NL_DUMP_STATS , NL_DUMP_XML ,
  NL_DUMP_ENV , NL_DUMP_EVENTS , __NL_DUMP_MAX
}
 Dumping types (dp_type) More...
 

Variables

int nl_debug = 0
 Debug level.
 
struct nl_dump_params nl_debug_dp
 

Error Code Helpers

int nl_get_errno (void)
 
char * nl_geterror (void)
 Return error message for an error code.
 
void nl_perror (const char *s)
 Print a libnl error message.
 

Time Translations

int nl_get_hz (void)
 Return the value of HZ.
 
uint32_t nl_us2ticks (uint32_t us)
 Convert micro seconds to ticks.
 
uint32_t nl_ticks2us (uint32_t ticks)
 Convert ticks to micro seconds.
 
long nl_time2int (const char *str)
 
char * nl_msec2str (uint64_t msec, char *buf, size_t len)
 Convert milliseconds to a character string.
 

Link Layer Protocol Translations

char * nl_llproto2str (int llproto, char *buf, size_t len)
 
int nl_str2llproto (const char *name)
 

Ethernet Protocol Translations

char * nl_ether_proto2str (int eproto, char *buf, size_t len)
 
int nl_str2ether_proto (const char *name)
 

Unit Pretty-Printing

double nl_cancel_down_bytes (unsigned long long l, char **unit)
 Cancel down a byte counter.
 
double nl_cancel_down_bits (unsigned long long l, char **unit)
 Cancel down a bit counter.
 
double nl_cancel_down_us (uint32_t l, char **unit)
 Cancel down a micro second value.
 

Generic Unit Translations

long nl_size2int (const char *str)
 Convert a character string to a size.
 
long nl_prob2int (const char *str)
 Convert a character string to a probability.
 

IP Protocol Translations

char * nl_ip_proto2str (int proto, char *buf, size_t len)
 
int nl_str2ip_proto (const char *name)
 

Dumping Helpers

void nl_new_line (struct nl_dump_params *params, int line)
 Handle a new line while dumping.
 
void nl_dump (struct nl_dump_params *params, const char *fmt,...)
 Dump a formatted character string.
 

Probability Constants

#define NL_PROB_MIN   0x0
 Lower probability limit.
 
#define NL_PROB_MAX   0xffffffff
 Upper probability limit.
 

Detailed Description

Macro Definition Documentation

◆ NL_PROB_MIN

#define NL_PROB_MIN   0x0

Definition at line 31 of file utils.h.

◆ NL_PROB_MAX

#define NL_PROB_MAX   0xffffffff

Definition at line 37 of file utils.h.

Enumeration Type Documentation

◆ nl_dump_type

Enumerator
NL_DUMP_BRIEF 

Dump object in a brief one-liner.

NL_DUMP_FULL 

Dump all attributes but no statistics.

NL_DUMP_STATS 

Dump all attributes including statistics.

NL_DUMP_XML 

Dump all attribtes in XML format.

NL_DUMP_ENV 

Dump all attribtues as env variables.

NL_DUMP_EVENTS 

Dump event.

Definition at line 21 of file types.h.

21 {
22 NL_DUMP_BRIEF, /**< Dump object in a brief one-liner */
23 NL_DUMP_FULL, /**< Dump all attributes but no statistics */
24 NL_DUMP_STATS, /**< Dump all attributes including statistics */
25 NL_DUMP_XML, /**< Dump all attribtes in XML format */
26 NL_DUMP_ENV, /**< Dump all attribtues as env variables */
27 NL_DUMP_EVENTS, /**< Dump event */
28 __NL_DUMP_MAX,
29};
@ NL_DUMP_EVENTS
Dump event.
Definition: types.h:27
@ NL_DUMP_FULL
Dump all attributes but no statistics.
Definition: types.h:23
@ NL_DUMP_BRIEF
Dump object in a brief one-liner.
Definition: types.h:22
@ NL_DUMP_ENV
Dump all attribtues as env variables.
Definition: types.h:26
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:24
@ NL_DUMP_XML
Dump all attribtes in XML format.
Definition: types.h:25

Function Documentation

◆ nl_get_errno()

int nl_get_errno ( void  )

Definition at line 132 of file utils.c.

133{
134 return nlerrno;
135}

◆ nl_geterror()

char * nl_geterror ( void  )
Returns
error message

Definition at line 142 of file utils.c.

143{
144 if (errbuf)
145 return errbuf;
146
147 if (nlerrno)
148 return strerror(nlerrno);
149
150 return "Sucess\n";
151}

Referenced by nl_perror().

◆ nl_perror()

void nl_perror ( const char *  s)
Parameters
serror message prefix

Prints the error message of the call that failed last.

If s is not NULL and *s is not a null byte the argument string is printed, followed by a colon and a blank. Then the error message and a new-line.

Definition at line 163 of file utils.c.

164{
165 if (s && *s)
166 fprintf(stderr, "%s: %s\n", s, nl_geterror());
167 else
168 fprintf(stderr, "%s\n", nl_geterror());
169}
char * nl_geterror(void)
Return error message for an error code.
Definition: utils.c:142

References nl_geterror().

◆ nl_cancel_down_bytes()

double nl_cancel_down_bytes ( unsigned long long  l,
char **  unit 
)
Parameters
lbyte counter
unitdestination unit pointer

Cancels down a byte counter until it reaches a reasonable unit. The chosen unit is assigned to unit. This function assume 1024 bytes in one kilobyte

Returns
The cancelled down byte counter in the new unit.

Definition at line 189 of file utils.c.

190{
191 if (l >= 1099511627776LL) {
192 *unit = "TiB";
193 return ((double) l) / 1099511627776LL;
194 } else if (l >= 1073741824) {
195 *unit = "GiB";
196 return ((double) l) / 1073741824;
197 } else if (l >= 1048576) {
198 *unit = "MiB";
199 return ((double) l) / 1048576;
200 } else if (l >= 1024) {
201 *unit = "KiB";
202 return ((double) l) / 1024;
203 } else {
204 *unit = "B";
205 return (double) l;
206 }
207}

◆ nl_cancel_down_bits()

double nl_cancel_down_bits ( unsigned long long  l,
char **  unit 
)
Parameters
lbit counter
unitdestination unit pointer

Cancels down bit counter until it reaches a reasonable unit. The chosen unit is assigned to unit. This function assume 1000 bits in one kilobit

Returns
The cancelled down bit counter in the new unit.

Definition at line 220 of file utils.c.

221{
222 if (l >= 1000000000000ULL) {
223 *unit = "Tbit";
224 return ((double) l) / 1000000000000ULL;
225 }
226
227 if (l >= 1000000000) {
228 *unit = "Gbit";
229 return ((double) l) / 1000000000;
230 }
231
232 if (l >= 1000000) {
233 *unit = "Mbit";
234 return ((double) l) / 1000000;
235 }
236
237 if (l >= 1000) {
238 *unit = "Kbit";
239 return ((double) l) / 1000;
240 }
241
242 *unit = "bit";
243 return (double) l;
244}

◆ nl_cancel_down_us()

double nl_cancel_down_us ( uint32_t  l,
char **  unit 
)
Parameters
lmicro seconds
unitdestination unit pointer

Cancels down a microsecond counter until it reaches a reasonable unit. The chosen unit is assigned to unit.

Returns
The cancelled down microsecond in the new unit

Definition at line 256 of file utils.c.

257{
258 if (l >= 1000000) {
259 *unit = "s";
260 return ((double) l) / 1000000;
261 } else if (l >= 1000) {
262 *unit = "ms";
263 return ((double) l) / 1000;
264 } else {
265 *unit = "us";
266 return (double) l;
267 }
268}

◆ nl_size2int()

long nl_size2int ( const char *  str)
Parameters
strsize encoded as character string

Converts the specified size as character to the corresponding number of bytes.

Supported formats are:

  • b,kb/k,m/mb,gb/g for bytes
  • bit,kbit/mbit/gbit

This function assume 1000 bits in one kilobit and 1024 bytes in one kilobyte

Returns
The number of bytes or -1 if the string is unparseable

Definition at line 293 of file utils.c.

294{
295 char *p;
296 long l = strtol(str, &p, 0);
297 if (p == str)
298 return -1;
299
300 if (*p) {
301 if (!strcasecmp(p, "kb") || !strcasecmp(p, "k"))
302 l *= 1024;
303 else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g"))
304 l *= 1024*1024*1024;
305 else if (!strcasecmp(p, "gbit"))
306 l *= 1000000000L/8;
307 else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m"))
308 l *= 1024*1024;
309 else if (!strcasecmp(p, "mbit"))
310 l *= 1000000/8;
311 else if (!strcasecmp(p, "kbit"))
312 l *= 1000/8;
313 else if (!strcasecmp(p, "bit"))
314 l /= 8;
315 else if (strcasecmp(p, "b") != 0)
316 return -1;
317 }
318
319 return l;
320}

◆ nl_prob2int()

long nl_prob2int ( const char *  str)
Parameters
strprobability encoded as character string

Converts the specified probability as character to the corresponding probability number.

Supported formats are:

  • 0.0-1.0
  • 0%-100%
Returns
The probability relative to NL_PROB_MIN and NL_PROB_MAX

Definition at line 335 of file utils.c.

336{
337 char *p;
338 double d = strtod(str, &p);
339
340 if (p == str)
341 return -1;
342
343 if (d > 1.0)
344 d /= 100.0f;
345
346 if (d > 1.0f || d < 0.0f)
347 return -1;
348
349 if (*p && strcmp(p, "%") != 0)
350 return -1;
351
352 return rint(d * NL_PROB_MAX);
353}
#define NL_PROB_MAX
Upper probability limit.
Definition: utils.h:37

References NL_PROB_MAX.

◆ nl_get_hz()

int nl_get_hz ( void  )

Definition at line 428 of file utils.c.

429{
430 return user_hz;
431}

◆ nl_us2ticks()

uint32_t nl_us2ticks ( uint32_t  us)
Parameters
usmicro seconds
Returns
number of ticks

Definition at line 439 of file utils.c.

440{
441 return us * ticks_per_usec;
442}

Referenced by rtnl_netem_set_delay(), and rtnl_netem_set_jitter().

◆ nl_ticks2us()

uint32_t nl_ticks2us ( uint32_t  ticks)
Parameters
ticksnumber of ticks
Returns
microseconds

Definition at line 450 of file utils.c.

451{
452 return ticks / ticks_per_usec;
453}

Referenced by rtnl_netem_get_delay(), and rtnl_netem_get_jitter().

◆ nl_time2int()

long nl_time2int ( const char *  str)

Definition at line 455 of file utils.c.

456{
457 char *p;
458 long l = strtol(str, &p, 0);
459 if (p == str)
460 return -1;
461
462 if (*p) {
463 if (!strcasecmp(p, "min") == 0 || !strcasecmp(p, "m"))
464 l *= 60;
465 else if (!strcasecmp(p, "hour") || !strcasecmp(p, "h"))
466 l *= 60*60;
467 else if (!strcasecmp(p, "day") || !strcasecmp(p, "d"))
468 l *= 60*60*24;
469 else if (strcasecmp(p, "s") != 0)
470 return -1;
471 }
472
473 return l;
474}

◆ nl_msec2str()

char * nl_msec2str ( uint64_t  msec,
char *  buf,
size_t  len 
)
Parameters
msecnumber of milliseconds
bufdestination buffer
lenbuffer length

Converts milliseconds to a character string split up in days, hours, minutes, seconds, and milliseconds and stores it in the specified destination buffer.

Returns
The destination buffer.

Definition at line 488 of file utils.c.

489{
490 int i, split[5];
491 char *units[] = {"d", "h", "m", "s", "msec"};
492
493#define _SPLIT(idx, unit) if ((split[idx] = msec / unit) > 0) msec %= unit
494 _SPLIT(0, 86400000); /* days */
495 _SPLIT(1, 3600000); /* hours */
496 _SPLIT(2, 60000); /* minutes */
497 _SPLIT(3, 1000); /* seconds */
498#undef _SPLIT
499 split[4] = msec;
500
501 memset(buf, 0, len);
502
503 for (i = 0; i < ARRAY_SIZE(split); i++) {
504 if (split[i] > 0) {
505 char t[64];
506 snprintf(t, sizeof(t), "%s%d%s",
507 strlen(buf) ? " " : "", split[i], units[i]);
508 strncat(buf, t, len - strlen(buf) - 1);
509 }
510 }
511
512 return buf;
513}

◆ nl_llproto2str()

char * nl_llproto2str ( int  llproto,
char *  buf,
size_t  len 
)

Definition at line 598 of file utils.c.

599{
600 return __type2str(llproto, buf, len, llprotos, ARRAY_SIZE(llprotos));
601}

◆ nl_str2llproto()

int nl_str2llproto ( const char *  name)

Definition at line 603 of file utils.c.

604{
605 return __str2type(name, llprotos, ARRAY_SIZE(llprotos));
606}

◆ nl_ether_proto2str()

char * nl_ether_proto2str ( int  eproto,
char *  buf,
size_t  len 
)

Definition at line 670 of file utils.c.

671{
672 return __type2str(eproto, buf, len, ether_protos,
673 ARRAY_SIZE(ether_protos));
674}

◆ nl_str2ether_proto()

int nl_str2ether_proto ( const char *  name)

Definition at line 676 of file utils.c.

677{
678 return __str2type(name, ether_protos, ARRAY_SIZE(ether_protos));
679}

◆ nl_ip_proto2str()

char * nl_ip_proto2str ( int  proto,
char *  buf,
size_t  len 
)

Definition at line 688 of file utils.c.

689{
690 struct protoent *p = getprotobynumber(proto);
691
692 if (p) {
693 snprintf(buf, len, "%s", p->p_name);
694 return buf;
695 }
696
697 snprintf(buf, len, "0x%x", proto);
698 return buf;
699}

◆ nl_str2ip_proto()

int nl_str2ip_proto ( const char *  name)

Definition at line 701 of file utils.c.

702{
703 struct protoent *p = getprotobyname(name);
704 unsigned long l;
705 char *end;
706
707 if (p)
708 return p->p_proto;
709
710 l = strtoul(name, &end, 0);
711 if (l == ULONG_MAX || *end != '\0')
712 return -1;
713
714 return (int) l;
715}

◆ nl_new_line()

void nl_new_line ( struct nl_dump_params params,
int  line 
)
Parameters
paramsDumping parameters
lineNumber of lines dumped already.

This function must be called before dumping any onto a new line. It will ensure proper prefixing as specified by the dumping parameters.

Note
This function will NOT dump any newlines itself

Definition at line 735 of file utils.c.

736{
737 if (params->dp_prefix) {
738 int i;
739 for (i = 0; i < params->dp_prefix; i++) {
740 if (params->dp_fd)
741 fprintf(params->dp_fd, " ");
742 else if (params->dp_buf)
743 strncat(params->dp_buf, " ",
744 params->dp_buflen -
745 sizeof(params->dp_buf) - 1);
746 }
747 }
748
749 if (params->dp_nl_cb)
750 params->dp_nl_cb(params, line);
751}
size_t dp_buflen
Length of the buffer dp_buf.
Definition: types.h:96
int dp_prefix
Specifies the number of whitespaces to be put in front of every new line (indentation).
Definition: types.h:47
char * dp_buf
Alternatively the output may be redirected into a buffer.
Definition: types.h:91
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:86
void(* dp_nl_cb)(struct nl_dump_params *, int)
A callback invoked for every new line, can be used to customize the indentation.
Definition: types.h:76

References nl_dump_params::dp_buf, nl_dump_params::dp_buflen, nl_dump_params::dp_fd, nl_dump_params::dp_nl_cb, and nl_dump_params::dp_prefix.

◆ nl_dump()

void nl_dump ( struct nl_dump_params params,
const char *  fmt,
  ... 
)
Parameters
paramsDumping parameters
fmtprintf style formatting string
...Arguments to formatting string

Dumps a printf style formatting string to the output device as specified by the dumping parameters.

Definition at line 762 of file utils.c.

763{
764 va_list args;
765
766 va_start(args, fmt);
767 __dp_dump(params, fmt, args);
768 va_end(args);
769}

Variable Documentation

◆ nl_debug

int nl_debug = 0

Definition at line 25 of file utils.c.

◆ nl_debug_dp

struct nl_dump_params nl_debug_dp
Initial value:
= {
.dp_type = NL_DUMP_FULL,
}

Definition at line 27 of file utils.c.