2004-11-16 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / drivers / lib / ip / transport / tcp / if.c
1 /*
2 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
3 * All rights reserved.
4 *
5 * This file is part of the Flux OSKit. The OSKit is free software, also known
6 * as "open source;" you can redistribute it and/or modify it under the terms
7 * of the GNU General Public License (GPL), version 2, as published by the Free
8 * Software Foundation (FSF). To explore alternate licensing terms, contact
9 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
10 *
11 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
14 * received a copy of the GPL along with the OSKit; see the file COPYING. If
15 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
16 */
17
18 #include "precomp.h"
19
20 int if_index = 0;
21 struct ifaddr **ifnet_addrs;
22
23 int ifqmaxlen = OSK_IFQ_MAXLEN;
24 struct ifnet *ifnet;
25
26 /*
27 * Network interface utility routines.
28 *
29 * Routines with ifa_ifwith* names take sockaddr *'s as
30 * parameters.
31 */
32
33 PVOID TCPPrepareInterface( PIP_INTERFACE IF ) {
34 NTSTATUS Status;
35 POSK_IFADDR ifaddr = exAllocatePool
36 ( NonPagedPool, sizeof(*ifaddr) + 2 * sizeof( struct sockaddr_in ) );
37 struct sockaddr_in *addr_in = (struct sockaddr_in *)&ifaddr[1];
38 struct sockaddr_in *dstaddr_in = (struct sockaddr_in *)&addr_in[1];
39 if( !ifaddr ) return NULL;
40
41 TI_DbgPrint(MID_TRACE,("Called\n"));
42
43 ifaddr->ifa_dstaddr = (struct sockaddr *)dstaddr_in;
44 /* XXX - Point-to-point interfaces not supported yet */
45 memset( &ifaddr->ifa_dstaddr, 0, sizeof( struct sockaddr ) );
46
47 ifaddr->ifa_addr = (struct sockaddr *)addr_in;
48 Status = GetInterfaceIPv4Address( IF,
49 ADE_UNICAST,
50 (PULONG)&addr_in->sin_addr.s_addr );
51
52 if( !NT_SUCCESS(Status) )
53 addr_in->sin_addr.s_addr = 0;
54
55 TI_DbgPrint(MID_TRACE,("Prepare interface %x : addr %x\n",
56 IF, addr_in->sin_addr.s_addr));
57
58 ifaddr->ifa_flags = 0; /* XXX what goes here? */
59 ifaddr->ifa_refcnt = 0; /* Anachronistic */
60 ifaddr->ifa_metric = 1; /* We can get it like in ninfo.c, if we want */
61 ifaddr->ifa_mtu = IF->MTU;
62
63 TI_DbgPrint(MID_TRACE,("Leaving\n"));
64
65 return ifaddr;
66 }
67
68 POSK_IFADDR TCPFindInterface( void *ClientData,
69 OSK_UINT AddrType,
70 OSK_UINT FindType,
71 OSK_SOCKADDR *ReqAddr,
72 OSK_IFADDR *Interface ) {
73 PNEIGHBOR_CACHE_ENTRY NCE;
74 IP_ADDRESS Destination;
75 struct sockaddr_in *addr_in = (struct sockaddr_in *)ReqAddr;
76
77 TI_DbgPrint(MID_TRACE,("called for type %d\n", FindType));
78
79 if( !ReqAddr ) {
80 TI_DbgPrint(MID_TRACE,("no addr or no ifaddr (%x)\n", ReqAddr));
81 return NULL;
82 }
83
84 Destination.Type = IP_ADDRESS_V4;
85 Destination.Address.IPv4Address = addr_in->sin_addr.s_addr;
86
87 TI_DbgPrint(MID_TRACE,("Address is %x\n", addr_in->sin_addr.s_addr));
88
89 NCE = RouterGetRoute(&Destination, NULL);
90
91 if( !NCE || !NCE->Interface ) {
92 TI_DbgPrint(MID_TRACE,("no neighbor cache or no interface (%x %x)\n",
93 NCE, NCE->Interface));
94 return NULL;
95 }
96
97 TI_DbgPrint(MID_TRACE,("NCE: %x\n", NCE));
98 TI_DbgPrint(MID_TRACE,("NCE->Interface: %x\n", NCE->Interface));
99 TI_DbgPrint(MID_TRACE,("NCE->Interface->TCPContext: %x\n",
100 NCE->Interface->TCPContext));
101
102 addr_in = (struct sockaddr_in *)
103 ((POSK_IFADDR)NCE->Interface->TCPContext)->ifa_addr;
104 TI_DbgPrint(MID_TRACE,("returning addr %x\n", addr_in->sin_addr.s_addr));
105
106 return NCE->Interface->TCPContext;
107 }
108