2017-02-28 01:26:43 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// $Id$
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-03-07 22:20:35 +03:00
|
|
|
// Copyright (C) 2003 by Mariusz Matuszek [NOmrmmSPAM @ users.sourceforge.net]
|
2017-03-07 00:30:05 +03:00
|
|
|
// Copyright (C) 2017 The Bochs Project
|
2017-02-28 01:26:43 +03:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2017-03-07 22:20:35 +03:00
|
|
|
// Mariusz Matuszek wrote the original version of eth_socket.cc based on
|
|
|
|
// eth_linux.cc.
|
2017-02-28 01:26:43 +03:00
|
|
|
//
|
|
|
|
// problems and limitations:
|
|
|
|
// - external program 'bxhub' is required
|
|
|
|
// - sends big packets using UDP, so only localhost is likely to work
|
|
|
|
// - author definitely doesn't know C++ and generally doesn't know what
|
|
|
|
// he is doing :)
|
|
|
|
//
|
2017-02-28 13:18:15 +03:00
|
|
|
// the idea is to provide a software multiport 'ethernet hub' and allow
|
|
|
|
// communication between multiple bochs instances on the same machine
|
2017-02-28 01:26:43 +03:00
|
|
|
// entirely in userspace and without need for root priviledges.
|
|
|
|
//
|
|
|
|
// The config line in .bochsrc should look like:
|
|
|
|
//
|
|
|
|
// ne2k: ioaddr=0x280, irq=10, mac=00:a:b:c:1:2, ethmod=socket, ethdev=<socknum>
|
|
|
|
//
|
|
|
|
// e.g.
|
|
|
|
//
|
|
|
|
// ne2k: ioaddr=0x280, irq=10, mac=00:a:b:c:1:2, ethmod=socket, ethdev=40000
|
|
|
|
//
|
2017-02-28 13:18:15 +03:00
|
|
|
// this module will bind to 127.0.0.1:<socknum> for RX packets
|
2017-02-28 01:26:43 +03:00
|
|
|
// TX packets will be sent to 127.0.0.1:<socknum + 1>
|
|
|
|
|
2017-03-07 22:20:35 +03:00
|
|
|
// Extensions by Volker Ruppert (2017):
|
|
|
|
// - Windows support
|
|
|
|
// - Support for connecting other machine using format 'host:port'.
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
|
2017-02-28 13:18:15 +03:00
|
|
|
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
|
2017-02-28 01:26:43 +03:00
|
|
|
// is used to know when we are exporting symbols and when we are importing.
|
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
2017-04-18 23:45:28 +03:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#define __USE_W32_SOCKETS
|
|
|
|
#endif
|
|
|
|
|
2017-02-28 01:26:43 +03:00
|
|
|
#include "iodev.h"
|
|
|
|
#include "netmod.h"
|
|
|
|
|
|
|
|
#if BX_NETWORKING && BX_NETMOD_SOCKET
|
|
|
|
|
2017-03-12 10:48:08 +03:00
|
|
|
// network driver plugin entry points
|
|
|
|
|
|
|
|
int CDECL libsocket_net_plugin_init(plugin_t *plugin, plugintype_t type)
|
|
|
|
{
|
|
|
|
// Nothing here yet
|
|
|
|
return 0; // Success
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDECL libsocket_net_plugin_fini(void)
|
|
|
|
{
|
|
|
|
// Nothing here yet
|
|
|
|
}
|
|
|
|
|
|
|
|
// network driver implementation
|
|
|
|
|
2017-02-28 01:26:43 +03:00
|
|
|
#define LOG_THIS netdev->
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <winsock2.h>
|
2017-04-18 23:45:28 +03:00
|
|
|
#include <ws2tcpip.h>
|
2017-02-28 01:26:43 +03:00
|
|
|
#else
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/types.h>
|
2017-03-07 00:30:05 +03:00
|
|
|
#include <netdb.h>
|
2017-02-28 19:52:15 +03:00
|
|
|
#define closesocket(s) close(s)
|
|
|
|
typedef int SOCKET;
|
|
|
|
#ifndef INVALID_SOCKET
|
|
|
|
#define INVALID_SOCKET -1
|
|
|
|
#endif
|
2017-02-28 01:26:43 +03:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#define MSG_DONTWAIT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BX_PACKET_POLL 1000 // Poll for a frame every 1000 usecs
|
|
|
|
|
|
|
|
//
|
|
|
|
// Define the class. This is private to this module
|
|
|
|
//
|
|
|
|
class bx_socket_pktmover_c : public eth_pktmover_c {
|
|
|
|
public:
|
|
|
|
bx_socket_pktmover_c(const char *netif, const char *macaddr,
|
|
|
|
eth_rx_handler_t rxh,
|
|
|
|
eth_rx_status_t rxstat,
|
|
|
|
bx_devmodel_c *dev, const char *script);
|
2017-03-07 00:30:05 +03:00
|
|
|
virtual ~bx_socket_pktmover_c();
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
void sendpkt(void *buf, unsigned io_len);
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned char *socket_macaddr[6];
|
2017-02-28 19:52:15 +03:00
|
|
|
SOCKET fd; // socket we listen on
|
2017-03-07 00:30:05 +03:00
|
|
|
struct sockaddr_in sin, sout; // target address for RX / TX
|
2017-02-28 01:26:43 +03:00
|
|
|
static void rx_timer_handler(void *);
|
|
|
|
void rx_timer(void);
|
|
|
|
int rx_timer_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Define the static class that registers the derived pktmover class,
|
|
|
|
// and allocates one on request.
|
|
|
|
//
|
|
|
|
class bx_socket_locator_c : public eth_locator_c {
|
|
|
|
public:
|
|
|
|
bx_socket_locator_c(void) : eth_locator_c("socket") {}
|
|
|
|
protected:
|
|
|
|
eth_pktmover_c *allocate(const char *netif, const char *macaddr,
|
|
|
|
eth_rx_handler_t rxh, eth_rx_status_t rxstat,
|
|
|
|
bx_devmodel_c *dev, const char *script) {
|
|
|
|
return (new bx_socket_pktmover_c(netif, macaddr, rxh, rxstat, dev, script));
|
|
|
|
}
|
|
|
|
} bx_socket_match;
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Define the methods for the bx_socket_pktmover derived class
|
|
|
|
//
|
|
|
|
|
|
|
|
// the constructor
|
|
|
|
//
|
|
|
|
bx_socket_pktmover_c::bx_socket_pktmover_c(const char *netif,
|
|
|
|
const char *macaddr,
|
|
|
|
eth_rx_handler_t rxh,
|
|
|
|
eth_rx_status_t rxstat,
|
|
|
|
bx_devmodel_c *dev,
|
|
|
|
const char *script)
|
|
|
|
{
|
2017-03-07 22:20:35 +03:00
|
|
|
struct hostent *hp;
|
|
|
|
int port;
|
2017-02-28 01:26:43 +03:00
|
|
|
#ifdef WIN32
|
2017-04-18 23:45:28 +03:00
|
|
|
ULONG nbl = 1;
|
2017-02-28 01:26:43 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
this->netdev = dev;
|
|
|
|
BX_INFO(("socket network driver"));
|
|
|
|
memcpy(socket_macaddr, macaddr, 6);
|
2017-03-07 22:20:35 +03:00
|
|
|
this->fd = INVALID_SOCKET;
|
2017-02-28 01:26:43 +03:00
|
|
|
|
2017-02-28 19:52:15 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err;
|
|
|
|
wVersionRequested = MAKEWORD(2, 0);
|
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
if (err != 0) {
|
|
|
|
BX_PANIC(("WSAStartup failed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-07 22:20:35 +03:00
|
|
|
if (isalpha(netif[0])) {
|
|
|
|
// Expecting format 'host:port', so split up 'netif' string.
|
|
|
|
char *host = strdup(netif);
|
|
|
|
char *substr = strtok(host, ":");
|
|
|
|
substr = strtok(NULL, ":");
|
|
|
|
if (!substr) {
|
|
|
|
BX_PANIC(("eth_socket: inet address is wrong (%s)", netif));
|
|
|
|
free(host);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hp = gethostbyname(host);
|
|
|
|
if (!hp) {
|
|
|
|
BX_PANIC(("eth_socket: gethostbyname failed (%s)", host));
|
|
|
|
free(host);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(host);
|
|
|
|
port = atoi(substr);
|
|
|
|
if (port == 0) {
|
|
|
|
BX_PANIC(("eth_socket: could not translate socket number '%s'", substr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use localhost and translate 'netif' to port number.
|
|
|
|
hp = gethostbyname("localhost");
|
|
|
|
port = atoi(netif);
|
|
|
|
if (port == 0) {
|
|
|
|
BX_PANIC(("eth_socket: could not translate socket number '%s'", netif));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 01:26:43 +03:00
|
|
|
// Open RX socket
|
|
|
|
//
|
2017-02-28 19:52:15 +03:00
|
|
|
if ((this->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
|
|
|
|
#ifndef WIN32
|
|
|
|
this->fd = INVALID_SOCKET;
|
2017-02-28 01:26:43 +03:00
|
|
|
if (errno == EACCES)
|
|
|
|
BX_PANIC(("eth_socket: insufficient priviledges to open socket"));
|
|
|
|
else
|
|
|
|
BX_PANIC(("eth_socket: could not open socket: %s", strerror(errno)));
|
2017-02-28 19:52:15 +03:00
|
|
|
#else
|
|
|
|
BX_PANIC(("eth_socket: could not open socket: error=%d", WSAGetLastError()));
|
|
|
|
#endif
|
2017-02-28 01:26:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind to given interface
|
|
|
|
//
|
|
|
|
sin.sin_family = AF_INET;
|
2017-03-07 22:20:35 +03:00
|
|
|
sin.sin_port = htons(port);
|
2017-03-07 00:30:05 +03:00
|
|
|
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
|
2017-03-07 22:20:35 +03:00
|
|
|
BX_PANIC(("eth_socket: could not bind to socket '%s' (%s)", netif, strerror(errno)));
|
2017-02-28 19:52:15 +03:00
|
|
|
closesocket(fd);
|
|
|
|
this->fd = INVALID_SOCKET;
|
2017-02-28 01:26:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up non-blocking i/o
|
|
|
|
//
|
|
|
|
#ifdef WIN32
|
|
|
|
if (ioctlsocket(this->fd, FIONBIO, &nbl) != 0) {
|
|
|
|
#else
|
|
|
|
if (fcntl(this->fd, F_SETFL, O_NONBLOCK) == -1) {
|
|
|
|
#endif
|
|
|
|
BX_PANIC(("eth_socket: could not set non-blocking i/o on socket"));
|
2017-02-28 19:52:15 +03:00
|
|
|
closesocket(this->fd);
|
|
|
|
this->fd = INVALID_SOCKET;
|
2017-02-28 01:26:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up destination address for TX ( sendto() )
|
|
|
|
//
|
|
|
|
sout.sin_family = AF_INET;
|
2017-03-07 22:20:35 +03:00
|
|
|
sout.sin_port = htons(port+1); // set TX to RX + 1
|
|
|
|
memcpy((char*) &(sout.sin_addr), hp->h_addr, hp->h_length);
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
// Start the rx poll
|
|
|
|
//
|
2017-02-28 13:18:15 +03:00
|
|
|
this->rx_timer_index =
|
2017-03-30 21:08:15 +03:00
|
|
|
DEV_register_timer(this, this->rx_timer_handler, BX_PACKET_POLL, 1, 1,
|
|
|
|
"eth_socket"); // continuous, active
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
this->rxh = rxh;
|
|
|
|
this->rxstat = rxstat;
|
2017-03-07 22:20:35 +03:00
|
|
|
BX_INFO(("socket network driver initialized: using socket '%s'", netif));
|
2017-02-28 01:26:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-07 00:30:05 +03:00
|
|
|
// the destructor
|
|
|
|
//
|
|
|
|
bx_socket_pktmover_c::~bx_socket_pktmover_c()
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-28 01:26:43 +03:00
|
|
|
// the output routine - called with pre-formatted ethernet frame.
|
|
|
|
void bx_socket_pktmover_c::sendpkt(void *buf, unsigned io_len)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2017-02-28 19:52:15 +03:00
|
|
|
if (this->fd != INVALID_SOCKET) {
|
|
|
|
status = sendto(this->fd, (char*)buf, io_len,
|
2017-03-07 00:30:05 +03:00
|
|
|
(MSG_NOSIGNAL | MSG_DONTWAIT),
|
2017-02-28 01:26:43 +03:00
|
|
|
(struct sockaddr*) &sout, sizeof(sout));
|
|
|
|
if (status == -1) {
|
|
|
|
BX_INFO(("eth_socket: write failed: %s", strerror(errno)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The receive poll process
|
|
|
|
//
|
|
|
|
void bx_socket_pktmover_c::rx_timer_handler(void *this_ptr)
|
|
|
|
{
|
|
|
|
bx_socket_pktmover_c *class_ptr = (bx_socket_pktmover_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->rx_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_socket_pktmover_c::rx_timer(void)
|
|
|
|
{
|
|
|
|
int nbytes = 0;
|
2017-03-09 01:09:50 +03:00
|
|
|
socklen_t slen = sizeof(sin);
|
2017-02-28 19:52:15 +03:00
|
|
|
Bit8u rxbuf[BX_PACKET_BUFSIZE];
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
// is socket open and bound?
|
2017-02-28 19:52:15 +03:00
|
|
|
if (this->fd == INVALID_SOCKET)
|
2017-02-28 01:26:43 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
// receive packet
|
2017-03-07 00:30:05 +03:00
|
|
|
nbytes = recvfrom(this->fd, (char*)rxbuf, sizeof(rxbuf), MSG_NOSIGNAL,
|
|
|
|
(struct sockaddr*) &sin, &slen);
|
2017-02-28 01:26:43 +03:00
|
|
|
|
|
|
|
if (nbytes == -1) {
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
BX_INFO(("eth_socket: error receiving packet: %s\n", strerror(errno)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let through broadcast and our mac address
|
|
|
|
if ((memcmp(rxbuf, this->socket_macaddr, 6) != 0) &&
|
|
|
|
(memcmp(rxbuf, broadcast_macaddr, 6) != 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->rxstat(this->netdev) & BX_NETDEV_RXREADY) {
|
|
|
|
BX_DEBUG(("eth_socket: got packet: %d bytes, dst=%x:%x:%x:%x:%x:%x, src=%x:%x:%x:%x:%x:%x\n", nbytes, rxbuf[0], rxbuf[1], rxbuf[2], rxbuf[3], rxbuf[4], rxbuf[5], rxbuf[6], rxbuf[7], rxbuf[8], rxbuf[9], rxbuf[10], rxbuf[11]));
|
|
|
|
this->rxh(this->netdev, rxbuf, nbytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* if BX_NETWORKING && BX_NETMOD_SOCKET */
|