ekg2

plugins/sniff/sniff_dns.h

Idź do dokumentacji tego pliku.
00001 void *mempcpy(void *dest, const void *src, size_t n);   /* forward */
00002 
00003 
00004 /* Copyrights notes: */
00005 
00006 /* symbols: digits[], special(), printable(), ns_name_unpack(), ns_name_ntop(), ns_name_uncompress()
00007  *      copied from glibc resolv/res_data.c
00008  * under licence: */
00009 
00010 /*
00011  * Copyright (c) 1996,1999 by Internet Software Consortium.
00012  *
00013  * Permission to use, copy, modify, and distribute this software for any
00014  * purpose with or without fee is hereby granted, provided that the above
00015  * copyright notice and this permission notice appear in all copies.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00018  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00020  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00021  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00022  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00023  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00024  * SOFTWARE.
00025  */
00026 
00027 /* symbol: dn_expand() 
00028  *      copied from glibc resolv/res_comp.c 
00029  * under licences: */
00030 
00031 /*
00032  * Copyright (c) 1985, 1993
00033  *    The Regents of the University of California.  All rights reserved.
00034  *
00035  * Redistribution and use in source and binary forms, with or without
00036  * modification, are permitted provided that the following conditions
00037  * are met:
00038  * 1. Redistributions of source code must retain the above copyright
00039  *    notice, this list of conditions and the following disclaimer.
00040  * 2. Redistributions in binary form must reproduce the above copyright
00041  *    notice, this list of conditions and the following disclaimer in the
00042  *    documentation and/or other materials provided with the distribution.
00043  * 4. Neither the name of the University nor the names of its contributors
00044  *    may be used to endorse or promote products derived from this software
00045  *    without specific prior written permission.
00046  *
00047  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00048  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00049  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00050  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00051  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00052  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00053  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00054  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00055  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00056  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00057  * SUCH DAMAGE.
00058  */
00059 
00060 /*
00061  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00062  *
00063  * Permission to use, copy, modify, and distribute this software for any
00064  * purpose with or without fee is hereby granted, provided that the above
00065  * copyright notice and this permission notice appear in all copies, and that
00066  * the name of Digital Equipment Corporation not be used in advertising or
00067  * publicity pertaining to distribution of the document or software without
00068  * specific, written prior permission.
00069  *
00070  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00071  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00072  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00073  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00074  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00075  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00076  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00077  * SOFTWARE.
00078  */
00079 
00080 /*
00081  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00082  *
00083  * Permission to use, copy, modify, and distribute this software for any
00084  * purpose with or without fee is hereby granted, provided that the above
00085  * copyright notice and this permission notice appear in all copies.
00086  *
00087  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00088  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00089  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00090  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00091  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00092  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00093  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00094  * SOFTWARE.
00095  */
00096 
00097 /* all struct and magic values
00098  *      copied from: glibc resolv/arpa/nameser.h && resolv/arpa/nameser_compat.h
00099  * under licences: */
00100 
00101 /*
00102  * Copyright (c) 1983, 1989, 1993
00103  *    The Regents of the University of California.  All rights reserved.
00104  *
00105  * Redistribution and use in source and binary forms, with or without
00106  * modification, are permitted provided that the following conditions
00107  * are met:
00108  * 1. Redistributions of source code must retain the above copyright
00109  *    notice, this list of conditions and the following disclaimer.
00110  * 2. Redistributions in binary form must reproduce the above copyright
00111  *    notice, this list of conditions and the following disclaimer in the
00112  *    documentation and/or other materials provided with the distribution.
00113  * 4. Neither the name of the University nor the names of its contributors
00114  *    may be used to endorse or promote products derived from this software
00115  *    without specific prior written permission.
00116  *
00117  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00118  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00119  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00120  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00121  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00122  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00123  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00124  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00125  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00126  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00127  * SUCH DAMAGE.
00128  */
00129 
00130 /*
00131  * Copyright (c) 1996-1999 by Internet Software Consortium.
00132  *
00133  * Permission to use, copy, modify, and distribute this software for any
00134  * purpose with or without fee is hereby granted, provided that the above
00135  * copyright notice and this permission notice appear in all copies.
00136  *
00137  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00138  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00139  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00140  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00141  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00142  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00143  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00144  * SOFTWARE.
00145  */
00146 
00147 typedef struct {
00148         unsigned        id :16;         /* query identification number */
00149         /* fields in third byte */
00150         unsigned        rd :1;          /* recursion desired */
00151         unsigned        tc :1;          /* truncated message */
00152         unsigned        aa :1;          /* authoritive answer */
00153         unsigned        opcode :4;      /* purpose of message */
00154         unsigned        qr :1;          /* response flag */
00155         /* fields in fourth byte */
00156         unsigned        rcode :4;       /* response code */
00157         unsigned        cd: 1;          /* checking disabled by resolver */
00158         unsigned        ad: 1;          /* authentic data from named */
00159         unsigned        unused :1;      /* unused bits (MBZ as of 4.9.3a3) */
00160         unsigned        ra :1;          /* recursion available */
00161         /* remaining bytes */
00162         unsigned        qdcount :16;    /* number of question entries */
00163         unsigned        ancount :16;    /* number of answer entries */
00164         unsigned        nscount :16;    /* number of authority entries */
00165         unsigned        arcount :16;    /* number of resource entries */
00166 } DNS_HEADER;
00167 
00168 
00169 #define DNS_HFIXEDSZ    12              /* #/bytes of fixed data in header */
00170 #define DNS_QFIXEDSZ     4              /* #/bytes of fixed data in query */
00171 
00172 #define NOERROR         0               /* No error occurred */
00173 #define FORMERR         1               /* format error */
00174 #define SERVFAIL        2               /* Server failure */
00175 #define NXDOMAIN        3               /* Name error */
00176 #define NOTIMP          4               /* Unimplemented */
00177 #define REFUSED         5               /* Operation refused */
00178 
00179 #define T_A             1               /* (IN) A */
00180 #define T_NS            2               /* (IN) NS */
00181 #define T_CNAME         5               /* (IN) CNAME */
00182 #define T_PTR           12              /* (IN) PTR */
00183 #define T_MX            15              /* (IN) MX */
00184 #define T_AAAA          28              /* (IN) AAAA */
00185 #define T_SRV           33              /* (IN) SRV */
00186 
00187 #define NS_MAXCDNAME    255             /* maximum compressed domain name */
00188 
00189 #define NS_CMPRSFLGS    0xc0            /* Flag bits indicating name compression. */
00190 
00191 static const char       digits[] = "0123456789";
00192 
00193 static int special(int ch) {
00194         switch (ch) {
00195                 case 0x22: /* '"' */
00196                 case 0x2E: /* '.' */
00197                 case 0x3B: /* ';' */
00198                 case 0x5C: /* '\\' */
00199                         /* Special modifiers in zone files. */
00200                 case 0x40: /* '@' */
00201                 case 0x24: /* '$' */
00202                         return (1);
00203                 default:
00204                         return (0);
00205         }
00206 }
00207 
00208 static int printable(int ch) {
00209         return (ch > 0x20 && ch < 0x7f);
00210 }
00211 
00212 int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, u_char *dst, size_t dstsiz)
00213 {
00214         const u_char *srcp, *dstlim;
00215         u_char *dstp;
00216         int n, len, checked;
00217 
00218         len = -1;
00219         checked = 0;
00220         dstp = dst;
00221         srcp = src;
00222         dstlim = dst + dstsiz;
00223         if (srcp < msg || srcp >= eom) {
00224                 errno = EMSGSIZE;
00225                 return (-1);
00226         }
00227         /* Fetch next label in domain name. */
00228         while ((n = *srcp++) != 0) {
00229                 /* Check for indirection. */
00230                 switch (n & NS_CMPRSFLGS) {
00231                 case 0x40:
00232                         if (n == 0x41) {
00233                                 if (dstp + 1 >= dstlim) {
00234                                         errno = EMSGSIZE;
00235                                         return (-1);
00236                                 }
00237                                 *dstp++ = 0x41;
00238                                 n = *srcp++ / 8;
00239                                 ++checked;
00240                         } else {
00241                                 errno = EMSGSIZE;
00242                                 return (-1);            /* flag error */
00243                         }
00244                         /* FALLTHROUGH */
00245                 case 0:
00246                         /* Limit checks. */
00247                         if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
00248                                 errno = EMSGSIZE;
00249                                 return (-1);
00250                         }
00251                         checked += n + 1;
00252                         dstp = mempcpy(dstp, srcp - 1, n + 1);
00253                         srcp += n;
00254                         break;
00255 
00256                 case NS_CMPRSFLGS:
00257                         if (srcp >= eom) {
00258                                 errno = EMSGSIZE;
00259                                 return (-1);
00260                         }
00261                         if (len < 0)
00262                                 len = srcp - src + 1;
00263                         srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
00264                         if (srcp < msg || srcp >= eom) {  /* Out of range. */
00265                                 errno = EMSGSIZE;
00266                                 return (-1);
00267                         }
00268                         checked += 2;
00269                         /*
00270                          * Check for loops in the compressed name;
00271                          * if we've looked at the whole message,
00272                          * there must be a loop.
00273                          */
00274                         if (checked >= eom - msg) {
00275                                 errno = EMSGSIZE;
00276                                 return (-1);
00277                         }
00278                         break;
00279 
00280                 default:
00281                         errno = EMSGSIZE;
00282                         return (-1);                    /* flag error */
00283                 }
00284         }
00285         *dstp = '\0';
00286         if (len < 0)
00287                 len = srcp - src;
00288         return (len);
00289 }
00290 
00291 int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
00292         const u_char *cp;
00293         char *dn, *eom;
00294         u_char c;
00295         u_int n;
00296 
00297         cp = src;
00298         dn = dst;
00299         eom = dst + dstsiz;
00300 
00301         while ((n = *cp++) != 0) {
00302                 if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
00303                         /* Some kind of compression pointer. */
00304                         errno = EMSGSIZE;
00305                         return (-1);
00306                 }
00307                 if (dn != dst) {
00308                         if (dn >= eom) {
00309                                 errno = EMSGSIZE;
00310                                 return (-1);
00311                         }
00312                         *dn++ = '.';
00313                 }
00314 
00315                 if (n == 0x41) {
00316                         n = *cp++ / 8;
00317                         if (dn + n * 2 + 4 >= eom) {
00318                                 errno = EMSGSIZE;
00319                                 return (-1);
00320                         }
00321                         *dn++ = '\\';
00322                         *dn++ = '[';
00323                         *dn++ = 'x';
00324 
00325                         while (n-- > 0) {
00326                                 c = *cp++;
00327                                 unsigned u = c >> 4;
00328                                 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
00329                                 u = c & 0xf;
00330                                 *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
00331                         }
00332 
00333                         *dn++ = ']';
00334                         continue;
00335                 }
00336 
00337                 if (dn + n >= eom) {
00338                         errno = EMSGSIZE;
00339                         return (-1);
00340                 }
00341                 for ((void)NULL; n > 0; n--) {
00342                         c = *cp++;
00343                         if (special(c)) {
00344                                 if (dn + 1 >= eom) {
00345                                         errno = EMSGSIZE;
00346                                         return (-1);
00347                                 }
00348                                 *dn++ = '\\';
00349                                 *dn++ = (char)c;
00350                         } else if (!printable(c)) {
00351                                 if (dn + 3 >= eom) {
00352                                         errno = EMSGSIZE;
00353                                         return (-1);
00354                                 }
00355                                 *dn++ = '\\';
00356                                 *dn++ = digits[c / 100];
00357                                 *dn++ = digits[(c % 100) / 10];
00358                                 *dn++ = digits[c % 10];
00359                         } else {
00360                                 if (dn >= eom) {
00361                                         errno = EMSGSIZE;
00362                                         return (-1);
00363                                 }
00364                                 *dn++ = (char)c;
00365                         }
00366                 }
00367         }
00368         if (dn == dst) {
00369                 if (dn >= eom) {
00370                         errno = EMSGSIZE;
00371                         return (-1);
00372                 }
00373                 *dn++ = '.';
00374         }
00375         if (dn >= eom) {
00376                 errno = EMSGSIZE;
00377                 return (-1);
00378         }
00379         *dn++ = '\0';
00380         return (dn - dst);
00381 }
00382 
00383 int ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src, char *dst, size_t dstsiz) {
00384         u_char tmp[NS_MAXCDNAME];
00385         int n;
00386 
00387         if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
00388                 return (-1);
00389         if (ns_name_ntop(tmp, dst, dstsiz) == -1)
00390                 return (-1);
00391         return (n);
00392 }
00393 
00394 int dn_expand(const char *msg, const char *eom, const char *src, char *dst, int dstsiz) {
00395         int n = ns_name_uncompress((u_char *) msg, (u_char *) eom, (u_char *) src, dst, (size_t)dstsiz);
00396 
00397         if (n > 0 && dst[0] == '.')
00398                 dst[0] = '\0';
00399         return (n);
00400 }
 All Struktury Danych Pliki Funkcje Zmienne Definicje typów Wyliczenia Wartości wyliczeń Definicje