[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bochs-cvs
Subject:    [Bochs-cvs] [11781] trunk/bochs/iodev/network
From:       vruppert () users ! sourceforge ! net
Date:       2013-08-22 11:53:44
Message-ID: E1VCTSV-0003HM-Gj () sfs-ml-3 ! v29 ! ch3 ! sourceforge ! com
[Download RAW message or body]

Revision: 11781
Author:   vruppert
Date:     2013-08-22 11:53:40 +0000 (Thu, 22 Aug 2013)
Log Message:
-----------
Added IP and UDP header data structure definitions and related code cleanups

Modified Paths:
--------------
    trunk/bochs/iodev/network/eth_slirp.cc
    trunk/bochs/iodev/network/eth_vnet.cc
    trunk/bochs/iodev/network/netmod.h

Modified: trunk/bochs/iodev/network/eth_slirp.cc
===================================================================
--- trunk/bochs/iodev/network/eth_slirp.cc	2013-08-21 18:45:36 UTC (rev 11780)
+++ trunk/bochs/iodev/network/eth_slirp.cc	2013-08-22 11:53:40 UTC (rev 11781)
@@ -321,31 +321,34 @@
   if (len < (14U+20U)) {
     return 0;
   }
-  if ((buf[14+0] & 0xf0) != 0x40) {
+
+  ip_header_t *iphdr = (ip_header_t *)((Bit8u *)buf +
+                                       sizeof(ethernet_header_t));
+  if (iphdr->version != 4) {
     return 0;
   }
-  l3header_len = ((unsigned)(buf[14+0] & 0x0f) << 2);
+  l3header_len = (iphdr->header_len << 2);
   if (l3header_len != 20) {
     return 0;
   }
   if (len < (14U+l3header_len)) return 0;
-  if (ip_checksum(&buf[14],l3header_len) != (Bit16u)0xffff) {
+  if (ip_checksum((Bit8u*)iphdr, l3header_len) != (Bit16u)0xffff) {
     return 0;
   }
 
-  total_len = get_net2(&buf[14+2]);
+  total_len = ntohs(iphdr->total_len);
 
-  if (memcmp(&buf[14+16],dhcp.host_ipv4addr, 4) &&
-      memcmp(&buf[14+16],broadcast_ipv4addr[0],4) &&
-      memcmp(&buf[14+16],broadcast_ipv4addr[1],4) &&
-      memcmp(&buf[14+16],broadcast_ipv4addr[2],4))
+  if (memcmp(&iphdr->dst_addr, dhcp.host_ipv4addr, 4) &&
+      memcmp(&iphdr->dst_addr, broadcast_ipv4addr[0],4) &&
+      memcmp(&iphdr->dst_addr, broadcast_ipv4addr[1],4) &&
+      memcmp(&iphdr->dst_addr, broadcast_ipv4addr[2],4))
   {
     return 0;
   }
 
-  fragment_flags = (unsigned)buf[14+6] >> 5;
-  fragment_offset = ((unsigned)get_net2(&buf[14+6]) & 0x1fff) << 3;
-  ipproto = buf[14+9];
+  fragment_flags = ntohs(iphdr->frag_offs) >> 13;
+  fragment_offset = (ntohs(iphdr->frag_offs) & 0x1fff) << 3;
+  ipproto = iphdr->protocol;
 
   if ((fragment_flags & 0x1) || (fragment_offset != 0)) {
     return 0;
@@ -357,8 +360,9 @@
   if (ipproto == 0x11) {
     // guest-to-host UDP IPv4
     if (l4pkt_len < 8) return 0;
-    udp_sourceport = get_net2(&l4pkt[0]);
-    udp_targetport = get_net2(&l4pkt[2]);
+    udp_header_t *udphdr = (udp_header_t *)l4pkt;
+    udp_sourceport = ntohs(udphdr->src_port);
+    udp_targetport = ntohs(udphdr->dst_port);
     if ((udp_targetport == 67) || (udp_targetport == 69)) { // BOOTP & TFTP
       if (udp_targetport == 67) { // BOOTP
         udp_reply_size = process_dhcp(netdev, &l4pkt[8], l4pkt_len-8, &reply_buffer[42], &dhcp);

Modified: trunk/bochs/iodev/network/eth_vnet.cc
===================================================================
--- trunk/bochs/iodev/network/eth_vnet.cc	2013-08-21 18:45:36 UTC (rev 11780)
+++ trunk/bochs/iodev/network/eth_vnet.cc	2013-08-22 11:53:40 UTC (rev 11781)
@@ -43,6 +43,12 @@
 #define BX_ETH_VNET_LOGGING 1
 #define BX_ETH_VNET_PCAP_LOGGING 0
 
+#ifndef WIN32
+#include <arpa/inet.h> /* ntohs, htons */
+#else
+#include <winsock.h>
+#endif
+
 #if BX_ETH_VNET_PCAP_LOGGING
 #include <pcap.h>
 #endif
@@ -284,16 +290,16 @@
       (!memcmp(&buf[0],&dhcp.host_macaddr[0],6) ||
        !memcmp(&buf[0],&broadcast_macaddr[0],6))) {
     switch (get_net2(&buf[12])) {
-    case 0x0800: // IPv4.
-      process_ipv4(buf, io_len);
-      break;
-    case 0x0806: // ARP.
-      if (process_arp(netdev, buf, io_len, replybuf, &dhcp) > 0) {
-        host_to_guest_arp(replybuf, MIN_RX_PACKET_LEN);
-      }
-      break;
-    default: // unknown packet type.
-      break;
+      case ETHERNET_TYPE_IPV4:
+        process_ipv4(buf, io_len);
+        break;
+    case ETHERNET_TYPE_ARP:
+        if (process_arp(netdev, buf, io_len, replybuf, &dhcp) > 0) {
+          host_to_guest_arp(replybuf, MIN_RX_PACKET_LEN);
+        }
+        break;
+      default: // unknown packet type.
+        break;
     }
   }
 }
@@ -383,30 +389,34 @@
     BX_INFO(("ip packet - too small packet"));
     return;
   }
-  if ((buf[14+0] & 0xf0) != 0x40) {
-    BX_INFO(("ipv%u packet - not implemented",((unsigned)buf[14+0] >> 4)));
+
+  ip_header_t *iphdr = (ip_header_t *)((Bit8u *)buf +
+                                       sizeof(ethernet_header_t));
+  if (iphdr->version != 4) {
+    BX_INFO(("ipv%u packet - not implemented", iphdr->version));
     return;
   }
-  l3header_len = ((unsigned)(buf[14+0] & 0x0f) << 2);
+  l3header_len = (iphdr->header_len << 2);
   if (l3header_len != 20) {
     BX_ERROR(("ip: option header is not implemented"));
     return;
   }
   if (io_len < (14U+l3header_len)) return;
-  if (ip_checksum(&buf[14],l3header_len) != (Bit16u)0xffff) {
+  if (ip_checksum((Bit8u*)iphdr, l3header_len) != (Bit16u)0xffff) {
     BX_INFO(("ip: invalid checksum"));
     return;
   }
 
-  total_len = get_net2(&buf[14+2]);
+  total_len = ntohs(iphdr->total_len);
+
   // FIXED By EaseWay
   // Ignore this check to tolerant some cases
   //if (io_len > (14U+total_len)) return;
 
-  if (memcmp(&buf[14+16],dhcp.host_ipv4addr,4) &&
-      memcmp(&buf[14+16],broadcast_ipv4addr[0],4) &&
-      memcmp(&buf[14+16],broadcast_ipv4addr[1],4) &&
-      memcmp(&buf[14+16],broadcast_ipv4addr[2],4))
+  if (memcmp(&iphdr->dst_addr, dhcp.host_ipv4addr, 4) &&
+      memcmp(&iphdr->dst_addr, broadcast_ipv4addr[0],4) &&
+      memcmp(&iphdr->dst_addr, broadcast_ipv4addr[1],4) &&
+      memcmp(&iphdr->dst_addr, broadcast_ipv4addr[2],4))
   {
     BX_INFO(("target IP address %u.%u.%u.%u is unknown",
       (unsigned)buf[14+16],(unsigned)buf[14+17],
@@ -414,10 +424,9 @@
     return;
   }
 
-//  packet_id = get_net2(&buf[14+4]);
-  fragment_flags = (unsigned)buf[14+6] >> 5;
-  fragment_offset = ((unsigned)get_net2(&buf[14+6]) & 0x1fff) << 3;
-  ipproto = buf[14+9];
+  fragment_flags = ntohs(iphdr->frag_offs) >> 13;
+  fragment_offset = (ntohs(iphdr->frag_offs) & 0x1fff) << 3;
+  ipproto = iphdr->protocol;
 
   if ((fragment_flags & 0x1) || (fragment_offset != 0)) {
     BX_INFO(("ignore fragmented packet!"));
@@ -570,16 +579,17 @@
   layer4_handler_t func;
 
   if (l4pkt_len < 8) return;
-  udp_sourceport = get_net2(&l4pkt[0]);
-  udp_targetport = get_net2(&l4pkt[2]);
-//  udp_len = get_net2(&l4pkt[4]);
+  udp_header_t *udphdr = (udp_header_t *)l4pkt;
+  udp_sourceport = ntohs(udphdr->src_port);
+  udp_targetport = ntohs(udphdr->dst_port);
+//  udp_len = ntohs(udphdr->length);
 
-  func = get_layer4_handler(0x11,udp_targetport);
+  func = get_layer4_handler(0x11, udp_targetport);
   if (func != (layer4_handler_t)NULL) {
-    (*func)((void *)this,ipheader,ipheader_len,
-      udp_sourceport,udp_targetport,&l4pkt[8],l4pkt_len-8);
+    (*func)((void *)this,ipheader, ipheader_len,
+      udp_sourceport, udp_targetport, &l4pkt[8], l4pkt_len-8);
   } else {
-    BX_INFO(("udp - unhandled port %u",udp_targetport));
+    BX_INFO(("udp - unhandled port %u", udp_targetport));
   }
 }
 

Modified: trunk/bochs/iodev/network/netmod.h
===================================================================
--- trunk/bochs/iodev/network/netmod.h	2013-08-21 18:45:36 UTC (rev 11780)
+++ trunk/bochs/iodev/network/netmod.h	2013-08-22 11:53:40 UTC (rev 11781)
@@ -72,6 +72,46 @@
 #endif
 ethernet_header_t;
 
+typedef struct ip_header {
+#if defined(_MSC_VER) && (_MSC_VER>=1300)
+  __declspec(align(1))
+#endif
+#ifdef BX_LITTLE_ENDIAN
+  Bit8u header_len : 4;
+  Bit8u version : 4;
+#else
+  Bit8u version : 4;
+  Bit8u header_len : 4;
+#endif
+  Bit8u tos;
+  Bit16u total_len;
+  Bit16u id;
+  Bit16u frag_offs;
+  Bit8u ttl;
+  Bit8u protocol;
+  Bit16u checksum;
+  Bit32u src_addr;
+  Bit32u dst_addr;
+} 
+#if !defined(_MSC_VER)
+  GCC_ATTRIBUTE((packed))
+#endif
+ip_header_t;
+
+typedef struct udp_header {
+#if defined(_MSC_VER) && (_MSC_VER>=1300)
+  __declspec(align(1))
+#endif
+  Bit16u src_port;
+  Bit16u dst_port;
+  Bit16u length;
+  Bit16u checksum;
+} 
+#if !defined(_MSC_VER)
+  GCC_ATTRIBUTE((packed))
+#endif
+udp_header_t;
+
 #if defined(_MSC_VER)
 #pragma pack(pop)
 #elif defined(__MWERKS__) && defined(macintosh)


------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
Bochs-cvs mailing list
Bochs-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bochs-cvs
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic