| 1 | /* |
| 2 | * Copyright (c) 1995-1999 by Internet Software Consortium. |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 15 | * SOFTWARE. |
| 16 | */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/param.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/time.h> |
| 22 | |
| 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <arpa/nameser.h> |
| 26 | |
| 27 | #include <ctype.h> |
| 28 | #include <netdb.h> |
| 29 | #include <resolv.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | const char *_res_opcodes[] = { |
| 36 | "QUERY" , |
| 37 | "IQUERY" , |
| 38 | "CQUERYM" , |
| 39 | "CQUERYU" , /* experimental */ |
| 40 | "NOTIFY" , /* experimental */ |
| 41 | "UPDATE" , |
| 42 | "6" , |
| 43 | "7" , |
| 44 | "8" , |
| 45 | "9" , |
| 46 | "10" , |
| 47 | "11" , |
| 48 | "12" , |
| 49 | "13" , |
| 50 | "ZONEINIT" , |
| 51 | "ZONEREF" , |
| 52 | }; |
| 53 | libresolv_hidden_data_def (_res_opcodes) |
| 54 | |
| 55 | void |
| 56 | p_query(const u_char *msg) { |
| 57 | fp_query(msg, stdout); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | fp_query(const u_char *msg, FILE *file) { |
| 62 | fp_nquery(msg, PACKETSZ, file); |
| 63 | } |
| 64 | libresolv_hidden_def (fp_query) |
| 65 | |
| 66 | void |
| 67 | fp_nquery(const u_char *msg, int len, FILE *file) { |
| 68 | if (__res_maybe_init (&_res, 0) == -1) |
| 69 | return; |
| 70 | |
| 71 | res_pquery(&_res, msg, len, file); |
| 72 | } |
| 73 | libresolv_hidden_def (fp_nquery) |
| 74 | |
| 75 | int |
| 76 | res_mkquery(int op, /* opcode of query */ |
| 77 | const char *dname, /* domain name */ |
| 78 | int class, int type, /* class and type of query */ |
| 79 | const u_char *data, /* resource record data */ |
| 80 | int datalen, /* length of data */ |
| 81 | const u_char *newrr_in, /* new rr for modify or append */ |
| 82 | u_char *buf, /* buffer to put query */ |
| 83 | int buflen) /* size of buffer */ |
| 84 | { |
| 85 | if (__res_maybe_init (&_res, 1) == -1) { |
| 86 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
| 87 | return (-1); |
| 88 | } |
| 89 | return (res_nmkquery(&_res, op, dname, class, type, |
| 90 | data, datalen, |
| 91 | newrr_in, buf, buflen)); |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | res_query(const char *name, /* domain name */ |
| 96 | int class, int type, /* class and type of query */ |
| 97 | u_char *answer, /* buffer to put answer */ |
| 98 | int anslen) /* size of answer buffer */ |
| 99 | { |
| 100 | if (__res_maybe_init (&_res, 1) == -1) { |
| 101 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
| 102 | return (-1); |
| 103 | } |
| 104 | return (res_nquery(&_res, name, class, type, answer, anslen)); |
| 105 | } |
| 106 | |
| 107 | int |
| 108 | res_isourserver(const struct sockaddr_in *inp) { |
| 109 | return (res_ourserver_p(&_res, (const struct sockaddr_in6 *) inp)); |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) { |
| 114 | if (__res_maybe_init (&_res, 1) == -1) { |
| 115 | /* errno should have been set by res_init() in this case. */ |
| 116 | return (-1); |
| 117 | } |
| 118 | |
| 119 | return (res_nsend(&_res, buf, buflen, ans, anssiz)); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void |
| 124 | res_close(void) { |
| 125 | /* |
| 126 | * Some stupid programs out there call res_close() before res_init(). |
| 127 | * Since _res._vcsock isn't explicitly initialized, these means that |
| 128 | * we could do a close(0), which might lead to some security problems. |
| 129 | * Therefore we check if res_init() was called before by looking at |
| 130 | * the RES_INIT bit in _res.options. If it hasn't been set we bail out |
| 131 | * early. */ |
| 132 | if ((_res.options & RES_INIT) == 0) |
| 133 | return; |
| 134 | /* We don't free the name server addresses because we never |
| 135 | did it and it would be done implicitly on shutdown. */ |
| 136 | __res_iclose(&_res, false); |
| 137 | } |
| 138 | |
| 139 | int |
| 140 | res_search(const char *name, /* domain name */ |
| 141 | int class, int type, /* class and type of query */ |
| 142 | u_char *answer, /* buffer to put answer */ |
| 143 | int anslen) /* size of answer */ |
| 144 | { |
| 145 | if (__res_maybe_init (&_res, 1) == -1) { |
| 146 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
| 147 | return (-1); |
| 148 | } |
| 149 | |
| 150 | return (res_nsearch(&_res, name, class, type, answer, anslen)); |
| 151 | } |
| 152 | |
| 153 | int |
| 154 | res_querydomain(const char *name, |
| 155 | const char *domain, |
| 156 | int class, int type, /* class and type of query */ |
| 157 | u_char *answer, /* buffer to put answer */ |
| 158 | int anslen) /* size of answer */ |
| 159 | { |
| 160 | if (__res_maybe_init (&_res, 1) == -1) { |
| 161 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
| 162 | return (-1); |
| 163 | } |
| 164 | |
| 165 | return (res_nquerydomain(&_res, name, domain, |
| 166 | class, type, |
| 167 | answer, anslen)); |
| 168 | } |
| 169 | |
| 170 | const char * |
| 171 | hostalias(const char *name) { |
| 172 | static char abuf[MAXDNAME]; |
| 173 | |
| 174 | return (res_hostalias(&_res, name, abuf, sizeof abuf)); |
| 175 | } |
| 176 | libresolv_hidden_def (hostalias) |
| 177 | |
| 178 | |
| 179 | |
| 180 | #include <shlib-compat.h> |
| 181 | |
| 182 | #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2) |
| 183 | # undef res_mkquery |
| 184 | # undef res_query |
| 185 | # undef res_querydomain |
| 186 | # undef res_search |
| 187 | weak_alias (__res_mkquery, res_mkquery); |
| 188 | weak_alias (__res_query, res_query); |
| 189 | weak_alias (__res_querydomain, res_querydomain); |
| 190 | weak_alias (__res_search, res_search); |
| 191 | #endif |
| 192 | |