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.11 2000/09/17 18:55:37 henry 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);
110 fprintf(stderr, "Usage: %s {ahnnn@aaa|-r}\n", argv[0]);
114 if (strcmp(argv[1], "-r") == 0) {
116 fprintf(stderr, "regress() returned?!?\n");
120 oops = atosa(argv[1], 0, &sa);
122 fprintf(stderr, "%s: conversion failed: %s\n", argv[0], oops);
125 n = satoa(sa, 0, buf, sizeof(buf));
126 if (n > sizeof(buf)) {
127 fprintf(stderr, "%s: reverse conv of `%d'", argv[0], sa.proto);
128 fprintf(stderr, "%lu@", sa.spi);
129 fprintf(stderr, "%s", inet_ntoa(sa.dst));
130 fprintf(stderr, " failed: need %ld bytes, have only %ld\n",
131 (long)n, (long)sizeof(buf));
141 char *output; /* NULL means error expected */
143 "esp257@1.2.3.0", "esp257@1.2.3.0",
144 "ah0x20@1.2.3.4", "ah32@1.2.3.4",
145 "tun011@111.2.3.99", "tun11@111.2.3.99",
149 "goo2@1.2.3.4", NULL,
150 "esp9@1.2.3.4", "esp9@1.2.3.4",
151 "espp9@1.2.3.4", NULL,
154 "esp7x7@1.2.3.4", NULL,
155 "esp77@1.0x2.3.4", NULL,
156 PASSTHROUGHNAME, PASSTHROUGHNAME,
171 for (r = rtab; r->input != NULL; r++) {
172 strcpy(in, r->input);
173 oops = atosa(in, 0, &sa);
174 if (oops != NULL && r->output == NULL)
175 {} /* okay, error expected */
176 else if (oops != NULL) {
177 printf("`%s' atosa failed: %s\n", r->input, oops);
179 } else if (r->output == NULL) {
180 printf("`%s' atosa succeeded unexpectedly\n",
184 n = satoa(sa, 'd', buf, sizeof(buf));
185 if (n > sizeof(buf)) {
186 printf("`%s' satoa failed: need %ld\n",
189 } else if (strcmp(r->output, buf) != 0) {
190 printf("`%s' gave `%s', expected `%s'\n",
191 r->input, buf, r->output);
199 #endif /* ATOSA_MAIN */