386BSD 0.1 development
[unix-history] / usr / src / sbin / route / ccitt_addr.c
CommitLineData
11c3630d
WJ
1/*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ccitt_addr.c 5.1 (Berkeley) 6/27/91
34 */
35/*
36 * parse CCITT addresses
37 *
38 * Addresses must have the format: [hpr],x121address[,userdata][,protocol]
39 * items enclosed with square brackets are optional
40 * 'h' or 'p' means hi priority (packet size = 128; specific to Datapac
41 * and necessary only for X.25(76) and non-negotiating X.25(80) DTE's)
42 * 'r' means reverse charge (remote DTE pays for call).
43 * The x121address consists of an optional netid and dot, followed
44 * by a dte address.
45 *
46 * Frank Pronk
47 * The University of British Columbia
48 * Laboratory for Computational Vision
49 * Copyright (c) 1984
50 */
51
52#include <sys/types.h>
53#include <sys/socket.h>
54#include <netccitt/x25.h>
55
56static char *copychar ();
57
58ccitt_addr (addr, xp)
59char *addr;
60register struct sockaddr_x25 *xp;
61{
62 register char *p, *ap, *limit;
63 int havenet = 0;
64
65 bzero ((char *)xp, sizeof (*xp));
66 xp->x25_family = AF_CCITT;
67 xp->x25_len = sizeof(*xp);
68 p = addr;
69
70 /*
71 * process optional priority and reverse charging flags
72 */
73
74 if (*p == 'p' || *p == 'r' || *p == 'h') {
75 while (*p == 'p' || *p == 'r' || *p == 'h') {
76 if (*p == 'p' || *p == 'h')
77 xp->x25_opts.op_psize = X25_PS128;
78 else if (*p == 'r')
79 xp->x25_opts.op_flags |= X25_REVERSE_CHARGE;
80 p++;
81 }
82 if (*p != ',')
83 return (0);
84 p++;
85 }
86 if (*p == '\0')
87 return (0);
88
89 /*
90 * [network id:]X.121 address
91 */
92
93 ap = xp->x25_addr;
94 limit = ap + sizeof (xp->x25_addr) - 1;
95 while (*p) {
96 if (*p == ',')
97 break;
98 if (*p == '.' || *p == ':') {
99 if (havenet)
100 return (0);
101 havenet++;
102 xp->x25_net = atoi (xp->x25_addr);
103 p++;
104 ap = xp->x25_addr;
105 *ap = '\0';
106 }
107 if (*p < '0' || *p > '9')
108 return (0);
109 if (ap >= limit)
110 return (0);
111 *ap++ = *p++;
112 }
113 if (*p == '\0')
114 return (1);
115
116 /*
117 * optional user data, bytes 4 to 16
118 */
119
120 p++;
121 ap = xp->x25_udata + 4; /* first four bytes are protocol id */
122 limit = ap + sizeof (xp->x25_udata) - 4;
123 xp->x25_udlen = 4;
124 while (*p) {
125 if (*p == ',')
126 break;
127 if (ap >= limit)
128 return (0);
129 p = copychar (p, ap++);
130 xp->x25_udlen++;
131 }
132 if (xp->x25_udlen == 4)
133 xp->x25_udlen = 0;
134 if (*p == '\0')
135 return (1);
136
137 p++;
138 ap = xp->x25_udata; /* protocol id */
139 limit = ap + (xp->x25_udlen ? 4 : sizeof(xp->x25_udata));
140 while (*p) {
141 if (*p == ',')
142 return (0);
143 if (ap >= limit)
144 return (0);
145 p = copychar (p, ap++);
146 }
147 if (xp->x25_udlen == 0)
148 xp->x25_udlen = ap - xp->x25_udata;
149 return (1);
150}
151
152static char *
153copychar (from, to)
154register char *from, *to;
155{
156 register int n;
157
158 if (*from != '\\' || from[1] < '0' || from[1] > '7') {
159 *to = *from++;
160 return (from);
161 }
162 n = *++from - '0';
163 from++;
164 if (*from >= '0' && *from <= '7') {
165 register int n1;
166
167 n = n*8 + *from++ - '0';
168 if (*from >= '0' && *from <= '7' && (n1 = n*8 + *from-'0') < 256) {
169 n = n1;
170 from++;
171 }
172 }
173 *to = n;
174 return (from);
175}