slirp.c revision b895bb5ba480cbdcd7abe96183427d61bd56a3d0
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#include "slirp.h"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef RT_OS_OS2
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include <paths.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#include <VBox/err.h>
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#include <VBox/pdmdrv.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#include <iprt/assert.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifndef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include <sys/ioctl.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include <poll.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include <Winnls.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define _WINSOCK2API_
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include <IPHlpApi.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#include <alias.h>
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if !defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_ENGAGE_EVENT1(so, fdset, label) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if( so->so_poll_index != -1 \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync && so->s == polls[so->so_poll_index].fd) { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync polls[so->so_poll_index].events |= N_(fdset ## _poll); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync break; /* out of this loop */ \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertRelease(poll_index < (nfds)); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertRelease(poll_index >= 0 && poll_index < (nfds)); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync polls[poll_index].fd = (so)->s; \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (so)->so_poll_index = poll_index; \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync polls[poll_index].events = N_(fdset ## _poll); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync polls[poll_index].revents = 0; \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync poll_index++; \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while(0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do { \
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if( so->so_poll_index != -1 \
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync && so->s == polls[so->so_poll_index].fd) { \
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync polls[so->so_poll_index].events |= \
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync break; /* out of this loop */ \
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync } \
5c127165d63f95b1763b4af2a2c6ae1025fc84fevboxsync AssertRelease(poll_index < (nfds)); \
5c127165d63f95b1763b4af2a2c6ae1025fc84fevboxsync polls[poll_index].fd = (so)->s; \
5c127165d63f95b1763b4af2a2c6ae1025fc84fevboxsync (so)->so_poll_index = poll_index; \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync polls[poll_index].events = \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync N_(fdset1 ## _poll) | N_(fdset1 ## _poll); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync poll_index++; \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while(0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_POLL_EVENTS(rc, error, so, events, label) do {} while (0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_CHECK_FD_SET(so, events, fdset) ( ((so)->so_poll_index != -1) \
82391de567696f10b21a762fde6a06fe3c266d28vboxsync && ((so)->so_poll_index <= ndfs) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync && ((so)->s == polls[so->so_poll_index].fd) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync && (polls[(so)->so_poll_index].revents & N_(fdset ## _poll)))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset) /*specific for Unix API */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_WIN_CHECK_FD_SET(so, events, fdset ) 0 /* specific for Windows Winsock API */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# ifndef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# ifndef RT_OS_LINUX
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define readfds_poll (POLLRDNORM)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define writefds_poll (POLLWRNORM)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define xfds_poll (POLLRDBAND|POLLWRBAND|POLLPRI)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define readfds_poll (POLLIN)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define writefds_poll (POLLOUT)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define xfds_poll (POLLPRI)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define rderr_poll (POLLERR)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define rdhup_poll (POLLHUP)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync# define nval_poll (POLLNVAL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define ICMP_ENGAGE_EVENT(so, fdset) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do { \
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (pData->icmp_socket.s != -1) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DO_ENGAGE_EVENT1((so), fdset, ICMP); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while (0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# else /* !RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif /* RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else /* defined(RT_OS_WINDOWS) */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/*
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * On Windows, we will be notified by IcmpSendEcho2() when the response arrives.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * So no call to WSAEventSelect necessary.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define ICMP_ENGAGE_EVENT(so, fdset) do {} while(0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_ENGAGE_EVENT1(so, fdset1, label) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = WSAEventSelect((so)->s, VBOX_SOCKET_EVENT, FD_ALL_EVENTS); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (rc == SOCKET_ERROR) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* This should not happen */ \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync error = WSAGetLastError(); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("WSAEventSelect (" #label ") error %d (so=%x, socket=%s, event=%x)\n", \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync error, (so), (so)->s, VBOX_SOCKET_EVENT)); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while(0); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CONTINUE(label)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_ENGAGE_EVENT2(so, fdset1, fdset2, label) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DO_ENGAGE_EVENT1((so), (fdset1), label)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_POLL_EVENTS(rc, error, so, events, label) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (rc) = WSAEnumNetworkEvents((so)->s, VBOX_SOCKET_EVENT, (events)); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if ((rc) == SOCKET_ERROR) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (error) = WSAGetLastError(); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("WSAEnumNetworkEvents " #label " error %d\n", (error))); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CONTINUE(label); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define acceptds_win FD_ACCEPT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define acceptds_win_bit FD_ACCEPT_BIT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define readfds_win FD_READ
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define readfds_win_bit FD_READ_BIT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define writefds_win FD_WRITE
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync# define writefds_win_bit FD_WRITE_BIT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define xfds_win FD_OOB
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define xfds_win_bit FD_OOB_BIT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_CHECK_FD_SET(so, events, fdset) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (((events).lNetworkEvents & fdset ## _win) && ((events).iErrorCode[fdset ## _win_bit] == 0))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_WIN_CHECK_FD_SET(so, events, fdset ) DO_CHECK_FD_SET((so), (events), fdset)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_UNIX_CHECK_FD_SET(so, events, fdset ) 1 /*specific for Unix API */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif /* defined(RT_OS_WINDOWS) */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#define TCP_ENGAGE_EVENT1(so, fdset) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DO_ENGAGE_EVENT1((so), fdset, tcp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define TCP_ENGAGE_EVENT2(so, fdset1, fdset2) \
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync DO_ENGAGE_EVENT2((so), fdset1, fdset2, tcp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define UDP_ENGAGE_EVENT(so, fdset) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DO_ENGAGE_EVENT1((so), fdset, udp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define POLL_TCP_EVENTS(rc, error, so, events) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DO_POLL_EVENTS((rc), (error), (so), (events), tcp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define POLL_UDP_EVENTS(rc, error, so, events) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DO_POLL_EVENTS((rc), (error), (so), (events), udp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#define CHECK_FD_SET(so, events, set) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (DO_CHECK_FD_SET((so), (events), set))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define WIN_CHECK_FD_SET(so, events, set) \
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync (DO_WIN_CHECK_FD_SET((so), (events), set))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define UNIX_CHECK_FD_SET(so, events, set) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (DO_UNIX_CHECK_FD_SET(so, events, set))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Loging macros
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if VBOX_WITH_DEBUG_NAT_SOCKETS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# if defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do { \
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync LogRel((" " #proto " %R[natsock] %R[natwinnetevents]\n", (so), (winevent))); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while (0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# else /* RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) \
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync do { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel((" " #proto " %R[natsock] %s %s %s er: %s, %s, %s\n", (so), \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CHECK_FD_SET(so, ign ,r_fdset) ? "READ":"", \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CHECK_FD_SET(so, ign, w_fdset) ? "WRITE":"", \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CHECK_FD_SET(so, ign, x_fdset) ? "OOB":"", \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CHECK_FD_SET(so, ign, rderr) ? "RDERR":"", \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CHECK_FD_SET(so, ign, rdhup) ? "RDHUP":"", \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CHECK_FD_SET(so, ign, nval) ? "RDNVAL":"")); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while (0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# endif /* !RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else /* VBOX_WITH_DEBUG_NAT_SOCKETS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define DO_LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) do {} while (0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif /* !VBOX_WITH_DEBUG_NAT_SOCKETS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define LOG_NAT_SOCK(so, proto, winevent, r_fdset, w_fdset, x_fdset) DO_LOG_NAT_SOCK((so), proto, (winevent), r_fdset, w_fdset, x_fdset)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic void acivate_port_forwarding(PNATState, struct ethhdr *);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic uint32_t find_guest_ip(PNATState, uint8_t *);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic const uint8_t special_ethaddr[6] =
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync};
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsyncstatic const uint8_t broadcast_ethaddr[6] =
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync};
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncconst uint8_t zerro_ethaddr[6] =
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync};
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic int get_dns_addr_domain(PNATState pData, bool fVerbose,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct in_addr *pdns_addr,
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync const char **ppszDomain)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* Get amount of memory required for operation */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ULONG flags = GAA_FLAG_INCLUDE_PREFIX; /*GAA_FLAG_INCLUDE_ALL_INTERFACES;*/ /* all interfaces registered in NDIS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PIP_ADAPTER_ADDRESSES addresses = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PIP_ADAPTER_ADDRESSES addr = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PIP_ADAPTER_DNS_SERVER_ADDRESS dns = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ULONG size = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int wlen = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync char *suffix;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct dns_entry *da = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct dns_domain_entry *dd = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ULONG ret = ERROR_SUCCESS;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* @todo add SKIPing flags to get only required information */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (ret != ERROR_BUFFER_OVERFLOW)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("NAT: error %lu occurred on capacity detection operation\n", ret));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return -1;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (size == 0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("NAT: Win socket API returns non capacity\n"));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync return -1;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync addresses = RTMemAllocZ(size);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (addresses == NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync LogRel(("NAT: No memory available \n"));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return -1;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ret = pData->pfGetAdaptersAddresses(AF_INET, 0, NULL /* reserved */, addresses, &size);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (ret != ERROR_SUCCESS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync LogRel(("NAT: error %lu occurred on fetching adapters info\n", ret));
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync RTMemFree(addresses);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync return -1;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync addr = addresses;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while(addr != NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int found;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (addr->OperStatus != IfOperStatusUp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync goto next;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync dns = addr->FirstDnsServerAddress;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while (dns != NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct sockaddr *saddr = dns->Address.lpSockaddr;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (saddr->sa_family != AF_INET)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync goto next_dns;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* add dns server to list */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync da = RTMemAllocZ(sizeof(struct dns_entry));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (da == NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("NAT: Can't allocate buffer for DNS entry\n"));
822e11c896dd36c9dc3609dff676059576b7d3devboxsync RTMemFree(addresses);
822e11c896dd36c9dc3609dff676059576b7d3devboxsync return VERR_NO_MEMORY;
822e11c896dd36c9dc3609dff676059576b7d3devboxsync }
822e11c896dd36c9dc3609dff676059576b7d3devboxsync LogRel(("NAT: adding %R[IP4] to DNS server list\n", &((struct sockaddr_in *)saddr)->sin_addr));
82391de567696f10b21a762fde6a06fe3c266d28vboxsync if ((((struct sockaddr_in *)saddr)->sin_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
82391de567696f10b21a762fde6a06fe3c266d28vboxsync da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
82391de567696f10b21a762fde6a06fe3c266d28vboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync else
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync da->de_addr.s_addr = ((struct sockaddr_in *)saddr)->sin_addr.s_addr;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync TAILQ_INSERT_HEAD(&pData->dns_list_head, da, de_list);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync if (addr->DnsSuffix == NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync goto next_dns;
82391de567696f10b21a762fde6a06fe3c266d28vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*uniq*/
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTUtf16ToUtf8(addr->DnsSuffix, &suffix);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (!suffix || strlen(suffix) == 0) {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTStrFree(suffix);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync goto next_dns;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync found = 0;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
822e11c896dd36c9dc3609dff676059576b7d3devboxsync if ( dd->dd_pszDomain != NULL
822e11c896dd36c9dc3609dff676059576b7d3devboxsync && strcmp(dd->dd_pszDomain, suffix) == 0)
822e11c896dd36c9dc3609dff676059576b7d3devboxsync {
822e11c896dd36c9dc3609dff676059576b7d3devboxsync found = 1;
822e11c896dd36c9dc3609dff676059576b7d3devboxsync RTStrFree(suffix);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync break;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
822e11c896dd36c9dc3609dff676059576b7d3devboxsync if (found == 0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
82391de567696f10b21a762fde6a06fe3c266d28vboxsync dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
82391de567696f10b21a762fde6a06fe3c266d28vboxsync if (dd == NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("NAT: not enough memory\n"));
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync RTStrFree(suffix);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync RTMemFree(addresses);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync return VERR_NO_MEMORY;
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
822e11c896dd36c9dc3609dff676059576b7d3devboxsync dd->dd_pszDomain = suffix;
822e11c896dd36c9dc3609dff676059576b7d3devboxsync LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync next_dns:
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync dns = dns->Next;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync next:
822e11c896dd36c9dc3609dff676059576b7d3devboxsync addr = addr->Next;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTMemFree(addresses);
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync return 0;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync}
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync#else /* !RT_OS_WINDOWS */
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsyncstatic int get_dns_addr_domain(PNATState pData, bool fVerbose,
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync struct in_addr *pdns_addr,
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync const char **ppszDomain)
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync{
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync char buff[512];
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync char buff2[256];
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync FILE *f = NULL;
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync int found = 0;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync struct in_addr tmp_addr;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync#ifdef RT_OS_OS2
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync /* Try various locations. */
822e11c896dd36c9dc3609dff676059576b7d3devboxsync char *etc = getenv("ETC");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if (etc)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync snprintf(buff, sizeof(buff), "%s/RESOLV2", etc);
822e11c896dd36c9dc3609dff676059576b7d3devboxsync f = fopen(buff, "rt");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync }
822e11c896dd36c9dc3609dff676059576b7d3devboxsync if (!f)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync snprintf(buff, sizeof(buff), "%s/RESOLV2", _PATH_ETC);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync f = fopen(buff, "rt");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync }
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if (!f)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync snprintf(buff, sizeof(buff), "%s/resolv.conf", _PATH_ETC);
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync f = fopen(buff, "rt");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync }
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync#else
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync#ifndef DEBUG_vvl
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync f = fopen("/etc/resolv.conf", "r");
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync#else
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync char *home = getenv("HOME");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync snprintf(buff, sizeof(buff), "%s/resolv.conf", home);
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync f = fopen(buff, "r");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if (f != NULL)
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync {
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync Log(("NAT: DNS we're using %s\n", buff));
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync else
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync f = fopen("/etc/resolv.conf", "r");
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync Log(("NAT: DNS we're using %s\n", buff));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync#endif
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (!f)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync return -1;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if (ppszDomain)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync *ppszDomain = NULL;
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync Log(("nat: DNS Servers:\n"));
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync while (fgets(buff, 512, f) != NULL)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct dns_entry *da = NULL;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
822e11c896dd36c9dc3609dff676059576b7d3devboxsync if (!inet_aton(buff2, &tmp_addr))
822e11c896dd36c9dc3609dff676059576b7d3devboxsync continue;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync /*localhost mask */
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync da = RTMemAllocZ(sizeof (struct dns_entry));
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if (da == NULL)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync LogRel(("can't alloc memory for DNS entry\n"));
822e11c896dd36c9dc3609dff676059576b7d3devboxsync return -1;
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync /*check */
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync da->de_addr.s_addr = tmp_addr.s_addr;
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync if ((da->de_addr.s_addr & htonl(IN_CLASSA_NET)) == ntohl(INADDR_LOOPBACK & IN_CLASSA_NET)) {
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync da->de_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync TAILQ_INSERT_HEAD(&pData->dns_list_head, da, de_list);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync found++;
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync if ((!strncmp(buff, "domain", 6) || !strncmp(buff, "search", 6)))
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync char *tok;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync char *saveptr;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync struct dns_domain_entry *dd = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int found = 0;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync tok = strtok_r(&buff[6], " \t\n", &saveptr);
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync if( tok != NULL
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync && strcmp(tok, dd->dd_pszDomain) == 0)
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync {
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync found = 1;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync break;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync }
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (tok != NULL && found == 0) {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync dd = RTMemAllocZ(sizeof(struct dns_domain_entry));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (dd == NULL)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync LogRel(("NAT: not enought memory to add domain list\n"));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return VERR_NO_MEMORY;
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync }
a597f00ac8a71003621fe61c58fe32706ca941b3vboxsync dd->dd_pszDomain = RTStrDup(tok);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("NAT: adding domain name %s to search list\n", dd->dd_pszDomain));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LIST_INSERT_HEAD(&pData->dns_domain_list_head, dd, dd_list);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync fclose(f);
822e11c896dd36c9dc3609dff676059576b7d3devboxsync if (!found)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return -1;
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync return 0;
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync}
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic int slirp_init_dns_list(PNATState pData)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync TAILQ_INIT(&pData->dns_list_head);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LIST_INIT(&pData->dns_domain_list_head);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return get_dns_addr_domain(pData, true, NULL, NULL);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsyncstatic void slirp_release_dns_list(PNATState pData)
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct dns_entry *de = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct dns_domain_entry *dd = NULL;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while(!TAILQ_EMPTY(&pData->dns_list_head)) {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync de = TAILQ_FIRST(&pData->dns_list_head);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync TAILQ_REMOVE(&pData->dns_list_head, de, de_list);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTMemFree(de);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while(!LIST_EMPTY(&pData->dns_domain_list_head)) {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync dd = LIST_FIRST(&pData->dns_domain_list_head);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LIST_REMOVE(dd, dd_list);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (dd->dd_pszDomain != NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTStrFree(dd->dd_pszDomain);
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync RTMemFree(dd);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncint get_dns_addr(PNATState pData, struct in_addr *pdns_addr)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return get_dns_addr_domain(pData, false, pdns_addr, NULL);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifndef VBOX_WITH_NAT_SERVICE
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncint slirp_init(PNATState *ppData, const char *pszNetAddr, uint32_t u32Netmask,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync bool fPassDomain, void *pvUser)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncint slirp_init(PNATState *ppData, uint32_t u32NetAddr, uint32_t u32Netmask,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync bool fPassDomain, void *pvUser)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int fNATfailed = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PNATState pData = RTMemAllocZ(sizeof(NATState));
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync *ppData = pData;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (!pData)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return VERR_NO_MEMORY;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (u32Netmask & 0x1f)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* CTL is x.x.x.15, bootp passes up to 16 IPs (15..31) */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return VERR_INVALID_PARAMETER;
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync pData->fPassDomain = fPassDomain;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pData->pvUser = pvUser;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pData->netmask = u32Netmask;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* sockets & TCP defaults */
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync pData->socket_rcv = 64 * _1K;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync pData->socket_snd = 64 * _1K;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync tcp_sndspace = 64 * _1K;
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync tcp_rcvspace = 64 * _1K;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef RT_OS_WINDOWS
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync WSADATA Data;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync WSAStartup(MAKEWORD(2, 0), &Data);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pData->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef VBOX_WITH_SLIRP_MT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync QSOCKET_LOCK_CREATE(tcb);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync QSOCKET_LOCK_CREATE(udb);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rc = RTReqCreateQueue(&pData->pReqQueue);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertReleaseRC(rc);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync link_up = 1;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync debug_init();
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if_init(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ip_init(pData);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync icmp_init(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* Initialise mbufs *after* setting the MTU */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync m_init(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifndef VBOX_WITH_NAT_SERVICE
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync inet_aton(pszNetAddr, &special_addr);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync special_addr.s_addr = u32NetAddr;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pData->slirp_ethaddr = &special_ethaddr[0];
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* @todo: add ability to configure this staff */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* set default addresses */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync inet_aton("127.0.0.1", &loopback_addr);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (slirp_init_dns_list(pData) < 0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync fNATfailed = 1;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync dnsproxy_init(pData);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync getouraddr(pData);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync int flags = 0;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync struct in_addr proxy_addr;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync pData->proxy_alias = LibAliasInit(pData, NULL);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (pData->proxy_alias == NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LogRel(("NAT: LibAlias default rule wasn't initialized\n"));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync AssertMsgFailed(("NAT: LibAlias default rule wasn't initialized\n"));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync flags = LibAliasSetMode(pData->proxy_alias, 0, 0);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync flags |= PKT_ALIAS_LOG; /* set logging */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync flags = LibAliasSetMode(pData->proxy_alias, flags, ~0);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync proxy_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LibAliasSetAddress(pData->proxy_alias, proxy_addr);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync ftp_alias_load(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync nbt_alias_load(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return fNATfailed ? VINF_NAT_DNS : VINF_SUCCESS;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/**
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Register statistics.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_register_statistics(PNATState pData, PPDMDRVINS pDrvIns)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef VBOX_WITH_STATISTICS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define COUNTER(name, type, units, dsc) \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do { \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync PDMDrvHlpSTAMRegisterF(pDrvIns, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync &pData->Stat ## name, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync type, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAMVISIBILITY_ALWAYS, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync units, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync dsc, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync "/Drivers/NAT%u/" #name, \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pDrvIns->iInstance); \
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync } while (0)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define PROFILE_COUNTER(name, dsc) COUNTER(name, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define COUNTING_COUNTER(name, dsc) COUNTER(name, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include "counters.h"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# undef COUNTER
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# undef PROFILE_COUNTER
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# undef COUNTING_COUNTER
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/** @todo register statistics for the variables dumped by:
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * ipstats(pData); tcpstats(pData); udpstats(pData); icmpstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * mbufstats(pData); sockstats(pData); */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif /* VBOX_WITH_STATISTICS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/**
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Deregister statistics.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_deregister_statistics(PNATState pData, PPDMDRVINS pDrvIns)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef VBOX_WITH_STATISTICS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define PROFILE_COUNTER(name, dsc) PDMDrvHlpSTAMDeregister(pDrvIns, &pData->Stat ## name)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# define COUNTING_COUNTER(name, dsc) PDMDrvHlpSTAMDeregister(pDrvIns, &pData->Stat ## name)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# include "counters.h"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# undef COUNTING_COUNTER
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync# undef PROFILE_COUNTER
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif /* VBOX_WITH_STATISTICS */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync}
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync/**
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Marks the link as up, making it possible to establish new connections.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsyncvoid slirp_link_up(PNATState pData)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync link_up = 1;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync
82391de567696f10b21a762fde6a06fe3c266d28vboxsync/**
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync * Marks the link as down and cleans up the current connections.
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync */
00331fbaff118e6a5077fe96327aca51a70459dbvboxsyncvoid slirp_link_down(PNATState pData)
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync{
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync struct socket *so;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while ((so = tcb.so_next) != &tcb)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (so->so_state & SS_NOFDREF || so->s == -1)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync sofree(pData, so);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync tcp_drop(pData, sototcpcb(so), 0);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while ((so = udb.so_next) != &udb)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync udp_detach(pData, so);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync link_up = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/**
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Terminates the slirp component.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_term(PNATState pData)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync pData->pfIcmpCloseHandle(pData->icmp_socket.sh);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync FreeLibrary(pData->hmIcmpLibrary);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTMemFree(pData->pvIcmpBuffer);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync closesocket(pData->icmp_socket.s);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync slirp_link_down(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync slirp_release_dns_list(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ftp_alias_unload(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync nbt_alias_unload(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync while(!LIST_EMPTY(&instancehead)) {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct libalias *la = LIST_FIRST(&instancehead);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /* libalias do all clean up */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LibAliasUninit(la);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync WSACleanup();
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef LOG_ENABLED
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Log(("\n"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync "NAT statistics\n"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync "--------------\n"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync "\n"));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ipstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync tcpstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync udpstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync icmpstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync mbufstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync sockstats(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync Log(("\n"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync "\n"
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync "\n"));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync RTMemFree(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync/*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * curtime kept to an accuracy of 1ms
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstatic void updtime(PNATState pData)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#ifdef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct _timeb tb;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync _ftime(&tb);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync curtime = (u_int)tb.time * (u_int)1000;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync curtime += (u_int)tb.millitm;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync gettimeofday(&tt, 0);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync curtime = (u_int)tt.tv_sec * (u_int)1000;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync curtime += (u_int)tt.tv_usec / (u_int)1000;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if ((tt.tv_usec % 1000) >= 500)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync curtime++;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef RT_OS_WINDOWS
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_select_fill(PNATState pData, int *pnfds)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else /* RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_select_fill(PNATState pData, int *pnfds, struct pollfd *polls)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif /* !RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct socket *so, *so_next;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int nfds;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int error;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int poll_index = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int i;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_PROFILE_START(&pData->StatFill, a);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync nfds = *pnfds;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * First, TCP sockets
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync do_slowtimo = 0;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (!link_up)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync goto done;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * *_slowtimo needs calling if there are IP fragments
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * in the fragment queue, or there are TCP connections active
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* XXX:
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * triggering of fragment expiration should be the same but use new macroses
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync do_slowtimo = (tcb.so_next != &tcb);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (!do_slowtimo)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync for (i = 0; i < IPREASS_NHASH; i++)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (!TAILQ_EMPTY(&ipq[i]))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync do_slowtimo = 1;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync break;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ICMP_ENGAGE_EVENT(&pData->icmp_socket, readfds);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_COUNTER_RESET(&pData->StatTCP);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_COUNTER_RESET(&pData->StatTCPHot);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync QSOCKET_FOREACH(so, so_next, tcp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* { */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if !defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync so->so_poll_index = -1;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync STAM_COUNTER_INC(&pData->StatTCP);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /*
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * See if we need a tcp_fasttimo
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if ( time_fasttimo == 0
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync && so->so_tcpcb != NULL
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync && so->so_tcpcb->t_flags & TF_DELACK)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync time_fasttimo = curtime; /* Flag when we want a fasttimo */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /*
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * NOFDREF can include still connecting to local-host,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * newly socreated() sockets etc. Don't want to select these.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (so->so_state & SS_NOFDREF || so->s == -1)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CONTINUE(tcp);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Set for reading sockets which are accepting
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (so->so_state & SS_FACCEPTCONN)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_COUNTER_INC(&pData->StatTCPHot);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync TCP_ENGAGE_EVENT1(so, readfds);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync CONTINUE(tcp);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Set for writing sockets which are connecting
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (so->so_state & SS_ISFCONNECTING)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync Log2(("connecting %R[natsock] engaged\n",so));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync STAM_COUNTER_INC(&pData->StatTCPHot);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync TCP_ENGAGE_EVENT1(so, writefds);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /*
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Set for writing if we are connected, can send more, and
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * we have something to send
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (CONN_CANFSEND(so) && so->so_rcv.sb_cc)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync STAM_COUNTER_INC(&pData->StatTCPHot);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync TCP_ENGAGE_EVENT1(so, writefds);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /*
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Set for reading (and urgent data) if we are connected, can
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync * receive more, and we have room for it XXX /2 ?
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync */
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2)))
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync {
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync STAM_COUNTER_INC(&pData->StatTCPHot);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync TCP_ENGAGE_EVENT2(so, readfds, xfds);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync LOOP_LABEL(tcp, so, so_next);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync /*
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync * UDP sockets
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync STAM_COUNTER_RESET(&pData->StatUDP);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync STAM_COUNTER_RESET(&pData->StatUDPHot);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync QSOCKET_FOREACH(so, so_next, udp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* { */
82391de567696f10b21a762fde6a06fe3c266d28vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_COUNTER_INC(&pData->StatUDP);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if !defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync so->so_poll_index = -1;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * See if it's timed out
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (so->so_expire)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (so->so_expire <= curtime)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync Log2(("NAT: %R[natsock] expired\n", so));
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (so->so_timeout != NULL)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync so->so_timeout(pData, so, so->so_timeout_arg);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#ifdef VBOX_WITH_SLIRP_MT
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /* we need so_next for continue our cycle*/
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync so_next = so->so_next;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync UDP_DETACH(pData, so, so_next);
82391de567696f10b21a762fde6a06fe3c266d28vboxsync CONTINUE_NO_UNLOCK(udp);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync }
82391de567696f10b21a762fde6a06fe3c266d28vboxsync else
82391de567696f10b21a762fde6a06fe3c266d28vboxsync do_slowtimo = 1; /* Let socket expire */
82391de567696f10b21a762fde6a06fe3c266d28vboxsync }
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*
82391de567696f10b21a762fde6a06fe3c266d28vboxsync * When UDP packets are received from over the link, they're
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * sendto()'d straight away, so no need for setting for writing
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Limit the number of packets queued by this session to 4.
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * Note that even though we try and limit this to 4 packets,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * the session could have more queued if the packets needed
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * to be fragmented.
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync *
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync * (XXX <= 4 ?)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_COUNTER_INC(&pData->StatUDPHot);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync UDP_ENGAGE_EVENT(so, readfds);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync LOOP_LABEL(udp, so, so_next);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncdone:
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync *pnfds = VBOX_EVENT_COUNT;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else /* RT_OS_WINDOWS */
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync AssertRelease(poll_index <= *pnfds);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync *pnfds = poll_index;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif /* !RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync STAM_PROFILE_STOP(&pData->StatFill, a);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_select_poll(PNATState pData, int fTimeout, int fIcmp)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else /* RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncvoid slirp_select_poll(PNATState pData, struct pollfd *polls, int ndfs)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync#endif /* !RT_OS_WINDOWS */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct socket *so, *so_next;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync int ret;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if defined(RT_OS_WINDOWS)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync WSANETWORKEVENTS NetworkEvents;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int rc;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int error;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#else
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync int poll_index = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_PROFILE_START(&pData->StatPoll, a);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync /* Update time */
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync updtime(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * See if anything has timed out
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (link_up)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (time_fasttimo && ((curtime - time_fasttimo) >= 2))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_PROFILE_START(&pData->StatFastTimer, a);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync tcp_fasttimo(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync time_fasttimo = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_PROFILE_STOP(&pData->StatFastTimer, a);
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (do_slowtimo && ((curtime - last_slowtimo) >= 499))
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_PROFILE_START(&pData->StatSlowTimer, a);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync ip_slowtimo(pData);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync tcp_slowtimo(pData);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync last_slowtimo = curtime;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync STAM_PROFILE_STOP(&pData->StatSlowTimer, a);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#if defined(RT_OS_WINDOWS)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (fTimeout)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync return; /* only timer update */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Check sockets
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (!link_up)
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync goto done;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#if defined(RT_OS_WINDOWS)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /*XXX: before renaming please make see define
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync * fIcmp in slirp_state.h
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (fIcmp)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync sorecvfrom(pData, &pData->icmp_socket);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#else
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if ( (pData->icmp_socket.s != -1)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync && CHECK_FD_SET(&pData->icmp_socket, ignored, readfds))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync sorecvfrom(pData, &pData->icmp_socket);
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync#endif
a3011b448b38c39a7222f2f1eb40c8349023f650vboxsync /*
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync * Check TCP sockets
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync QSOCKET_FOREACH(so, so_next, tcp)
82391de567696f10b21a762fde6a06fe3c266d28vboxsync /* { */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifdef VBOX_WITH_SLIRP_MT
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if ( so->so_state & SS_NOFDREF
82391de567696f10b21a762fde6a06fe3c266d28vboxsync && so->so_deleted == 1)
82391de567696f10b21a762fde6a06fe3c266d28vboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync struct socket *son, *sop = NULL;
82391de567696f10b21a762fde6a06fe3c266d28vboxsync QSOCKET_LOCK(tcb);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (so->so_next != NULL)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync if (so->so_next != &tcb)
82391de567696f10b21a762fde6a06fe3c266d28vboxsync SOCKET_LOCK(so->so_next);
82391de567696f10b21a762fde6a06fe3c266d28vboxsync son = so->so_next;
82391de567696f10b21a762fde6a06fe3c266d28vboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if ( so->so_prev != &tcb
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync && so->so_prev != NULL)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync SOCKET_LOCK(so->so_prev);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync sop = so->so_prev;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync QSOCKET_UNLOCK(tcb);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync remque(pData, so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync NSOCK_DEC();
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync SOCKET_UNLOCK(so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync SOCKET_LOCK_DESTROY(so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync RTMemFree(so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync so_next = son;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (sop != NULL)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync SOCKET_UNLOCK(sop);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync CONTINUE_NO_UNLOCK(tcp);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#endif
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * FD_ISSET is meaningless on these sockets
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * (and they can crash the program)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (so->so_state & SS_NOFDREF || so->s == -1)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync CONTINUE(tcp);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync POLL_TCP_EVENTS(rc, error, so, &NetworkEvents);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync LOG_NAT_SOCK(so, TCP, &NetworkEvents, readfds, writefds, xfds);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Check for URG data
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * This will soread as well, so no need to
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * test for readfds below if this succeeds
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* out-of-band data */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (CHECK_FD_SET(so, NetworkEvents, xfds))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync sorecvoob(pData, so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Check sockets for reading
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync else if ( CHECK_FD_SET(so, NetworkEvents, readfds)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || WIN_CHECK_FD_SET(so, NetworkEvents, acceptds))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Check for incoming connections
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (so->so_state & SS_FACCEPTCONN)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync TCP_CONNECT(pData, so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#if defined(RT_OS_WINDOWS)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (!(NetworkEvents.lNetworkEvents & FD_CLOSE))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#endif
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync CONTINUE(tcp);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync ret = soread(pData, so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* Output it if we read something */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (RT_LIKELY(ret > 0))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync TCP_OUTPUT(pData, sototcpcb(so));
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#if defined(RT_OS_WINDOWS)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Check for FD_CLOSE events.
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * in some cases once FD_CLOSE engaged on socket it could be flashed latter (for some reasons)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if ( (NetworkEvents.lNetworkEvents & FD_CLOSE)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || (so->so_close == 1))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync so->so_close = 1; /* mark it */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * drain the socket
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync for (;;)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync ret = soread(pData, so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (ret > 0)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync TCP_OUTPUT(pData, sototcpcb(so));
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync else
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync break;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync CONTINUE(tcp);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#endif
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Check sockets for writing
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (CHECK_FD_SET(so, NetworkEvents, writefds))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Check for non-blocking, still-connecting sockets
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (so->so_state & SS_ISFCONNECTING)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync Log2(("connecting %R[natsock] catched\n", so));
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* Connected */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync so->so_state &= ~SS_ISFCONNECTING;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * This should be probably guarded by PROBE_CONN too. Anyway,
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * we disable it on OS/2 because the below send call returns
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * EFAULT which causes the opened TCP socket to close right
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * after it has been opened and connected.
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#ifndef RT_OS_OS2
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync ret = send(so->s, (const char *)&ret, 0, 0);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (ret < 0)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* XXXXX Must fix, zero bytes is a NOP */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if ( errno == EAGAIN
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || errno == EWOULDBLOCK
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || errno == EINPROGRESS
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || errno == ENOTCONN)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync CONTINUE(tcp);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* else failed */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync so->so_state = SS_NOFDREF;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* else so->so_state &= ~SS_ISFCONNECTING; */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#endif
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Continue tcp_input
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* continue; */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync else
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync SOWRITE(ret, pData, so);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * XXX If we wrote something (a lot), there could be the need
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * for a window update. In the worst case, the remote will send
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * a window probe to get things going again.
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /*
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * Probe a still-connecting, non-blocking socket
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync * to check if it's still alive
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync#ifdef PROBE_CONN
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (so->so_state & SS_ISFCONNECTING)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync ret = recv(so->s, (char *)&ret, 0, 0);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (ret < 0)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* XXX */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if ( errno == EAGAIN
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || errno == EWOULDBLOCK
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || errno == EINPROGRESS
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync || errno == ENOTCONN)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync CONTINUE(tcp); /* Still connecting, continue */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* else failed */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync so->so_state = SS_NOFDREF;
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* tcp_input will take care of it */
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync else
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync ret = send(so->s, &ret, 0, 0);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if (ret < 0)
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync /* XXX */
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync if ( errno == EAGAIN
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync || errno == EWOULDBLOCK
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync || errno == EINPROGRESS
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync || errno == ENOTCONN)
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync {
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync CONTINUE(tcp);
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync /* else failed */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync so->so_state = SS_NOFDREF;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync else
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync so->so_state &= ~SS_ISFCONNECTING;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync TCP_INPUT((struct mbuf *)NULL, sizeof(struct ip),so);
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync } /* SS_ISFCONNECTING */
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#endif
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync#ifndef RT_OS_WINDOWS
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync if ( UNIX_CHECK_FD_SET(so, NetworkEvents, rdhup)
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync || UNIX_CHECK_FD_SET(so, NetworkEvents, rderr))
7b4c4bb29760b28b5727231ad446462a5b0cc01avboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int err;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int inq, outq;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync int status;
99f33ab590a3a65e0cd082dd8d67779efb9cc6c9vboxsync socklen_t optlen = sizeof(int);
2ad9f8a731c73f6ac74044d42d47bbaf6f44a566vboxsync inq = outq = 0;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync status = getsockopt(so->s, SOL_SOCKET, SO_ERROR, &err, &optlen);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (status != 0)
00331fbaff118e6a5077fe96327aca51a70459dbvboxsync Log(("NAT: can't get error status from %R[natsock]\n", so));
#ifndef RT_OS_SOLARIS
status = ioctl(so->s, FIONREAD, &inq); /* tcp(7) recommends SIOCINQ which is Linux specific */
if (status != 0 || status != EINVAL)
{
/* EINVAL returned if socket in listen state tcp(7)*/
Log(("NAT: can't get depth of IN queue status from %R[natsock]\n", so));
}
status = ioctl(so->s, TIOCOUTQ, &outq); /* SIOCOUTQ see previous comment */
if (status != 0)
Log(("NAT: can't get depth of OUT queue from %R[natsock]\n", so));
#else
/*
* Solaris has bit different ioctl commands and its handlings
* hint: streamio(7) I_NREAD
*/
#endif
if ( so->so_state & SS_ISFCONNECTING
|| UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
{
/**
* Check if we need here take care about gracefull connection
* @todo try with proxy server
*/
if (UNIX_CHECK_FD_SET(so, NetworkEvents, readfds))
{
/*
* Never meet inq != 0 or outq != 0, anyway let it stay for a while
* in case it happens we'll able to detect it.
* Give TCP/IP stack wait or expire the socket.
*/
Log(("NAT: %R[natsock] err(%d:%s) s(in:%d,out:%d)happens on read I/O, "
"other side close connection \n", so, err, strerror(err), inq, outq));
CONTINUE(tcp);
}
goto tcp_input_close;
}
if ( !UNIX_CHECK_FD_SET(so, NetworkEvents, readfds)
&& !UNIX_CHECK_FD_SET(so, NetworkEvents, writefds)
&& !UNIX_CHECK_FD_SET(so, NetworkEvents, xfds))
{
Log(("NAT: system expires the socket %R[natsock] err(%d:%s) s(in:%d,out:%d) happens on non-I/O. ",
so, err, strerror(err), inq, outq));
goto tcp_input_close;
}
Log(("NAT: %R[natsock] we've met(%d:%s) s(in:%d, out:%d) unhandled combination hup (%d) "
"rederr(%d) on (r:%d, w:%d, x:%d)\n",
so, err, strerror(err),
inq, outq,
UNIX_CHECK_FD_SET(so, ign, rdhup),
UNIX_CHECK_FD_SET(so, ign, rderr),
UNIX_CHECK_FD_SET(so, ign, readfds),
UNIX_CHECK_FD_SET(so, ign, writefds),
UNIX_CHECK_FD_SET(so, ign, xfds)));
/*
* Give OS's TCP/IP stack a chance to resolve an issue or expire the socket.
*/
CONTINUE(tcp);
tcp_input_close:
so->so_state = SS_NOFDREF; /*cause connection valid tcp connection termination and socket closing */
TCP_INPUT(pData, (struct mbuf *)NULL, sizeof(struct ip), so);
CONTINUE(tcp);
}
#endif
LOOP_LABEL(tcp, so, so_next);
}
/*
* Now UDP sockets.
* Incoming packets are sent straight away, they're not buffered.
* Incoming UDP data isn't buffered either.
*/
QSOCKET_FOREACH(so, so_next, udp)
/* { */
#ifdef VBOX_WITH_SLIRP_MT
if ( so->so_state & SS_NOFDREF
&& so->so_deleted == 1)
{
struct socket *son, *sop = NULL;
QSOCKET_LOCK(udb);
if (so->so_next != NULL)
{
if (so->so_next != &udb)
SOCKET_LOCK(so->so_next);
son = so->so_next;
}
if ( so->so_prev != &udb
&& so->so_prev != NULL)
{
SOCKET_LOCK(so->so_prev);
sop = so->so_prev;
}
QSOCKET_UNLOCK(udb);
remque(pData, so);
NSOCK_DEC();
SOCKET_UNLOCK(so);
SOCKET_LOCK_DESTROY(so);
RTMemFree(so);
so_next = son;
if (sop != NULL)
SOCKET_UNLOCK(sop);
CONTINUE_NO_UNLOCK(udp);
}
#endif
POLL_UDP_EVENTS(rc, error, so, &NetworkEvents);
LOG_NAT_SOCK(so, UDP, &NetworkEvents, readfds, writefds, xfds);
if (so->s != -1 && CHECK_FD_SET(so, NetworkEvents, readfds))
{
SORECVFROM(pData, so);
}
LOOP_LABEL(udp, so, so_next);
}
done:
#ifndef VBOX_WITH_SLIRP_MT
/*
* See if we can start outputting
*/
if (if_queued && link_up)
if_start(pData);
#endif
STAM_PROFILE_STOP(&pData->StatPoll, a);
}
struct arphdr
{
unsigned short ar_hrd; /* format of hardware address */
unsigned short ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
unsigned short ar_op; /* ARP opcode (command) */
/*
* Ethernet looks like this : This bit is variable sized however...
*/
unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
unsigned char ar_sip[4]; /* sender IP address */
unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
unsigned char ar_tip[4]; /* target IP address */
};
AssertCompileSize(struct arphdr, 28);
static void arp_input(PNATState pData, struct mbuf *m)
{
struct ethhdr *eh;
struct ethhdr *reh;
struct arphdr *ah;
struct arphdr *rah;
int ar_op;
struct ex_list *ex_ptr;
uint32_t htip;
uint32_t tip;
struct mbuf *mr;
eh = mtod(m, struct ethhdr *);
ah = (struct arphdr *)&eh[1];
htip = ntohl(*(uint32_t*)ah->ar_tip);
tip = *(uint32_t*)ah->ar_tip;
mr = m_get(pData);
reh = mtod(mr, struct ethhdr *);
memcpy(reh->h_source, eh->h_source, ETH_ALEN); /* XXX: if_encap will swap src and dst*/
Log4(("NAT: arp:%R[ether]->%R[ether]\n",
reh->h_source, reh->h_dest));
Log4(("NAT: arp: %R[IP4]\n", &tip));
mr->m_data += if_maxlinkhdr;
mr->m_len = sizeof(struct arphdr);
rah = mtod(mr, struct arphdr *);
ar_op = ntohs(ah->ar_op);
switch(ar_op)
{
case ARPOP_REQUEST:
#ifdef VBOX_WITH_NAT_SERVICE
if (tip == special_addr.s_addr) goto arp_ok;
#endif
if ((htip & pData->netmask) == ntohl(special_addr.s_addr))
{
if ( CTL_CHECK(htip, CTL_DNS)
|| CTL_CHECK(htip, CTL_ALIAS)
|| CTL_CHECK(htip, CTL_TFTP))
goto arp_ok;
for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
{
if ((htip & ~pData->netmask) == ex_ptr->ex_addr)
{
goto arp_ok;
}
}
return;
arp_ok:
rah->ar_hrd = htons(1);
rah->ar_pro = htons(ETH_P_IP);
rah->ar_hln = ETH_ALEN;
rah->ar_pln = 4;
rah->ar_op = htons(ARPOP_REPLY);
memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN);
switch (htip & ~pData->netmask)
{
case CTL_DNS:
case CTL_ALIAS:
rah->ar_sha[5] = (uint8_t)(htip & ~pData->netmask);
break;
default:;
}
memcpy(rah->ar_sip, ah->ar_tip, 4);
memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
memcpy(rah->ar_tip, ah->ar_sip, 4);
if_encap(pData, ETH_P_ARP, mr);
m_free(pData, m);
}
break;
default:
break;
}
}
void slirp_input(PNATState pData, const uint8_t *pkt, int pkt_len)
{
struct mbuf *m;
int proto;
static bool fWarnedIpv6;
struct ethhdr *eh = (struct ethhdr*)pkt;
Log2(("NAT: slirp_input %d\n", pkt_len));
if (pkt_len < ETH_HLEN)
{
LogRel(("NAT: packet having size %d has been ingnored\n", pkt_len));
return;
}
Log4(("NAT: in:%R[ether]->%R[ether]\n", &eh->h_source, &eh->h_dest));
if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) == 0)
{
/* @todo vasily: add ether logging routine in debug.c */
Log(("NAT: packet was addressed to other MAC\n"));
RTMemFree((void *)pkt);
return;
}
m = m_get(pData);
if (!m)
{
LogRel(("NAT: can't allocate new mbuf\n"));
return;
}
/* Note: we add to align the IP header */
if (M_FREEROOM(m) < pkt_len)
m_inc(m, pkt_len);
m->m_len = pkt_len ;
memcpy(m->m_data, pkt, pkt_len);
if (pData->port_forwarding_activated == 0)
acivate_port_forwarding(pData, mtod(m, struct ethhdr *));
proto = ntohs(*(uint16_t *)(pkt + 12));
switch(proto)
{
case ETH_P_ARP:
arp_input(pData, m);
break;
case ETH_P_IP:
/* Update time. Important if the network is very quiet, as otherwise
* the first outgoing connection gets an incorrect timestamp. */
updtime(pData);
m_adj(m, ETH_HLEN);
ip_input(pData, m);
break;
case ETH_P_IPV6:
m_free(pData, m);
if (!fWarnedIpv6)
{
LogRel(("NAT: IPv6 not supported\n"));
fWarnedIpv6 = true;
}
break;
default:
Log(("NAT: Unsupported protocol %x\n", proto));
m_free(pData, m);
break;
}
RTMemFree((void *)pkt);
}
/* output the IP packet to the ethernet device */
void if_encap(PNATState pData, uint16_t eth_proto, struct mbuf *m)
{
struct ethhdr *eh;
uint8_t *buf = RTMemAlloc(1600);
STAM_PROFILE_START(&pData->StatIF_encap, a);
m->m_data -= if_maxlinkhdr;
m->m_len += ETH_HLEN;
eh = mtod(m, struct ethhdr *);
if(MBUF_HEAD(m) != m->m_data)
{
LogRel(("NAT: ethernet detects corruption of the packet"));
AssertMsgFailed(("!!Ethernet frame corrupted!!"));
}
if (memcmp(eh->h_source, special_ethaddr, ETH_ALEN) != 0)
{
memcpy(eh->h_dest, eh->h_source, ETH_ALEN);
memcpy(eh->h_source, special_ethaddr, ETH_ALEN);
Assert(memcmp(eh->h_dest, special_ethaddr, ETH_ALEN) != 0);
if (memcmp(eh->h_dest, zerro_ethaddr, ETH_ALEN) == 0)
{
/* don't do anything */
goto done;
}
}
eh->h_proto = htons(eth_proto);
memcpy(buf, mtod(m, uint8_t *), m->m_len);
slirp_output(pData->pvUser, NULL, buf, m->m_len);
done:
STAM_PROFILE_STOP(&pData->StatIF_encap, a);
m_free(pData, m);
}
/**
* Still we're using dhcp server leasing to map ether to IP
* @todo see rt_lookup_in_cache
*/
static uint32_t find_guest_ip(PNATState pData, uint8_t *eth_addr)
{
int i;
if (memcmp(eth_addr, zerro_ethaddr, ETH_ALEN) == 0
|| memcmp(eth_addr, broadcast_ethaddr, ETH_ALEN) == 0)
goto done;
for (i = 0; i < NB_ADDR; i++)
{
if ( bootp_clients[i].allocated
&& memcmp(bootp_clients[i].macaddr, eth_addr, ETH_ALEN) == 0)
return bootp_clients[i].addr.s_addr;
}
done:
return INADDR_ANY;
}
/**
* We need check if we've activated port forwarding
* for specific machine ... that of course relates to
* service mode
* @todo finish this for service case
*/
static void acivate_port_forwarding(PNATState pData, struct ethhdr *ethdr)
{
struct port_forward_rule *rule = NULL;
pData->port_forwarding_activated = 1;
/* check mac here */
LIST_FOREACH(rule, &pData->port_forward_rule_head, list)
{
struct socket *so;
struct alias_link *link;
struct libalias *lib;
int flags;
struct sockaddr sa;
struct sockaddr_in *psin;
socklen_t socketlen;
struct in_addr alias;
int rc;
uint32_t guest_addr; /* need to understand if we already give address to guest */
if (rule->activated)
continue; /*already activated */
#ifdef VBOX_WITH_NAT_SERVICE
if (memcmp(rule->mac_address, ethdr->h_source, ETH_ALEN) != 0)
continue; /*not right mac, @todo: it'd be better do the list port forwarding per mac */
guest_addr = find_guest_ip(pData, ethdr->h_source);
#else
if (memcmp(client_ethaddr, ethdr->h_source, ETH_ALEN) != 0)
continue;
guest_addr = find_guest_ip(pData, ethdr->h_source);
Assert(rule->guest_addr.s_addr == guest_addr);
#endif
if (guest_addr == INADDR_ANY)
{
/* the address wasn't granted */
pData->port_forwarding_activated = 0;
return;
}
LogRel(("NAT: set redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
rule->host_port, rule->guest_port));
if (rule->proto == IPPROTO_UDP)
{
so = udp_listen(pData, rule->bind_ip.s_addr, htons(rule->host_port), guest_addr,
htons(rule->guest_port), 0);
}
else
{
so = solisten(pData, rule->bind_ip.s_addr, htons(rule->host_port), guest_addr,
htons(rule->guest_port), 0);
}
if (so == NULL)
{
LogRel(("NAT: failed redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
rule->host_port, rule->guest_port));
goto remove_port_forwarding;
}
psin = (struct sockaddr_in *)&sa;
psin->sin_family = AF_INET;
psin->sin_port = 0;
psin->sin_addr.s_addr = INADDR_ANY;
socketlen = sizeof(struct sockaddr);
rc = getsockname(so->s, &sa, &socketlen);
if (rc < 0 || sa.sa_family != AF_INET)
{
LogRel(("NAT: failed redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
rule->host_port, rule->guest_port));
goto remove_port_forwarding;
}
psin = (struct sockaddr_in *)&sa;
lib = LibAliasInit(pData, NULL);
flags = LibAliasSetMode(lib, 0, 0);
flags |= PKT_ALIAS_LOG; /* set logging */
flags |= PKT_ALIAS_REVERSE; /* set logging */
flags = LibAliasSetMode(lib, flags, ~0);
alias.s_addr = htonl(ntohl(guest_addr) | CTL_ALIAS);
link = LibAliasRedirectPort(lib, psin->sin_addr, htons(rule->host_port),
alias, htons(rule->guest_port),
special_addr, -1, /* not very clear for now*/
rule->proto);
if (link == NULL)
{
LogRel(("NAT: failed redirect %s hp:%d gp:%d\n", (rule->proto == IPPROTO_UDP?"UDP":"TCP"),
rule->host_port, rule->guest_port));
goto remove_port_forwarding;
}
so->so_la = lib;
rule->activated = 1;
continue;
remove_port_forwarding:
LIST_REMOVE(rule, list);
RTMemFree(rule);
}
}
/**
* Changes in 3.1 instead of opening new socket do the following:
* gain more information:
* 1. bind IP
* 2. host port
* 3. guest port
* 4. proto
* 5. guest MAC address
* the guest's MAC address is rather important for service, but we easily
* could get it from VM configuration in DrvNAT or Service, the idea is activating
* corresponding port-forwarding
*/
int slirp_redir(PNATState pData, int is_udp, struct in_addr host_addr, int host_port,
struct in_addr guest_addr, int guest_port, const uint8_t *ethaddr)
{
struct port_forward_rule *rule = NULL;
Assert(memcmp(ethaddr, zerro_ethaddr, ETH_ALEN) == 0);
rule = RTMemAllocZ(sizeof(struct port_forward_rule));
if (rule == NULL)
return 1;
rule->proto = (is_udp ? IPPROTO_UDP : IPPROTO_TCP);
rule->host_port = host_port;
rule->guest_port = guest_port;
#ifndef VBOX_WITH_NAT_SERVICE
rule->guest_addr.s_addr = guest_addr.s_addr;
#endif
rule->bind_ip.s_addr = host_addr.s_addr;
memcmp(rule->mac_address, ethaddr, ETH_ALEN);
/* @todo add mac address */
LIST_INSERT_HEAD(&pData->port_forward_rule_head, rule, list);
return 0;
}
int slirp_add_exec(PNATState pData, int do_pty, const char *args, int addr_low_byte,
int guest_port)
{
return add_exec(&exec_list, do_pty, (char *)args,
addr_low_byte, htons(guest_port));
}
void slirp_set_ethaddr(PNATState pData, const uint8_t *ethaddr)
{
#ifndef VBOX_WITH_NAT_SERVICE
memcpy(client_ethaddr, ethaddr, ETH_ALEN);
#endif
}
#if defined(RT_OS_WINDOWS)
HANDLE *slirp_get_events(PNATState pData)
{
return pData->phEvents;
}
void slirp_register_external_event(PNATState pData, HANDLE hEvent, int index)
{
pData->phEvents[index] = hEvent;
}
#endif
unsigned int slirp_get_timeout_ms(PNATState pData)
{
if (link_up)
{
if (time_fasttimo)
return 2;
if (do_slowtimo)
return 500; /* see PR_SLOWHZ */
}
return 0;
}
#ifndef RT_OS_WINDOWS
int slirp_get_nsock(PNATState pData)
{
return pData->nsock;
}
#endif
/*
* this function called from NAT thread
*/
void slirp_post_sent(PNATState pData, void *pvArg)
{
struct socket *so = 0;
struct tcpcb *tp = 0;
struct mbuf *m = (struct mbuf *)pvArg;
m_free(pData, m);
}
#ifdef VBOX_WITH_SLIRP_MT
void slirp_process_queue(PNATState pData)
{
RTReqProcess(pData->pReqQueue, RT_INDEFINITE_WAIT);
}
void *slirp_get_queue(PNATState pData)
{
return pData->pReqQueue;
}
#endif
void slirp_set_dhcp_TFTP_prefix(PNATState pData, const char *tftpPrefix)
{
Log2(("tftp_prefix:%s\n", tftpPrefix));
tftp_prefix = tftpPrefix;
}
void slirp_set_dhcp_TFTP_bootfile(PNATState pData, const char *bootFile)
{
Log2(("bootFile:%s\n", bootFile));
bootp_filename = bootFile;
}
void slirp_set_dhcp_next_server(PNATState pData, const char *next_server)
{
Log2(("next_server:%s\n", next_server));
if (next_server == NULL)
pData->tftp_server.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
else
inet_aton(next_server, &pData->tftp_server);
}
int slirp_set_binding_address(PNATState pData, char *addr)
{
if (addr == NULL || (inet_aton(addr, &pData->bindIP) == 0))
{
pData->bindIP.s_addr = INADDR_ANY;
return 1;
}
return 0;
}
void slirp_set_dhcp_dns_proxy(PNATState pData, bool fDNSProxy)
{
Log2(("NAT: DNS proxy switched %s\n", (fDNSProxy ? "on" : "off")));
pData->use_dns_proxy = fDNSProxy;
}
#define CHECK_ARG(name, val, lim_min, lim_max) \
do { \
if ((val) < (lim_min) || (val) > (lim_max)) \
{ \
LogRel(("NAT: (" #name ":%d) has been ignored, " \
"because out of range (%d, %d)\n", (val), (lim_min), (lim_max))); \
return; \
} \
else \
{ \
LogRel(("NAT: (" #name ":%d)\n", (val))); \
} \
} while (0)
/* don't allow user set less 8kB and more than 1M values */
#define _8K_1M_CHECK_ARG(name, val) CHECK_ARG(name, (val), 8, 1024)
void slirp_set_rcvbuf(PNATState pData, int kilobytes)
{
_8K_1M_CHECK_ARG("SOCKET_RCVBUF", kilobytes);
pData->socket_rcv = kilobytes;
}
void slirp_set_sndbuf(PNATState pData, int kilobytes)
{
_8K_1M_CHECK_ARG("SOCKET_SNDBUF", kilobytes);
pData->socket_snd = kilobytes * _1K;
}
void slirp_set_tcp_rcvspace(PNATState pData, int kilobytes)
{
_8K_1M_CHECK_ARG("TCP_RCVSPACE", kilobytes);
tcp_rcvspace = kilobytes * _1K;
}
void slirp_set_tcp_sndspace(PNATState pData, int kilobytes)
{
_8K_1M_CHECK_ARG("TCP_SNDSPACE", kilobytes);
tcp_sndspace = kilobytes * _1K;
}