]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/res_mkquery.c
UefiCpuPkg/MicrocodeUpdateDxe: Fix coding style issues in INF file
[mirror_edk2.git] / StdLib / BsdSocketLib / res_mkquery.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1985, 1993\r
3 * The Regents of the University of California. All rights reserved.\r
4 *\r
5 * Portions copyright (c) 1999, 2000\r
6 * Intel Corporation.\r
7 * All rights reserved.\r
8 *\r
9 * Redistribution and use in source and binary forms, with or without\r
10 * modification, are permitted provided that the following conditions\r
11 * are met:\r
12 *\r
13 * 1. Redistributions of source code must retain the above copyright\r
14 * notice, this list of conditions and the following disclaimer.\r
15 *\r
16 * 2. Redistributions in binary form must reproduce the above copyright\r
17 * notice, this list of conditions and the following disclaimer in the\r
18 * documentation and/or other materials provided with the distribution.\r
19 *\r
20 * 3. All advertising materials mentioning features or use of this software\r
21 * must display the following acknowledgement:\r
22 *\r
23 * This product includes software developed by the University of\r
24 * California, Berkeley, Intel Corporation, and its contributors.\r
25 *\r
26 * 4. Neither the name of University, Intel Corporation, or their respective\r
27 * contributors may be used to endorse or promote products derived from\r
28 * this software without specific prior written permission.\r
29 *\r
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
41 *\r
42 */\r
43\r
44/*\r
45 * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
46 *\r
47 * Permission to use, copy, modify, and distribute this software for any\r
48 * purpose with or without fee is hereby granted, provided that the above\r
49 * copyright notice and this permission notice appear in all copies, and that\r
50 * the name of Digital Equipment Corporation not be used in advertising or\r
51 * publicity pertaining to distribution of the document or software without\r
52 * specific, written prior permission.\r
53 *\r
54 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
55 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
56 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
57 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
58 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
59 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
60 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
61 * SOFTWARE.\r
62 */\r
63\r
64/*\r
65 * Portions Copyright (c) 1996 by Internet Software Consortium.\r
66 *\r
67 * Permission to use, copy, modify, and distribute this software for any\r
68 * purpose with or without fee is hereby granted, provided that the above\r
69 * copyright notice and this permission notice appear in all copies.\r
70 *\r
71 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
72 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
73 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
74 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
75 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
76 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
77 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
78 * SOFTWARE.\r
79 */\r
80\r
81#if defined(LIBC_SCCS) && !defined(lint)\r
82static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";\r
83static char orig_rcsid[] = "From: Id: res_mkquery.c,v 8.9 1997/04/24 22:22:36 vixie Exp $";\r
84static char rcsid[] = "$Id: res_mkquery.c,v 1.1.1.1 2003/11/19 01:51:37 kyu3 Exp $";\r
85#endif /* LIBC_SCCS and not lint */\r
86\r
87#include <sys/types.h>\r
88#include <sys/param.h>\r
89#include <netinet/in.h>\r
90#include <arpa/nameser.h>\r
91#include <netdb.h>\r
92#include <resolv.h>\r
93#include <stdio.h>\r
94#include <string.h>\r
95\r
96#include "res_config.h"\r
97\r
98/*\r
99 * Form all types of queries.\r
100 * Returns the size of the result or -1.\r
101 */\r
102int\r
103res_mkquery(\r
104 int op, /* opcode of query */\r
105 const char *dname, /* domain name */\r
106 int class, /* class of query */\r
107 int type, /* type of query */\r
108 const u_char *data, /* resource record data */\r
109 int datalen, /* length of data */\r
110 const u_char *newrr_in, /* new rr for modify or append */\r
111 u_char *buf, /* buffer to put query */\r
112 int buflen /* size of buffer */\r
113 )\r
114{\r
115 register HEADER *hp;\r
116 register u_char *cp;\r
117 register int n;\r
118 u_char *dnptrs[20], **dpp, **lastdnptr;\r
119\r
120 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
121 h_errno = NETDB_INTERNAL;\r
122 return (-1);\r
123 }\r
124#ifdef DEBUG\r
125 if (_res.options & RES_DEBUG)\r
126 printf(";; res_mkquery(%d, %s, %d, %d)\n",\r
127 op, dname, class, type);\r
128#endif\r
129 /*\r
130 * Initialize header fields.\r
131 */\r
132 if ((buf == NULL) || (buflen < HFIXEDSZ))\r
133 return (-1);\r
134 memset(buf, 0, HFIXEDSZ);\r
135 hp = (HEADER *) buf;\r
136 hp->id = htons(++_res.id);\r
137 hp->opcode = op;\r
138 hp->rd = (_res.options & RES_RECURSE) != 0;\r
139 hp->rcode = NOERROR;\r
140 cp = buf + HFIXEDSZ;\r
141 buflen -= HFIXEDSZ;\r
142 dpp = dnptrs;\r
143 *dpp++ = buf;\r
144 *dpp++ = NULL;\r
145 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];\r
146 /*\r
147 * perform opcode specific processing\r
148 */\r
149 switch (op) {\r
150 case QUERY: /*FALLTHROUGH*/\r
151 case NS_NOTIFY_OP:\r
152 if ((buflen -= QFIXEDSZ) < 0)\r
153 return (-1);\r
154 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)\r
155 return (-1);\r
156 cp += n;\r
157 buflen -= n;\r
158 __putshort((u_int16_t)type, cp);\r
159 cp += INT16SZ;\r
160 __putshort((u_int16_t)class, cp);\r
161 cp += INT16SZ;\r
162 hp->qdcount = htons(1);\r
163 if (op == QUERY || data == NULL)\r
164 break;\r
165 /*\r
166 * Make an additional record for completion domain.\r
167 */\r
168 buflen -= RRFIXEDSZ;\r
169 n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);\r
170 if (n < 0)\r
171 return (-1);\r
172 cp += n;\r
173 buflen -= n;\r
174 __putshort(T_NULL, cp);\r
175 cp += INT16SZ;\r
176 __putshort((u_int16_t)class, cp);\r
177 cp += INT16SZ;\r
178 __putlong(0, cp);\r
179 cp += INT32SZ;\r
180 __putshort(0, cp);\r
181 cp += INT16SZ;\r
182 hp->arcount = htons(1);\r
183 break;\r
184\r
185 case IQUERY:\r
186 /*\r
187 * Initialize answer section\r
188 */\r
189 if (buflen < 1 + RRFIXEDSZ + datalen)\r
190 return (-1);\r
191 *cp++ = '\0'; /* no domain name */\r
192 __putshort((u_int16_t)type, cp);\r
193 cp += INT16SZ;\r
194 __putshort((u_int16_t)class, cp);\r
195 cp += INT16SZ;\r
196 __putlong(0, cp);\r
197 cp += INT32SZ;\r
198 __putshort((u_int16_t)datalen, cp);\r
199 cp += INT16SZ;\r
200 if (datalen) {\r
201 memcpy(cp, data, datalen);\r
202 cp += datalen;\r
203 }\r
204 hp->ancount = htons(1);\r
205 break;\r
206\r
207 default:\r
208 return (-1);\r
209 }\r
210 return ((int)(cp - buf));\r
211}\r