]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libfreeswan/libfreeswan/atosa.c
- fixed stroke error output to starter
[people/ms/strongswan.git] / src / libfreeswan / libfreeswan / atosa.c
1 /*
2 * convert from ASCII form of SA ID to binary
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: atosa.c,v 1.1 2004/03/15 20:35:26 as Exp $
16 */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 static struct satype {
21 char *prefix;
22 size_t prelen; /* strlen(prefix) */
23 int proto;
24 } satypes[] = {
25 { "ah", 2, SA_AH },
26 { "esp", 3, SA_ESP },
27 { "tun", 3, SA_IPIP },
28 { "comp", 4, SA_COMP },
29 { NULL, 0, 0, }
30 };
31
32 /*
33 - atosa - convert ASCII "ah507@10.0.0.1" to SA identifier
34 */
35 const char * /* NULL for success, else string literal */
36 atosa(src, srclen, sa)
37 const char *src;
38 size_t srclen; /* 0 means "apply strlen" */
39 struct sa_id *sa;
40 {
41 const char *at;
42 const char *addr;
43 const char *spi = NULL;
44 struct satype *sat;
45 unsigned long ul;
46 const char *oops;
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 */
50
51 if (srclen == 0)
52 srclen = strlen(src);
53 if (srclen == 0)
54 return "empty string";
55 if (srclen < MINLEN)
56 return "string too short to be SA specifier";
57 if (srclen == PTNLEN && memcmp(src, ptname, PTNLEN) == 0) {
58 src = PASSTHROUGHIS;
59 srclen = strlen(src);
60 }
61
62 at = memchr(src, '@', srclen);
63 if (at == NULL)
64 return "no @ in SA specifier";
65
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 */
72 }
73 if (sat->prefix == NULL)
74 return "SA specifier lacks valid protocol prefix";
75
76 if (spi >= at)
77 return "no SPI in SA specifier";
78 oops = atoul(spi, at - spi, 13, &ul);
79 if (oops != NULL)
80 return oops;
81 sa->spi = htonl(ul);
82
83 addr = at + 1;
84 oops = atoaddr(addr, srclen - (addr - src), &sa->dst);
85 if (oops != NULL)
86 return oops;
87
88 return NULL;
89 }
90
91
92
93 #ifdef ATOSA_MAIN
94
95 #include <stdio.h>
96 #include <sys/socket.h>
97 #include <netinet/in.h>
98 #include <arpa/inet.h>
99
100 void regress(void);
101
102 int
103 main(int argc, char *argv[])
104 {
105 struct sa_id sa;
106 char buf[100];
107 const char *oops;
108 size_t n;
109
110 if (argc < 2) {
111 fprintf(stderr, "Usage: %s {ahnnn@aaa|-r}\n", argv[0]);
112 exit(2);
113 }
114
115 if (strcmp(argv[1], "-r") == 0) {
116 regress();
117 fprintf(stderr, "regress() returned?!?\n");
118 exit(1);
119 }
120
121 oops = atosa(argv[1], 0, &sa);
122 if (oops != NULL) {
123 fprintf(stderr, "%s: conversion failed: %s\n", argv[0], oops);
124 exit(1);
125 }
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));
133 exit(1);
134 }
135 printf("%s\n", buf);
136
137 exit(0);
138 }
139
140 struct rtab {
141 char *input;
142 char *output; /* NULL means error expected */
143 } rtab[] = {
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"},
147 {"", NULL},
148 {"_", NULL},
149 {"ah2.2", NULL},
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},
158 {NULL, NULL}
159 };
160
161 void
162 regress(void)
163 {
164 struct rtab *r;
165 int status = 0;
166 struct sa_id sa;
167 char in[100];
168 char buf[100];
169 const char *oops;
170 size_t n;
171
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);
179 status = 1;
180 } else if (r->output == NULL) {
181 printf("`%s' atosa succeeded unexpectedly\n",
182 r->input);
183 status = 1;
184 } else {
185 n = satoa(sa, 'd', buf, sizeof(buf));
186 if (n > sizeof(buf)) {
187 printf("`%s' satoa failed: need %ld\n",
188 r->input, (long)n);
189 status = 1;
190 } else if (strcmp(r->output, buf) != 0) {
191 printf("`%s' gave `%s', expected `%s'\n",
192 r->input, buf, r->output);
193 status = 1;
194 }
195 }
196 }
197 exit(status);
198 }
199
200 #endif /* ATOSA_MAIN */