#include <palacios/vmm_socket.h>
#include <palacios/vmm.h>
#include <palacios/vmm_debug.h>
-#include <palacios/vmm_stddef.h>
+#include <palacios/vmm_types.h>
struct v3_socket_hooks * sock_hooks = 0;
+void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
+ sock_hooks = hooks;
+ PrintDebug("V3 sockets inited\n");
-//static int v3_socket_api_test(void);
+ return;
+}
-void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
- PrintInfo("Initializing Socket Interface\n");
- sock_hooks = hooks;
- PrintDebug("V3 sockets inited\n");
+uint32_t v3_inet_addr(const char * ip_str) {
+ uint32_t val;
+ int base, n, c;
+ uint32_t parts[4];
+ uint32_t * pp = parts;
- //v3_socket_api_test();
-
- return;
-}
+ 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;
+ }
+ }
-void v3_init_sock_set(struct v3_sock_set * sock_set) {
- sock_set->num_socks = 0;
- sock_set->socks = NULL;
+ 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;
+ }
+ }
- return;
-}
+ /*
+ * Check for trailing characters.
+ */
+ if ( (c != '\0') &&
+ ( (!isprint(c)) || (!isspace(c)) ) ) {
+ return 0;
+ }
+ /*
+ * Concoct the address according to
+ * the number of parts specified.
+ */
+ n = pp - parts + 1;
+ switch (n) {
+ case 0:
+ return 0; /* initial nondigit */
+ case 1: /* a -- 32 bits */
+ break;
+ case 2: /* a.b -- 8.24 bits */
+ if (val > 0xffffffUL) {
+ return 0;
+ }
-/* 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));
+ val |= parts[0] << 24;
+ break;
- new_entry->sock = sock;
- new_entry->is_set = 0;
+ case 3: /* a.b.c -- 8.8.16 bits */
+ if (val > 0xffff) {
+ return 0;
+ }
- if (sock_set->socks) {
- new_entry->next = sock_set->socks;
- }
+ val |= (parts[0] << 24) | (parts[1] << 16);
+ break;
- sock_set->socks = new_entry;
+ case 4: /* a.b.c.d -- 8.8.8.8 bits */
+ if (val > 0xff) {
+ return 0;
+ }
- sock_set->num_socks++;
+ val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+ break;
+ }
+
+ if (val) {
+ return v3_htonl(val);
+ }
+
+ return -1;
}
-// deletes socket from sockset
-void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
- struct v3_sock_entry * iter, * back_ptr;
+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;
- iter = sock_set->socks;
- back_ptr = NULL;
+ rp = str;
+ ap = (uint8_t *)&addr;
- 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;
- }
+ for(n = 0; n < 4; n++) {
+ i = 0;
- V3_Free(iter);
+ do {
+ rem = *ap % (uint8_t)10;
- sock_set->num_socks--;
- break;
- }
+ *ap /= (uint8_t)10;
- back_ptr = iter;
- }
-}
+ inv[i++] = '0' + rem;
+ } while(*ap);
-// checks is_set vairable
-int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
- struct v3_sock_entry * iter;
+ while(i--) {
+ *rp++ = inv[i];
+ }
- v3_foreach_sock(sock_set, iter) {
- if (iter->sock == sock) {
- return iter->is_set;
+ *rp++ = '.';
+ ap++;
}
- }
- return -1;
-}
+ *--rp = 0;
-// 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;
- }
+ return str;
}
-#if 0
-static 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 BEGIN: Sockets API\n");
- sock = sock_hooks->tcp_socket(0, 0, 0);
- if( sock == NULL ){
- PrintDebug( "ERROR: tcp_socket() failed!\n");
- return -1;
- }
- port = 80;
- if( sock_hooks->bind_socket(sock, port) < 0){
- PrintDebug("bind error\n");
- return -1;
- }
- if( sock_hooks->listen(sock, 1) < 0) {
- PrintDebug("listen error\n" );
- return -1;
- }
- PrintDebug( "Going into mainloop: server listening on port %d\n", port);
+uint16_t v3_htons(uint16_t n) {
+ return (((n & 0xff) << 8) | ((n & 0xff00) >> 8));
+}
- client = sock_hooks->accept(sock, &remote_ip , &port);
- PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
-
- sock_hooks->send(client, "Welcome!\n", 9);
+uint16_t v3_ntohs(uint16_t n) {
+ return v3_htons(n);
+}
- 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;
- }
- buf[rc] = '\0';
+uint32_t v3_htonl(uint32_t n) {
+ return (((n & 0xff) << 24) |
+ ((n & 0xff00) << 8) |
+ ((n & 0xff0000UL) >> 8) |
+ ((n & 0xff000000UL) >> 24));
+}
- PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
- }
- PrintDebug("TEST END: Sockets API\n");
- return 0;
+uint32_t v3_ntohl(uint32_t n) {
+ return v3_htonl(n);
}
-
-#endif