Previous 15994 Revisions Next

r13147 Monday 31st October, 2011 at 01:28:37 UTC by R. Belmont
SDL: Adding Carl's TAP/TUN Linux network support and config shell script (nw, will be credited in MAME)
[ROOT]makefile
[src/osd/sdl]netdev.c* netdev.h* netdev_tap.c* netdev_tap.h* sdl.mak sdlmain.c taputil.sh*

src/osd/sdl/sdl.mak
r13146r13147
134134ifeq ($(TARGETOS),linux)
135135BASE_TARGETOS = unix
136136SYNC_IMPLEMENTATION = tc
137SDL_NETWORK = on
137138endif
138139
139140ifeq ($(TARGETOS),freebsd)
r13146r13147
497498endif # NO_X11
498499
499500#-------------------------------------------------
501# Network (TAP/TUN)
502#-------------------------------------------------
503
504ifdef USE_NETWORK
505ifeq ($(SDL_NETWORK),on)
506OSDOBJS += \
507   $(SDLOBJ)/netdev.o \
508   $(SDLOBJ)/netdev_tap.o
509
510DEFS += -DSDLMAME_NETWORK
511endif
512endif
513
514#-------------------------------------------------
500515# Dependencies
501516#-------------------------------------------------
502517
src/osd/sdl/netdev_tap.c
r0r13147
1#include <unistd.h>
2#include <fcntl.h>
3#include <sys/ioctl.h>
4#include <net/if.h>
5#include <errno.h>
6
7#include "emu.h"
8#include "osdnet.h"
9
10#ifdef __linux__
11#define IFF_TAP      0x0002
12#define IFF_NO_PI   0x1000
13#define TUNSETIFF     _IOW('T', 202, int)
14#endif
15
16class netdev_tap : public netdev
17{
18public:
19   netdev_tap(const char *name, class device_network_interface *ifdev, int rate);
20   ~netdev_tap();
21   
22   int send(UINT8 *buf, int len);
23   void set_mac(const char *mac);
24protected:
25   int recv_dev(UINT8 **buf);
26private:
27   int m_fd;
28   char m_ifname[10];
29   char m_mac[6];
30   UINT8 m_buf[2048];
31};
32
33netdev_tap::netdev_tap(const char *name, class device_network_interface *ifdev, int rate)
34   : netdev(ifdev, rate)
35{
36#ifdef __linux__
37   struct ifreq ifr;
38
39   m_fd = -1;
40   if((m_fd = open("/dev/net/tun", O_RDWR)) == -1) {
41      logerror("tap: open failed %d\n", errno);
42      return;
43   }
44
45   memset(&ifr, 0, sizeof(ifr));
46   ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
47   sprintf(ifr.ifr_name, "tap-mess-%d-0", getuid());
48   if(ioctl(m_fd, TUNSETIFF, (void *)&ifr) == -1) {
49      logerror("tap: ioctl failed %d\n", errno);
50      close(m_fd);
51      m_fd = -1;
52      return;
53   }
54   strncpy(m_ifname, ifr.ifr_name, 10);
55   fcntl(m_fd, F_SETFL, O_NONBLOCK);
56   
57#else
58   m_fd = -1;
59#endif
60}
61
62netdev_tap::~netdev_tap()
63{
64   close(m_fd);
65}
66
67void netdev_tap::set_mac(const char *mac)
68{
69   memcpy(m_mac, mac, 6);
70}
71
72int netdev_tap::send(UINT8 *buf, int len)
73{
74   if(m_fd == -1) return 0;
75   len = write(m_fd, buf, len);
76   return (len == -1)?0:len;
77}
78
79int netdev_tap::recv_dev(UINT8 **buf)
80{
81   int len;
82   if(m_fd == -1) return 0;
83   len = read(m_fd, m_buf, sizeof(m_buf));
84   *buf = m_buf;
85   return (len == -1)?0:len;
86}
87
88static CREATE_NETDEV(create_tap)
89{
90   class netdev_tap *dev = new netdev_tap(ifname, ifdev, rate);
91   return dynamic_cast<netdev *>(dev);
92}
93
94void init_tap()
95{
96   add_netdev("tap", create_tap);
97}
src/osd/sdl/netdev_tap.h
r0r13147
1#ifndef __NETDEV_H
2#define __NETDEV_H
3
4void init_tap();
5
6#endif
src/osd/sdl/taputil.sh
r0r13147
1#!/bin/sh
2
3NAME=$2
4UID=`id -u $NAME`
5HOSTIP=$4
6EMUIP=$3
7TAP="tap-mess-$UID-0"
8
9if [ `id -u` != "0" ]
10then
11echo "must be run as root"
12exit
13fi
14
15if [ "$1" = "-d" ]
16then
17echo 0 > /proc/sys/net/ipv4/ip_forward
18echo 0 > /proc/sys/net/ipv4/conf/all/proxy_arp
19chmod 660 /dev/net/tun
20ip tuntap del dev $TAP mode tap
21exit
22fi
23
24if [ "$#" != "5" ]
25then
26echo "usage: mess-tap [-c] [-d] USER EMUADDR HOSTADDR MASK\n"
27echo "-c\t\tcreate interface"
28echo "-d\t\tdelete interface"
29echo "USER\t\tuser to own interface, required to delete"
30echo "EMUADDR\temulated machine ip address"
31echo "HOSTADDR\thost ip address"
32exit
33fi
34
35echo 1 > /proc/sys/net/ipv4/ip_forward
36echo 1 > /proc/sys/net/ipv4/conf/all/proxy_arp
37chmod 666 /dev/net/tun
38
39ip tuntap add dev $TAP mode tap user $NAME pi
40ip link set $TAP up arp on
41ip addr replace dev $TAP $HOSTIP/32
42ip route replace $EMUIP via $HOSTIP dev $TAP
src/osd/sdl/netdev.c
r0r13147
1#include "emu.h"
2#include "netdev_tap.h"
3
4void sdlnetdev_init(running_machine &machine)
5{
6   init_tap();
7}
src/osd/sdl/sdlmain.c
r13146r13147
4444#include "output.h"
4545#include "osdsdl.h"
4646#include "sdlos.h"
47#include "netdev.h"
4748
4849// we override SDL's normal startup on Win32
4950// please see sdlprefix.h as well
r13146r13147
646647   }
647648
648649   sdlinput_init(machine);
649
650650   sdlaudio_init(machine);
651
652651   sdloutput_init(machine);
653652
653#ifdef SDLMAME_NETWORK
654   sdlnetdev_init(machine);
655#endif
656
654657   if (options.oslog())
655658      machine.add_logerror_callback(output_oslog);
656659
src/osd/sdl/netdev.h
r0r13147
1#ifndef __NETDEV_H
2#define __NETDEV_H
3
4void sdlnetdev_init(running_machine &machine);
5
6#endif
makefile
r13146r13147
7575UNAME = $(shell uname -a)
7676
7777ifeq ($(firstword $(filter Linux,$(UNAME))),Linux)
78TARGETOS = unix
78TARGETOS = linux
7979endif
8080ifeq ($(firstword $(filter Solaris,$(UNAME))),Solaris)
8181TARGETOS = solaris

Previous 15994 Revisions Next


© 1998-2018 The MESS Team