2 * OLSR ad-hoc routing table management protocol
3 * Copyright (C) 2004 Thomas Lopatic (thomas@lopatic.de)
5 * Derived from its Linux counterpart.
6 * Copyright (C) 2003 Andreas Tønnesen (andreto@ifi.uio.no)
8 * This file is part of the olsr.org OLSR daemon.
10 * olsr.org is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * olsr.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with olsr.org; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * $Id: net.c,v 1.6 2004/11/17 16:54:41 tlopatic Exp $
31 #define WIN32_LEAN_AND_MEAN
38 void WinSockPError(char *Str);
41 int olsr_printf(int, char *, ...);
43 int getsocket(struct sockaddr *Addr, int BuffSize, char *Int)
49 Sock = socket(AF_INET, SOCK_DGRAM, 0);
53 WinSockPError("getsocket/socket()");
57 if (setsockopt(Sock, SOL_SOCKET, SO_BROADCAST,
58 (char *)&On, sizeof (On)) < 0)
60 WinSockPError("getsocket/setsockopt(SO_BROADCAST)");
65 while (BuffSize > 8192)
67 if (setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char *)&BuffSize,
68 sizeof (BuffSize)) == 0)
75 fprintf(stderr, "Cannot set IPv4 socket receive buffer.\n");
77 if (bind(Sock, Addr, sizeof (struct sockaddr_in)) < 0)
79 WinSockPError("getsocket/bind()");
84 if (WSAIoctl(Sock, FIONBIO, &On, sizeof (On), NULL, 0, &Len, NULL, NULL) < 0)
86 WinSockPError("WSAIoctl");
94 int getsocket6(struct sockaddr_in6 *Addr, int BuffSize, char *Int)
99 Sock = socket(AF_INET6, SOCK_DGRAM, 0);
103 WinSockPError("getsocket6/socket()");
107 if (setsockopt(Sock, SOL_SOCKET, SO_BROADCAST,
108 (char *)&On, sizeof (On)) < 0)
110 WinSockPError("getsocket6/setsockopt(SO_BROADCAST)");
115 while (BuffSize > 8192)
117 if (setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char *)&BuffSize,
118 sizeof (BuffSize)) == 0)
124 if (BuffSize <= 8192)
125 fprintf(stderr, "Cannot set IPv6 socket receive buffer.\n");
127 if (bind(Sock, (struct sockaddr *)Addr, sizeof (struct sockaddr_in6)) < 0)
129 WinSockPError("getsocket6/bind()");
137 static OVERLAPPED RouterOver;
139 int enable_ip_forwarding(int Ver)
142 unsigned int __stdcall (*EnableRouter)(HANDLE *Hand, OVERLAPPED *Over);
147 Lib = LoadLibrary("iphlpapi.dll");
152 EnableRouter = (unsigned int _stdcall (*)(HANDLE *, OVERLAPPED *))
153 GetProcAddress(Lib, "EnableRouter");
155 if (EnableRouter == NULL)
158 memset(&RouterOver, 0, sizeof (OVERLAPPED));
160 RouterOver.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
162 if (RouterOver.hEvent == NULL)
164 PError("CreateEvent()");
168 if (EnableRouter(&Hand, &RouterOver) != ERROR_IO_PENDING)
170 PError("EnableRouter()");
174 olsr_printf(3, "Routing enabled.\n");
179 int disable_ip_forwarding(int Ver)
182 unsigned int __stdcall (*UnenableRouter)(OVERLAPPED *Over,
183 unsigned int *Count);
188 Lib = LoadLibrary("iphlpapi.dll");
193 UnenableRouter = (unsigned int _stdcall (*)(OVERLAPPED *, unsigned int *))
194 GetProcAddress(Lib, "UnenableRouter");
196 if (UnenableRouter == NULL)
199 if (UnenableRouter(&RouterOver, &Count) != NO_ERROR)
201 PError("UnenableRouter()");
205 olsr_printf(3, "Routing disabled, count = %u.\n", Count);
210 int restore_settings(int Ver)
212 disable_ip_forwarding(Ver);
217 static int SetEnableRedirKey(unsigned long New)
224 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
225 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
226 0, KEY_READ | KEY_WRITE, &Key) != ERROR_SUCCESS)
231 if (RegQueryValueEx(Key, "EnableICMPRedirect", NULL, &Type,
232 (unsigned char *)&Old, &Len) != ERROR_SUCCESS ||
239 if (RegSetValueEx(Key, "EnableICMPRedirect", 0, REG_DWORD,
240 (unsigned char *)&New, sizeof (New)))
250 void DisableIcmpRedirects(void)
254 Res = SetEnableRedirKey(0);
259 fprintf(stderr, "\n*** IMPORTANT *** IMPORTANT *** IMPORTANT *** IMPORTANT *** IMPORTANT ***\n\n");
263 fprintf(stderr, "Cannot disable ICMP redirect processing in the registry.\n");
264 fprintf(stderr, "Please disable it manually. Continuing in 3 seconds...\n");
270 fprintf(stderr, "I have disabled ICMP redirect processing in the registry for you.\n");
271 fprintf(stderr, "REBOOT NOW, so that these changes take effect. Exiting...\n\n");