1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 static char sccsid[] = "@(#)af.c 5.6 (Berkeley) 06/15/87"; 9 #endif not lint 10 11 #include "defs.h" 12 13 /* 14 * Address family support routines 15 */ 16 int inet_hash(), inet_netmatch(), inet_output(), 17 inet_portmatch(), inet_portcheck(), 18 inet_checkhost(), inet_rtflags(), inet_sendroute(), inet_canon(); 19 char *inet_format(); 20 21 #define NIL { 0 } 22 #define INET \ 23 { inet_hash, inet_netmatch, inet_output, \ 24 inet_portmatch, inet_portcheck, inet_checkhost, \ 25 inet_rtflags, inet_sendroute, inet_canon, \ 26 inet_format \ 27 } 28 29 struct afswitch afswitch[AF_MAX] = { 30 NIL, /* 0- unused */ 31 NIL, /* 1- Unix domain, unused */ 32 INET, /* Internet */ 33 }; 34 35 int af_max = sizeof(afswitch) / sizeof(afswitch[0]); 36 37 struct sockaddr_in inet_default = { AF_INET, INADDR_ANY }; 38 39 inet_hash(sin, hp) 40 register struct sockaddr_in *sin; 41 struct afhash *hp; 42 { 43 register u_long n; 44 45 n = inet_netof(sin->sin_addr); 46 if (n) 47 while ((n & 0xff) == 0) 48 n >>= 8; 49 hp->afh_nethash = n; 50 hp->afh_hosthash = ntohl(sin->sin_addr.s_addr); 51 hp->afh_hosthash &= 0x7fffffff; 52 } 53 54 inet_netmatch(sin1, sin2) 55 struct sockaddr_in *sin1, *sin2; 56 { 57 58 return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr)); 59 } 60 61 /* 62 * Verify the message is from the right port. 63 */ 64 inet_portmatch(sin) 65 register struct sockaddr_in *sin; 66 { 67 68 return (sin->sin_port == sp->s_port); 69 } 70 71 /* 72 * Verify the message is from a "trusted" port. 73 */ 74 inet_portcheck(sin) 75 struct sockaddr_in *sin; 76 { 77 78 return (ntohs(sin->sin_port) <= IPPORT_RESERVED); 79 } 80 81 /* 82 * Internet output routine. 83 */ 84 inet_output(s, flags, sin, size) 85 int s, flags; 86 struct sockaddr_in *sin; 87 int size; 88 { 89 struct sockaddr_in dst; 90 91 dst = *sin; 92 sin = &dst; 93 if (sin->sin_port == 0) 94 sin->sin_port = sp->s_port; 95 if (sendto(s, packet, size, flags, sin, sizeof (*sin)) < 0) 96 perror("sendto"); 97 } 98 99 /* 100 * Return 1 if the address is believed 101 * for an Internet host -- THIS IS A KLUDGE. 102 */ 103 inet_checkhost(sin) 104 struct sockaddr_in *sin; 105 { 106 u_long i = ntohl(sin->sin_addr.s_addr); 107 108 #ifndef IN_EXPERIMENTAL 109 #define IN_EXPERIMENTAL(i) (((long) (i) & 0xe0000000) == 0xe0000000) 110 #endif 111 112 if (IN_EXPERIMENTAL(i) || sin->sin_port != 0) 113 return (0); 114 if (i != 0 && (i & 0xff000000) == 0) 115 return (0); 116 for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++) 117 if (sin->sin_zero[i]) 118 return (0); 119 return (1); 120 } 121 122 inet_canon(sin) 123 struct sockaddr_in *sin; 124 { 125 126 sin->sin_port = 0; 127 } 128 129 char * 130 inet_format(sin) 131 struct sockaddr_in *sin; 132 { 133 char *inet_ntoa(); 134 135 return (inet_ntoa(sin->sin_addr)); 136 } 137