]> git.ipfire.org Git - people/ms/strongswan.git/blame - src/libfreeswan/subnettoa.c
_copyright: Replicate copyright text here instead of calling libfreeswan.
[people/ms/strongswan.git] / src / libfreeswan / subnettoa.c
CommitLineData
997358a6
MW
1/*
2 * convert binary form of subnet description to ASCII
3 * Copyright (C) 1998, 1999 Henry Spencer.
7daf5226 4 *
997358a6
MW
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
7daf5226 9 *
997358a6
MW
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
997358a6
MW
14 */
15#include "internal.h"
16#include "freeswan.h"
17
18/*
19 - subnettoa - convert address and mask to ASCII "addr/mask"
20 * Output expresses the mask as a bit count if possible, else dotted decimal.
21 */
22size_t /* space needed for full conversion */
23subnettoa(addr, mask, format, dst, dstlen)
24struct in_addr addr;
25struct in_addr mask;
26int format; /* character */
27char *dst; /* need not be valid if dstlen is 0 */
28size_t dstlen;
29{
30 size_t len;
31 size_t rest;
32 int n;
33 char *p;
34
35 switch (format) {
36 case 0:
37 break;
38 default:
39 return 0;
40 break;
41 }
42
43 len = addrtoa(addr, 0, dst, dstlen);
44 if (len < dstlen) {
45 dst[len - 1] = '/';
46 p = dst + len;
47 rest = dstlen - len;
48 } else {
49 p = NULL;
50 rest = 0;
51 }
52
53 n = masktobits(mask);
54 if (n >= 0)
55 len += ultoa((unsigned long)n, 10, p, rest);
56 else
57 len += addrtoa(mask, 0, p, rest);
58
59 return len;
60}