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.
45 #include <sys/types.h> // for time_t
46 #endif /* defined WINCE */
48 #define WIN32_LEAN_AND_MEAN
64 #define WIDE_STRING(s) L##s
65 #else /* defined WINCE */
66 #define WIDE_STRING(s) TEXT(s)
67 #endif /* defined WINCE */
69 void WinSockPError(const char *Str);
70 void PError(const char *);
72 void DisableIcmpRedirects(void);
75 gethemusocket(struct sockaddr_in *pin)
79 OLSR_PRINTF(1, " Connecting to switch daemon port 10150...");
81 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
86 /* connect to PORT on HOST */
87 if (connect(sock, (struct sockaddr *)pin, sizeof(*pin)) < 0) {
89 fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno));
90 printf("connection refused\n");
97 /* Keep TCP socket blocking */
102 getsocket(int bufspace, struct interface_olsr *ifp __attribute__ ((unused)))
104 struct sockaddr_in Addr;
107 int Sock = socket(AF_INET, SOCK_DGRAM, 0);
109 WinSockPError("getsocket/socket()");
113 if (setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char *)&On, sizeof(On)) < 0) {
114 WinSockPError("getsocket/setsockopt(SO_BROADCAST)");
119 if (setsockopt(Sock, SOL_SOCKET, SO_REUSEADDR, (char *)&On, sizeof(On)) < 0) {
120 WinSockPError("getsocket/setsockopt(SO_REUSEADDR)");
125 while (bufspace > 8192) {
126 if (setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char *)&bufspace, sizeof(bufspace)) == 0)
132 if (bufspace <= 8192) {
133 OLSR_PRINTF(1, "Cannot set IPv4 socket receive buffer.\n");
135 memset(&Addr, 0, sizeof(Addr));
136 Addr.sin_family = AF_INET;
137 Addr.sin_port = htons(olsr_cnf->olsrport);
140 Addr.sin_addr.s_addr = ifp->int_addr.sin_addr.s_addr;
143 if (bind(Sock, (struct sockaddr *)&Addr, sizeof(Addr)) < 0) {
144 WinSockPError("getsocket/bind()");
149 if (WSAIoctl(Sock, FIONBIO, &On, sizeof(On), NULL, 0, &Len, NULL, NULL) < 0) {
150 WinSockPError("WSAIoctl");
159 getsocket6(int bufspace, struct interface_olsr *ifp __attribute__ ((unused)))
161 struct sockaddr_in6 Addr6;
163 int Sock = socket(AF_INET6, SOCK_DGRAM, 0);
165 WinSockPError("getsocket6/socket()");
169 if (setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char *)&On, sizeof(On)) < 0) {
170 WinSockPError("getsocket6/setsockopt(SO_BROADCAST)");
175 if (setsockopt(Sock, SOL_SOCKET, SO_REUSEADDR, (char *)&On, sizeof(On)) < 0) {
176 WinSockPError("getsocket6/setsockopt(SO_REUSEADDR)");
181 while (bufspace > 8192) {
182 if (setsockopt(Sock, SOL_SOCKET, SO_RCVBUF, (char *)&bufspace, sizeof(bufspace)) == 0)
188 if (bufspace <= 8192)
189 fprintf(stderr, "Cannot set IPv6 socket receive buffer.\n");
191 memset(&Addr6, 0, sizeof(Addr6));
192 Addr6.sin6_family = AF_INET6;
193 Addr6.sin6_port = htons(olsr_cnf->olsrport);
196 memcpy(&Addr6.sin6_addr, &ifp->int6_addr.sin6_addr, sizeof(struct in6_addr));
199 if (bind(Sock, (struct sockaddr *)&Addr6, sizeof(Addr6)) < 0) {
200 WinSockPError("getsocket6/bind()");
208 static OVERLAPPED RouterOver;
210 void net_os_set_global_ifoptions(void)
213 unsigned int __stdcall(*enable_router)(HANDLE *, OVERLAPPED *);
216 Lib = LoadLibrary(WIDE_STRING("iphlpapi.dll"));
221 enable_router = (unsigned int __stdcall(*)(HANDLE *, OVERLAPPED *))GetProcAddress(Lib, WIDE_STRING("EnableRouter"));
223 if (enable_router == NULL)
226 memset(&RouterOver, 0, sizeof(OVERLAPPED));
228 RouterOver.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
230 if (RouterOver.hEvent == NULL) {
231 PError("CreateEvent()");
235 if (enable_router(&Hand, &RouterOver) != ERROR_IO_PENDING) {
236 PError("EnableRouter()");
240 OLSR_PRINTF(3, "Routing enabled.\n");
246 disable_ip_forwarding(int Ver)
249 unsigned int __stdcall(*unenable_router)(OVERLAPPED *, unsigned int *);
254 Lib = LoadLibrary(WIDE_STRING("iphlpapi.dll"));
259 unenable_router = (unsigned int __stdcall(*)(OVERLAPPED *, unsigned int *))GetProcAddress(Lib, WIDE_STRING("UnenableRouter"));
261 if (unenable_router == NULL)
264 if (unenable_router(&RouterOver, &Count) != NO_ERROR) {
265 PError("UnenableRouter()");
269 OLSR_PRINTF(3, "Routing disabled, count = %u.\n", Count);
276 net_os_restore_ifoptions(void)
278 disable_ip_forwarding(olsr_cnf->ip_version);
284 SetEnableRedirKey(unsigned long New)
292 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters", 0, KEY_READ | KEY_WRITE, &Key) !=
298 if (RegQueryValueEx(Key, "EnableICMPRedirect", NULL, &Type, (unsigned char *)&Old, &Len) != ERROR_SUCCESS || Type != REG_DWORD)
301 if (RegSetValueEx(Key, "EnableICMPRedirect", 0, REG_DWORD, (unsigned char *)&New, sizeof(New))) {
308 #else /* !defined WINCE */
310 #endif /* !defined WINCE */
314 DisableIcmpRedirects(void)
318 Res = SetEnableRedirKey(0);
323 fprintf(stderr, "\n*** IMPORTANT *** IMPORTANT *** IMPORTANT *** IMPORTANT *** IMPORTANT ***\n\n");
325 fprintf(stderr, "I have disabled ICMP redirect processing in the registry for you.\n");
326 fprintf(stderr, "REBOOT NOW, so that these changes take effect. Exiting...\n\n");
332 join_mcast(struct interface_olsr *Nic, int Sock)
334 /* See linux/in6.h */
335 struct ipaddr_str buf;
336 struct ipv6_mreq McastReq;
338 McastReq.ipv6mr_multiaddr = Nic->int6_multaddr.sin6_addr;
339 McastReq.ipv6mr_interface = Nic->if_index;
341 OLSR_PRINTF(3, "Interface %s joining multicast %s...", Nic->int_name,
342 olsr_ip_to_string(&buf, (union olsr_ip_addr *)&Nic->int6_multaddr.sin6_addr));
344 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&McastReq, sizeof(struct ipv6_mreq)) < 0) {
345 perror("Join multicast");
350 #ifdef IPV6_JOIN_GROUP
351 /* Join receiver group */
352 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&McastReq, sizeof(struct ipv6_mreq)) < 0)
353 #else /* IPV6_JOIN_GROUP */
354 /* Join receiver group */
355 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&McastReq, sizeof(struct ipv6_mreq)) < 0)
356 #endif /* IPV6_JOIN_GROUP */
358 perror("Join multicast send");
362 if (setsockopt(Sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *)&McastReq.ipv6mr_interface, sizeof(McastReq.ipv6mr_interface)) < 0) {
363 perror("Set multicast if");
367 OLSR_PRINTF(3, "OK\n");
372 * Wrapper for sendto(2)
376 olsr_sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr * to, socklen_t tolen)
378 return sendto(s, buf, len, flags, to, tolen);
382 * Wrapper for recvfrom(2)
386 olsr_recvfrom(int s, void *buf, size_t len, int flags __attribute__ ((unused)), struct sockaddr * from, socklen_t * fromlen)
388 return recvfrom(s, buf, len, 0, from, fromlen);
392 * Wrapper for select(2)
396 olsr_select(int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval *timeout)
401 Sleep(timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
406 return select(nfds, readfds, writefds, exceptfds, timeout);
409 return select(nfds, readfds, writefds, exceptfds, timeout);
418 * indent-tabs-mode: nil