File: ipv4_format.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (118 lines) | stat: -rw-r--r-- 2,613 bytes parent folder | download | duplicates (4)
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
118
#include "ipv4.h"

static char* format_part(unsigned char i, char* s)
{
  if (i >= 10) {
    s = format_part(i / 10, s);
    i = i % 10;
  }
  *s++ = i + '0';
  return s;
}

/** Produce a formatted C string from an IPv4 address.

\note The return value is statically allocated.  Multiple calls to this
function will return pointers to the same string.
*/
const char* ipv4_format(const ipv4addr* addr)
{
  static char buf[16];
  buf[fmt_ipv4addr(buf, addr)] = 0;
  return buf;
}

/** Produce a reverse-formatted C string from an IPv4 address.

\note The return value is statically allocated.  Multiple calls to this
function will return pointers to the same string.
*/
const char* ipv4_format_reverse(const ipv4addr* addr)
{
  static char buf[16];
  buf[fmt_ipv4addr_reverse(buf, addr)] = 0;
  return buf;
}

/** Produce a formatted string from an IPv4 address.

The given buffer must be at least 15 bytes long, or 16 bytes if it needs
to contain the standard trailing \c NUL byte.

\return The number of bytes written to the buffer.

\note This routine is thread and recursion safe.
*/
unsigned fmt_ipv4addr(char* buffer, const ipv4addr* addr)
{
  char* s = buffer;
  s = format_part(addr->addr[0], s); *s++ = '.';
  s = format_part(addr->addr[1], s); *s++ = '.';
  s = format_part(addr->addr[2], s); *s++ = '.';
  s = format_part(addr->addr[3], s);
  return s - buffer;
}

/** Produce a reverse-formatted string from an IPv4 address.

The given buffer must be at least 15 bytes long, or 16 bytes if it needs
to contain the standard trailing \c NUL byte.

\return The number of bytes written to the buffer.

\note This routine is thread and recursion safe.
*/
unsigned fmt_ipv4addr_reverse(char* buffer, const ipv4addr* addr)
{
  char* s = buffer;
  s = format_part(addr->addr[3], s); *s++ = '.';
  s = format_part(addr->addr[2], s); *s++ = '.';
  s = format_part(addr->addr[1], s); *s++ = '.';
  s = format_part(addr->addr[0], s);
  return s - buffer;
}

#ifdef SELFTEST_MAIN
void testfn(const ipv4addr* ip, unsigned (*fn)(char*,const ipv4addr*))
{
  char buffer[32];
  unsigned i;
  debugfn(i = fn(buffer, ip));
  buffer[i++] = '\n';
  buffer[i] = 0;
  obuf_putsflush(&outbuf, buffer);
}

void test(const ipv4addr* ip)
{
  testfn(ip, fmt_ipv4addr);
  testfn(ip, fmt_ipv4addr_reverse);
}

MAIN
{
  ipv4addr ip = {{1,2,3,4}};
  test(&IPV4ADDR_ANY);
  test(&IPV4ADDR_BROADCAST);
  test(&IPV4ADDR_LOOPBACK);
  test(&ip);
}
#endif
#ifdef SELFTEST_EXP
result=7
0.0.0.0
result=7
0.0.0.0
result=15
255.255.255.255
result=15
255.255.255.255
result=9
127.0.0.1
result=9
1.0.0.127
result=7
1.2.3.4
result=7
4.3.2.1
#endif