]>
Commit | Line | Data |
---|---|---|
997358a6 MW |
1 | /* |
2 | * addresses 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: addrtoa.c,v 1.1 2004/03/15 20:35:25 as Exp $ | |
16 | */ | |
17 | #include "internal.h" | |
18 | #include "freeswan.h" | |
19 | ||
20 | #define NBYTES 4 /* bytes in an address */ | |
21 | #define PERBYTE 4 /* three digits plus a dot or NUL */ | |
22 | #define BUFLEN (NBYTES*PERBYTE) | |
23 | ||
24 | #if BUFLEN != ADDRTOA_BUF | |
25 | #error "ADDRTOA_BUF in freeswan.h inconsistent with addrtoa() code" | |
26 | #endif | |
27 | ||
28 | /* | |
29 | - addrtoa - convert binary address to ASCII dotted decimal | |
30 | */ | |
31 | size_t /* space needed for full conversion */ | |
32 | addrtoa(addr, format, dst, dstlen) | |
33 | struct in_addr addr; | |
34 | int format; /* character */ | |
35 | char *dst; /* need not be valid if dstlen is 0 */ | |
36 | size_t dstlen; | |
37 | { | |
38 | unsigned long a = ntohl(addr.s_addr); | |
39 | int i; | |
40 | size_t n; | |
41 | unsigned long byte; | |
42 | char buf[BUFLEN]; | |
43 | char *p; | |
44 | ||
45 | switch (format) { | |
46 | case 0: | |
47 | break; | |
48 | default: | |
49 | return 0; | |
50 | break; | |
51 | } | |
52 | ||
53 | p = buf; | |
54 | for (i = NBYTES-1; i >= 0; i--) { | |
55 | byte = (a >> (i*8)) & 0xff; | |
56 | p += ultoa(byte, 10, p, PERBYTE); | |
57 | if (i != 0) | |
58 | *(p-1) = '.'; | |
59 | } | |
60 | n = p - buf; | |
61 | ||
62 | if (dstlen > 0) { | |
63 | if (n > dstlen) | |
64 | buf[dstlen - 1] = '\0'; | |
65 | strcpy(dst, buf); | |
66 | } | |
67 | return n; | |
68 | } |