EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

StdLib/BsdSocketLib/res_mkupdate.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996 by Internet Software Consortium.
00003  *
00004  * Permission to use, copy, modify, and distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00009  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00010  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00011  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00012  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00013  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00014  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00015  * SOFTWARE.
00016  */
00017 
00018 /*
00019  * Portions copyright (c) 1999, 2000
00020  * Intel Corporation.
00021  * All rights reserved.
00022  *
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  *
00027  * 1. Redistributions of source code must retain the above copyright
00028  *    notice, this list of conditions and the following disclaimer.
00029  *
00030  * 2. Redistributions in binary form must reproduce the above copyright
00031  *    notice, this list of conditions and the following disclaimer in the
00032  *    documentation and/or other materials provided with the distribution.
00033  *
00034  * 3. All advertising materials mentioning features or use of this software
00035  *    must display the following acknowledgement:
00036  *
00037  *    This product includes software developed by Intel Corporation and
00038  *    its contributors.
00039  *
00040  * 4. Neither the name of Intel Corporation or its contributors may be
00041  *    used to endorse or promote products derived from this software
00042  *    without specific prior written permission.
00043  *
00044  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
00045  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00046  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00047  * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
00048  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00049  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00050  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00051  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00052  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00053  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00054  * THE POSSIBILITY OF SUCH DAMAGE.
00055  *
00056  */
00057 
00058 /*
00059  * Based on the Dynamic DNS reference implementation by Viraj Bais
00060  * <viraj_bais@ccm.fm.intel.com>
00061  */
00062 
00063 #if !defined(lint) && !defined(SABER)
00064 static char rcsid[] = "$Id: res_mkupdate.c,v 1.1.1.1 2003/11/19 01:51:38 kyu3 Exp $";
00065 #endif /* not lint */
00066 
00067 #include <sys/types.h>
00068 #include <sys/param.h>
00069 
00070 #include <netinet/in.h>
00071 #include <arpa/nameser.h>
00072 #include <arpa/inet.h>
00073 
00074 #include <errno.h>
00075 #include <limits.h>
00076 #include <netdb.h>
00077 #include <resolv.h>
00078 #include <stdio.h>
00079 #include <stdlib.h>
00080 #include <string.h>
00081 #include <unistd.h>
00082 #include <ctype.h>
00083 
00084 #include "res_config.h"
00085 
00086 static int getnum_str(u_char **, u_char *);
00087 static int getword_str(char *, int, u_char **, u_char *);
00088 
00089 #define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
00090 
00091 /*
00092  * Form update packets.
00093  * Returns the size of the resulting packet if no error
00094  * On error,
00095  *  returns -1 if error in reading a word/number in rdata
00096  *         portion for update packets
00097  *      -2 if length of buffer passed is insufficient
00098  *      -3 if zone section is not the first section in
00099  *         the linked list, or section order has a problem
00100  *      -4 on a number overflow
00101  *      -5 unknown operation or no records
00102  */
00103 int
00104 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
00105     ns_updrec *rrecp_start = rrecp_in;
00106     HEADER *hp;
00107     u_char *cp, *sp1, *sp2, *startp, *endp;
00108     int n, i, soanum, multiline;
00109     ns_updrec *rrecp;
00110     struct in_addr ina;
00111         char buf2[MAXDNAME];
00112     int section, numrrs = 0, counts[ns_s_max];
00113     u_int16_t rtype, rclass;
00114     u_int32_t n1, rttl;
00115     u_char *dnptrs[20], **dpp, **lastdnptr;
00116 
00117     if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
00118         h_errno = NETDB_INTERNAL;
00119         return (-1);
00120     }
00121 
00122     /*
00123      * Initialize header fields.
00124      */
00125     if ((buf == NULL) || (buflen < HFIXEDSZ))
00126         return (-1);
00127     memset(buf, 0, HFIXEDSZ);
00128     hp = (HEADER *) buf;
00129     hp->id = htons(++_res.id);
00130     hp->opcode = ns_o_update;
00131     hp->rcode = NOERROR;
00132     sp1 = buf + 2*INT16SZ;  /* save pointer to zocount */
00133     cp = buf + HFIXEDSZ;
00134     buflen -= HFIXEDSZ;
00135     dpp = dnptrs;
00136     *dpp++ = buf;
00137     *dpp++ = NULL;
00138     lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
00139 
00140     if (rrecp_start == NULL)
00141         return (-5);
00142     else if (rrecp_start->r_section != S_ZONE)
00143         return (-3);
00144 
00145     memset(counts, 0, sizeof counts);
00146     for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {
00147         numrrs++;
00148                 section = rrecp->r_section;
00149         if (section < 0 || section >= ns_s_max)
00150             return (-1);
00151         counts[section]++;
00152         for (i = section + 1; i < ns_s_max; i++)
00153             if (counts[i])
00154                 return (-3);
00155         rtype = rrecp->r_type;
00156         rclass = rrecp->r_class;
00157         rttl = rrecp->r_ttl;
00158         /* overload class and type */
00159         if (section == S_PREREQ) {
00160             rttl = 0;
00161             switch (rrecp->r_opcode) {
00162             case YXDOMAIN:
00163                 rclass = C_ANY;
00164                 rtype = T_ANY;
00165                 rrecp->r_size = 0;
00166                 break;
00167             case NXDOMAIN:
00168                 rclass = C_NONE;
00169                 rtype = T_ANY;
00170                 rrecp->r_size = 0;
00171                 break;
00172             case NXRRSET:
00173                 rclass = C_NONE;
00174                 rrecp->r_size = 0;
00175                 break;
00176             case YXRRSET:
00177                 if (rrecp->r_size == 0)
00178                     rclass = C_ANY;
00179                 break;
00180             default:
00181                 fprintf(stderr,
00182                     "res_mkupdate: incorrect opcode: %d\n",
00183                     rrecp->r_opcode);
00184                 fflush(stderr);
00185                 return (-1);
00186             }
00187         } else if (section == S_UPDATE) {
00188             switch (rrecp->r_opcode) {
00189             case DELETE:
00190                 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
00191                 break;
00192             case ADD:
00193                 break;
00194             default:
00195                 fprintf(stderr,
00196                     "res_mkupdate: incorrect opcode: %d\n",
00197                     rrecp->r_opcode);
00198                 fflush(stderr);
00199                 return (-1);
00200             }
00201         }
00202 
00203         /*
00204          * XXX  appending default domain to owner name is omitted,
00205          *  fqdn must be provided
00206          */
00207         if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
00208                  lastdnptr)) < 0)
00209             return (-1);
00210         cp += n;
00211         ShrinkBuffer(n + 2*INT16SZ);
00212         PUTSHORT(rtype, cp);
00213         PUTSHORT(rclass, cp);
00214         if (section == S_ZONE) {
00215             if (numrrs != 1 || rrecp->r_type != T_SOA)
00216                 return (-3);
00217             continue;
00218         }
00219         ShrinkBuffer(INT32SZ + INT16SZ);
00220         PUTLONG(rttl, cp);
00221         sp2 = cp;  /* save pointer to length byte */
00222         cp += INT16SZ;
00223         if (rrecp->r_size == 0) {
00224             if (section == S_UPDATE && rclass != C_ANY)
00225                 return (-1);
00226             else {
00227                 PUTSHORT(0, sp2);
00228                 continue;
00229             }
00230         }
00231         startp = rrecp->r_data;
00232         endp = startp + rrecp->r_size - 1;
00233         /* XXX this should be done centrally. */
00234         switch (rrecp->r_type) {
00235         case T_A:
00236             if (!getword_str(buf2, sizeof buf2, &startp, endp))
00237                 return (-1);
00238             if (!inet_aton(buf2, &ina))
00239                 return (-1);
00240             n1 = ntohl(ina.s_addr);
00241             ShrinkBuffer(INT32SZ);
00242             PUTLONG(n1, cp);
00243             break;
00244         case T_CNAME:
00245         case T_MB:
00246         case T_MG:
00247         case T_MR:
00248         case T_NS:
00249         case T_PTR:
00250             if (!getword_str(buf2, sizeof buf2, &startp, endp))
00251                 return (-1);
00252             n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
00253             if (n < 0)
00254                 return (-1);
00255             cp += n;
00256             ShrinkBuffer(n);
00257             break;
00258         case T_MINFO:
00259         case T_SOA:
00260         case T_RP:
00261             for (i = 0; i < 2; i++) {
00262                 if (!getword_str(buf2, sizeof buf2, &startp,
00263                          endp))
00264                 return (-1);
00265                 n = dn_comp(buf2, cp, buflen,
00266                         dnptrs, lastdnptr);
00267                 if (n < 0)
00268                     return (-1);
00269                 cp += n;
00270                 ShrinkBuffer(n);
00271             }
00272             if (rrecp->r_type == T_SOA) {
00273                 ShrinkBuffer(5 * INT32SZ);
00274                 while (isspace(*startp) || !*startp)
00275                     startp++;
00276                 if (*startp == '(') {
00277                     multiline = 1;
00278                     startp++;
00279                 } else
00280                     multiline = 0;
00281                 /* serial, refresh, retry, expire, minimum */
00282                 for (i = 0; i < 5; i++) {
00283                     soanum = getnum_str(&startp, endp);
00284                     if (soanum < 0)
00285                         return (-1);
00286                     PUTLONG(soanum, cp);
00287                 }
00288                 if (multiline) {
00289                     while (isspace(*startp) || !*startp)
00290                         startp++;
00291                     if (*startp != ')')
00292                         return (-1);
00293                 }
00294             }
00295             break;
00296         case T_MX:
00297         case T_AFSDB:
00298         case T_RT:
00299             n = getnum_str(&startp, endp);
00300             if (n < 0)
00301                 return (-1);
00302             PUTSHORT(n, cp);
00303             ShrinkBuffer(INT16SZ);
00304             if (!getword_str(buf2, sizeof buf2, &startp, endp))
00305                 return (-1);
00306             n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
00307             if (n < 0)
00308                 return (-1);
00309             cp += n;
00310             ShrinkBuffer(n);
00311             break;
00312         case T_PX:
00313             n = getnum_str(&startp, endp);
00314             if (n < 0)
00315                 return (-1);
00316             PUTSHORT(n, cp);
00317             ShrinkBuffer(INT16SZ);
00318             for (i = 0; i < 2; i++) {
00319                 if (!getword_str(buf2, sizeof buf2, &startp,
00320                          endp))
00321                     return (-1);
00322                 n = dn_comp(buf2, cp, buflen, dnptrs,
00323                         lastdnptr);
00324                 if (n < 0)
00325                     return (-1);
00326                 cp += n;
00327                 ShrinkBuffer(n);
00328             }
00329             break;
00330         case T_WKS:
00331         case T_HINFO:
00332         case T_TXT:
00333         case T_X25:
00334         case T_ISDN:
00335         case T_NSAP:
00336         case T_LOC:
00337             /* XXX - more fine tuning needed here */
00338             ShrinkBuffer(rrecp->r_size);
00339             memcpy(cp, rrecp->r_data, rrecp->r_size);
00340             cp += rrecp->r_size;
00341             break;
00342         default:
00343             return (-1);
00344         } /*switch*/
00345         n = (u_int16_t)((cp - sp2) - INT16SZ);
00346         PUTSHORT(n, sp2);
00347     } /*for*/
00348 
00349     hp->qdcount = htons(counts[0]);
00350     hp->ancount = htons(counts[1]);
00351     hp->nscount = htons(counts[2]);
00352     hp->arcount = htons(counts[3]);
00353     return ((int)(cp - buf));
00354 }
00355 
00356 /*
00357  * Get a whitespace delimited word from a string (not file)
00358  * into buf. modify the start pointer to point after the
00359  * word in the string.
00360  */
00361 static int
00362 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
00363         char *cp;
00364         int c;
00365 
00366         for (cp = buf; *startpp <= endp; ) {
00367                 c = **startpp;
00368                 if (isspace(c) || c == '\0') {
00369                         if (cp != buf) /* trailing whitespace */
00370                                 break;
00371                         else { /* leading whitespace */
00372                                 (*startpp)++;
00373                                 continue;
00374                         }
00375                 }
00376                 (*startpp)++;
00377                 if (cp >= buf+size-1)
00378                         break;
00379                 *cp++ = (u_char)c;
00380         }
00381         *cp = '\0';
00382         return (cp != buf);
00383 }
00384 
00385 /*
00386  * Get a whitespace delimited number from a string (not file) into buf
00387  * update the start pointer to point after the number in the string.
00388  */
00389 static int
00390 getnum_str(u_char **startpp, u_char *endp) {
00391         int c;
00392         int n;
00393         int seendigit = 0;
00394         int m = 0;
00395 
00396         for (n = 0; *startpp <= endp; ) {
00397                 c = **startpp;
00398                 if (isspace(c) || c == '\0') {
00399                         if (seendigit) /* trailing whitespace */
00400                                 break;
00401                         else { /* leading whitespace */
00402                                 (*startpp)++;
00403                                 continue;
00404                         }
00405                 }
00406                 if (c == ';') {
00407                         while ((*startpp <= endp) &&
00408                    ((c = **startpp) != '\n'))
00409                     (*startpp)++;
00410                         if (seendigit)
00411                                 break;
00412                         continue;
00413                 }
00414                 if (!isdigit(c)) {
00415                         if (c == ')' && seendigit) {
00416                                 (*startpp)--;
00417                                 break;
00418                         }
00419             return (-1);
00420                 }
00421                 (*startpp)++;
00422                 n = n * 10 + (c - '0');
00423                 seendigit = 1;
00424         }
00425         return (n + m);
00426 }
00427 
00428 /*
00429  * Allocate a resource record buffer & save rr info.
00430  */
00431 ns_updrec *
00432 res_mkupdrec(int section, const char *dname,
00433          u_int class, u_int type, u_long ttl) {
00434     ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
00435 
00436     if (!rrecp || !(rrecp->r_dname = strdup(dname)))
00437         return (NULL);
00438     rrecp->r_class = (u_int16_t)class;
00439     rrecp->r_type = (u_int16_t)type;
00440     rrecp->r_ttl = (u_int32_t)ttl;
00441     rrecp->r_section = (u_int8_t)section;
00442     return (rrecp);
00443 }
00444 
00445 /*
00446  * Free a resource record buffer created by res_mkupdrec.
00447  */
00448 void
00449 res_freeupdrec(ns_updrec *rrecp) {
00450     /* Note: freeing r_dp is the caller's responsibility. */
00451     if (rrecp->r_dname != NULL)
00452         free(rrecp->r_dname);
00453     free(rrecp);
00454 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines