Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


linux build fix and deinitialization fix
[palacios.git] / palacios / src / palacios / vmm_socket.c
index 21063b9..683f6e4 100644 (file)
 #include <palacios/vmm_socket.h>
 #include <palacios/vmm.h>
 #include <palacios/vmm_debug.h>
-#include <palacios/vmm_stddef.h>
+#include <palacios/vmm_types.h>
+#include <palacios/vm_guest.h>
+
+static struct v3_socket_hooks * sock_hooks = 0;
+
+
+uint32_t v3_inet_addr(const char * ip_str) {
+    uint32_t val;
+    int base, n, c;
+    uint32_t parts[4];
+    uint32_t * pp = parts;
+
+    c = *ip_str;
+    for (;;) {
+       /*
+        * Collect number up to ``.''.
+        * Values are specified as for C:
+        * 0x=hex, 0=octal, 1-9=decimal.
+        */
+       if (!isdigit(c)) {
+           return (0);
+       }
+
+       val = 0;
+       base = 10;
+
+       if (c == '0') {
+           c = *++ip_str;
+           if ((c == 'x') || (c == 'X')) {
+               base = 16;
+               c = *++ip_str;
+           } else {
+               base = 8;
+           }
+       }
+
+       for (;;) {
+           if (isdigit(c)) {
+               val = (val * base) + (int)(c - '0');
+               c = *++ip_str;
+           } else if ((base == 16) && (isxdigit(c))) {
+               val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
+               c = *++ip_str;
+           } else {
+               break;
+           }
+       }
+
+       if (c == '.') {
+           /*
+            * Internet format:
+            *  a.b.c.d
+            *  a.b.c   (with c treated as 16 bits)
+            *  a.b (with b treated as 24 bits)
+            */
+           if (pp >= parts + 3) {
+               return 0;
+           }
+
+           *pp++ = val;
+           c = *++ip_str;
+       } else {
+           break;
+       }
+    }
 
+    /*
+     * Check for trailing characters.
+     */
+    if ( (c != '\0') && 
+        ( (!isprint(c)) || (!isspace(c)) ) ) {
+       return 0;
+    }
 
-struct v3_socket_hooks * sock_hooks = 0;
+    /*
+     * Concoct the address according to
+     * the number of parts specified.
+     */
+    n = pp - parts + 1;
 
+    switch (n) {
+       case 0:
+           return 0;       /* initial nondigit */
 
-//int v3_socket_api_test(void);
+       case 1:             /* a -- 32 bits */
+           break;
 
+       case 2:             /* a.b -- 8.24 bits */
+           if (val > 0xffffffUL) {
+               return 0;
+           }
 
-void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
-  PrintInfo("Initializing Socket Interface\n");
-  sock_hooks = hooks;
+           val |= parts[0] << 24;
+           break;
 
-  PrintDebug("V3 sockets inited\n");
+       case 3:             /* a.b.c -- 8.8.16 bits */
+           if (val > 0xffff) {
+               return 0;
+           }
 
-  v3_socket_api_test();
+           val |= (parts[0] << 24) | (parts[1] << 16);
+           break;
+
+       case 4:             /* a.b.c.d -- 8.8.8.8 bits */
+           if (val > 0xff) {
+               return 0;
+           }
+
+           val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+           break;
+    }
   
-  return;
+    if (val) {
+       return v3_htonl(val);
+    }
+  
+    return -1;
 }
 
 
+char * v3_inet_ntoa(uint32_t addr) {
+    static char str[16];
+    char inv[3];
+    char * rp;
+    uint8_t * ap;
+    uint8_t rem;
+    uint8_t n;
+    uint8_t i;
 
-void v3_init_sock_set(struct v3_sock_set * sock_set) {
-  sock_set->num_socks = 0;
-  sock_set->socks = NULL;
+    rp = str;
+    ap = (uint8_t *)&addr;
 
-  return;
-}
+    for (n = 0; n < 4; n++) {
+       i = 0;
+
+       do {
+           rem = *ap % (uint8_t)10;
 
+           *ap /= (uint8_t)10;
+
+           inv[i++] = '0' + rem;
+       } while(*ap);
+
+       while(i--) {
+           *rp++ = inv[i];
+       }
 
+       *rp++ = '.';
+       ap++;
+    }
 
+    *--rp = 0;
 
+    return str;
+}
 
