3 * The olsr.org Optimized Link-State Routing daemon (olsrd)
4 * Copyright (c) 2004, Thomas Lopatic (thomas@lopatic.de)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
43 #include <sys/types.h> // for time_t
46 #define WIN32_LEAN_AND_MEAN
61 #define WIDE_STRING(s) L##s
63 #define WIDE_STRING(s) TEXT(s)
66 void WinSockPError(const char *Str);
67 void PError(const char *);
69 void DisableIcmpRedirects(void);
72 gethemusocket(struct sockaddr_in *pin)
76 OLSR_PRINTF(1, " Connecting to switch daemon port 10150...");
78 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
83 /* connect to PORT on HOST */
84 if (connect(sock, (struct sockaddr *)pin, sizeof(*pin)) < 0) {
86 fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
87 printf("connection refused\n");
94 /* Keep TCP socket blocking */
99 getsocket(int BuffSize, struct interface *ifp __attribute__ ((unused)))
101 struct sockaddr_in Addr;
104 int Sock = socket(AF_INET, SOCK_DGRAM, 0);
106 WinSockPError("getsocket/socket()");
110 if (setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char *)&On, sizeof(On)) < 0) {
111 WinSockPError("getsocket/setsockopt(SO_BROADCAST)");
116 if (setsockopt(Sock, SOL_SOCKET, SO_REUSEADDR, (char *)&On, sizeof(On)) < 0) {
117 WinSockPError("getsocket/setsockopt(SO_REUSEADDR)");
122 while (BuffSize > 8192) {
123 if (setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char *)&BuffSize, sizeof(BuffSize)) == 0)
129 if (BuffSize <= 8192) {
130 OLSR_PRINTF(1, "Cannot set IPv4 socket receive buffer.\n");
132 memset(&Addr, 0, sizeof(Addr));
133 Addr.sin_family = AF_INET;
134 Addr.sin_port = htons(olsr_cnf->olsrport);
137 Addr.sin_addr.s_addr = ifp->int_addr.sin_addr.s_addr;
140 if (bind(Sock, (struct sockaddr *)&Addr, sizeof(Addr)) < 0) {
141 WinSockPError("getsocket/bind()");
146 if (WSAIoctl(Sock, FIONBIO, &On, sizeof(On), NULL, 0, &Len, NULL, NULL) < 0) {
147 WinSockPError("WSAIoctl");
156 getsocket6(int BuffSize, struct interface *ifp __attribute__ ((unused)))
158 struct sockaddr_in6 Addr6;
160 int Sock = socket(AF_INET6, SOCK_DGRAM, 0);
162 WinSockPError("getsocket6/socket()");
166 if (setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char *)&On, sizeof(On)) < 0) {
167 WinSockPError("getsocket6/setsockopt(SO_BROADCAST)");
172 if (setsockopt(Sock, SOL_SOCKET, SO_REUSEADDR, (char *)&On, sizeof(On)) < 0) {
173 WinSockPError("getsocket6/setsockopt(SO_REUSEADDR)");
178 while (BuffSize > 8192) {
179 if (setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char *)&BuffSize, sizeof(BuffSize)) == 0)
185 if (BuffSize <= 8192)
186 fprintf(stderr, "Cannot set IPv6 socket receive buffer.\n");
188 memset(&Addr6, 0, sizeof(Addr6));
189 Addr6.sin6_family = AF_INET6;
190 Addr6.sin6_port = htons(olsr_cnf->olsrport);
193 memcpy(&Addr6.sin6_addr, &ifp->int6_addr.sin6_addr, sizeof(struct in6_addr));
196 if (bind(Sock, (struct sockaddr *)&Addr6, sizeof(Addr6)) < 0) {
197 WinSockPError("getsocket6/bind()");
205 static OVERLAPPED RouterOver;
207 void net_os_set_global_ifoptions(void)
210 unsigned int __stdcall(*enable_router)(HANDLE *, OVERLAPPED *);
213 Lib = LoadLibrary(WIDE_STRING("iphlpapi.dll"));
218 enable_router = (unsigned int __stdcall(*)(HANDLE *, OVERLAPPED *))GetProcAddress(Lib, WIDE_STRING("EnableRouter"));
220 if (enable_router == NULL)
223 memset(&RouterOver, 0, sizeof(OVERLAPPED));
225 RouterOver.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
227 if (RouterOver.hEvent == NULL) {
228 PError("CreateEvent()");
232 if (enable_router(&Hand, &RouterOver) != ERROR_IO_PENDING) {
233 PError("EnableRouter()");
237 OLSR_PRINTF(3, "Routing enabled.\n");
243 disable_ip_forwarding(int Ver)
246 unsigned int __stdcall(*unenable_router)(OVERLAPPED *, unsigned int *);
251 Lib = LoadLibrary(WIDE_STRING("iphlpapi.dll"));
256 unenable_router = (unsigned int __stdcall(*)(OVERLAPPED *, unsigned int *))GetProcAddress(Lib, WIDE_STRING("UnenableRouter"));
258 if (unenable_router == NULL)
261 if (unenable_router(&RouterOver, &Count) != NO_ERROR) {
262 PError("UnenableRouter()");
266 OLSR_PRINTF(3, "Routing disabled, count = %u.\n", Count);
273 net_os_restore_ifoptions(void)
275 disable_ip_forwarding(olsr_cnf->ip_version);
281 SetEnableRedirKey(unsigned long New)
289 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ | KEY_WRITE, &Key) !=
295 if (RegQueryValueEx(Key, "EnableICMPRedirect", NULL, &Type, (unsigned char *)&Old, &Len) != ERROR_SUCCESS || Type != REG_DWORD)
298 if (RegSetValueEx(Key, "EnableICMPRedirect", 0, REG_DWORD, (unsigned char *)&New, sizeof(New))) {
311 DisableIcmpRedirects(void)
315 Res = SetEnableRedirKey(0);
320 fprintf(stderr, "\n*** IMPORTANT *** IMPORTANT *** IMPORTANT *** IMPORTANT *** IMPORTANT ***\n\n");
324 fprintf(stderr, "Cannot disable ICMP redirect processing in the registry.\n");
325 fprintf(stderr, "Please disable it manually. Continuing in 3 seconds...\n");
332 fprintf(stderr, "I have disabled ICMP redirect processing in the registry for you.\n");
333 fprintf(stderr, "REBOOT NOW, so that these changes take effect. Exiting...\n\n");
339 join_mcast(struct interface *Nic, int Sock)
341 /* See linux/in6.h */
342 struct ipaddr_str buf;
343 struct ipv6_mreq McastReq;
345 McastReq.ipv6mr_multiaddr = Nic->int6_multaddr.sin6_addr;
346 McastReq.ipv6mr_interface = Nic->if_index;
348 OLSR_PRINTF(3, "Interface %s joining multicast %s...", Nic->int_name,
349 olsr_ip_to_string(&buf, (union olsr_ip_addr *)&Nic->int6_multaddr.sin6_addr));
351 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&McastReq, sizeof(struct ipv6_mreq)) < 0) {
352 perror("Join multicast");
357 #ifdef IPV6_JOIN_GROUP
358 /* Join reciever group */
359 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&McastReq, sizeof(struct ipv6_mreq)) < 0)
361 /* Join reciever group */
362 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&McastReq, sizeof(struct ipv6_mreq)) < 0)
365 perror("Join multicast send");
369 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *)&McastReq.ipv6mr_interface, sizeof(McastReq.ipv6mr_interface)) < 0) {
370 perror("Set multicast if");
374 OLSR_PRINTF(3, "OK\n");
379 * Wrapper for sendto(2)
383 olsr_sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr * to, socklen_t tolen)
385 return sendto(s, buf, len, flags, to, tolen);
389 * Wrapper for recvfrom(2)
393 olsr_recvfrom(int s, void *buf, size_t len, int flags __attribute__ ((unused)), struct sockaddr * from, socklen_t * fromlen)
395 return recvfrom(s, buf, len, 0, from, fromlen);
399 * Wrapper for select(2)
403 olsr_select(int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval *timeout)
408 Sleep(timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
413 return select(nfds, readfds, writefds, exceptfds, timeout);
416 return select(nfds, readfds, writefds, exceptfds, timeout);
423 * indent-tabs-mode: nil