]>
git.ipfire.org Git - people/ms/strongswan.git/blob - src/libfreeswan/libfreeswan/atosa.c
2 * convert from ASCII form of SA ID to binary
3 * Copyright (C) 1998, 1999 Henry Spencer.
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>.
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.
15 * RCSID $Id: atosa.c,v 1.1 2004/03/15 20:35:26 as Exp $
20 static struct satype
{
22 size_t prelen
; /* strlen(prefix) */
27 { "tun", 3, SA_IPIP
},
28 { "comp", 4, SA_COMP
},
33 - atosa - convert ASCII "ah507@10.0.0.1" to SA identifier
35 const char * /* NULL for success, else string literal */
36 atosa(src
, srclen
, sa
)
38 size_t srclen
; /* 0 means "apply strlen" */
43 const char *spi
= NULL
;
47 # define MINLEN 5 /* ah0@0 is as short as it can get */
48 static char ptname
[] = PASSTHROUGHNAME
;
49 # define PTNLEN (sizeof(ptname)-1) /* -1 for NUL */
54 return "empty string";
56 return "string too short to be SA specifier";
57 if (srclen
== PTNLEN
&& memcmp(src
, ptname
, PTNLEN
) == 0) {
62 at
= memchr(src
, '@', srclen
);
64 return "no @ in SA specifier";
66 for (sat
= satypes
; sat
->prefix
!= NULL
; sat
++)
67 if (sat
->prelen
< srclen
&&
68 strncmp(src
, sat
->prefix
, sat
->prelen
) == 0) {
69 sa
->proto
= sat
->proto
;
70 spi
= src
+ sat
->prelen
;
71 break; /* NOTE BREAK OUT */
73 if (sat
->prefix
== NULL
)
74 return "SA specifier lacks valid protocol prefix";
77 return "no SPI in SA specifier";
78 oops
= atoul(spi
, at
- spi
, 13, &ul
);
84 oops
= atoaddr(addr
, srclen
- (addr
- src
), &sa
->dst
);
96 #include <sys/socket.h>
97 #include <netinet/in.h>
98 #include <arpa/inet.h>
103 main(int argc
, char *argv
[])
111 fprintf(stderr
, "Usage: %s {ahnnn@aaa|-r}\n", argv
[0]);
115 if (strcmp(argv
[1], "-r") == 0) {
117 fprintf(stderr
, "regress() returned?!?\n");
121 oops
= atosa(argv
[1], 0, &sa
);
123 fprintf(stderr
, "%s: conversion failed: %s\n", argv
[0], oops
);
126 n
= satoa(sa
, 0, buf
, sizeof(buf
));
127 if (n
> sizeof(buf
)) {
128 fprintf(stderr
, "%s: reverse conv of `%d'", argv
[0], sa
.proto
);
129 fprintf(stderr
, "%lu@", (long unsigned int)sa
.spi
);
130 fprintf(stderr
, "%s", inet_ntoa(sa
.dst
));
131 fprintf(stderr
, " failed: need %ld bytes, have only %ld\n",
132 (long)n
, (long)sizeof(buf
));
142 char *output
; /* NULL means error expected */
144 {"esp257@1.2.3.0", "esp257@1.2.3.0"},
145 {"ah0x20@1.2.3.4", "ah32@1.2.3.4"},
146 {"tun011@111.2.3.99", "tun11@111.2.3.99"},
150 {"goo2@1.2.3.4", NULL
},
151 {"esp9@1.2.3.4", "esp9@1.2.3.4"},
152 {"espp9@1.2.3.4", NULL
},
153 {"es9@1.2.3.4", NULL
},
154 {"ah@1.2.3.4", NULL
},
155 {"esp7x7@1.2.3.4", NULL
},
156 {"esp77@1.0x2.3.4", NULL
},
157 {PASSTHROUGHNAME
, PASSTHROUGHNAME
},
172 for (r
= rtab
; r
->input
!= NULL
; r
++) {
173 strcpy(in
, r
->input
);
174 oops
= atosa(in
, 0, &sa
);
175 if (oops
!= NULL
&& r
->output
== NULL
)
176 {} /* okay, error expected */
177 else if (oops
!= NULL
) {
178 printf("`%s' atosa failed: %s\n", r
->input
, oops
);
180 } else if (r
->output
== NULL
) {
181 printf("`%s' atosa succeeded unexpectedly\n",
185 n
= satoa(sa
, 'd', buf
, sizeof(buf
));
186 if (n
> sizeof(buf
)) {
187 printf("`%s' satoa failed: need %ld\n",
190 } else if (strcmp(r
->output
, buf
) != 0) {
191 printf("`%s' gave `%s', expected `%s'\n",
192 r
->input
, buf
, r
->output
);
200 #endif /* ATOSA_MAIN */