OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / lib / subnettoa.c
1 /*
2  * convert binary form of subnet description to ASCII
3  * Copyright (C) 1998, 1999  Henry Spencer.
4  * 
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>.
9  * 
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.
14  *
15  * RCSID $Id: subnettoa.c,v 1.7 1999/04/10 23:24:22 henry Exp $
16  */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /*
21  - subnettoa - convert address and mask to ASCII "addr/mask"
22  * Output expresses the mask as a bit count if possible, else dotted decimal.
23  */
24 size_t                          /* space needed for full conversion */
25 subnettoa(addr, mask, format, dst, dstlen)
26 struct in_addr addr;
27 struct in_addr mask;
28 int format;                     /* character */
29 char *dst;                      /* need not be valid if dstlen is 0 */
30 size_t dstlen;
31 {
32         size_t len;
33         size_t rest;
34         int n;
35         char *p;
36
37         switch (format) {
38         case 0:
39                 break;
40         default:
41                 return 0;
42                 break;
43         }
44
45         len = addrtoa(addr, 0, dst, dstlen);
46         if (len < dstlen) {
47                 dst[len - 1] = '/';
48                 p = dst + len;
49                 rest = dstlen - len;
50         } else {
51                 p = NULL;
52                 rest = 0;
53         }
54
55         n = masktobits(mask);
56         if (n >= 0)
57                 len += ultoa((unsigned long)n, 10, p, rest);
58         else
59                 len += addrtoa(mask, 0, p, rest);
60
61         return len;
62 }