sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libresolv2 / common / resolv / res_mkupdate.c
bloba32441dbd9167fc99095493ce8f7a2045c696579
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /*! \file
19 * \brief
20 * Based on the Dynamic DNS reference implementation by Viraj Bais
21 * <viraj_bais@ccm.fm.intel.com>
24 static const char rcsid[] = "$Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <netdb.h>
38 #include <resolv.h>
39 #include <res_update.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <ctype.h>
46 #include "port_after.h"
48 /* Options. Leave them on. */
49 #define DEBUG
50 #define MAXPORT 1024
52 static int getnum_str(u_char **, u_char *);
53 static int gethexnum_str(u_char **, u_char *);
54 static int getword_str(char *, int, u_char **, u_char *);
55 static int getstr_str(char *, int, u_char **, u_char *);
57 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
59 /* Forward. */
61 int res_protocolnumber(const char *);
62 int res_servicenumber(const char *);
64 /*%
65 * Form update packets.
66 * Returns the size of the resulting packet if no error
68 * On error,
69 * returns
70 *\li -1 if error in reading a word/number in rdata
71 * portion for update packets
72 *\li -2 if length of buffer passed is insufficient
73 *\li -3 if zone section is not the first section in
74 * the linked list, or section order has a problem
75 *\li -4 on a number overflow
76 *\li -5 unknown operation or no records
78 int
79 res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
80 ns_updrec *rrecp_start = rrecp_in;
81 HEADER *hp;
82 u_char *cp, *sp2, *startp, *endp;
83 int n, i, soanum, multiline;
84 ns_updrec *rrecp;
85 struct in_addr ina;
86 struct in6_addr in6a;
87 char buf2[MAXDNAME];
88 u_char buf3[MAXDNAME];
89 int section, numrrs = 0, counts[ns_s_max];
90 u_int16_t rtype, rclass;
91 u_int32_t n1, rttl;
92 u_char *dnptrs[20], **dpp, **lastdnptr;
93 int siglen, keylen, certlen;
96 * Initialize header fields.
98 if ((buf == NULL) || (buflen < HFIXEDSZ))
99 return (-1);
100 memset(buf, 0, HFIXEDSZ);
101 hp = (HEADER *) buf;
102 statp->id = res_nrandomid(statp);
103 hp->id = htons(statp->id);
104 hp->opcode = ns_o_update;
105 hp->rcode = NOERROR;
106 cp = buf + HFIXEDSZ;
107 buflen -= HFIXEDSZ;
108 dpp = dnptrs;
109 *dpp++ = buf;
110 *dpp++ = NULL;
111 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
113 if (rrecp_start == NULL)
114 return (-5);
115 else if (rrecp_start->r_section != S_ZONE)
116 return (-3);
118 memset(counts, 0, sizeof counts);
119 for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
120 numrrs++;
121 section = rrecp->r_section;
122 if (section < 0 || section >= ns_s_max)
123 return (-1);
124 counts[section]++;
125 for (i = section + 1; i < ns_s_max; i++)
126 if (counts[i])
127 return (-3);
128 rtype = rrecp->r_type;
129 rclass = rrecp->r_class;
130 rttl = rrecp->r_ttl;
131 /* overload class and type */
132 if (section == S_PREREQ) {
133 rttl = 0;
134 switch (rrecp->r_opcode) {
135 case YXDOMAIN:
136 rclass = C_ANY;
137 rtype = T_ANY;
138 rrecp->r_size = 0;
139 break;
140 case NXDOMAIN:
141 rclass = C_NONE;
142 rtype = T_ANY;
143 rrecp->r_size = 0;
144 break;
145 case NXRRSET:
146 rclass = C_NONE;
147 rrecp->r_size = 0;
148 break;
149 case YXRRSET:
150 if (rrecp->r_size == 0)
151 rclass = C_ANY;
152 break;
153 default:
154 fprintf(stderr,
155 "res_mkupdate: incorrect opcode: %d\n",
156 rrecp->r_opcode);
157 fflush(stderr);
158 return (-1);
160 } else if (section == S_UPDATE) {
161 switch (rrecp->r_opcode) {
162 case DELETE:
163 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
164 break;
165 case ADD:
166 break;
167 default:
168 fprintf(stderr,
169 "res_mkupdate: incorrect opcode: %d\n",
170 rrecp->r_opcode);
171 fflush(stderr);
172 return (-1);
177 * XXX appending default domain to owner name is omitted,
178 * fqdn must be provided
180 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
181 lastdnptr)) < 0)
182 return (-1);
183 cp += n;
184 ShrinkBuffer(n + 2*INT16SZ);
185 PUTSHORT(rtype, cp);
186 PUTSHORT(rclass, cp);
187 if (section == S_ZONE) {
188 if (numrrs != 1 || rrecp->r_type != T_SOA)
189 return (-3);
190 continue;
192 ShrinkBuffer(INT32SZ + INT16SZ);
193 PUTLONG(rttl, cp);
194 sp2 = cp; /*%< save pointer to length byte */
195 cp += INT16SZ;
196 if (rrecp->r_size == 0) {
197 if (section == S_UPDATE && rclass != C_ANY)
198 return (-1);
199 else {
200 PUTSHORT(0, sp2);
201 continue;
204 startp = rrecp->r_data;
205 endp = startp + rrecp->r_size - 1;
206 /* XXX this should be done centrally. */
207 switch (rrecp->r_type) {
208 case T_A:
209 if (!getword_str(buf2, sizeof buf2, &startp, endp))
210 return (-1);
211 if (!inet_aton(buf2, &ina))
212 return (-1);
213 n1 = ntohl(ina.s_addr);
214 ShrinkBuffer(INT32SZ);
215 PUTLONG(n1, cp);
216 break;
217 case T_CNAME:
218 case T_MB:
219 case T_MG:
220 case T_MR:
221 case T_NS:
222 case T_PTR:
223 case ns_t_dname:
224 if (!getword_str(buf2, sizeof buf2, &startp, endp))
225 return (-1);
226 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
227 if (n < 0)
228 return (-1);
229 cp += n;
230 ShrinkBuffer(n);
231 break;
232 case T_MINFO:
233 case T_SOA:
234 case T_RP:
235 for (i = 0; i < 2; i++) {
236 if (!getword_str(buf2, sizeof buf2, &startp,
237 endp))
238 return (-1);
239 n = dn_comp(buf2, cp, buflen,
240 dnptrs, lastdnptr);
241 if (n < 0)
242 return (-1);
243 cp += n;
244 ShrinkBuffer(n);
246 if (rrecp->r_type == T_SOA) {
247 ShrinkBuffer(5 * INT32SZ);
248 while (isspace(*startp) || !*startp)
249 startp++;
250 if (*startp == '(') {
251 multiline = 1;
252 startp++;
253 } else
254 multiline = 0;
255 /* serial, refresh, retry, expire, minimum */
256 for (i = 0; i < 5; i++) {
257 soanum = getnum_str(&startp, endp);
258 if (soanum < 0)
259 return (-1);
260 PUTLONG(soanum, cp);
262 if (multiline) {
263 while (isspace(*startp) || !*startp)
264 startp++;
265 if (*startp != ')')
266 return (-1);
269 break;
270 case T_MX:
271 case T_AFSDB:
272 case T_RT:
273 n = getnum_str(&startp, endp);
274 if (n < 0)
275 return (-1);
276 ShrinkBuffer(INT16SZ);
277 PUTSHORT(n, cp);
278 if (!getword_str(buf2, sizeof buf2, &startp, endp))
279 return (-1);
280 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
281 if (n < 0)
282 return (-1);
283 cp += n;
284 ShrinkBuffer(n);
285 break;
286 case T_SRV:
287 n = getnum_str(&startp, endp);
288 if (n < 0)
289 return (-1);
290 ShrinkBuffer(INT16SZ);
291 PUTSHORT(n, cp);
293 n = getnum_str(&startp, endp);
294 if (n < 0)
295 return (-1);
296 ShrinkBuffer(INT16SZ);
297 PUTSHORT(n, cp);
299 n = getnum_str(&startp, endp);
300 if (n < 0)
301 return (-1);
302 ShrinkBuffer(INT16SZ);
303 PUTSHORT(n, cp);
305 if (!getword_str(buf2, sizeof buf2, &startp, endp))
306 return (-1);
307 n = dn_comp(buf2, cp, buflen, NULL, NULL);
308 if (n < 0)
309 return (-1);
310 cp += n;
311 ShrinkBuffer(n);
312 break;
313 case T_PX:
314 n = getnum_str(&startp, endp);
315 if (n < 0)
316 return (-1);
317 PUTSHORT(n, cp);
318 ShrinkBuffer(INT16SZ);
319 for (i = 0; i < 2; i++) {
320 if (!getword_str(buf2, sizeof buf2, &startp,
321 endp))
322 return (-1);
323 n = dn_comp(buf2, cp, buflen, dnptrs,
324 lastdnptr);
325 if (n < 0)
326 return (-1);
327 cp += n;
328 ShrinkBuffer(n);
330 break;
331 case T_WKS: {
332 char bm[MAXPORT/8];
333 unsigned int maxbm = 0;
335 if (!getword_str(buf2, sizeof buf2, &startp, endp))
336 return (-1);
337 if (!inet_aton(buf2, &ina))
338 return (-1);
339 n1 = ntohl(ina.s_addr);
340 ShrinkBuffer(INT32SZ);
341 PUTLONG(n1, cp);
343 if (!getword_str(buf2, sizeof buf2, &startp, endp))
344 return (-1);
345 if ((i = res_protocolnumber(buf2)) < 0)
346 return (-1);
347 ShrinkBuffer(1);
348 *cp++ = i & 0xff;
350 for (i = 0; i < MAXPORT/8 ; i++)
351 bm[i] = 0;
353 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
354 if ((n = res_servicenumber(buf2)) <= 0)
355 return (-1);
357 if (n < MAXPORT) {
358 bm[n/8] |= (0x80>>(n%8));
359 if ((unsigned)n > maxbm)
360 maxbm = n;
361 } else
362 return (-1);
364 maxbm = maxbm/8 + 1;
365 ShrinkBuffer(maxbm);
366 memcpy(cp, bm, maxbm);
367 cp += maxbm;
368 break;
370 case T_HINFO:
371 for (i = 0; i < 2; i++) {
372 if ((n = getstr_str(buf2, sizeof buf2,
373 &startp, endp)) < 0)
374 return (-1);
375 if (n > 255)
376 return (-1);
377 ShrinkBuffer(n+1);
378 *cp++ = n;
379 memcpy(cp, buf2, n);
380 cp += n;
382 break;
383 case T_TXT:
384 for (;;) {
385 if ((n = getstr_str(buf2, sizeof buf2,
386 &startp, endp)) < 0) {
387 if (cp != (sp2 + INT16SZ))
388 break;
389 return (-1);
391 if (n > 255)
392 return (-1);
393 ShrinkBuffer(n+1);
394 *cp++ = n;
395 memcpy(cp, buf2, n);
396 cp += n;
398 break;
399 case T_X25:
400 /* RFC1183 */
401 if ((n = getstr_str(buf2, sizeof buf2, &startp,
402 endp)) < 0)
403 return (-1);
404 if (n > 255)
405 return (-1);
406 ShrinkBuffer(n+1);
407 *cp++ = n;
408 memcpy(cp, buf2, n);
409 cp += n;
410 break;
411 case T_ISDN:
412 /* RFC1183 */
413 if ((n = getstr_str(buf2, sizeof buf2, &startp,
414 endp)) < 0)
415 return (-1);
416 if ((n > 255) || (n == 0))
417 return (-1);
418 ShrinkBuffer(n+1);
419 *cp++ = n;
420 memcpy(cp, buf2, n);
421 cp += n;
422 if ((n = getstr_str(buf2, sizeof buf2, &startp,
423 endp)) < 0)
424 n = 0;
425 if (n > 255)
426 return (-1);
427 ShrinkBuffer(n+1);
428 *cp++ = n;
429 memcpy(cp, buf2, n);
430 cp += n;
431 break;
432 case T_NSAP:
433 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
434 ShrinkBuffer(n);
435 memcpy(cp, buf2, n);
436 cp += n;
437 } else {
438 return (-1);
440 break;
441 case T_LOC:
442 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
443 ShrinkBuffer(n);
444 memcpy(cp, buf2, n);
445 cp += n;
446 } else
447 return (-1);
448 break;
449 case ns_t_sig:
451 int sig_type, success, dateerror;
452 u_int32_t exptime, timesigned;
454 /* type */
455 if ((n = getword_str(buf2, sizeof buf2,
456 &startp, endp)) < 0)
457 return (-1);
458 sig_type = sym_ston(__p_type_syms, buf2, &success);
459 if (!success || sig_type == ns_t_any)
460 return (-1);
461 ShrinkBuffer(INT16SZ);
462 PUTSHORT(sig_type, cp);
463 /* alg */
464 n = getnum_str(&startp, endp);
465 if (n < 0)
466 return (-1);
467 ShrinkBuffer(1);
468 *cp++ = n;
469 /* labels */
470 n = getnum_str(&startp, endp);
471 if (n <= 0 || n > 255)
472 return (-1);
473 ShrinkBuffer(1);
474 *cp++ = n;
475 /* ottl & expire */
476 if (!getword_str(buf2, sizeof buf2, &startp, endp))
477 return (-1);
478 exptime = ns_datetosecs(buf2, &dateerror);
479 if (!dateerror) {
480 ShrinkBuffer(INT32SZ);
481 PUTLONG(rttl, cp);
483 else {
484 char *ulendp;
485 u_int32_t ottl;
487 errno = 0;
488 ottl = strtoul(buf2, &ulendp, 10);
489 if (errno != 0 ||
490 (ulendp != NULL && *ulendp != '\0'))
491 return (-1);
492 ShrinkBuffer(INT32SZ);
493 PUTLONG(ottl, cp);
494 if (!getword_str(buf2, sizeof buf2, &startp,
495 endp))
496 return (-1);
497 exptime = ns_datetosecs(buf2, &dateerror);
498 if (dateerror)
499 return (-1);
501 /* expire */
502 ShrinkBuffer(INT32SZ);
503 PUTLONG(exptime, cp);
504 /* timesigned */
505 if (!getword_str(buf2, sizeof buf2, &startp, endp))
506 return (-1);
507 timesigned = ns_datetosecs(buf2, &dateerror);
508 if (!dateerror) {
509 ShrinkBuffer(INT32SZ);
510 PUTLONG(timesigned, cp);
512 else
513 return (-1);
514 /* footprint */
515 n = getnum_str(&startp, endp);
516 if (n < 0)
517 return (-1);
518 ShrinkBuffer(INT16SZ);
519 PUTSHORT(n, cp);
520 /* signer name */
521 if (!getword_str(buf2, sizeof buf2, &startp, endp))
522 return (-1);
523 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
524 if (n < 0)
525 return (-1);
526 cp += n;
527 ShrinkBuffer(n);
528 /* sig */
529 if ((n = getword_str(buf2, sizeof buf2,
530 &startp, endp)) < 0)
531 return (-1);
532 siglen = b64_pton(buf2, buf3, sizeof(buf3));
533 if (siglen < 0)
534 return (-1);
535 ShrinkBuffer(siglen);
536 memcpy(cp, buf3, siglen);
537 cp += siglen;
538 break;
540 case ns_t_key:
541 /* flags */
542 n = gethexnum_str(&startp, endp);
543 if (n < 0)
544 return (-1);
545 ShrinkBuffer(INT16SZ);
546 PUTSHORT(n, cp);
547 /* proto */
548 n = getnum_str(&startp, endp);
549 if (n < 0)
550 return (-1);
551 ShrinkBuffer(1);
552 *cp++ = n;
553 /* alg */
554 n = getnum_str(&startp, endp);
555 if (n < 0)
556 return (-1);
557 ShrinkBuffer(1);
558 *cp++ = n;
559 /* key */
560 if ((n = getword_str(buf2, sizeof buf2,
561 &startp, endp)) < 0)
562 return (-1);
563 keylen = b64_pton(buf2, buf3, sizeof(buf3));
564 if (keylen < 0)
565 return (-1);
566 ShrinkBuffer(keylen);
567 memcpy(cp, buf3, keylen);
568 cp += keylen;
569 break;
570 case ns_t_nxt:
572 int success, nxt_type;
573 u_char data[32];
574 int maxtype;
576 /* next name */
577 if (!getword_str(buf2, sizeof buf2, &startp, endp))
578 return (-1);
579 n = dn_comp(buf2, cp, buflen, NULL, NULL);
580 if (n < 0)
581 return (-1);
582 cp += n;
583 ShrinkBuffer(n);
584 maxtype = 0;
585 memset(data, 0, sizeof data);
586 for (;;) {
587 if (!getword_str(buf2, sizeof buf2, &startp,
588 endp))
589 break;
590 nxt_type = sym_ston(__p_type_syms, buf2,
591 &success);
592 if (!success || !ns_t_rr_p(nxt_type))
593 return (-1);
594 NS_NXT_BIT_SET(nxt_type, data);
595 if (nxt_type > maxtype)
596 maxtype = nxt_type;
598 n = maxtype/NS_NXT_BITS+1;
599 ShrinkBuffer(n);
600 memcpy(cp, data, n);
601 cp += n;
602 break;
604 case ns_t_cert:
605 /* type */
606 n = getnum_str(&startp, endp);
607 if (n < 0)
608 return (-1);
609 ShrinkBuffer(INT16SZ);
610 PUTSHORT(n, cp);
611 /* key tag */
612 n = getnum_str(&startp, endp);
613 if (n < 0)
614 return (-1);
615 ShrinkBuffer(INT16SZ);
616 PUTSHORT(n, cp);
617 /* alg */
618 n = getnum_str(&startp, endp);
619 if (n < 0)
620 return (-1);
621 ShrinkBuffer(1);
622 *cp++ = n;
623 /* cert */
624 if ((n = getword_str(buf2, sizeof buf2,
625 &startp, endp)) < 0)
626 return (-1);
627 certlen = b64_pton(buf2, buf3, sizeof(buf3));
628 if (certlen < 0)
629 return (-1);
630 ShrinkBuffer(certlen);
631 memcpy(cp, buf3, certlen);
632 cp += certlen;
633 break;
634 case ns_t_aaaa:
635 if (!getword_str(buf2, sizeof buf2, &startp, endp))
636 return (-1);
637 if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
638 return (-1);
639 ShrinkBuffer(NS_IN6ADDRSZ);
640 memcpy(cp, &in6a, NS_IN6ADDRSZ);
641 cp += NS_IN6ADDRSZ;
642 break;
643 case ns_t_naptr:
644 /* Order Preference Flags Service Replacement Regexp */
645 /* Order */
646 n = getnum_str(&startp, endp);
647 if (n < 0 || n > 65535)
648 return (-1);
649 ShrinkBuffer(INT16SZ);
650 PUTSHORT(n, cp);
651 /* Preference */
652 n = getnum_str(&startp, endp);
653 if (n < 0 || n > 65535)
654 return (-1);
655 ShrinkBuffer(INT16SZ);
656 PUTSHORT(n, cp);
657 /* Flags */
658 if ((n = getstr_str(buf2, sizeof buf2,
659 &startp, endp)) < 0) {
660 return (-1);
662 if (n > 255)
663 return (-1);
664 ShrinkBuffer(n+1);
665 *cp++ = n;
666 memcpy(cp, buf2, n);
667 cp += n;
668 /* Service Classes */
669 if ((n = getstr_str(buf2, sizeof buf2,
670 &startp, endp)) < 0) {
671 return (-1);
673 if (n > 255)
674 return (-1);
675 ShrinkBuffer(n+1);
676 *cp++ = n;
677 memcpy(cp, buf2, n);
678 cp += n;
679 /* Pattern */
680 if ((n = getstr_str(buf2, sizeof buf2,
681 &startp, endp)) < 0) {
682 return (-1);
684 if (n > 255)
685 return (-1);
686 ShrinkBuffer(n+1);
687 *cp++ = n;
688 memcpy(cp, buf2, n);
689 cp += n;
690 /* Replacement */
691 if (!getword_str(buf2, sizeof buf2, &startp, endp))
692 return (-1);
693 n = dn_comp(buf2, cp, buflen, NULL, NULL);
694 if (n < 0)
695 return (-1);
696 cp += n;
697 ShrinkBuffer(n);
698 break;
699 default:
700 return (-1);
701 } /*switch*/
702 n = (u_int16_t)((cp - sp2) - INT16SZ);
703 PUTSHORT(n, sp2);
704 } /*for*/
706 hp->qdcount = htons(counts[0]);
707 hp->ancount = htons(counts[1]);
708 hp->nscount = htons(counts[2]);
709 hp->arcount = htons(counts[3]);
710 return (cp - buf);
714 * Get a whitespace delimited word from a string (not file)
715 * into buf. modify the start pointer to point after the
716 * word in the string.
718 static int
719 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
720 char *cp;
721 int c;
723 for (cp = buf; *startpp <= endp; ) {
724 c = **startpp;
725 if (isspace(c) || c == '\0') {
726 if (cp != buf) /*%< trailing whitespace */
727 break;
728 else { /*%< leading whitespace */
729 (*startpp)++;
730 continue;
733 (*startpp)++;
734 if (cp >= buf+size-1)
735 break;
736 *cp++ = (u_char)c;
738 *cp = '\0';
739 return (cp != buf);
743 * get a white spae delimited string from memory. Process quoted strings
744 * and \\DDD escapes. Return length or -1 on error. Returned string may
745 * contain nulls.
747 static char digits[] = "0123456789";
748 static int
749 getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
750 char *cp;
751 int c, c1 = 0;
752 int inquote = 0;
753 int seen_quote = 0;
754 int escape = 0;
755 int dig = 0;
757 for (cp = buf; *startpp <= endp; ) {
758 if ((c = **startpp) == '\0')
759 break;
760 /* leading white space */
761 if ((cp == buf) && !seen_quote && isspace(c)) {
762 (*startpp)++;
763 continue;
766 switch (c) {
767 case '\\':
768 if (!escape) {
769 escape = 1;
770 dig = 0;
771 c1 = 0;
772 (*startpp)++;
773 continue;
775 goto do_escape;
776 case '"':
777 if (!escape) {
778 inquote = !inquote;
779 seen_quote = 1;
780 (*startpp)++;
781 continue;
783 /* fall through */
784 default:
785 do_escape:
786 if (escape) {
787 switch (c) {
788 case '0':
789 case '1':
790 case '2':
791 case '3':
792 case '4':
793 case '5':
794 case '6':
795 case '7':
796 case '8':
797 case '9':
798 c1 = c1 * 10 +
799 (strchr(digits, c) - digits);
801 if (++dig == 3) {
802 c = c1 &0xff;
803 break;
805 (*startpp)++;
806 continue;
808 escape = 0;
809 } else if (!inquote && isspace(c))
810 goto done;
811 if (cp >= buf+size-1)
812 goto done;
813 *cp++ = (u_char)c;
814 (*startpp)++;
817 done:
818 *cp = '\0';
819 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
823 * Get a whitespace delimited base 16 number from a string (not file) into buf
824 * update the start pointer to point after the number in the string.
826 static int
827 gethexnum_str(u_char **startpp, u_char *endp) {
828 int c, n;
829 int seendigit = 0;
830 int m = 0;
832 if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
833 return getnum_str(startpp, endp);
834 (*startpp)+=2;
835 for (n = 0; *startpp <= endp; ) {
836 c = **startpp;
837 if (isspace(c) || c == '\0') {
838 if (seendigit) /*%< trailing whitespace */
839 break;
840 else { /*%< leading whitespace */
841 (*startpp)++;
842 continue;
845 if (c == ';') {
846 while ((*startpp <= endp) &&
847 ((c = **startpp) != '\n'))
848 (*startpp)++;
849 if (seendigit)
850 break;
851 continue;
853 if (!isxdigit(c)) {
854 if (c == ')' && seendigit) {
855 (*startpp)--;
856 break;
858 return (-1);
860 (*startpp)++;
861 if (isdigit(c))
862 n = n * 16 + (c - '0');
863 else
864 n = n * 16 + (tolower(c) - 'a' + 10);
865 seendigit = 1;
867 return (n + m);
871 * Get a whitespace delimited base 10 number from a string (not file) into buf
872 * update the start pointer to point after the number in the string.
874 static int
875 getnum_str(u_char **startpp, u_char *endp) {
876 int c, n;
877 int seendigit = 0;
878 int m = 0;
880 for (n = 0; *startpp <= endp; ) {
881 c = **startpp;
882 if (isspace(c) || c == '\0') {
883 if (seendigit) /*%< trailing whitespace */
884 break;
885 else { /*%< leading whitespace */
886 (*startpp)++;
887 continue;
890 if (c == ';') {
891 while ((*startpp <= endp) &&
892 ((c = **startpp) != '\n'))
893 (*startpp)++;
894 if (seendigit)
895 break;
896 continue;
898 if (!isdigit(c)) {
899 if (c == ')' && seendigit) {
900 (*startpp)--;
901 break;
903 return (-1);
905 (*startpp)++;
906 n = n * 10 + (c - '0');
907 seendigit = 1;
909 return (n + m);
913 * Allocate a resource record buffer & save rr info.
915 ns_updrec *
916 res_mkupdrec(int section, const char *dname,
917 u_int class, u_int type, u_long ttl) {
918 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
920 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
921 if (rrecp)
922 free((char *)rrecp);
923 return (NULL);
925 INIT_LINK(rrecp, r_link);
926 INIT_LINK(rrecp, r_glink);
927 rrecp->r_class = (ns_class)class;
928 rrecp->r_type = (ns_type)type;
929 rrecp->r_ttl = ttl;
930 rrecp->r_section = (ns_sect)section;
931 return (rrecp);
935 * Free a resource record buffer created by res_mkupdrec.
937 void
938 res_freeupdrec(ns_updrec *rrecp) {
939 /* Note: freeing r_dp is the caller's responsibility. */
940 free(rrecp->r_dname);
941 free(rrecp);
944 struct valuelist {
945 struct valuelist * next;
946 struct valuelist * prev;
947 char * name;
948 char * proto;
949 int port;
951 static struct valuelist *servicelist, *protolist;
953 static void
954 res_buildservicelist() {
955 struct servent *sp;
956 struct valuelist *slp;
958 #ifdef MAYBE_HESIOD
959 setservent(0);
960 #else
961 setservent(1);
962 #endif
963 while ((sp = getservent()) != NULL) {
964 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
965 if (!slp)
966 break;
967 slp->name = strdup(sp->s_name);
968 slp->proto = strdup(sp->s_proto);
969 if ((slp->name == NULL) || (slp->proto == NULL)) {
970 free(slp->name);
971 free(slp->proto);
972 free(slp);
973 break;
975 slp->port = ntohs((u_int16_t)sp->s_port); /*%< host byt order */
976 slp->next = servicelist;
977 slp->prev = NULL;
978 if (servicelist)
979 servicelist->prev = slp;
980 servicelist = slp;
982 endservent();
985 void
986 res_destroyservicelist() {
987 struct valuelist *slp, *slp_next;
989 for (slp = servicelist; slp != NULL; slp = slp_next) {
990 slp_next = slp->next;
991 free(slp->name);
992 free(slp->proto);
993 free(slp);
995 servicelist = NULL;
998 void
999 res_buildprotolist(void) {
1000 struct protoent *pp;
1001 struct valuelist *slp;
1003 #ifdef MAYBE_HESIOD
1004 setprotoent(0);
1005 #else
1006 setprotoent(1);
1007 #endif
1008 while ((pp = getprotoent()) != NULL) {
1009 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
1010 if (!slp)
1011 break;
1012 slp->name = strdup(pp->p_name);
1013 if (slp->name == NULL) {
1014 free(slp);
1015 break;
1017 slp->port = pp->p_proto; /*%< host byte order */
1018 slp->next = protolist;
1019 slp->prev = NULL;
1020 if (protolist)
1021 protolist->prev = slp;
1022 protolist = slp;
1024 endprotoent();
1027 void
1028 res_destroyprotolist(void) {
1029 struct valuelist *plp, *plp_next;
1031 for (plp = protolist; plp != NULL; plp = plp_next) {
1032 plp_next = plp->next;
1033 free(plp->name);
1034 free(plp);
1036 protolist = NULL;
1039 static int
1040 findservice(const char *s, struct valuelist **list) {
1041 struct valuelist *lp = *list;
1042 int n;
1044 for (; lp != NULL; lp = lp->next)
1045 if (strcasecmp(lp->name, s) == 0) {
1046 if (lp != *list) {
1047 lp->prev->next = lp->next;
1048 if (lp->next)
1049 lp->next->prev = lp->prev;
1050 (*list)->prev = lp;
1051 lp->next = *list;
1052 *list = lp;
1054 return (lp->port); /*%< host byte order */
1056 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1057 n = -1;
1058 return (n);
1062 * Convert service name or (ascii) number to int.
1065 res_servicenumber(const char *p) {
1066 if (servicelist == NULL)
1067 res_buildservicelist();
1068 return (findservice(p, &servicelist));
1072 * Convert protocol name or (ascii) number to int.
1075 res_protocolnumber(const char *p) {
1076 if (protolist == NULL)
1077 res_buildprotolist();
1078 return (findservice(p, &protolist));
1081 static struct servent *
1082 cgetservbyport(u_int16_t port, const char *proto) { /*%< Host byte order. */
1083 struct valuelist **list = &servicelist;
1084 struct valuelist *lp = *list;
1085 static struct servent serv;
1087 port = ntohs(port);
1088 for (; lp != NULL; lp = lp->next) {
1089 if (port != (u_int16_t)lp->port) /*%< Host byte order. */
1090 continue;
1091 if (strcasecmp(lp->proto, proto) == 0) {
1092 if (lp != *list) {
1093 lp->prev->next = lp->next;
1094 if (lp->next)
1095 lp->next->prev = lp->prev;
1096 (*list)->prev = lp;
1097 lp->next = *list;
1098 *list = lp;
1100 serv.s_name = lp->name;
1101 serv.s_port = htons((u_int16_t)lp->port);
1102 serv.s_proto = lp->proto;
1103 return (&serv);
1106 return (0);
1109 static struct protoent *
1110 cgetprotobynumber(int proto) { /*%< Host byte order. */
1111 struct valuelist **list = &protolist;
1112 struct valuelist *lp = *list;
1113 static struct protoent prot;
1115 for (; lp != NULL; lp = lp->next)
1116 if (lp->port == proto) { /*%< Host byte order. */
1117 if (lp != *list) {
1118 lp->prev->next = lp->next;
1119 if (lp->next)
1120 lp->next->prev = lp->prev;
1121 (*list)->prev = lp;
1122 lp->next = *list;
1123 *list = lp;
1125 prot.p_name = lp->name;
1126 prot.p_proto = lp->port; /*%< Host byte order. */
1127 return (&prot);
1129 return (0);
1132 const char *
1133 res_protocolname(int num) {
1134 static char number[8];
1135 struct protoent *pp;
1137 if (protolist == NULL)
1138 res_buildprotolist();
1139 pp = cgetprotobynumber(num);
1140 if (pp == 0) {
1141 (void) sprintf(number, "%d", num);
1142 return (number);
1144 return (pp->p_name);
1147 const char *
1148 res_servicename(u_int16_t port, const char *proto) { /*%< Host byte order. */
1149 static char number[8];
1150 struct servent *ss;
1152 if (servicelist == NULL)
1153 res_buildservicelist();
1154 ss = cgetservbyport(htons(port), proto);
1155 if (ss == 0) {
1156 (void) sprintf(number, "%d", port);
1157 return (number);
1159 return (ss->s_name);