-/* This should probably check if the socket is already added */
-// adds socket to the sockset
-void v3_set_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
-  struct v3_sock_entry * new_entry = V3_Malloc(sizeof(struct v3_sock_entry));
 
-  new_entry->sock = sock;
-  new_entry->is_set = 0;
 
-  if (sock_set->socks) {
-    new_entry->next = sock_set->socks;
-  }
 
-  sock_set->socks = new_entry;
 
-  sock_set->num_socks++;
+uint16_t v3_htons(uint16_t n) {
+    return (((n & 0xff) << 8) | ((n & 0xff00) >> 8));
 }
 
 
-// deletes socket from sockset
-void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
-  struct v3_sock_entry * iter, * back_ptr;
+uint16_t v3_ntohs(uint16_t n) {
+    return v3_htons(n);
+}
 
-  iter = sock_set->socks;
-  back_ptr = NULL;
 
-  v3_foreach_sock(sock_set, iter) {
-    if (iter->sock == sock) {
-      if (back_ptr == NULL) {
-       sock_set->socks = iter->next;
-      } else {
-       back_ptr->next = iter->next;
-      }
+uint32_t v3_htonl(uint32_t n) {
+    return (((n & 0xff) << 24) |
+           ((n & 0xff00) << 8) |
+           ((n & 0xff0000UL) >> 8) |
+           ((n & 0xff000000UL) >> 24));
+}
 
-      V3_Free(iter);
 
-      sock_set->num_socks--;
-      break;
-    }
+uint32_t v3_ntohl(uint32_t n) {
+  return v3_htonl(n);
+}
+
 
-    back_ptr = iter;
-  }
+v3_sock_t v3_create_udp_socket(struct v3_vm_info * vm) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->udp_socket);
+    void * priv_data = NULL;
+
+    if (vm) {
+       priv_data = vm->host_priv_data;
+    }
+    
+    return sock_hooks->udp_socket(0, 0, priv_data);
 }
 
-// checks is_set vairable 
-int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
-  struct v3_sock_entry * iter;
+v3_sock_t v3_create_tcp_socket(struct v3_vm_info * vm) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->tcp_socket);
+    void * priv_data = NULL;
 
-  v3_foreach_sock(sock_set, iter) {
-    if (iter->sock == sock) {
-      return iter->is_set;
+    if (vm) {
+       priv_data = vm->host_priv_data;
     }
-  }
-  return -1;
-}
-
-
-// clears all is_set variables.
-void v3_zero_sockset(struct v3_sock_set * sock_set) {
-  struct v3_sock_entry * iter;
-  v3_foreach_sock(sock_set, iter) {
-    iter->is_set = 0;
-  }
-}
-
-#if 1
-
-int
-v3_socket_api_test(void)
-{
-       unsigned int port;
-       char buf[1024];
-       int rc = 0;
-       V3_SOCK sock; 
-       V3_SOCK client;
-       unsigned int remote_ip;
-       
-       PrintDebug("\nIn Palacios: Test V3_Socket Macros\n");
-       sock = V3_Create_TCP_Socket();
-       if( ((int)sock) < 0 ){
-               PrintDebug( "ERROR: tcp_socket() failed!\n");
-               return -1;
-       }
 
-       port = 80;
+    return sock_hooks->tcp_socket(0, 1, 0, priv_data);
+}
 
-       if( V3_Bind_Socket(sock, port) < 0){
-               PrintDebug("bind error\n");
-               return -1;
-       }
+void v3_socket_close(v3_sock_t sock) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->close);
 
-       if( V3_Listen_Socket(sock, 1) < 0) {
-               PrintDebug("listen error\n" );
-               return -1;
-       }
+    sock_hooks->close(sock);
+}
 
-       PrintDebug( "Going into mainloop: server listening on port %d\n", port);
+int v3_socket_bind(const v3_sock_t sock, uint16_t port) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->bind);
 
-       client = V3_Accept_Socket(sock, &remote_ip, &port);
+    return sock_hooks->bind(sock, port);
+}
 
