aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/ipaddr.bi
blob: 73c49f826bdf6e1a50fedd025e1b47b899e10c9a (plain)
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
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2007-2011, 2015 Sergey Poznyakoff

   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 3, 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.  If not, see <http://www.gnu.org/licenses/>. */

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

MF_DEFUN(ntohl, NUMBER, NUMBER n)
{
	MF_RETURN(ntohl((uint32_t) n));
}
END

MF_DEFUN(htonl, NUMBER, NUMBER n)
{
	MF_RETURN(htonl((uint32_t) n));
}
END

/* FIXME: The following functions assume binary complement arithmetics.
   This is hardly a limitation, the similar approach works in GNU Radius
   for years. Nevertheless, this assumption should be noted. */

MF_DEFUN(ntohs, NUMBER, NUMBER n)
{
	MF_RETURN((uint32_t) ntohs(((uint16_t) n) & 0xffff));
}
END

MF_DEFUN(htons, NUMBER, NUMBER n)
{
	MF_RETURN((uint32_t) htons(((uint16_t) n) & 0xffff));
}
END

MF_DEFUN(inet_aton, NUMBER, STRING s)
{
	struct in_addr addr;

	MF_ASSERT(inet_aton(s, &addr),
		  mfe_invip,
		  _("invalid IP address (%s)"),
		  s);
	MF_RETURN(ntohl(addr.s_addr));
}
END

MF_DEFUN(inet_ntoa, STRING, NUMBER ip)
{
	struct in_addr addr;

	addr.s_addr = htonl(ip);
	MF_RETURN(inet_ntoa(addr));
}
END

MF_DEFUN(len_to_netmask, NUMBER, NUMBER x)
{
	unsigned long n = (unsigned long) x;
	unsigned long netmask;

	MF_ASSERT(n <= 32, mfe_range,
		  _("invalid netmask: %lu"), n);
	n = 32 - n;
	if (n == 32)
		netmask = 0;
	else
		netmask = (0xfffffffful >> n) << n;
	MF_RETURN(netmask);
}
END

MF_DEFUN(netmask_to_len, NUMBER, NUMBER x)
{
	unsigned long n = (unsigned long) x;
	unsigned long i;

	for (i = 32; i > 0; i--) {
		if (n & 1)
			break;
		n >>= 1;
	}
	MF_RETURN(i);
}
END

MF_INIT


Return to:

Send suggestions and report system problems to the System administrator.