diff options
Diffstat (limited to 'src/lib/libc/net/res_mkquery.c')
| -rw-r--r-- | src/lib/libc/net/res_mkquery.c | 139 |
1 files changed, 47 insertions, 92 deletions
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c index 6fbd6a3859..5faba01748 100644 --- a/src/lib/libc/net/res_mkquery.c +++ b/src/lib/libc/net/res_mkquery.c | |||
| @@ -1,7 +1,11 @@ | |||
| 1 | /*- | 1 | /* $OpenBSD: res_mkquery.c,v 1.6 1997/03/13 19:07:39 downsj Exp $ */ |
| 2 | |||
| 3 | /* | ||
| 4 | * ++Copyright++ 1985, 1993 | ||
| 5 | * - | ||
| 2 | * Copyright (c) 1985, 1993 | 6 | * Copyright (c) 1985, 1993 |
| 3 | * The Regents of the University of California. All rights reserved. | 7 | * The Regents of the University of California. All rights reserved. |
| 4 | * | 8 | * |
| 5 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions |
| 7 | * are met: | 11 | * are met: |
| @@ -12,12 +16,12 @@ | |||
| 12 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: | 18 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of | 19 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. | 20 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors | 21 | * 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 | 22 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. | 23 | * without specific prior written permission. |
| 20 | * | 24 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -52,14 +56,22 @@ | |||
| 52 | */ | 56 | */ |
| 53 | 57 | ||
| 54 | #if defined(LIBC_SCCS) && !defined(lint) | 58 | #if defined(LIBC_SCCS) && !defined(lint) |
| 55 | static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.5 1996/12/14 06:49:39 tholo Exp $"; | 59 | #if 0 |
| 60 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
| 61 | static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $"; | ||
| 62 | #else | ||
| 63 | static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.6 1997/03/13 19:07:39 downsj Exp $"; | ||
| 64 | #endif | ||
| 56 | #endif /* LIBC_SCCS and not lint */ | 65 | #endif /* LIBC_SCCS and not lint */ |
| 57 | 66 | ||
| 67 | #include <sys/types.h> | ||
| 58 | #include <sys/param.h> | 68 | #include <sys/param.h> |
| 59 | #include <netinet/in.h> | 69 | #include <netinet/in.h> |
| 60 | #include <arpa/nameser.h> | 70 | #include <arpa/nameser.h> |
| 61 | #include <resolv.h> | 71 | |
| 62 | #include <stdio.h> | 72 | #include <stdio.h> |
| 73 | #include <netdb.h> | ||
| 74 | #include <resolv.h> | ||
| 63 | #include <string.h> | 75 | #include <string.h> |
| 64 | 76 | ||
| 65 | /* | 77 | /* |
| @@ -70,9 +82,9 @@ static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.5 1996/12/14 06:49:39 tholo E | |||
| 70 | int | 82 | int |
| 71 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | 83 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) |
| 72 | int op; /* opcode of query */ | 84 | int op; /* opcode of query */ |
| 73 | const char *dname; /* domain name */ | 85 | const char *dname; /* domain name */ |
| 74 | int class, type; /* class and type of query */ | 86 | int class, type; /* class and type of query */ |
| 75 | const u_char *data; /* resource record data */ | 87 | const u_char *data; /* resource record data */ |
| 76 | int datalen; /* length of data */ | 88 | int datalen; /* length of data */ |
| 77 | const u_char *newrr_in; /* new rr for modify or append */ | 89 | const u_char *newrr_in; /* new rr for modify or append */ |
| 78 | u_char *buf; /* buffer to put query */ | 90 | u_char *buf; /* buffer to put query */ |
| @@ -81,11 +93,12 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | |||
| 81 | register HEADER *hp; | 93 | register HEADER *hp; |
| 82 | register u_char *cp; | 94 | register u_char *cp; |
| 83 | register int n; | 95 | register int n; |
| 84 | #ifdef ALLOW_UPDATES | 96 | u_char *dnptrs[20], **dpp, **lastdnptr; |
| 85 | struct rrec *newrr = (struct rrec *) newrr_in; | ||
| 86 | #endif /* ALLOW_UPDATES */ | ||
| 87 | u_char *dnptrs[10], **dpp, **lastdnptr; | ||
| 88 | 97 | ||
| 98 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | ||
| 99 | h_errno = NETDB_INTERNAL; | ||
| 100 | return (-1); | ||
| 101 | } | ||
| 89 | #ifdef DEBUG | 102 | #ifdef DEBUG |
| 90 | if (_res.options & RES_DEBUG) | 103 | if (_res.options & RES_DEBUG) |
| 91 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | 104 | printf(";; res_mkquery(%d, %s, %d, %d)\n", |
| @@ -94,37 +107,36 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | |||
| 94 | /* | 107 | /* |
| 95 | * Initialize header fields. | 108 | * Initialize header fields. |
| 96 | */ | 109 | */ |
| 97 | if ((buf == NULL) || (buflen < sizeof(HEADER))) | 110 | if ((buf == NULL) || (buflen < HFIXEDSZ)) |
| 98 | return(-1); | 111 | return (-1); |
| 99 | bzero(buf, sizeof(HEADER)); | 112 | bzero(buf, HFIXEDSZ); |
| 100 | hp = (HEADER *) buf; | 113 | hp = (HEADER *) buf; |
| 101 | hp->id = htons(++_res.id); | 114 | hp->id = htons(++_res.id); |
| 102 | hp->opcode = op; | 115 | hp->opcode = op; |
| 103 | hp->pr = (_res.options & RES_PRIMARY) != 0; | ||
| 104 | hp->rd = (_res.options & RES_RECURSE) != 0; | 116 | hp->rd = (_res.options & RES_RECURSE) != 0; |
| 105 | hp->rcode = NOERROR; | 117 | hp->rcode = NOERROR; |
| 106 | cp = buf + sizeof(HEADER); | 118 | cp = buf + HFIXEDSZ; |
| 107 | buflen -= sizeof(HEADER); | 119 | buflen -= HFIXEDSZ; |
| 108 | dpp = dnptrs; | 120 | dpp = dnptrs; |
| 109 | *dpp++ = buf; | 121 | *dpp++ = buf; |
| 110 | *dpp++ = NULL; | 122 | *dpp++ = NULL; |
| 111 | lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]); | 123 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; |
| 112 | /* | 124 | /* |
| 113 | * perform opcode specific processing | 125 | * perform opcode specific processing |
| 114 | */ | 126 | */ |
| 115 | switch (op) { | 127 | switch (op) { |
| 116 | case QUERY: | 128 | case QUERY: /*FALLTHROUGH*/ |
| 117 | case NS_NOTIFY_OP: | 129 | case NS_NOTIFY_OP: |
| 118 | if ((buflen -= QFIXEDSZ) < 0) | 130 | if ((buflen -= QFIXEDSZ) < 0) |
| 119 | return(-1); | 131 | return (-1); |
| 120 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | 132 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) |
| 121 | return (-1); | 133 | return (-1); |
| 122 | cp += n; | 134 | cp += n; |
| 123 | buflen -= n; | 135 | buflen -= n; |
| 124 | __putshort(type, cp); | 136 | __putshort(type, cp); |
| 125 | cp += sizeof(u_int16_t); | 137 | cp += INT16SZ; |
| 126 | __putshort(class, cp); | 138 | __putshort(class, cp); |
| 127 | cp += sizeof(u_int16_t); | 139 | cp += INT16SZ; |
| 128 | hp->qdcount = htons(1); | 140 | hp->qdcount = htons(1); |
| 129 | if (op == QUERY || data == NULL) | 141 | if (op == QUERY || data == NULL) |
| 130 | break; | 142 | break; |
| @@ -132,18 +144,19 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | |||
| 132 | * Make an additional record for completion domain. | 144 | * Make an additional record for completion domain. |
| 133 | */ | 145 | */ |
| 134 | buflen -= RRFIXEDSZ; | 146 | buflen -= RRFIXEDSZ; |
| 135 | if ((n = dn_comp((const char *)data, cp, buflen, dnptrs, lastdnptr)) < 0) | 147 | n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr); |
| 148 | if (n < 0) | ||
| 136 | return (-1); | 149 | return (-1); |
| 137 | cp += n; | 150 | cp += n; |
| 138 | buflen -= n; | 151 | buflen -= n; |
| 139 | __putshort(T_NULL, cp); | 152 | __putshort(T_NULL, cp); |
| 140 | cp += sizeof(u_int16_t); | 153 | cp += INT16SZ; |
| 141 | __putshort(class, cp); | 154 | __putshort(class, cp); |
| 142 | cp += sizeof(u_int16_t); | 155 | cp += INT16SZ; |
| 143 | __putlong(0, cp); | 156 | __putlong(0, cp); |
| 144 | cp += sizeof(u_int32_t); | 157 | cp += INT32SZ; |
| 145 | __putshort(0, cp); | 158 | __putshort(0, cp); |
| 146 | cp += sizeof(u_int16_t); | 159 | cp += INT16SZ; |
| 147 | hp->arcount = htons(1); | 160 | hp->arcount = htons(1); |
| 148 | break; | 161 | break; |
| 149 | 162 | ||
| @@ -155,13 +168,13 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | |||
| 155 | return (-1); | 168 | return (-1); |
| 156 | *cp++ = '\0'; /* no domain name */ | 169 | *cp++ = '\0'; /* no domain name */ |
| 157 | __putshort(type, cp); | 170 | __putshort(type, cp); |
| 158 | cp += sizeof(u_int16_t); | 171 | cp += INT16SZ; |
| 159 | __putshort(class, cp); | 172 | __putshort(class, cp); |
| 160 | cp += sizeof(u_int16_t); | 173 | cp += INT16SZ; |
| 161 | __putlong(0, cp); | 174 | __putlong(0, cp); |
| 162 | cp += sizeof(u_int32_t); | 175 | cp += INT32SZ; |
| 163 | __putshort(datalen, cp); | 176 | __putshort(datalen, cp); |
| 164 | cp += sizeof(u_int16_t); | 177 | cp += INT16SZ; |
| 165 | if (datalen) { | 178 | if (datalen) { |
| 166 | bcopy(data, cp, datalen); | 179 | bcopy(data, cp, datalen); |
| 167 | cp += datalen; | 180 | cp += datalen; |
| @@ -169,64 +182,6 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | |||
| 169 | hp->ancount = htons(1); | 182 | hp->ancount = htons(1); |
| 170 | break; | 183 | break; |
| 171 | 184 | ||
| 172 | #ifdef ALLOW_UPDATES | ||
| 173 | /* | ||
| 174 | * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA | ||
| 175 | * (Record to be modified is followed by its replacement in msg.) | ||
| 176 | */ | ||
| 177 | case UPDATEM: | ||
| 178 | case UPDATEMA: | ||
| 179 | |||
| 180 | case UPDATED: | ||
| 181 | /* | ||
| 182 | * The res code for UPDATED and UPDATEDA is the same; user | ||
| 183 | * calls them differently: specifies data for UPDATED; server | ||
| 184 | * ignores data if specified for UPDATEDA. | ||
| 185 | */ | ||
| 186 | case UPDATEDA: | ||
| 187 | buflen -= RRFIXEDSZ + datalen; | ||
| 188 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
| 189 | return (-1); | ||
| 190 | cp += n; | ||
| 191 | __putshort(type, cp); | ||
| 192 | cp += sizeof(u_int16_t); | ||
| 193 | __putshort(class, cp); | ||
| 194 | cp += sizeof(u_int16_t); | ||
| 195 | __putlong(0, cp); | ||
| 196 | cp += sizeof(u_int32_t); | ||
| 197 | __putshort(datalen, cp); | ||
| 198 | cp += sizeof(u_int16_t); | ||
| 199 | if (datalen) { | ||
| 200 | bcopy(data, cp, datalen); | ||
| 201 | cp += datalen; | ||
| 202 | } | ||
| 203 | if ( (op == UPDATED) || (op == UPDATEDA) ) { | ||
| 204 | hp->ancount = htons(0); | ||
| 205 | break; | ||
| 206 | } | ||
| 207 | /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */ | ||
| 208 | |||
| 209 | case UPDATEA: /* Add new resource record */ | ||
| 210 | buflen -= RRFIXEDSZ + datalen; | ||
| 211 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
| 212 | return (-1); | ||
| 213 | cp += n; | ||
| 214 | __putshort(newrr->r_type, cp); | ||
| 215 | cp += sizeof(u_int16_t); | ||
| 216 | __putshort(newrr->r_class, cp); | ||
| 217 | cp += sizeof(u_int16_t); | ||
| 218 | __putlong(0, cp); | ||
| 219 | cp += sizeof(u_int32_t); | ||
| 220 | __putshort(newrr->r_size, cp); | ||
| 221 | cp += sizeof(u_int16_t); | ||
| 222 | if (newrr->r_size) { | ||
| 223 | bcopy(newrr->r_data, cp, newrr->r_size); | ||
| 224 | cp += newrr->r_size; | ||
| 225 | } | ||
| 226 | hp->ancount = htons(0); | ||
| 227 | break; | ||
| 228 | |||
| 229 | #endif /* ALLOW_UPDATES */ | ||
| 230 | default: | 185 | default: |
| 231 | return (-1); | 186 | return (-1); |
| 232 | } | 187 | } |
