1*7166Ssam /* in.c 4.2 82/06/13 */ 27159Ssam 37159Ssam #include "../h/param.h" 47159Ssam #include "../h/mbuf.h" 57159Ssam #include "../h/protosw.h" 67159Ssam #include "../h/socket.h" 77159Ssam #include "../h/socketvar.h" 87159Ssam #include "../net/in.h" 97159Ssam #include "../net/in_systm.h" 10*7166Ssam #include "../net/if.h" 11*7166Ssam #include "../net/route.h" 127159Ssam #include "../net/af.h" 137159Ssam 147159Ssam #ifdef INET 157159Ssam inet_hash(sin, hp) 167159Ssam register struct sockaddr_in *sin; 177159Ssam struct afhash *hp; 187159Ssam { 197159Ssam COUNT(INET_HASH); 207159Ssam hp->afh_nethash = in_netof(sin->sin_addr); 217159Ssam hp->afh_hosthash = ntohl(sin->sin_addr.s_addr); 227159Ssam if (hp->afh_hosthash < 0) 237159Ssam hp->afh_hosthash = -hp->afh_hosthash; 247159Ssam } 257159Ssam 267159Ssam inet_netmatch(sin1, sin2) 277159Ssam struct sockaddr_in *sin1, *sin2; 287159Ssam { 297159Ssam COUNT(INET_NETMATCH); 307159Ssam return (sin1->sin_addr.s_net == sin2->sin_addr.s_net); 317159Ssam } 327159Ssam 337159Ssam /* 347159Ssam * Formulate an Internet address from network + host. Used in 357159Ssam * building addresses stored in the ifnet structure. 367159Ssam */ 377159Ssam struct in_addr 387159Ssam if_makeaddr(net, host) 397159Ssam int net, host; 407159Ssam { 417159Ssam u_long addr; 427159Ssam 437159Ssam COUNT(IF_MAKEADDR); 447159Ssam if (net < 128) 457159Ssam addr = (net << 24) | host; 467159Ssam else if (net < 65536) 477159Ssam addr = (net << 16) | host; 487159Ssam else 497159Ssam addr = (net << 8) | host; 507159Ssam #ifdef vax 517159Ssam addr = htonl(addr); 527159Ssam #endif 537159Ssam return (*(struct in_addr *)&addr); 547159Ssam } 557159Ssam 567159Ssam /* 577159Ssam * Return the network number from an internet 587159Ssam * address; handles class a/b/c network #'s. 597159Ssam */ 607159Ssam in_netof(in) 617159Ssam struct in_addr in; 627159Ssam { 637159Ssam 647159Ssam return (IN_NETOF(in)); 657159Ssam } 667159Ssam 677159Ssam /* 687159Ssam * Return the local network address portion of an 697159Ssam * internet address; handles class a/b/c network 707159Ssam * number formats. 717159Ssam */ 727159Ssam in_lnaof(in) 737159Ssam struct in_addr in; 747159Ssam { 757159Ssam 767159Ssam return (IN_LNAOF(in)); 777159Ssam } 787159Ssam 797159Ssam /* 807159Ssam * Initialize an interface's routing 817159Ssam * table entry according to the network. 827159Ssam * INTERNET SPECIFIC. 837159Ssam */ 847159Ssam if_rtinit(ifp, flags) 857159Ssam register struct ifnet *ifp; 867159Ssam int flags; 877159Ssam { 887159Ssam struct sockaddr_in sin; 897159Ssam 907159Ssam COUNT(IF_RTINIT); 917159Ssam if (ifp->if_flags & IFF_ROUTE) 927159Ssam return; 937159Ssam bzero((caddr_t)&sin, sizeof (sin)); 947159Ssam sin.sin_family = AF_INET; 957159Ssam sin.sin_addr = if_makeaddr(ifp->if_net, 0); 967159Ssam rtinit(&sin, &ifp->if_addr, flags); 977159Ssam } 987159Ssam #endif 99