qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Brandon Bennett" <bennetb@gmail.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] PATCH [2/2] Patches to support Juniper Olives - net udp
Date: Thu, 16 Oct 2008 15:46:11 -0600	[thread overview]
Message-ID: <d81b6b610810161446u191804c5u61aad234e8df62b8@mail.gmail.com> (raw)

This patch is to support udp -net option.  This transport is
compatible with dynamips udp transport.  This will allow Juniper
Olives (or any QEMU) to connect directly to a dynamips/dynagen/gns3
lab without the hassle of creating tun/tap interfaces and bridging.

This code was taken from mmm123's version of pemu which is a modified
version of QEMU for emulating Cisco Pix firewalls.   The code has been
updated to more fit QEMU's coding practices.

Original code can be found here: http://7200emu.hacki.at/viewtopic.php?t=5383

-Brandon Bennett

Index: vl.c
===================================================================
--- vl.c	(revision 5487)
+++ vl.c	(working copy)
@@ -5087,6 +5087,72 @@

 }

+typedef struct UDPState {
+  VLANClientState *vc;
+  int fd;
+  struct sockaddr_in sender;
+} UDPState;
+
+static void net_udp_send (void *opaque)
+{
+  UDPState *s = opaque;
+  uint8_t buf[4096];
+  int size;
+  size = recvfrom(s->fd, buf, sizeof(buf), 0, NULL, NULL);
+
+  if (size > 0)
+    qemu_send_packet(s->vc, buf, size);
+}
+
+static void net_udp_receive(void *opaque, const uint8_t *buf, int size)
+{
+  UDPState *s = opaque;
+  int res;
+  res = sendto(s->fd, buf, size, 0, (struct sockaddr *)&s->sender,
sizeof (s->sender));
+}
+
+static int net_udp_init(VLANState *vlan, int lport, char *rhost, int rport)
+{
+  UDPState *s;
+  struct sockaddr_in reciever;
+  int ret;
+
+  s = qemu_mallocz(sizeof(DUDPState));
+  if (!s)
+    return -1;
+
+  s->fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+
+  if (s->fd < 0) {
+    perror("socket")
+    return -1;
+  }
+
+  reciever.sin_family = AF_INET;
+  reciever.sin_addr.s_addr = INADDR_ANY;
+  reciever.sin_port = htons(lport);
+
+  ret = bind(s->rfd, (struct sockaddr *)&reciever, sizeof(reciever))
+  if (ret < 0) {
+    perror("bind");
+    return -1;
+  }
+
+  memset((char*)&s->sender, sizeof(s->sender), 0);
+  s->sender.sin_family = AF_INET;
+  s->sender.sin_port = htons(rport);
+  inet_aton(rhost, &s->sender.sin_addr);
+
+  s->vc = qemu_new_vlan_client(vlan, net_udp_receive, NULL, s);
+  qemu_set_fd_handler(s->rfd, net_udp_send, NULL, s);
+
+  snprintf(s->vc->info_str, sizeof(s->vc->info_str),
+      "udp: %i->%s:%i",
+      lport, rhost, rport);
+
+  return 0;
+}
+
 static const char *get_opt_name(char *buf, int buf_size, const char *p)
 {
     char *q;
@@ -5313,6 +5379,26 @@
 	ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
     } else
 #endif
+    if (!strcmp(device, "udp")) {
+        int sport, dport;
+        char buf[128];
+
+        if (get_param_value(buf, sizeof(buf), "dport", p) <= 0) {
+          fprintf(stderr, "You must specify a destination port with dport\n");
+          exit(0);
+        }
+        dport = atoi(buf);
+        if (get_param_value(buf, sizeof(buf), "sport", p)<=0) {
+          fprintf(stderr, "You must specify a source port with sport\n");
+          exit(0);
+        }
+        sport = atoi(buf);
+        if (get_param_value(buf, sizeof(buf), "daddr", p) <= 0) {
+          fprintf(stderr, "You must specify a destination address
with daddr\n");
+          exit(0);
+        }
+       ret = net_dudp_init(vlan,sport,daddr,dport);
+    } else
     {
         fprintf(stderr, "Unknown network device: %s\n", device);
         return -1;
@@ -8225,6 +8311,8 @@
            "                Use group 'groupname' and mode
'octalmode' to change default\n"
            "                ownership and permissions for
communication port.\n"
 #endif
+           "-net udp[,vlan=n]sport=sport,dport=dport,daddr=host\n"
+           "                connect the vlan 'n' to a udp host (for
dynamips)\n"
            "-net none       use it alone to have zero network
devices; if no -net option\n"
            "                is provided, the default is '-net nic -net user'\n"
            "\n"

             reply	other threads:[~2008-10-16 21:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-16 21:46 Brandon Bennett [this message]
2008-10-27  0:39 ` [Qemu-devel] PATCH [2/2] Patches to support Juniper Olives - net udp andrzej zaborowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d81b6b610810161446u191804c5u61aad234e8df62b8@mail.gmail.com \
    --to=bennetb@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).