1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * multilink.c - support routines for multilink.
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
5*7c478bd9Sstevel@tonic-gate * All rights reserved.
6*7c478bd9Sstevel@tonic-gate *
7*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000 Paul Mackerras.
8*7c478bd9Sstevel@tonic-gate * All rights reserved.
9*7c478bd9Sstevel@tonic-gate *
10*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
11*7c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are
12*7c478bd9Sstevel@tonic-gate * duplicated in all such forms. The name of the author may not be
13*7c478bd9Sstevel@tonic-gate * used to endorse or promote products derived from this software
14*7c478bd9Sstevel@tonic-gate * without specific prior written permission.
15*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17*7c478bd9Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18*7c478bd9Sstevel@tonic-gate */
19*7c478bd9Sstevel@tonic-gate
20*7c478bd9Sstevel@tonic-gate #include <string.h>
21*7c478bd9Sstevel@tonic-gate #include <ctype.h>
22*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
23*7c478bd9Sstevel@tonic-gate #include <netdb.h>
24*7c478bd9Sstevel@tonic-gate #include <errno.h>
25*7c478bd9Sstevel@tonic-gate #include <signal.h>
26*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gate #include "pppd.h"
29*7c478bd9Sstevel@tonic-gate #include "fsm.h"
30*7c478bd9Sstevel@tonic-gate #include "lcp.h"
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
33*7c478bd9Sstevel@tonic-gate #include "tdb.h"
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gate #define set_ip_epdisc(ep, addr) ( \
37*7c478bd9Sstevel@tonic-gate ep->length = 4, \
38*7c478bd9Sstevel@tonic-gate ep->value[0] = addr >> 24, \
39*7c478bd9Sstevel@tonic-gate ep->value[1] = addr >> 16, \
40*7c478bd9Sstevel@tonic-gate ep->value[2] = addr >> 8, \
41*7c478bd9Sstevel@tonic-gate ep->value[3] = addr \
42*7c478bd9Sstevel@tonic-gate )
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #ifdef HAVE_MULTILINK
45*7c478bd9Sstevel@tonic-gate bool endpoint_specified; /* user gave explicit endpoint discriminator */
46*7c478bd9Sstevel@tonic-gate char *bundle_id; /* identifier for our bundle */
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate static int get_default_epdisc __P((struct epdisc *));
49*7c478bd9Sstevel@tonic-gate static int parse_num __P((char *str, const char *key, int *valp));
50*7c478bd9Sstevel@tonic-gate static int owns_unit __P((TDB_DATA pid, int unit));
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #define process_exists(n) (kill(0, (n)) == 0 || errno != ESRCH)
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate void
mp_check_options()55*7c478bd9Sstevel@tonic-gate mp_check_options()
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate lcp_options *wo = &lcp_wantoptions[0];
58*7c478bd9Sstevel@tonic-gate lcp_options *ao = &lcp_allowoptions[0];
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate if (!multilink)
61*7c478bd9Sstevel@tonic-gate return;
62*7c478bd9Sstevel@tonic-gate /* if we're doing multilink, we have to negotiate MRRU */
63*7c478bd9Sstevel@tonic-gate if (!wo->neg_mrru) {
64*7c478bd9Sstevel@tonic-gate /* mrru not specified, default to mru */
65*7c478bd9Sstevel@tonic-gate wo->mrru = wo->mru;
66*7c478bd9Sstevel@tonic-gate wo->neg_mrru = 1;
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate ao->mrru = ao->mru;
69*7c478bd9Sstevel@tonic-gate ao->neg_mrru = 1;
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate if (!wo->neg_endpoint && !noendpoint) {
72*7c478bd9Sstevel@tonic-gate /* get a default endpoint value */
73*7c478bd9Sstevel@tonic-gate wo->neg_endpoint = get_default_epdisc(&wo->endpoint);
74*7c478bd9Sstevel@tonic-gate if (wo->neg_endpoint)
75*7c478bd9Sstevel@tonic-gate dbglog("using default endpoint %s",
76*7c478bd9Sstevel@tonic-gate epdisc_to_str(&wo->endpoint));
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate * Make a new bundle or join us to an existing bundle
82*7c478bd9Sstevel@tonic-gate * if we are doing multilink.
83*7c478bd9Sstevel@tonic-gate */
84*7c478bd9Sstevel@tonic-gate int
mp_join_bundle()85*7c478bd9Sstevel@tonic-gate mp_join_bundle()
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate lcp_options *go = &lcp_gotoptions[0];
88*7c478bd9Sstevel@tonic-gate lcp_options *ho = &lcp_hisoptions[0];
89*7c478bd9Sstevel@tonic-gate int unit, pppd_pid;
90*7c478bd9Sstevel@tonic-gate int l;
91*7c478bd9Sstevel@tonic-gate char *p;
92*7c478bd9Sstevel@tonic-gate TDB_DATA key, pid, rec;
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate if (!go->neg_mrru || !ho->neg_mrru) {
95*7c478bd9Sstevel@tonic-gate /* not doing multilink */
96*7c478bd9Sstevel@tonic-gate if (go->neg_mrru)
97*7c478bd9Sstevel@tonic-gate notice("oops, multilink negotiated only for receive");
98*7c478bd9Sstevel@tonic-gate multilink = 0;
99*7c478bd9Sstevel@tonic-gate if (demand) {
100*7c478bd9Sstevel@tonic-gate /* already have a bundle */
101*7c478bd9Sstevel@tonic-gate cfg_bundle(0, 0, 0, 0);
102*7c478bd9Sstevel@tonic-gate return 0;
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate make_new_bundle(0, 0, 0, 0);
105*7c478bd9Sstevel@tonic-gate set_ifunit(1);
106*7c478bd9Sstevel@tonic-gate return 0;
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate * Find the appropriate bundle or join a new one.
111*7c478bd9Sstevel@tonic-gate * First we make up a name for the bundle.
112*7c478bd9Sstevel@tonic-gate * The length estimate is worst-case assuming every
113*7c478bd9Sstevel@tonic-gate * character has to be quoted.
114*7c478bd9Sstevel@tonic-gate *
115*7c478bd9Sstevel@tonic-gate * Note - RFC 1990 requires that an unnegotiated endpoint
116*7c478bd9Sstevel@tonic-gate * discriminator value be equivalent to negotiating with class
117*7c478bd9Sstevel@tonic-gate * zero. Do not test ho->neg_endpoint here.
118*7c478bd9Sstevel@tonic-gate */
119*7c478bd9Sstevel@tonic-gate l = 4 * strlen(peer_authname) + 10;
120*7c478bd9Sstevel@tonic-gate l += 3 * ho->endpoint.length + 8;
121*7c478bd9Sstevel@tonic-gate if (bundle_name)
122*7c478bd9Sstevel@tonic-gate l += 3 * strlen(bundle_name) + 2;
123*7c478bd9Sstevel@tonic-gate bundle_id = malloc(l);
124*7c478bd9Sstevel@tonic-gate if (bundle_id == NULL)
125*7c478bd9Sstevel@tonic-gate novm("bundle identifier");
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate p = bundle_id;
128*7c478bd9Sstevel@tonic-gate p += slprintf(p, l-1, "BUNDLE=\"%q\"", peer_authname);
129*7c478bd9Sstevel@tonic-gate *p++ = '/';
130*7c478bd9Sstevel@tonic-gate p += slprintf(p, bundle_id+l-p, "%s", epdisc_to_str(&ho->endpoint));
131*7c478bd9Sstevel@tonic-gate if (bundle_name)
132*7c478bd9Sstevel@tonic-gate p += slprintf(p, bundle_id+l-p, "/%v", bundle_name);
133*7c478bd9Sstevel@tonic-gate dbglog("bundle_id = %s", bundle_id+7);
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate * For demand mode, we only need to configure the bundle
137*7c478bd9Sstevel@tonic-gate * and attach the link.
138*7c478bd9Sstevel@tonic-gate */
139*7c478bd9Sstevel@tonic-gate if (demand) {
140*7c478bd9Sstevel@tonic-gate cfg_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
141*7c478bd9Sstevel@tonic-gate script_setenv("BUNDLE", bundle_id + 7, 1);
142*7c478bd9Sstevel@tonic-gate return 0;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate * Check if the bundle ID is already in the database.
147*7c478bd9Sstevel@tonic-gate */
148*7c478bd9Sstevel@tonic-gate unit = -1;
149*7c478bd9Sstevel@tonic-gate tdb_writelock(pppdb);
150*7c478bd9Sstevel@tonic-gate key.dptr = bundle_id;
151*7c478bd9Sstevel@tonic-gate key.dsize = p - bundle_id;
152*7c478bd9Sstevel@tonic-gate pid = tdb_fetch(pppdb, key);
153*7c478bd9Sstevel@tonic-gate if (pid.dptr != NULL) {
154*7c478bd9Sstevel@tonic-gate /* bundle ID exists, see if the pppd record exists */
155*7c478bd9Sstevel@tonic-gate rec = tdb_fetch(pppdb, pid);
156*7c478bd9Sstevel@tonic-gate if (rec.dptr != NULL) {
157*7c478bd9Sstevel@tonic-gate /* it does, parse the interface number */
158*7c478bd9Sstevel@tonic-gate parse_num(rec.dptr, "IFNAME=ppp", &unit);
159*7c478bd9Sstevel@tonic-gate /* check the pid value */
160*7c478bd9Sstevel@tonic-gate if (!parse_num(rec.dptr, "PPPD_PID=", &pppd_pid)
161*7c478bd9Sstevel@tonic-gate || !process_exists(pppd_pid)
162*7c478bd9Sstevel@tonic-gate || !owns_unit(pid, unit))
163*7c478bd9Sstevel@tonic-gate unit = -1;
164*7c478bd9Sstevel@tonic-gate free(rec.dptr);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate free(pid.dptr);
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate
169*7c478bd9Sstevel@tonic-gate if (unit >= 0) {
170*7c478bd9Sstevel@tonic-gate /* attach to existing unit */
171*7c478bd9Sstevel@tonic-gate if (bundle_attach(unit)) {
172*7c478bd9Sstevel@tonic-gate set_ifunit(0);
173*7c478bd9Sstevel@tonic-gate script_setenv("BUNDLE", bundle_id + 7, 0);
174*7c478bd9Sstevel@tonic-gate tdb_writeunlock(pppdb);
175*7c478bd9Sstevel@tonic-gate info("Link attached to %s", ifname);
176*7c478bd9Sstevel@tonic-gate return 1;
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate /* attach failed because bundle doesn't exist */
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /* we have to make a new bundle */
182*7c478bd9Sstevel@tonic-gate make_new_bundle(go->mrru, ho->mrru, go->neg_ssnhf, ho->neg_ssnhf);
183*7c478bd9Sstevel@tonic-gate set_ifunit(1);
184*7c478bd9Sstevel@tonic-gate script_setenv("BUNDLE", bundle_id + 7, 1);
185*7c478bd9Sstevel@tonic-gate tdb_writeunlock(pppdb);
186*7c478bd9Sstevel@tonic-gate info("New bundle %s created", ifname);
187*7c478bd9Sstevel@tonic-gate return 0;
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate static int
parse_num(str,key,valp)191*7c478bd9Sstevel@tonic-gate parse_num(str, key, valp)
192*7c478bd9Sstevel@tonic-gate char *str;
193*7c478bd9Sstevel@tonic-gate const char *key;
194*7c478bd9Sstevel@tonic-gate int *valp;
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate char *p, *endp;
197*7c478bd9Sstevel@tonic-gate int i;
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate p = strstr(str, key);
200*7c478bd9Sstevel@tonic-gate if (p != 0) {
201*7c478bd9Sstevel@tonic-gate p += strlen(key);
202*7c478bd9Sstevel@tonic-gate i = strtol(p, &endp, 10);
203*7c478bd9Sstevel@tonic-gate if (endp != p && (*endp == 0 || *endp == ';')) {
204*7c478bd9Sstevel@tonic-gate *valp = i;
205*7c478bd9Sstevel@tonic-gate return 1;
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate return 0;
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate * Check whether the pppd identified by `key' still owns ppp unit `unit'.
213*7c478bd9Sstevel@tonic-gate */
214*7c478bd9Sstevel@tonic-gate static int
owns_unit(key,unit)215*7c478bd9Sstevel@tonic-gate owns_unit(key, unit)
216*7c478bd9Sstevel@tonic-gate TDB_DATA key;
217*7c478bd9Sstevel@tonic-gate int unit;
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate char ifkey[32];
220*7c478bd9Sstevel@tonic-gate TDB_DATA kd, vd;
221*7c478bd9Sstevel@tonic-gate int ret = 0;
222*7c478bd9Sstevel@tonic-gate
223*7c478bd9Sstevel@tonic-gate (void) slprintf(ifkey, sizeof(ifkey), "IFNAME=ppp%d", unit);
224*7c478bd9Sstevel@tonic-gate kd.dptr = ifkey;
225*7c478bd9Sstevel@tonic-gate kd.dsize = strlen(ifkey);
226*7c478bd9Sstevel@tonic-gate vd = tdb_fetch(pppdb, kd);
227*7c478bd9Sstevel@tonic-gate if (vd.dptr != NULL) {
228*7c478bd9Sstevel@tonic-gate ret = vd.dsize == key.dsize
229*7c478bd9Sstevel@tonic-gate && memcmp(vd.dptr, key.dptr, vd.dsize) == 0;
230*7c478bd9Sstevel@tonic-gate free(vd.dptr);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate return ret;
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate static int
get_default_epdisc(ep)236*7c478bd9Sstevel@tonic-gate get_default_epdisc(ep)
237*7c478bd9Sstevel@tonic-gate struct epdisc *ep;
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate struct hostent *hp;
240*7c478bd9Sstevel@tonic-gate u_int32_t addr;
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate if (get_first_hwaddr(ep->value, sizeof(ep->value))) {
243*7c478bd9Sstevel@tonic-gate ep->class = EPD_MAC;
244*7c478bd9Sstevel@tonic-gate ep->length = 6;
245*7c478bd9Sstevel@tonic-gate return 1;
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate /* see if our hostname corresponds to a reasonable IP address */
249*7c478bd9Sstevel@tonic-gate hp = gethostbyname(hostname);
250*7c478bd9Sstevel@tonic-gate if (hp != NULL) {
251*7c478bd9Sstevel@tonic-gate addr = *(u_int32_t *)hp->h_addr;
252*7c478bd9Sstevel@tonic-gate if (!bad_ip_adrs(addr)) {
253*7c478bd9Sstevel@tonic-gate addr = ntohl(addr);
254*7c478bd9Sstevel@tonic-gate if (!LOCAL_IP_ADDR(addr)) {
255*7c478bd9Sstevel@tonic-gate ep->class = EPD_IP;
256*7c478bd9Sstevel@tonic-gate set_ip_epdisc(ep, addr);
257*7c478bd9Sstevel@tonic-gate return 1;
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate
262*7c478bd9Sstevel@tonic-gate return 0;
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate #endif /* HAVE_MULTILINK */
265*7c478bd9Sstevel@tonic-gate
266*7c478bd9Sstevel@tonic-gate /*
267*7c478bd9Sstevel@tonic-gate * epdisc_to_str - make a printable string from an endpoint discriminator.
268*7c478bd9Sstevel@tonic-gate */
269*7c478bd9Sstevel@tonic-gate
270*7c478bd9Sstevel@tonic-gate static char *endp_class_names[] = {
271*7c478bd9Sstevel@tonic-gate "null", "local", "IP", "MAC", "magic", "phone"
272*7c478bd9Sstevel@tonic-gate };
273*7c478bd9Sstevel@tonic-gate
274*7c478bd9Sstevel@tonic-gate char *
epdisc_to_str(ep)275*7c478bd9Sstevel@tonic-gate epdisc_to_str(ep)
276*7c478bd9Sstevel@tonic-gate struct epdisc *ep;
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate static char str[MAX_ENDP_LEN*3+8];
279*7c478bd9Sstevel@tonic-gate u_char *p = ep->value;
280*7c478bd9Sstevel@tonic-gate int i, mask = 0;
281*7c478bd9Sstevel@tonic-gate char *q, c, c2;
282*7c478bd9Sstevel@tonic-gate
283*7c478bd9Sstevel@tonic-gate if (ep->class == EPD_NULL && ep->length == 0)
284*7c478bd9Sstevel@tonic-gate return "null";
285*7c478bd9Sstevel@tonic-gate if (ep->class == EPD_IP && ep->length == 4) {
286*7c478bd9Sstevel@tonic-gate u_int32_t addr;
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate GETLONG(addr, p);
289*7c478bd9Sstevel@tonic-gate (void) slprintf(str, sizeof(str), "IP:%I", htonl(addr));
290*7c478bd9Sstevel@tonic-gate return str;
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate
293*7c478bd9Sstevel@tonic-gate c = ':';
294*7c478bd9Sstevel@tonic-gate c2 = '.';
295*7c478bd9Sstevel@tonic-gate if (ep->class == EPD_MAC && ep->length == 6)
296*7c478bd9Sstevel@tonic-gate c2 = ':';
297*7c478bd9Sstevel@tonic-gate else if (ep->class == EPD_MAGIC && (ep->length % 4) == 0)
298*7c478bd9Sstevel@tonic-gate mask = 3;
299*7c478bd9Sstevel@tonic-gate q = str;
300*7c478bd9Sstevel@tonic-gate if (ep->class <= EPD_PHONENUM)
301*7c478bd9Sstevel@tonic-gate q += slprintf(q, sizeof(str)-1, "%s",
302*7c478bd9Sstevel@tonic-gate endp_class_names[ep->class]);
303*7c478bd9Sstevel@tonic-gate else
304*7c478bd9Sstevel@tonic-gate q += slprintf(q, sizeof(str)-1, "%d", ep->class);
305*7c478bd9Sstevel@tonic-gate c = ':';
306*7c478bd9Sstevel@tonic-gate for (i = 0; i < ep->length && i < MAX_ENDP_LEN; ++i) {
307*7c478bd9Sstevel@tonic-gate if ((i & mask) == 0) {
308*7c478bd9Sstevel@tonic-gate *q++ = c;
309*7c478bd9Sstevel@tonic-gate c = c2;
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate q += slprintf(q, str + sizeof(str) - q, "%.2x", ep->value[i]);
312*7c478bd9Sstevel@tonic-gate }
313*7c478bd9Sstevel@tonic-gate return str;
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate
hexc_val(int c)316*7c478bd9Sstevel@tonic-gate static int hexc_val(int c)
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate if (c >= 'a')
319*7c478bd9Sstevel@tonic-gate return c - 'a' + 10;
320*7c478bd9Sstevel@tonic-gate if (c >= 'A')
321*7c478bd9Sstevel@tonic-gate return c - 'A' + 10;
322*7c478bd9Sstevel@tonic-gate return c - '0';
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate
325*7c478bd9Sstevel@tonic-gate int
str_to_epdisc(ep,str)326*7c478bd9Sstevel@tonic-gate str_to_epdisc(ep, str)
327*7c478bd9Sstevel@tonic-gate struct epdisc *ep;
328*7c478bd9Sstevel@tonic-gate char *str;
329*7c478bd9Sstevel@tonic-gate {
330*7c478bd9Sstevel@tonic-gate int i, l;
331*7c478bd9Sstevel@tonic-gate char *p, *endp;
332*7c478bd9Sstevel@tonic-gate
333*7c478bd9Sstevel@tonic-gate for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
334*7c478bd9Sstevel@tonic-gate int sl = strlen(endp_class_names[i]);
335*7c478bd9Sstevel@tonic-gate if (strncasecmp(str, endp_class_names[i], sl) == 0) {
336*7c478bd9Sstevel@tonic-gate str += sl;
337*7c478bd9Sstevel@tonic-gate break;
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate }
340*7c478bd9Sstevel@tonic-gate if (i > EPD_PHONENUM) {
341*7c478bd9Sstevel@tonic-gate /* not a class name, try a decimal class number */
342*7c478bd9Sstevel@tonic-gate i = strtol(str, &endp, 10);
343*7c478bd9Sstevel@tonic-gate if (endp == str) {
344*7c478bd9Sstevel@tonic-gate option_error("cannot parse endpoint class in \"%s\"",
345*7c478bd9Sstevel@tonic-gate str);
346*7c478bd9Sstevel@tonic-gate return 0; /* can't parse class number */
347*7c478bd9Sstevel@tonic-gate }
348*7c478bd9Sstevel@tonic-gate str = endp;
349*7c478bd9Sstevel@tonic-gate }
350*7c478bd9Sstevel@tonic-gate ep->class = i;
351*7c478bd9Sstevel@tonic-gate if (*str == 0) {
352*7c478bd9Sstevel@tonic-gate ep->length = 0;
353*7c478bd9Sstevel@tonic-gate return 1;
354*7c478bd9Sstevel@tonic-gate }
355*7c478bd9Sstevel@tonic-gate if (*str != ':' && *str != '.') {
356*7c478bd9Sstevel@tonic-gate option_error("invalid class/value separator '%c'", *str);
357*7c478bd9Sstevel@tonic-gate return 0;
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate ++str;
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate if (i == EPD_IP) {
362*7c478bd9Sstevel@tonic-gate u_int32_t addr;
363*7c478bd9Sstevel@tonic-gate i = parse_dotted_ip(str, &addr);
364*7c478bd9Sstevel@tonic-gate if (i == 0 || str[i] != 0)
365*7c478bd9Sstevel@tonic-gate return 0;
366*7c478bd9Sstevel@tonic-gate set_ip_epdisc(ep, addr);
367*7c478bd9Sstevel@tonic-gate dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
368*7c478bd9Sstevel@tonic-gate return 1;
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate if (i == EPD_MAC &&
371*7c478bd9Sstevel@tonic-gate get_if_hwaddr(ep->value, sizeof(ep->value), str) >= 0) {
372*7c478bd9Sstevel@tonic-gate ep->length = 6;
373*7c478bd9Sstevel@tonic-gate dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
374*7c478bd9Sstevel@tonic-gate return 1;
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate
377*7c478bd9Sstevel@tonic-gate p = str;
378*7c478bd9Sstevel@tonic-gate for (l = 0; l < MAX_ENDP_LEN; ++l) {
379*7c478bd9Sstevel@tonic-gate if (*str == 0)
380*7c478bd9Sstevel@tonic-gate break;
381*7c478bd9Sstevel@tonic-gate if (p <= str)
382*7c478bd9Sstevel@tonic-gate for (p = str; isxdigit(*p); ++p)
383*7c478bd9Sstevel@tonic-gate ;
384*7c478bd9Sstevel@tonic-gate i = p - str;
385*7c478bd9Sstevel@tonic-gate if (i == 0) {
386*7c478bd9Sstevel@tonic-gate option_error("no valid hex digits in \"%s\"", str);
387*7c478bd9Sstevel@tonic-gate return 0;
388*7c478bd9Sstevel@tonic-gate }
389*7c478bd9Sstevel@tonic-gate ep->value[l] = hexc_val(*str++);
390*7c478bd9Sstevel@tonic-gate if ((i & 1) == 0)
391*7c478bd9Sstevel@tonic-gate ep->value[l] = (ep->value[l] << 4) + hexc_val(*str++);
392*7c478bd9Sstevel@tonic-gate if (*str == ':' || *str == '.')
393*7c478bd9Sstevel@tonic-gate ++str;
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate if (*str != 0) {
396*7c478bd9Sstevel@tonic-gate option_error("too many bytes in value; max is %d",
397*7c478bd9Sstevel@tonic-gate MAX_ENDP_LEN);
398*7c478bd9Sstevel@tonic-gate return 0;
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate if (ep->class == EPD_MAC && l != 6) {
401*7c478bd9Sstevel@tonic-gate option_error("bad endpoint; MAC address must have 6 bytes");
402*7c478bd9Sstevel@tonic-gate return 0;
403*7c478bd9Sstevel@tonic-gate }
404*7c478bd9Sstevel@tonic-gate ep->length = l;
405*7c478bd9Sstevel@tonic-gate dbglog("str_to_epdisc -> %s", epdisc_to_str(ep));
406*7c478bd9Sstevel@tonic-gate return 1;
407*7c478bd9Sstevel@tonic-gate }
408