]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/res_mkupdate.c
UefiCpuPkg/MicrocodeUpdateDxe: Fix coding style issues in INF file
[mirror_edk2.git] / StdLib / BsdSocketLib / res_mkupdate.c
CommitLineData
0e565888
OM
1/** @file\r
2 Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>\r
3 This program and the accompanying materials are licensed and made available\r
4 under the terms and conditions of the BSD License which accompanies this\r
5 distribution. The full text of the license may be found at\r
6 http://opensource.org/licenses/bsd-license.php.\r
7\r
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
10**/\r
d7ce7006 11/*\r
12 * Copyright (c) 1996 by Internet Software Consortium.\r
13 *\r
14 * Permission to use, copy, modify, and distribute this software for any\r
15 * purpose with or without fee is hereby granted, provided that the above\r
16 * copyright notice and this permission notice appear in all copies.\r
17 *\r
18 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
19 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
20 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
21 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
22 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
23 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
24 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
25 * SOFTWARE.\r
26 */\r
27\r
28/*\r
29 * Portions copyright (c) 1999, 2000\r
30 * Intel Corporation.\r
31 * All rights reserved.\r
32 *\r
33 * Redistribution and use in source and binary forms, with or without\r
34 * modification, are permitted provided that the following conditions\r
35 * are met:\r
36 *\r
37 * 1. Redistributions of source code must retain the above copyright\r
38 * notice, this list of conditions and the following disclaimer.\r
39 *\r
40 * 2. Redistributions in binary form must reproduce the above copyright\r
41 * notice, this list of conditions and the following disclaimer in the\r
42 * documentation and/or other materials provided with the distribution.\r
43 *\r
44 * 3. All advertising materials mentioning features or use of this software\r
45 * must display the following acknowledgement:\r
46 *\r
47 * This product includes software developed by Intel Corporation and\r
48 * its contributors.\r
49 *\r
50 * 4. Neither the name of Intel Corporation or its contributors may be\r
51 * used to endorse or promote products derived from this software\r
52 * without specific prior written permission.\r
53 *\r
54 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r
55 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
57 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE\r
58 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r
64 * THE POSSIBILITY OF SUCH DAMAGE.\r
65 *\r
66 */\r
67\r
68/*\r
69 * Based on the Dynamic DNS reference implementation by Viraj Bais\r
70 * <viraj_bais@ccm.fm.intel.com>\r
71 */\r
72\r
d7ce7006 73#include <sys/types.h>\r
74#include <sys/param.h>\r
75\r
76#include <netinet/in.h>\r
77#include <arpa/nameser.h>\r
78#include <arpa/inet.h>\r
79\r
80#include <errno.h>\r
81#include <limits.h>\r
82#include <netdb.h>\r
83#include <resolv.h>\r
84#include <stdio.h>\r
85#include <stdlib.h>\r
86#include <string.h>\r
87#include <unistd.h>\r
88#include <ctype.h>\r
89\r
90#include "res_config.h"\r
91\r
92static int getnum_str(u_char **, u_char *);\r
93static int getword_str(char *, int, u_char **, u_char *);\r
94\r
95#define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);\r
96\r
97/*\r
98 * Form update packets.\r
99 * Returns the size of the resulting packet if no error\r
100 * On error,\r
101 * returns -1 if error in reading a word/number in rdata\r
102 * portion for update packets\r
103 * -2 if length of buffer passed is insufficient\r
104 * -3 if zone section is not the first section in\r
105 * the linked list, or section order has a problem\r
106 * -4 on a number overflow\r
107 * -5 unknown operation or no records\r
108 */\r
109int\r
110res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {\r
111 ns_updrec *rrecp_start = rrecp_in;\r
112 HEADER *hp;\r
0e565888 113 u_char *cp, *sp2, *startp, *endp;\r
d7ce7006 114 int n, i, soanum, multiline;\r
115 ns_updrec *rrecp;\r
116 struct in_addr ina;\r
117 char buf2[MAXDNAME];\r
118 int section, numrrs = 0, counts[ns_s_max];\r
119 u_int16_t rtype, rclass;\r
120 u_int32_t n1, rttl;\r
121 u_char *dnptrs[20], **dpp, **lastdnptr;\r
122\r
123 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
124 h_errno = NETDB_INTERNAL;\r
125 return (-1);\r
126 }\r
127\r
128 /*\r
129 * Initialize header fields.\r
130 */\r
131 if ((buf == NULL) || (buflen < HFIXEDSZ))\r
132 return (-1);\r
133 memset(buf, 0, HFIXEDSZ);\r
134 hp = (HEADER *) buf;\r
135 hp->id = htons(++_res.id);\r
136 hp->opcode = ns_o_update;\r
137 hp->rcode = NOERROR;\r
d7ce7006 138 cp = buf + HFIXEDSZ;\r
139 buflen -= HFIXEDSZ;\r
140 dpp = dnptrs;\r
141 *dpp++ = buf;\r
142 *dpp++ = NULL;\r
143 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];\r
144\r
145 if (rrecp_start == NULL)\r
146 return (-5);\r
147 else if (rrecp_start->r_section != S_ZONE)\r
148 return (-3);\r
149\r
150 memset(counts, 0, sizeof counts);\r
151 for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {\r
152 numrrs++;\r
153 section = rrecp->r_section;\r
154 if (section < 0 || section >= ns_s_max)\r
155 return (-1);\r
156 counts[section]++;\r
157 for (i = section + 1; i < ns_s_max; i++)\r
158 if (counts[i])\r
159 return (-3);\r
160 rtype = rrecp->r_type;\r
161 rclass = rrecp->r_class;\r
162 rttl = rrecp->r_ttl;\r
163 /* overload class and type */\r
164 if (section == S_PREREQ) {\r
165 rttl = 0;\r
166 switch (rrecp->r_opcode) {\r
167 case YXDOMAIN:\r
168 rclass = C_ANY;\r
169 rtype = T_ANY;\r
170 rrecp->r_size = 0;\r
171 break;\r
172 case NXDOMAIN:\r
173 rclass = C_NONE;\r
174 rtype = T_ANY;\r
175 rrecp->r_size = 0;\r
176 break;\r
177 case NXRRSET:\r
178 rclass = C_NONE;\r
179 rrecp->r_size = 0;\r
180 break;\r
181 case YXRRSET:\r
182 if (rrecp->r_size == 0)\r
183 rclass = C_ANY;\r
184 break;\r
185 default:\r
186 fprintf(stderr,\r
187 "res_mkupdate: incorrect opcode: %d\n",\r
188 rrecp->r_opcode);\r
189 fflush(stderr);\r
190 return (-1);\r
191 }\r
192 } else if (section == S_UPDATE) {\r
193 switch (rrecp->r_opcode) {\r
194 case DELETE:\r
195 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;\r
196 break;\r
197 case ADD:\r
198 break;\r
199 default:\r
200 fprintf(stderr,\r
201 "res_mkupdate: incorrect opcode: %d\n",\r
202 rrecp->r_opcode);\r
203 fflush(stderr);\r
204 return (-1);\r
205 }\r
206 }\r
207\r
208 /*\r
209 * XXX appending default domain to owner name is omitted,\r
210 * fqdn must be provided\r
211 */\r
212 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,\r
213 lastdnptr)) < 0)\r
214 return (-1);\r
215 cp += n;\r
216 ShrinkBuffer(n + 2*INT16SZ);\r
217 PUTSHORT(rtype, cp);\r
218 PUTSHORT(rclass, cp);\r
219 if (section == S_ZONE) {\r
220 if (numrrs != 1 || rrecp->r_type != T_SOA)\r
221 return (-3);\r
222 continue;\r
223 }\r
224 ShrinkBuffer(INT32SZ + INT16SZ);\r
225 PUTLONG(rttl, cp);\r
226 sp2 = cp; /* save pointer to length byte */\r
227 cp += INT16SZ;\r
228 if (rrecp->r_size == 0) {\r
229 if (section == S_UPDATE && rclass != C_ANY)\r
230 return (-1);\r
231 else {\r
232 PUTSHORT(0, sp2);\r
233 continue;\r
234 }\r
235 }\r
236 startp = rrecp->r_data;\r
237 endp = startp + rrecp->r_size - 1;\r
238 /* XXX this should be done centrally. */\r
239 switch (rrecp->r_type) {\r
240 case T_A:\r
241 if (!getword_str(buf2, sizeof buf2, &startp, endp))\r
242 return (-1);\r
243 if (!inet_aton(buf2, &ina))\r
244 return (-1);\r
245 n1 = ntohl(ina.s_addr);\r
246 ShrinkBuffer(INT32SZ);\r
247 PUTLONG(n1, cp);\r
248 break;\r
249 case T_CNAME:\r
250 case T_MB:\r
251 case T_MG:\r
252 case T_MR:\r
253 case T_NS:\r
254 case T_PTR:\r
255 if (!getword_str(buf2, sizeof buf2, &startp, endp))\r
256 return (-1);\r
257 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);\r
258 if (n < 0)\r
259 return (-1);\r
260 cp += n;\r
261 ShrinkBuffer(n);\r
262 break;\r
263 case T_MINFO:\r
264 case T_SOA:\r
265 case T_RP:\r
266 for (i = 0; i < 2; i++) {\r
267 if (!getword_str(buf2, sizeof buf2, &startp,\r
268 endp))\r
269 return (-1);\r
270 n = dn_comp(buf2, cp, buflen,\r
271 dnptrs, lastdnptr);\r
272 if (n < 0)\r
273 return (-1);\r
274 cp += n;\r
275 ShrinkBuffer(n);\r
276 }\r
277 if (rrecp->r_type == T_SOA) {\r
278 ShrinkBuffer(5 * INT32SZ);\r
279 while (isspace(*startp) || !*startp)\r
280 startp++;\r
281 if (*startp == '(') {\r
282 multiline = 1;\r
283 startp++;\r
284 } else\r
285 multiline = 0;\r
286 /* serial, refresh, retry, expire, minimum */\r
287 for (i = 0; i < 5; i++) {\r
288 soanum = getnum_str(&startp, endp);\r
289 if (soanum < 0)\r
290 return (-1);\r
291 PUTLONG(soanum, cp);\r
292 }\r
293 if (multiline) {\r
294 while (isspace(*startp) || !*startp)\r
295 startp++;\r
296 if (*startp != ')')\r
297 return (-1);\r
298 }\r
299 }\r
300 break;\r
301 case T_MX:\r
302 case T_AFSDB:\r
303 case T_RT:\r
304 n = getnum_str(&startp, endp);\r
305 if (n < 0)\r
306 return (-1);\r
307 PUTSHORT(n, cp);\r
308 ShrinkBuffer(INT16SZ);\r
309 if (!getword_str(buf2, sizeof buf2, &startp, endp))\r
310 return (-1);\r
311 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);\r
312 if (n < 0)\r
313 return (-1);\r
314 cp += n;\r
315 ShrinkBuffer(n);\r
316 break;\r
317 case T_PX:\r
318 n = getnum_str(&startp, endp);\r
319 if (n < 0)\r
320 return (-1);\r
321 PUTSHORT(n, cp);\r
322 ShrinkBuffer(INT16SZ);\r
323 for (i = 0; i < 2; i++) {\r
324 if (!getword_str(buf2, sizeof buf2, &startp,\r
325 endp))\r
326 return (-1);\r
327 n = dn_comp(buf2, cp, buflen, dnptrs,\r
328 lastdnptr);\r
329 if (n < 0)\r
330 return (-1);\r
331 cp += n;\r
332 ShrinkBuffer(n);\r
333 }\r
334 break;\r
335 case T_WKS:\r
336 case T_HINFO:\r
337 case T_TXT:\r
338 case T_X25:\r
339 case T_ISDN:\r
340 case T_NSAP:\r
341 case T_LOC:\r
342 /* XXX - more fine tuning needed here */\r
343 ShrinkBuffer(rrecp->r_size);\r
344 memcpy(cp, rrecp->r_data, rrecp->r_size);\r
345 cp += rrecp->r_size;\r
346 break;\r
347 default:\r
348 return (-1);\r
349 } /*switch*/\r
350 n = (u_int16_t)((cp - sp2) - INT16SZ);\r
351 PUTSHORT(n, sp2);\r
352 } /*for*/\r
353\r
354 hp->qdcount = htons(counts[0]);\r
355 hp->ancount = htons(counts[1]);\r
356 hp->nscount = htons(counts[2]);\r
357 hp->arcount = htons(counts[3]);\r
358 return ((int)(cp - buf));\r
359}\r
360\r
361/*\r
362 * Get a whitespace delimited word from a string (not file)\r
363 * into buf. modify the start pointer to point after the\r
364 * word in the string.\r
365 */\r
366static int\r
367getword_str(char *buf, int size, u_char **startpp, u_char *endp) {\r
368 char *cp;\r
369 int c;\r
370\r
371 for (cp = buf; *startpp <= endp; ) {\r
372 c = **startpp;\r
373 if (isspace(c) || c == '\0') {\r
374 if (cp != buf) /* trailing whitespace */\r
375 break;\r
376 else { /* leading whitespace */\r
377 (*startpp)++;\r
378 continue;\r
379 }\r
380 }\r
381 (*startpp)++;\r
382 if (cp >= buf+size-1)\r
383 break;\r
384 *cp++ = (u_char)c;\r
385 }\r
386 *cp = '\0';\r
387 return (cp != buf);\r
388}\r
389\r
390/*\r
391 * Get a whitespace delimited number from a string (not file) into buf\r
392 * update the start pointer to point after the number in the string.\r
393 */\r
394static int\r
395getnum_str(u_char **startpp, u_char *endp) {\r
396 int c;\r
397 int n;\r
398 int seendigit = 0;\r
399 int m = 0;\r
400\r
401 for (n = 0; *startpp <= endp; ) {\r
402 c = **startpp;\r
403 if (isspace(c) || c == '\0') {\r
404 if (seendigit) /* trailing whitespace */\r
405 break;\r
406 else { /* leading whitespace */\r
407 (*startpp)++;\r
408 continue;\r
409 }\r
410 }\r
411 if (c == ';') {\r
412 while ((*startpp <= endp) &&\r
413 ((c = **startpp) != '\n'))\r
414 (*startpp)++;\r
415 if (seendigit)\r
416 break;\r
417 continue;\r
418 }\r
419 if (!isdigit(c)) {\r
420 if (c == ')' && seendigit) {\r
421 (*startpp)--;\r
422 break;\r
423 }\r
424 return (-1);\r
425 }\r
426 (*startpp)++;\r
427 n = n * 10 + (c - '0');\r
428 seendigit = 1;\r
429 }\r
430 return (n + m);\r
431}\r
432\r
433/*\r
434 * Allocate a resource record buffer & save rr info.\r
435 */\r
436ns_updrec *\r
437res_mkupdrec(int section, const char *dname,\r
438 u_int class, u_int type, u_long ttl) {\r
439 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));\r
440\r
7053c3b3
DM
441 if (!rrecp || !(rrecp->r_dname = strdup(dname))) {\r
442 free(rrecp);\r
d7ce7006 443 return (NULL);\r
7053c3b3 444 }\r
d7ce7006 445 rrecp->r_class = (u_int16_t)class;\r
446 rrecp->r_type = (u_int16_t)type;\r
447 rrecp->r_ttl = (u_int32_t)ttl;\r
448 rrecp->r_section = (u_int8_t)section;\r
449 return (rrecp);\r
450}\r
451\r
452/*\r
453 * Free a resource record buffer created by res_mkupdrec.\r
454 */\r
455void\r
456res_freeupdrec(ns_updrec *rrecp) {\r
457 /* Note: freeing r_dp is the caller's responsibility. */\r
458 if (rrecp->r_dname != NULL)\r
459 free(rrecp->r_dname);\r
460 free(rrecp);\r
461}\r