tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / minires / res_mkupdate.c
blob14aff556459931fd22e8f2e463dc920ef64b7823
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-2003 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.
17 * Internet Systems Consortium, Inc.
18 * 950 Charter Street
19 * Redwood City, CA 94063
20 * <info@isc.org>
21 * http://www.isc.org/
25 * Based on the Dynamic DNS reference implementation by Viraj Bais
26 * <viraj_bais@ccm.fm.intel.com>
29 #if !defined(lint) && !defined(SABER) && !defined(__AROS__)
30 static const char rcsid[] = "$Id$";
31 #endif /* not lint */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <sys/socket.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <ctype.h>
49 #include "minires/minires.h"
50 #include "arpa/nameser.h"
52 /* Options. Leave them on. */
53 #define DEBUG
54 #define MAXPORT 1024
56 static int getnum_str(const u_char **, const u_char *);
57 static int gethexnum_str(const u_char **, const u_char *);
58 static int getword_str(char *, int,
59 const unsigned char **,
60 const unsigned char *);
61 static int getphrase_str(char *, int, const u_char **, const u_char *);
62 static int getstr_str(char *, int, const u_char **, const u_char *);
64 struct valuelist {
65 struct valuelist * next;
66 struct valuelist * prev;
67 char * name;
68 char * proto;
69 int port;
72 static int findservice(const char *, struct valuelist **);
73 static struct servent *cgetservbyport(unsigned, const char *);
75 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
77 /* Forward. */
79 int res_protocolnumber(const char *);
80 int res_servicenumber(const char *);
81 static struct protoent *cgetprotobynumber(int);
84 * Form update packets.
85 * Returns the size of the resulting packet if no error
86 * On error,
87 * returns -1 if error in reading a word/number in rdata
88 * portion for update packets
89 * -2 if length of buffer passed is insufficient
90 * -3 if zone section is not the first section in
91 * the linked list, or section order has a problem
92 * -4 on a number overflow
93 * -5 unknown operation or no records
95 int
96 res_nmkupdate(res_state statp,
97 ns_updrec *rrecp_in, double *bp, unsigned *blp) {
98 ns_updrec *rrecp_start = rrecp_in;
99 HEADER *hp;
100 u_char *cp, *sp2;
101 const unsigned char *startp, *endp;
102 int n, i, soanum, multiline;
103 ns_updrec *rrecp;
104 struct in_addr ina;
105 char buf2[MAXDNAME];
106 u_char buf3[MAXDNAME];
107 int section, numrrs = 0, counts[ns_s_max];
108 u_int16_t rtype, rclass;
109 u_int32_t n1, rttl;
110 u_char *dnptrs[20], **dpp, **lastdnptr;
111 unsigned certlen;
112 __unused unsigned siglen;
113 int keylen;
114 unsigned buflen = *blp;
115 u_char *buf = (unsigned char *)bp;
118 * Initialize header fields.
120 if ((buf == NULL) || (buflen < HFIXEDSZ))
121 return -1;
122 memset(buf, 0, HFIXEDSZ);
123 hp = (HEADER *) buf;
124 hp->id = htons(++statp->id);
125 hp->opcode = ns_o_update;
126 hp->rcode = NOERROR;
127 cp = buf + HFIXEDSZ;
128 buflen -= HFIXEDSZ;
129 dpp = dnptrs;
130 *dpp++ = buf;
131 *dpp++ = NULL;
132 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
134 if (rrecp_start == NULL)
135 return (-5);
136 else if (rrecp_start->r_section != S_ZONE)
137 return (-3);
139 memset(counts, 0, sizeof counts);
140 for (rrecp = rrecp_start; rrecp; rrecp = ISC_LIST_NEXT(rrecp,
141 r_glink)) {
142 numrrs++;
143 section = rrecp->r_section;
144 if (section < 0 || section >= ns_s_max)
145 return (-1);
146 counts[section]++;
147 for (i = section + 1; i < ns_s_max; i++)
148 if (counts[i])
149 return (-3);
150 rtype = rrecp->r_type;
151 rclass = rrecp->r_class;
152 rttl = rrecp->r_ttl;
153 /* overload class and type */
154 if (section == S_PREREQ) {
155 rttl = 0;
156 switch (rrecp->r_opcode) {
157 case YXDOMAIN:
158 rclass = C_ANY;
159 rtype = T_ANY;
160 rrecp->r_size = 0;
161 break;
162 case NXDOMAIN:
163 rclass = C_NONE;
164 rtype = T_ANY;
165 rrecp->r_size = 0;
166 break;
167 case NXRRSET:
168 rclass = C_NONE;
169 rrecp->r_size = 0;
170 break;
171 case YXRRSET:
172 if (rrecp->r_size == 0)
173 rclass = C_ANY;
174 break;
175 default:
176 fprintf(stderr,
177 "res_mkupdate: incorrect opcode: %d\n",
178 rrecp->r_opcode);
179 fflush(stderr);
180 return (-1);
182 } else if (section == S_UPDATE) {
183 switch (rrecp->r_opcode) {
184 case DELETE:
185 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
186 break;
187 case ADD:
188 break;
189 default:
190 fprintf(stderr,
191 "res_mkupdate: incorrect opcode: %d\n",
192 rrecp->r_opcode);
193 fflush(stderr);
194 return (-1);
199 * XXX appending default domain to owner name is omitted,
200 * fqdn must be provided
202 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
203 lastdnptr)) < 0)
204 return (-1);
205 cp += n;
206 ShrinkBuffer(n + 2*INT16SZ);
207 PUTSHORT(rtype, cp);
208 PUTSHORT(rclass, cp);
209 if (section == S_ZONE) {
210 if (numrrs != 1 || rrecp->r_type != T_SOA)
211 return (-3);
212 continue;
214 ShrinkBuffer(INT32SZ + INT16SZ);
215 PUTLONG(rttl, cp);
216 sp2 = cp; /* save pointer to length byte */
217 cp += INT16SZ;
218 if (rrecp->r_size == 0) {
219 if (section == S_UPDATE && rclass != C_ANY)
220 return (-1);
221 else {
222 PUTSHORT(0, sp2);
223 continue;
226 startp = rrecp->r_data;
227 endp = startp + rrecp->r_size - 1;
228 /* XXX this should be done centrally. */
229 switch (rrecp->r_type) {
230 case T_A:
231 if (!getword_str(buf2, sizeof buf2, &startp, endp))
232 return (-1);
233 if (!inet_aton(buf2, &ina))
234 return (-1);
235 n1 = ntohl(ina.s_addr);
236 ShrinkBuffer(INT32SZ);
237 PUTLONG(n1, cp);
238 break;
239 case T_CNAME:
240 case T_MB:
241 case T_MG:
242 case T_MR:
243 case T_NS:
244 case T_PTR:
245 if (!getphrase_str(buf2, sizeof buf2, &startp, endp))
246 return (-1);
247 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
248 if (n < 0)
249 return (-1);
250 cp += n;
251 ShrinkBuffer(n);
252 break;
253 case T_MINFO:
254 case T_SOA:
255 case T_RP:
256 for (i = 0; i < 2; i++) {
257 if (!getword_str(buf2, sizeof buf2, &startp,
258 endp))
259 return (-1);
260 n = dn_comp(buf2, cp, buflen,
261 dnptrs, lastdnptr);
262 if (n < 0)
263 return (-1);
264 cp += n;
265 ShrinkBuffer(n);
267 if (rrecp->r_type == T_SOA) {
268 ShrinkBuffer(5 * INT32SZ);
269 while (isspace(*startp) || !*startp)
270 startp++;
271 if (*startp == '(') {
272 multiline = 1;
273 startp++;
274 } else
275 multiline = 0;
276 /* serial, refresh, retry, expire, minimum */
277 for (i = 0; i < 5; i++) {
278 soanum = getnum_str(&startp, endp);
279 if (soanum < 0)
280 return (-1);
281 PUTLONG(soanum, cp);
283 if (multiline) {
284 while (isspace(*startp) || !*startp)
285 startp++;
286 if (*startp != ')')
287 return (-1);
290 break;
291 case T_MX:
292 case T_AFSDB:
293 case T_RT:
294 n = getnum_str(&startp, endp);
295 if (n < 0)
296 return (-1);
297 ShrinkBuffer(INT16SZ);
298 PUTSHORT(n, cp);
299 if (!getword_str(buf2, sizeof buf2, &startp, endp))
300 return (-1);
301 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
302 if (n < 0)
303 return (-1);
304 cp += n;
305 ShrinkBuffer(n);
306 break;
307 case T_SRV:
308 n = getnum_str(&startp, endp);
309 if (n < 0)
310 return (-1);
311 ShrinkBuffer(INT16SZ);
312 PUTSHORT(n, cp);
314 n = getnum_str(&startp, endp);
315 if (n < 0)
316 return (-1);
317 ShrinkBuffer(INT16SZ);
318 PUTSHORT(n, cp);
320 n = getnum_str(&startp, endp);
321 if (n < 0)
322 return (-1);
323 ShrinkBuffer(INT16SZ);
324 PUTSHORT(n, cp);
326 if (!getword_str(buf2, sizeof buf2, &startp, endp))
327 return (-1);
328 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
329 if (n < 0)
330 return (-1);
331 cp += n;
332 ShrinkBuffer(n);
333 break;
334 case T_PX:
335 n = getnum_str(&startp, endp);
336 if (n < 0)
337 return (-1);
338 PUTSHORT(n, cp);
339 ShrinkBuffer(INT16SZ);
340 for (i = 0; i < 2; i++) {
341 if (!getword_str(buf2, sizeof buf2, &startp,
342 endp))
343 return (-1);
344 n = dn_comp(buf2, cp, buflen, dnptrs,
345 lastdnptr);
346 if (n < 0)
347 return (-1);
348 cp += n;
349 ShrinkBuffer(n);
351 break;
352 case T_WKS: {
353 char bm[MAXPORT/8];
354 unsigned maxbm = 0;
356 if (!getword_str(buf2, sizeof buf2, &startp, endp))
357 return (-1);
358 if (!inet_aton(buf2, &ina))
359 return (-1);
360 n1 = ntohl(ina.s_addr);
361 ShrinkBuffer(INT32SZ);
362 PUTLONG(n1, cp);
364 if (!getword_str(buf2, sizeof buf2, &startp, endp))
365 return (-1);
366 if ((i = res_protocolnumber(buf2)) < 0)
367 return (-1);
368 ShrinkBuffer(1);
369 *cp++ = i & 0xff;
371 for (i = 0; i < MAXPORT/8 ; i++)
372 bm[i] = 0;
374 while (getword_str(buf2, sizeof buf2, &startp, endp)) {
375 if ((n1 = res_servicenumber(buf2)) <= 0)
376 return (-1);
378 if (n1 < MAXPORT) {
379 bm[n1/8] |= (0x80>>(n1%8));
380 if (n1 > maxbm)
381 maxbm = n1;
382 } else
383 return (-1);
385 maxbm = maxbm/8 + 1;
386 ShrinkBuffer(maxbm);
387 memcpy(cp, bm, maxbm);
388 cp += maxbm;
389 break;
391 case T_HINFO:
392 for (i = 0; i < 2; i++) {
393 if ((n = getstr_str(buf2, sizeof buf2,
394 &startp, endp)) < 0)
395 return (-1);
396 if (n > 255)
397 return (-1);
398 ShrinkBuffer(n+1);
399 *cp++ = n;
400 memcpy(cp, buf2, (unsigned)n);
401 cp += n;
403 break;
404 case T_TXT:
405 while (1) {
406 if ((n = getstr_str(buf2, sizeof buf2,
407 &startp, endp)) < 0) {
408 if (cp != (sp2 + INT16SZ))
409 break;
410 return (-1);
412 if (n > 255)
413 return (-1);
414 ShrinkBuffer(n+1);
415 *cp++ = n;
416 memcpy(cp, buf2, (unsigned)n);
417 cp += n;
419 break;
420 case T_X25:
421 /* RFC 1183 */
422 if ((n = getstr_str(buf2, sizeof buf2, &startp,
423 endp)) < 0)
424 return (-1);
425 if (n > 255)
426 return (-1);
427 ShrinkBuffer(n+1);
428 *cp++ = n;
429 memcpy(cp, buf2, (unsigned)n);
430 cp += n;
431 break;
432 case T_ISDN:
433 /* RFC 1183 */
434 if ((n = getstr_str(buf2, sizeof buf2, &startp,
435 endp)) < 0)
436 return (-1);
437 if ((n > 255) || (n == 0))
438 return (-1);
439 ShrinkBuffer(n+1);
440 *cp++ = n;
441 memcpy(cp, buf2, (unsigned)n);
442 cp += n;
443 if ((n = getstr_str(buf2, sizeof buf2, &startp,
444 endp)) < 0)
445 n = 0;
446 if (n > 255)
447 return (-1);
448 ShrinkBuffer(n+1);
449 *cp++ = n;
450 memcpy(cp, buf2, (unsigned)n);
451 cp += n;
452 break;
453 #if 0
454 case T_NSAP:
455 if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
456 ShrinkBuffer(n);
457 memcpy(cp, buf2, n);
458 cp += n;
459 } else {
460 return (-1);
462 break;
463 case T_LOC:
464 if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
465 ShrinkBuffer(n);
466 memcpy(cp, buf2, n);
467 cp += n;
468 } else
469 return (-1);
470 break;
471 case ns_t_sig:
473 int sig_type, success, dateerror;
474 u_int32_t exptime, timesigned;
476 /* type */
477 if ((n = getword_str(buf2, sizeof buf2,
478 &startp, endp)) < 0)
479 return (-1);
480 sig_type = sym_ston(__p_type_syms, buf2, &success);
481 if (!success || sig_type == ns_t_any)
482 return (-1);
483 ShrinkBuffer(INT16SZ);
484 PUTSHORT(sig_type, cp);
485 /* alg */
486 n = getnum_str(&startp, endp);
487 if (n < 0)
488 return (-1);
489 ShrinkBuffer(1);
490 *cp++ = n;
491 /* labels */
492 n = getnum_str(&startp, endp);
493 if (n <= 0 || n > 255)
494 return (-1);
495 ShrinkBuffer(1);
496 *cp++ = n;
497 /* ottl & expire */
498 if (!getword_str(buf2, sizeof buf2, &startp, endp))
499 return (-1);
500 exptime = ns_datetosecs(buf2, &dateerror);
501 if (!dateerror) {
502 ShrinkBuffer(INT32SZ);
503 PUTLONG(rttl, cp);
505 else {
506 char *ulendp;
507 u_int32_t ottl;
509 ottl = strtoul(buf2, &ulendp, 10);
510 if (ulendp != NULL && *ulendp != '\0')
511 return (-1);
512 ShrinkBuffer(INT32SZ);
513 PUTLONG(ottl, cp);
514 if (!getword_str(buf2, sizeof buf2, &startp,
515 endp))
516 return (-1);
517 exptime = ns_datetosecs(buf2, &dateerror);
518 if (dateerror)
519 return (-1);
521 /* expire */
522 ShrinkBuffer(INT32SZ);
523 PUTLONG(exptime, cp);
524 /* timesigned */
525 if (!getword_str(buf2, sizeof buf2, &startp, endp))
526 return (-1);
527 timesigned = ns_datetosecs(buf2, &dateerror);
528 if (!dateerror) {
529 ShrinkBuffer(INT32SZ);
530 PUTLONG(timesigned, cp);
532 else
533 return (-1);
534 /* footprint */
535 n = getnum_str(&startp, endp);
536 if (n < 0)
537 return (-1);
538 ShrinkBuffer(INT16SZ);
539 PUTSHORT(n, cp);
540 /* signer name */
541 if (!getword_str(buf2, sizeof buf2, &startp, endp))
542 return (-1);
543 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
544 if (n < 0)
545 return (-1);
546 cp += n;
547 ShrinkBuffer(n);
548 /* sig */
549 if ((n = getword_str(buf2, sizeof buf2,
550 &startp, endp)) < 0)
551 return (-1);
552 siglen = b64_pton(buf2, buf3, sizeof(buf3));
553 if (siglen < 0)
554 return (-1);
555 ShrinkBuffer(siglen);
556 memcpy(cp, buf3, siglen);
557 cp += siglen;
558 break;
560 case ns_t_nxt:
562 int success, nxt_type;
563 u_char data[32];
564 int maxtype;
566 /* next name */
567 if (!getword_str(buf2, sizeof buf2, &startp, endp))
568 return (-1);
569 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
570 if (n < 0)
571 return (-1);
572 cp += n;
573 ShrinkBuffer(n);
574 maxtype = 0;
575 memset(data, 0, sizeof data);
576 while (1) {
577 if (!getword_str(buf2, sizeof buf2, &startp,
578 endp))
579 break;
580 nxt_type = sym_ston(__p_type_syms, buf2,
581 &success);
582 if (!success || !ns_t_rr_p(nxt_type))
583 return (-1);
584 NS_NXT_BIT_SET(nxt_type, data);
585 if (nxt_type > maxtype)
586 maxtype = nxt_type;
588 n = maxtype/NS_NXT_BITS+1;
589 ShrinkBuffer(n);
590 memcpy(cp, data, n);
591 cp += n;
592 break;
594 #endif
595 #if 1
596 case ns_t_key:
597 /* flags */
598 n = gethexnum_str(&startp, endp);
599 if (n < 0)
600 return (-1);
601 ShrinkBuffer(INT16SZ);
602 PUTSHORT(n, cp);
603 /* proto */
604 n = getnum_str(&startp, endp);
605 if (n < 0)
606 return (-1);
607 ShrinkBuffer(1);
608 *cp++ = n;
609 /* alg */
610 n = getnum_str(&startp, endp);
611 if (n < 0)
612 return (-1);
613 ShrinkBuffer(1);
614 *cp++ = n;
615 /* key */
616 if ((n = getword_str(buf2, sizeof buf2,
617 &startp, endp)) < 0)
618 return (-1);
619 keylen = b64_pton(buf2, buf3, sizeof(buf3));
620 if (keylen < 0)
621 return (-1);
622 ShrinkBuffer(keylen);
623 memcpy(cp, buf3, keylen);
624 cp += keylen;
625 break;
626 case ns_t_cert:
627 /* type */
628 n = getnum_str(&startp, endp);
629 if (n < 0)
630 return (-1);
631 ShrinkBuffer(INT16SZ);
632 PUTSHORT(n, cp);
633 /* key tag */
634 n = getnum_str(&startp, endp);
635 if (n < 0)
636 return (-1);
637 ShrinkBuffer(INT16SZ);
638 PUTSHORT(n, cp);
639 /* alg */
640 n = getnum_str(&startp, endp);
641 if (n < 0)
642 return (-1);
643 ShrinkBuffer(1);
644 *cp++ = n;
645 /* cert */
646 if ((n = getword_str(buf2, sizeof buf2,
647 &startp, endp)) < 0)
648 return (-1);
649 certlen = b64_pton(buf2, buf3, sizeof(buf3));
650 if (certlen < 0)
651 return (-1);
652 ShrinkBuffer(certlen);
653 memcpy(cp, buf3, certlen);
654 cp += certlen;
655 break;
656 #endif
657 default:
658 fprintf(stderr, "NSupdate of RR type: %d not implemented\n",
659 rrecp->r_type);
660 return (-1);
661 } /*switch*/
662 n = (u_int16_t)((cp - sp2) - INT16SZ);
663 PUTSHORT(n, sp2);
664 } /*for*/
666 hp->qdcount = htons(counts[0]);
667 hp->ancount = htons(counts[1]);
668 hp->nscount = htons(counts[2]);
669 hp->arcount = htons(counts[3]);
670 *blp = cp - buf;
671 return 0;
675 * Get a whitespace delimited word from a string (not file)
676 * into buf. modify the start pointer to point after the
677 * word in the string.
679 static int
680 getword_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
681 char *cp;
682 int c;
684 for (cp = buf; *startpp <= endp; ) {
685 c = **startpp;
686 if (isspace(c) || c == '\0') {
687 if (cp != buf) /* trailing whitespace */
688 break;
689 else { /* leading whitespace */
690 (*startpp)++;
691 continue;
694 (*startpp)++;
695 if (cp >= buf+size-1)
696 break;
697 *cp++ = (u_char)c;
699 *cp = '\0';
700 return (cp != buf);
704 * Get a phrase - possibly containing blanks - from a string (not file)
705 * into buf. modify the start pointer to point after the
706 * phrase in the string.
708 static int
709 getphrase_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
710 char *cp;
711 int c;
713 for (cp = buf; *startpp <= endp; ) {
714 c = **startpp;
715 if (isspace(c) && cp == buf ) {
716 /* leading whitespace */
717 (*startpp)++;
718 continue;
720 else if ( c == '\0' ) {
721 break;
723 (*startpp)++;
724 if (cp >= buf+size-1)
725 break;
726 *cp++ = (u_char)c;
728 *cp = '\0';
729 return (cp != buf);
733 * get a white spae delimited string from memory. Process quoted strings
734 * and \DDD escapes. Return length or -1 on error. Returned string may
735 * contain nulls.
737 static char digits[] = "0123456789";
738 static int
739 getstr_str(char *buf, int size, const u_char **startpp, const u_char *endp) {
740 char *cp;
741 int c, c1 = 0;
742 int inquote = 0;
743 int seen_quote = 0;
744 int escape = 0;
745 int dig = 0;
747 for (cp = buf; *startpp <= endp; ) {
748 if ((c = **startpp) == '\0')
749 break;
750 /* leading white space */
751 if ((cp == buf) && !seen_quote && isspace(c)) {
752 (*startpp)++;
753 continue;
756 switch (c) {
757 case '\\':
758 if (!escape) {
759 escape = 1;
760 dig = 0;
761 c1 = 0;
762 (*startpp)++;
763 continue;
765 goto do_escape;
766 case '"':
767 if (!escape) {
768 inquote = !inquote;
769 seen_quote = 1;
770 (*startpp)++;
771 continue;
773 /* fall through */
774 default:
775 do_escape:
776 if (escape) {
777 switch (c) {
778 case '0':
779 case '1':
780 case '2':
781 case '3':
782 case '4':
783 case '5':
784 case '6':
785 case '7':
786 case '8':
787 case '9':
788 c1 = c1 * 10 +
789 (strchr(digits, c) - digits);
791 if (++dig == 3) {
792 c = c1 &0xff;
793 break;
795 (*startpp)++;
796 continue;
798 escape = 0;
799 } else if (!inquote && isspace(c))
800 goto done;
801 if (cp >= buf+size-1)
802 goto done;
803 *cp++ = (u_char)c;
804 (*startpp)++;
807 done:
808 *cp = '\0';
809 return ((cp == buf)? (seen_quote? 0: -1): (cp - buf));
812 * Get a whitespace delimited base 16 number from a string (not file) into buf
813 * update the start pointer to point after the number in the string.
815 static int
816 gethexnum_str(const u_char **startpp, const u_char *endp) {
817 int c, n;
818 int seendigit = 0;
819 int m = 0;
821 if (*startpp + 2 >= endp ||
822 strncasecmp((const char *)*startpp, "0x", 2) != 0)
823 return getnum_str(startpp, endp);
824 (*startpp)+=2;
825 for (n = 0; *startpp <= endp; ) {
826 c = **startpp;
827 if (isspace(c) || c == '\0') {
828 if (seendigit) /* trailing whitespace */
829 break;
830 else { /* leading whitespace */
831 (*startpp)++;
832 continue;
835 if (c == ';') {
836 while ((*startpp <= endp) &&
837 ((c = **startpp) != '\n'))
838 (*startpp)++;
839 if (seendigit)
840 break;
841 continue;
843 if (!isxdigit(c)) {
844 if (c == ')' && seendigit) {
845 (*startpp)--;
846 break;
848 return (-1);
850 (*startpp)++;
851 if (isdigit(c))
852 n = n * 16 + (c - '0');
853 else
854 n = n * 16 + (tolower(c) - 'a' + 10);
855 seendigit = 1;
857 return (n + m);
861 * Get a whitespace delimited base 16 number from a string (not file) into buf
862 * update the start pointer to point after the number in the string.
864 static int
865 getnum_str(const u_char **startpp, const u_char *endp) {
866 int c, n;
867 int seendigit = 0;
868 int m = 0;
870 for (n = 0; *startpp <= endp; ) {
871 c = **startpp;
872 if (isspace(c) || c == '\0') {
873 if (seendigit) /* trailing whitespace */
874 break;
875 else { /* leading whitespace */
876 (*startpp)++;
877 continue;
880 if (c == ';') {
881 while ((*startpp <= endp) &&
882 ((c = **startpp) != '\n'))
883 (*startpp)++;
884 if (seendigit)
885 break;
886 continue;
888 if (!isdigit(c)) {
889 if (c == ')' && seendigit) {
890 (*startpp)--;
891 break;
893 return (-1);
895 (*startpp)++;
896 n = n * 10 + (c - '0');
897 seendigit = 1;
899 return (n + m);
903 * Allocate a resource record buffer & save rr info.
905 ns_updrec *
906 res_mkupdrec(int section, const char *dname,
907 u_int class, u_int type, u_long ttl) {
908 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
910 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
911 if (rrecp)
912 free((char *)rrecp);
913 return (NULL);
915 rrecp->r_class = class;
916 rrecp->r_type = type;
917 rrecp->r_ttl = ttl;
918 rrecp->r_section = section;
919 return (rrecp);
923 * Free a resource record buffer created by res_mkupdrec.
925 void
926 res_freeupdrec(ns_updrec *rrecp) {
927 /* Note: freeing r_dp is the caller's responsibility. */
928 if (rrecp->r_dname != NULL)
929 free(rrecp->r_dname);
930 free(rrecp);
933 static struct valuelist *servicelist, *protolist;
935 void
936 res_buildservicelist() {
937 __unused struct servent *sp;
938 __unused struct valuelist *slp;
940 #ifndef SET_SERVENT_MISSING
941 #ifdef MAYBE_HESIOD
942 setservent(0);
943 #else
944 setservent(1);
945 #endif
946 #endif
947 #ifndef __AROS__
948 while ((sp = getservent()) != NULL) {
949 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
950 if (!slp)
951 break;
952 slp->name = strdup(sp->s_name);
953 slp->proto = strdup(sp->s_proto);
954 if ((slp->name == NULL) || (slp->proto == NULL)) {
955 if (slp->name) free(slp->name);
956 if (slp->proto) free(slp->proto);
957 free(slp);
958 break;
960 slp->port = ntohs((u_int16_t)sp->s_port); /* host byt order */
961 slp->next = servicelist;
962 slp->prev = NULL;
963 if (servicelist)
964 servicelist->prev = slp;
965 servicelist = slp;
967 endservent();
968 #endif
971 void
972 res_destroyservicelist() {
973 struct valuelist *slp, *slp_next;
975 for (slp = servicelist; slp != NULL; slp = slp_next) {
976 slp_next = slp->next;
977 free(slp->name);
978 free(slp->proto);
979 free(slp);
981 servicelist = (struct valuelist *)0;
984 void
985 res_buildprotolist() {
986 struct protoent *pp;
987 struct valuelist *slp;
989 #ifndef SET_PROTOENT_MISSING
990 #ifdef MAYBE_HESIOD
991 setprotoent(0);
992 #else
993 setprotoent(1);
994 #endif
995 #endif
996 while ((pp = getprotoent()) != NULL) {
997 slp = (struct valuelist *)malloc(sizeof(struct valuelist));
998 if (!slp)
999 break;
1000 slp->name = strdup(pp->p_name);
1001 if (slp->name == NULL) {
1002 free(slp);
1003 break;
1005 slp->port = pp->p_proto; /* host byte order */
1006 slp->next = protolist;
1007 slp->prev = NULL;
1008 if (protolist)
1009 protolist->prev = slp;
1010 protolist = slp;
1012 endprotoent();
1015 void
1016 res_destroyprotolist() {
1017 struct valuelist *plp, *plp_next;
1019 for (plp = protolist; plp != NULL; plp = plp_next) {
1020 plp_next = plp->next;
1021 free(plp->name);
1022 free(plp);
1024 protolist = (struct valuelist *)0;
1027 static int
1028 findservice(const char *s, struct valuelist **list) {
1029 struct valuelist *lp = *list;
1030 int n;
1032 for (; lp != NULL; lp = lp->next)
1033 if (strcasecmp(lp->name, s) == 0) {
1034 if (lp != *list) {
1035 lp->prev->next = lp->next;
1036 if (lp->next)
1037 lp->next->prev = lp->prev;
1038 (*list)->prev = lp;
1039 lp->next = *list;
1040 *list = lp;
1042 return (lp->port); /* host byte order */
1044 if (sscanf(s, "%d", &n) != 1 || n <= 0)
1045 n = -1;
1046 return (n);
1050 * Convert service name or (ascii) number to int.
1053 res_servicenumber(const char *p) {
1054 if (servicelist == (struct valuelist *)0)
1055 res_buildservicelist();
1056 return (findservice(p, &servicelist));
1060 * Convert protocol name or (ascii) number to int.
1063 res_protocolnumber(const char *p) {
1064 if (protolist == (struct valuelist *)0)
1065 res_buildprotolist();
1066 return (findservice(p, &protolist));
1069 static struct servent *
1070 cgetservbyport(unsigned port, const char *proto) { /* Host byte order. */
1071 struct valuelist **list = &servicelist;
1072 struct valuelist *lp = *list;
1073 static struct servent serv;
1075 port = ntohs(port);
1076 for (; lp != NULL; lp = lp->next) {
1077 if (port != (u_int16_t)lp->port) /* Host byte order. */
1078 continue;
1079 if (strcasecmp(lp->proto, proto) == 0) {
1080 if (lp != *list) {
1081 lp->prev->next = lp->next;
1082 if (lp->next)
1083 lp->next->prev = lp->prev;
1084 (*list)->prev = lp;
1085 lp->next = *list;
1086 *list = lp;
1088 serv.s_name = lp->name;
1089 serv.s_port = htons((u_int16_t)lp->port);
1090 serv.s_proto = lp->proto;
1091 return (&serv);
1094 return (0);
1097 static struct protoent *
1098 cgetprotobynumber(int proto) { /* Host byte order. */
1099 struct valuelist **list = &protolist;
1100 struct valuelist *lp = *list;
1101 static struct protoent prot;
1103 for (; lp != NULL; lp = lp->next)
1104 if (lp->port == proto) { /* Host byte order. */
1105 if (lp != *list) {
1106 lp->prev->next = lp->next;
1107 if (lp->next)
1108 lp->next->prev = lp->prev;
1109 (*list)->prev = lp;
1110 lp->next = *list;
1111 *list = lp;
1113 prot.p_name = lp->name;
1114 prot.p_proto = lp->port; /* Host byte order. */
1115 return (&prot);
1117 return (0);
1120 const char *
1121 res_protocolname(int num) {
1122 static char number[8];
1123 struct protoent *pp;
1125 if (protolist == (struct valuelist *)0)
1126 res_buildprotolist();
1127 pp = cgetprotobynumber(num);
1128 if (pp == 0) {
1129 (void) sprintf(number, "%d", num);
1130 return (number);
1132 return (pp->p_name);
1135 const char *
1136 res_servicename(u_int16_t port, const char *proto) { /* Host byte order. */
1137 static char number[8];
1138 struct servent *ss;
1140 if (servicelist == (struct valuelist *)0)
1141 res_buildservicelist();
1142 ss = cgetservbyport(htons(port), proto);
1143 if (ss == 0) {
1144 (void) sprintf(number, "%d", port);
1145 return (number);
1147 return (ss->s_name);