Fixed compilation error
[bochs-mirror.git] / iodev / eth_arpback.cc
blob82a8fb1834d114d09d3ef04d86a9c88dd7635a98
1 /////////////////////////////////////////////////////////////////////////
2 // $Id: eth_arpback.cc,v 1.19 2008/01/26 22:24:01 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2001 MandrakeSoft S.A.
6 //
7 // MandrakeSoft S.A.
8 // 43, rue d'Aboukir
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 // eth_arpback.cc - basic ethernet packetmover, only responds to ARP
29 // Various networking docs:
30 // http://www.graphcomp.com/info/rfc/
31 // rfc0826: arp
32 // rfc0903: rarp
34 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
35 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
36 // is used to know when we are exporting symbols and when we are importing.
37 #define BX_PLUGGABLE
39 #define NO_DEVICE_INCLUDES
40 #include "iodev.h"
42 #if BX_NETWORKING && defined(ETH_ARPBACK)
44 #include "eth.h"
45 #include "crc32.h"
46 #include "eth_packetmaker.h"
47 #define LOG_THIS bx_devices.pluginNE2kDevice->
50 //static const Bit8u external_mac[]={0xB0, 0xC4, 0x20, 0x20, 0x00, 0x00, 0x00};
51 //static const Bit8u internal_mac[]={0xB0, 0xC4, 0x20, 0x00, 0x00, 0x00, 0x00};
52 //static const Bit8u external_ip[]={ 192, 168, 0, 2, 0x00 };
53 //static const Bit8u ethtype_arp[]={0x08, 0x06, 0x00};
57 // Define the class. This is private to this module
59 class bx_arpback_pktmover_c : public eth_pktmover_c {
60 public:
61 bx_arpback_pktmover_c(const char *netif, const char *macaddr,
62 eth_rx_handler_t rxh,
63 void *rxarg);
64 void sendpkt(void *buf, unsigned io_len);
65 private:
66 int rx_timer_index;
67 static void rx_timer_handler(void *);
68 void rx_timer(void);
69 FILE *txlog, *txlog_txt;
70 //Bit8u arpbuf[MAX_FRAME_SIZE];
71 //Bit32u buflen;
72 //bx_bool bufvalid;
73 //CRC_Generator mycrc;
74 eth_ETHmaker packetmaker;
79 // Define the static class that registers the derived pktmover class,
80 // and allocates one on request.
82 class bx_arpback_locator_c : public eth_locator_c {
83 public:
84 bx_arpback_locator_c(void) : eth_locator_c("arpback") {}
85 protected:
86 eth_pktmover_c *allocate(const char *netif, const char *macaddr,
87 eth_rx_handler_t rxh,
88 void *rxarg, char *script) {
89 return (new bx_arpback_pktmover_c(netif, macaddr, rxh, rxarg, script));
91 } bx_arpback_match;
95 // Define the methods for the bx_arpback_pktmover derived class
98 // the constructor
99 bx_arpback_pktmover_c::bx_arpback_pktmover_c(const char *netif,
100 const char *macaddr,
101 eth_rx_handler_t rxh,
102 void *rxarg,
103 char *script)
105 this->rx_timer_index =
106 bx_pc_system.register_timer(this, this->rx_timer_handler, 1000,
107 1, 1, "eth_arpback"); // continuous, active
108 this->rxh = rxh;
109 this->rxarg = rxarg;
110 //bufvalid=0;
111 packetmaker.init();
112 #if BX_ETH_NULL_LOGGING
113 // Start the rx poll
114 // eventually Bryce wants txlog to dump in pcap format so that
115 // tcpdump -r FILE can read it and interpret packets.
116 txlog = fopen ("ne2k-tx.log", "wb");
117 if (!txlog) BX_PANIC (("open ne2k-tx.log failed"));
118 txlog_txt = fopen ("ne2k-txdump.txt", "wb");
119 if (!txlog_txt) BX_PANIC (("open ne2k-txdump.txt failed"));
120 fprintf (txlog_txt, "arpback packetmover readable log file\n");
121 fprintf (txlog_txt, "net IF = %s\n", netif);
122 fprintf (txlog_txt, "MAC address = ");
123 for (int i=0; i<6; i++)
124 fprintf (txlog_txt, "%02x%s", 0xff & macaddr[i], i<5?":" : "");
125 fprintf (txlog_txt, "\n--\n");
126 fflush (txlog_txt);
127 #endif
130 void
131 bx_arpback_pktmover_c::sendpkt(void *buf, unsigned io_len)
133 if(io_len<BX_PACKET_BUFSIZE) {
134 eth_packet barney;
135 memcpy(barney.buf,buf,io_len);
136 barney.len=io_len;
137 if(packetmaker.ishandler(barney)) {
138 packetmaker.sendpacket(barney);
141 if(( (!memcmp(buf, external_mac, 6)) || (!memcmp(buf, broadcast_macaddr, 6)) )
142 && (!memcmp(((Bit8u *)buf)+12, ethtype_arp, 2)) ) {
143 Bit32u tempcrc;
144 memcpy(arpbuf,buf,io_len); //move to temporary buffer
145 memcpy(arpbuf, arpbuf+6, 6); //set destination to sender
146 memcpy(arpbuf+6, external_mac, 6); //set sender to us
147 memcpy(arpbuf+32, arpbuf+22, 10); //move destination to sender
148 memcpy(arpbuf+22, external_mac, 6); //set sender to us
149 memcpy(arpbuf+28, external_ip, 4); //set sender to us
150 arpbuf[21]=2; //make this a reply and not a request
151 tempcrc=mycrc.get_CRC(arpbuf,io_len);
152 memcpy(arpbuf+io_len, &tempcrc, 4);
153 buflen=io_len;//+4
154 bufvalid=1;
158 #if BX_ETH_NULL_LOGGING
159 BX_DEBUG (("sendpkt length %u", io_len));
160 // dump raw bytes to a file, eventually dump in pcap format so that
161 // tcpdump -r FILE can interpret them for us.
162 int n = fwrite (buf, io_len, 1, txlog);
163 if (n != 1) BX_ERROR (("fwrite to txlog failed, length %u", io_len));
164 // dump packet in hex into an ascii log file
165 fprintf (txlog_txt, "NE2K transmitting a packet, length %u\n", io_len);
166 Bit8u *charbuf = (Bit8u *)buf;
167 for (n=0; n<(int)io_len; n++) {
168 if (((n % 16) == 0) && n>0)
169 fprintf (txlog_txt, "\n");
170 fprintf (txlog_txt, "%02x ", charbuf[n]);
172 fprintf (txlog_txt, "\n--\n");
173 // flush log so that we see the packets as they arrive w/o buffering
174 fflush (txlog);
175 fflush (txlog_txt);
176 #endif
179 void bx_arpback_pktmover_c::rx_timer_handler (void * this_ptr)
181 #if BX_ETH_NULL_LOGGING
182 BX_DEBUG (("rx_timer_handler"));
183 #endif
184 bx_arpback_pktmover_c *class_ptr = ((bx_arpback_pktmover_c *)this_ptr);
186 class_ptr->rx_timer();
189 void bx_arpback_pktmover_c::rx_timer (void)
191 //int nbytes = 0;
192 //struct bpf_hdr *bhdr;
193 eth_packet rubble;
195 if(packetmaker.getpacket(rubble)) {
196 //bufvalid=0;
197 void * buf=rubble.buf;
198 unsigned io_len=rubble.len;
199 Bit32u n;
200 fprintf (txlog_txt, "NE2K receiving a packet, length %u\n", io_len);
201 Bit8u *charbuf = (Bit8u *)buf;
202 for (n=0; n<io_len; n++) {
203 if (((n % 16) == 0) && n>0)
204 fprintf (txlog_txt, "\n");
205 fprintf (txlog_txt, "%02x ", charbuf[n]);
207 fprintf (txlog_txt, "\n--\n");
208 fflush (txlog_txt);
210 (*rxh)(rxarg, buf, io_len);
214 #endif /* if BX_NETWORKING && defined(ETH_ARPBACK) */