home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / routed / af.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  4.6 KB  |  181 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. /*static char sccsid[] = "from: @(#)af.c    5.11 (Berkeley) 2/28/91";*/
  36. static char rcsid[] = "$Id: af.c,v 1.1 1994/05/23 09:08:11 rzsfl Exp rzsfl $";
  37. #endif /* not lint */
  38.  
  39. #include "defs.h"
  40.  
  41. /*
  42.  * Address family support routines
  43.  */
  44. int    inet_hash(), inet_netmatch(), inet_output(),
  45.     inet_portmatch(), inet_portcheck(),
  46.     inet_checkhost(), inet_rtflags(), inet_sendroute(), inet_canon();
  47. char    *inet_format();
  48.  
  49. #define NIL    { 0 }
  50. #define    INET \
  51.     { inet_hash,        inet_netmatch,        inet_output, \
  52.       inet_portmatch,    inet_portcheck,        inet_checkhost, \
  53.       inet_rtflags,        inet_sendroute,        inet_canon, \
  54.       inet_format \
  55.     }
  56.  
  57. struct afswitch afswitch[AF_MAX] = {
  58.     NIL,        /* 0- unused */
  59.     NIL,        /* 1- Unix domain, unused */
  60.     INET,        /* Internet */
  61. };
  62.  
  63. int af_max = sizeof(afswitch) / sizeof(afswitch[0]);
  64.  
  65. struct sockaddr_in inet_default = {
  66. #ifdef RTM_ADD
  67.     sizeof (inet_default),
  68. #endif
  69.     AF_INET, INADDR_ANY };
  70.  
  71. inet_hash(sin, hp)
  72.     register struct sockaddr_in *sin;
  73.     struct afhash *hp;
  74. {
  75.     register u_long n;
  76.  
  77.     n = inet_netof(sin->sin_addr);
  78.     if (n)
  79.         while ((n & 0xff) == 0)
  80.         n >>= 8;
  81.     hp->afh_nethash = n;
  82.     hp->afh_hosthash = ntohl(sin->sin_addr.s_addr);
  83.     hp->afh_hosthash &= 0x7fffffff;
  84. }
  85.  
  86. inet_netmatch(sin1, sin2)
  87.     struct sockaddr_in *sin1, *sin2;
  88. {
  89.  
  90.     return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr));
  91. }
  92.  
  93. /*
  94.  * Verify the message is from the right port.
  95.  */
  96. inet_portmatch(sin)
  97.     register struct sockaddr_in *sin;
  98. {
  99.     
  100.     return (sin->sin_port == sp->s_port);
  101. }
  102.  
  103. /*
  104.  * Verify the message is from a "trusted" port.
  105.  */
  106. inet_portcheck(sin)
  107.     struct sockaddr_in *sin;
  108. {
  109.  
  110.     return (ntohs(sin->sin_port) <= IPPORT_RESERVED);
  111. }
  112.  
  113. /*
  114.  * Internet output routine.
  115.  */
  116. inet_output(s, flags, sin, size)
  117.     int s, flags;
  118.     struct sockaddr_in *sin;
  119.     int size;
  120. {
  121.     struct sockaddr_in dst;
  122.  
  123.     dst = *sin;
  124.     sin = &dst;
  125.     if (sin->sin_port == 0)
  126.         sin->sin_port = sp->s_port;
  127. #ifndef __linux__
  128.     if (sin->sin_len == 0)
  129.         sin->sin_len = sizeof (*sin);
  130. #endif
  131.     flags = 0;
  132.     if (sendto(s, packet, size, flags,
  133.         (struct sockaddr *)sin, sizeof (*sin)) < 0)
  134.         perror("sendto");
  135. }
  136.  
  137. /*
  138.  * Return 1 if the address is believed
  139.  * for an Internet host -- THIS IS A KLUDGE.
  140.  */
  141. inet_checkhost(sin)
  142.     struct sockaddr_in *sin;
  143. {
  144.     u_long i = ntohl(sin->sin_addr.s_addr);
  145.  
  146. #undef    IN_EXPERIMENTAL
  147. #ifndef IN_EXPERIMENTAL
  148. #define    IN_EXPERIMENTAL(i)    (((long) (i) & 0xe0000000) == 0xe0000000)
  149. #endif
  150.  
  151.     if (IN_EXPERIMENTAL(i) || sin->sin_port != 0)
  152.         return (0);
  153.     if (i != 0 && (i & 0xff000000) == 0)
  154.         return (0);
  155. #ifndef __linux__
  156.     for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++)
  157.         if (sin->sin_zero[i])
  158.             return (0);
  159. #endif
  160.     return (1);
  161. }
  162.  
  163. inet_canon(sin)
  164.     struct sockaddr_in *sin;
  165. {
  166.  
  167.     sin->sin_port = 0;
  168. #ifndef __linux__
  169.     sin->sin_len = sizeof(*sin);
  170. #endif
  171. }
  172.  
  173. char *
  174. inet_format(sin)
  175.     struct sockaddr_in *sin;
  176. {
  177.     char *inet_ntoa();
  178.  
  179.     return (inet_ntoa(sin->sin_addr));
  180. }
  181.