1/* This file is part of Mailfromd.             -*- c -*-
2   Copyright (C) 2007-2021 Sergey Poznyakoff
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 3, or (at your option)
7   any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17MF_BUILTIN_MODULE
18
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22
23MF_DEFUN(ntohl, NUMBER, NUMBER n)
24{
25	MF_RETURN(ntohl((uint32_t) n));
26}
27END
28
29MF_DEFUN(htonl, NUMBER, NUMBER n)
30{
31	MF_RETURN(htonl((uint32_t) n));
32}
33END
34
35/* FIXME: The following functions assume binary complement arithmetics.
36   This is hardly a limitation, the similar approach works in GNU Radius
37   for years. Nevertheless, this assumption should be noted. */
38
39MF_DEFUN(ntohs, NUMBER, NUMBER n)
40{
41	MF_RETURN((uint32_t) ntohs(((uint16_t) n) & 0xffff));
42}
43END
44
45MF_DEFUN(htons, NUMBER, NUMBER n)
46{
47	MF_RETURN((uint32_t) htons(((uint16_t) n) & 0xffff));
48}
49END
50
51MF_DEFUN(inet_aton, NUMBER, STRING s)
52{
53	struct in_addr addr;
54
55	MF_ASSERT(inet_aton(s, &addr),
56		  mfe_invip,
57		  _("invalid IP address (%s)"),
58		  s);
59	MF_RETURN(ntohl(addr.s_addr));
60}
61END
62
63MF_DEFUN(inet_ntoa, STRING, NUMBER ip)
64{
65	struct in_addr addr;
66
67	addr.s_addr = htonl(ip);
68	MF_RETURN(inet_ntoa(addr));
69}
70END
71
72MF_DEFUN(len_to_netmask, NUMBER, NUMBER x)
73{
74	unsigned long n = (unsigned long) x;
75	unsigned long netmask;
76
77	MF_ASSERT(n <= 32, mfe_range,
78		  _("invalid netmask: %lu"), n);
79	n = 32 - n;
80	if (n == 32)
81		netmask = 0;
82	else
83		netmask = (0xfffffffful >> n) << n;
84	MF_RETURN(netmask);
85}
86END
87
88MF_DEFUN(netmask_to_len, NUMBER, NUMBER x)
89{
90	unsigned long n = (unsigned long) x;
91	unsigned long i;
92
93	for (i = 32; i > 0; i--) {
94		if (n & 1)
95			break;
96		n >>= 1;
97	}
98	MF_RETURN(i);
99}
100END
101
102