-       PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
-            
-       V3_Send(client, "Welcome!\n", 9);
+int v3_socket_listen(const v3_sock_t sock, int backlog) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->listen);
 
-       while(1)
-       {               
-            V3_Send(client, buf, rc);
-             rc = V3_Recv(client, buf, sizeof(buf)-1);
-            if( rc <= 0 ){
-                               PrintDebug( "Closed connection\n");
-                               V3_Close_Socket(client);
-                               break;
-            }
+    return sock_hooks->listen(sock, backlog);
+}
 
-            buf[rc] = '\0';
+v3_sock_t v3_socket_accept(const v3_sock_t sock, uint32_t * remote_ip, uint32_t * port) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->accept);
 
-            PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
-        }
+    return sock_hooks->accept(sock, remote_ip, port);
+}
 
-       PrintDebug("TEST END: Sockets API\n");
-       return 0;
+int v3_connect_to_ip(const v3_sock_t sock, const uint32_t hostip, const uint16_t port) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->connect_to_ip);
+    
+    return sock_hooks->connect_to_ip(sock, hostip, port);
 }
 
-#endif
+int v3_connect_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->connect_to_host);
 
+    return sock_hooks->connect_to_host(sock, hostname, port);
+}
 
-#if 0
+int v3_socket_send(const v3_sock_t sock, const uint8_t * buf, const uint32_t len) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->send);
 
-static int
-socket_api_test(void)
-{
-       unsigned int port;
-       char buf[1024];
-       int rc = 0;
-       V3_SOCK sock; 
-       V3_SOCK client;
-       unsigned int remote_ip;
-       
-       PrintDebug("\nIn Palacios: TEST BEGIN: Sockets API\n");
-       sock = sock_hooks->tcp_socket(0, 0, 0);
-       if( sock == NULL ){
-               PrintDebug( "ERROR: tcp_socket() failed!\n");
-               return -1;
-       }
+    return sock_hooks->send(sock, buf, len);
+}
 
-       port = 80;
+int v3_socket_recv(const v3_sock_t sock, uint8_t * buf, const uint32_t len) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->recv);
 
-       if( sock_hooks->bind_socket(sock, port) < 0){
-               PrintDebug("bind error\n");
-               return -1;
-       }
+    return sock_hooks->recv(sock, buf, len);
+}
 
-       if( sock_hooks->listen(sock, 1) < 0) {
-               PrintDebug("listen error\n" );
-               return -1;
-       }
+int v3_socket_send_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port, 
+                          const uint8_t * buf, const uint32_t len) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->sendto_host);
 
-       PrintDebug( "Going into mainloop: server listening on port %d\n", port);
+    return sock_hooks->sendto_host(sock, hostname, port, buf, len);
+}
 
-       client = sock_hooks->accept(sock, &remote_ip , &port);
+int v3_socket_send_to_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port, 
+                        const uint8_t * buf, const uint32_t len) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->sendto_ip);
 
-       PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
-            
-       sock_hooks->send(client, "Welcome!\n", 9);
+    return sock_hooks->sendto_ip(sock, ip, port, buf, len);
+}
 
-       while(1)
-       {               
-            sock_hooks->send(client, buf, rc);
-            rc = sock_hooks->recv(client, buf, sizeof(buf)-1);
-            if( rc <= 0 ){
-                               PrintDebug( "Closed connection\n");
-                               sock_hooks->close(client);
-                               break;
-            }
+int v3_socket_recv_from_host(const v3_sock_t sock, const char * hostname, const uint16_t port, 
+                            uint8_t * buf, const uint32_t len) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->recvfrom_host);
 
-            buf[rc] = '\0';
+    return sock_hooks->recvfrom_host(sock, hostname, port, buf, len);
+}
 
-            PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
-        }
+int v3_socket_recv_from_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port, 
+                          uint8_t * buf, const uint32_t len) {
+    V3_ASSERT(sock_hooks);
+    V3_ASSERT(sock_hooks->recvfrom_ip);
 
-       PrintDebug("TEST END: Sockets API\n");
-       return 0;
+    return sock_hooks->recvfrom_ip(sock, ip, port, buf, len);
 }
 
-#endif
+
+
+
+void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
+    sock_hooks = hooks;
+    PrintDebug("V3 sockets inited\n");
+
+    return;
+}