(linenum→info "unix/slp.c:2238")

glibc/2.7/inet/inet6_rth.c

    1: /* Copyright (C) 2006 Free Software Foundation, Inc.
    2:    This file is part of the GNU C Library.
    3:    Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.
    4: 
    5:    The GNU C Library is free software; you can redistribute it and/or
    6:    modify it under the terms of the GNU Lesser General Public
    7:    License as published by the Free Software Foundation; either
    8:    version 2.1 of the License, or (at your option) any later version.
    9: 
   10:    The GNU C Library is distributed in the hope that it will be useful,
   11:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13:    Lesser General Public License for more details.
   14: 
   15:    You should have received a copy of the GNU Lesser General Public
   16:    License along with the GNU C Library; if not, write to the Free
   17:    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   18:    02111-1307 USA.  */
   19: 
   20: #include <string.h>
   21: #include <netinet/in.h>
   22: #include <netinet/ip6.h>
   23: 
   24: 
   25: /* RFC 3542, 7.1
   26: 
   27:    This function returns the number of bytes required to hold a
   28:    Routing header of the specified type containing the specified
   29:    number of segments (addresses).  For an IPv6 Type 0 Routing header,
   30:    the number of segments must be between 0 and 127, inclusive.  */
   31: socklen_t
   32: inet6_rth_space (int type, int segments)
   33: {
   34:   switch (type)
   35:     {
   36:     case IPV6_RTHDR_TYPE_0:
   37:       if (segments < 0 || segments > 127)
   38:         return 0;
   39: 
   40:       return sizeof (struct ip6_rthdr0) + segments * sizeof (struct in6_addr);
   41:     }
   42: 
   43:   return 0;
   44: }
   45: 
   46: 
   47: /* RFC 3542, 7.2
   48: 
   49:    This function initializes the buffer pointed to by BP to contain a
   50:    Routing header of the specified type and sets ip6r_len based on the
   51:    segments parameter.  */
   52: void *
   53: inet6_rth_init (void *bp, socklen_t bp_len, int type, int segments)
   54: {
   55:   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
   56: 
   57:   switch (type)
   58:     {
   59:     case IPV6_RTHDR_TYPE_0:
   60:       /* Make sure the parameters are valid and the buffer is large enough.  */
   61:       if (segments < 0 || segments > 127)
   62:         break;
   63: 
   64:       socklen_t len = (sizeof (struct ip6_rthdr0)
   65:                        + segments * sizeof (struct in6_addr));
   66:       if (len > bp_len)
   67:         break;
   68: 
   69:       /* Some implementations seem to initialize the whole memory area.  */
   70:       memset (bp, '\0', len);
   71: 
   72:       /* Length in units of 8 octets.  */
   73:       rthdr->ip6r_len = segments * sizeof (struct in6_addr) / 8;
   74:       rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
   75:       return bp;
   76:     }
   77: 
   78:   return NULL;
   79: }
   80: 
   81: 
   82: /* RFC 3542, 7.3
   83: 
   84:    This function adds the IPv6 address pointed to by addr to the end of
   85:    the Routing header being constructed.  */
   86: int
   87: inet6_rth_add (void *bp, const struct in6_addr *addr)
   88: {
   89:   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
   90: 
   91:   switch (rthdr->ip6r_type)
   92:     {
   93:       struct ip6_rthdr0 *rthdr0;
   94:     case IPV6_RTHDR_TYPE_0:
   95:       rthdr0 = (struct ip6_rthdr0 *) rthdr;
   96: 
   97:       memcpy (&rthdr0->ip6r0_addr[rthdr0->ip6r0_segleft++],
   98:               addr, sizeof (struct in6_addr));
   99: 
  100:       return 0;
  101:     }
  102: 
  103:   return -1;
  104: }
  105: 
  106: 
  107: /* RFC 3542, 7.4
  108: 
  109:    This function takes a Routing header extension header (pointed to by
  110:    the first argument) and writes a new Routing header that sends
  111:    datagrams along the reverse of that route.  The function reverses the
  112:    order of the addresses and sets the segleft member in the new Routing
  113:    header to the number of segments.  */
  114: int
  115: inet6_rth_reverse (const void *in, void *out)
  116: {
  117:   struct ip6_rthdr *in_rthdr = (struct ip6_rthdr *) in;
  118: 
  119:   switch (in_rthdr->ip6r_type)
  120:     {
  121:       struct ip6_rthdr0 *in_rthdr0;
  122:       struct ip6_rthdr0 *out_rthdr0;
  123:     case IPV6_RTHDR_TYPE_0:
  124:       in_rthdr0 = (struct ip6_rthdr0 *) in;
  125:       out_rthdr0 = (struct ip6_rthdr0 *) out;
  126: 
  127:       /* Copy header, not the addresses.  The memory regions can overlap.  */
  128:       memmove (out_rthdr0, in_rthdr0, sizeof (struct ip6_rthdr0));
  129: 
  130:       int total = in_rthdr0->ip6r0_segleft * 8 / sizeof (struct in6_addr);
  131:       for (int i = 0; i < total / 2; ++i)
  132:         {
  133:           /* Remember, IN_RTHDR0 and OUT_RTHDR0 might overlap.  */
  134:           struct in6_addr temp = in_rthdr0->ip6r0_addr[i];
  135:           out_rthdr0->ip6r0_addr[i] = in_rthdr0->ip6r0_addr[total - 1 - i];
  136:           out_rthdr0->ip6r0_addr[total - 1 - i] = temp;
  137:         }
  138:       if (total % 2 != 0 && in != out)
  139:         out_rthdr0->ip6r0_addr[total / 2] = in_rthdr0->ip6r0_addr[total / 2];
  140: 
  141:       return 0;
  142:     }
  143: 
  144:   return -1;
  145: }
  146: 
  147: 
  148: /* RFC 3542, 7.5
  149: 
  150:    This function returns the number of segments (addresses) contained in
  151:    the Routing header described by BP.  */
  152: int
  153: inet6_rth_segments (const void *bp)
  154: {
  155:   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
  156: 
  157:   switch (rthdr->ip6r_type)
  158:     {
  159:     case IPV6_RTHDR_TYPE_0:
  160: 
  161:       return rthdr->ip6r_len * 8 / sizeof (struct in6_addr);
  162:     }
  163: 
  164:   return -1;
  165: }
  166: 
  167: 
  168: /* RFC 3542, 7.6
  169: 
  170:    This function returns a pointer to the IPv6 address specified by
  171:    index (which must have a value between 0 and one less than the
  172:    value returned by 'inet6_rth_segments') in the Routing header
  173:    described by BP.  */
  174: struct in6_addr *
  175: inet6_rth_getaddr (const void *bp, int index)
  176: {
  177:   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
  178: 
  179:   switch (rthdr->ip6r_type)
  180:     {
  181:        struct ip6_rthdr0 *rthdr0;
  182:     case IPV6_RTHDR_TYPE_0:
  183:       rthdr0 = (struct ip6_rthdr0 *) rthdr;
  184: 
  185:       if (index >= rthdr0->ip6r0_len * 8 / sizeof (struct in6_addr))
  186:         break;
  187: 
  188:       return &rthdr0->ip6r0_addr[index];
  189:     }
  190: 
  191:   return NULL;
  192: }
Syntax (Markdown)