X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_socket.c;h=acf881dd23d33fc3e06c3e0eb6bf93f9ea3a1134;hb=4f0bb09709fcff0e08aef60c1f0253bbef91f608;hp=57ee546fd44b41218b93d21019485157488e56b2;hpb=413abe8daa6db12345b9229976457c2c03412f4f;p=palacios.git diff --git a/palacios/src/palacios/vmm_socket.c b/palacios/src/palacios/vmm_socket.c index 57ee546..acf881d 100644 --- a/palacios/src/palacios/vmm_socket.c +++ b/palacios/src/palacios/vmm_socket.c @@ -21,218 +21,192 @@ #include #include #include -#include +#include struct v3_socket_hooks * sock_hooks = 0; - -//static int v3_socket_api_test(void); - - void V3_Init_Sockets(struct v3_socket_hooks * hooks) { - PrintInfo("Initializing Socket Interface\n"); - sock_hooks = hooks; + sock_hooks = hooks; + PrintDebug("V3 sockets inited\n"); - PrintDebug("V3 sockets inited\n"); - - //v3_socket_api_test(); - - return; + return; } -void v3_init_sock_set(struct v3_sock_set * sock_set) { - sock_set->num_socks = 0; - sock_set->socks = NULL; - - return; -} - +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; + } + } -/* 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)); + 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; + } + } - new_entry->sock = sock; - new_entry->is_set = 0; + /* + * Check for trailing characters. + */ + if ( (c != '\0') && + ( (!isprint(c)) || (!isspace(c)) ) ) { + return 0; + } - if (sock_set->socks) { - new_entry->next = sock_set->socks; - } + /* + * Concoct the address according to + * the number of parts specified. + */ + n = pp - parts + 1; - sock_set->socks = new_entry; + switch (n) { + case 0: + return 0; /* initial nondigit */ - sock_set->num_socks++; -} + case 1: /* a -- 32 bits */ + break; + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffffUL) { + return 0; + } -// deletes socket from sockset -void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock) { - struct v3_sock_entry * iter, * back_ptr; + val |= parts[0] << 24; + break; - iter = sock_set->socks; - back_ptr = NULL; + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff) { + return 0; + } - 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; - } + val |= (parts[0] << 24) | (parts[1] << 16); + break; - V3_Free(iter); + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff) { + return 0; + } - sock_set->num_socks--; - break; + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; } - - back_ptr = iter; - } -} - -// checks is_set vairable -int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock) { - struct v3_sock_entry * iter; - - v3_foreach_sock(sock_set, iter) { - if (iter->sock == sock) { - return iter->is_set; + + if (val) { + return v3_htonl(val); } - } - 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; - } + + return -1; } -#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 V3_Socket Macros\n"); - sock = V3_Create_TCP_Socket(); - if( sock == NULL ){ - PrintDebug( "ERROR: tcp_socket() failed!\n"); - return -1; - } - port = 80; +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; - if( V3_Bind_Socket(sock, port) < 0){ - PrintDebug("bind error\n"); - return -1; - } + rp = str; + ap = (uint8_t *)&addr; - if( V3_Listen_Socket(sock, 1) < 0) { - PrintDebug("listen error\n" ); - return -1; - } + for(n = 0; n < 4; n++) { + i = 0; - PrintDebug( "Going into mainloop: server listening on port %d\n", port); + do { + rem = *ap % (uint8_t)10; - client = V3_Accept_Socket(sock, &remote_ip , &port); + *ap /= (uint8_t)10; - PrintDebug(" New connection from %d port: %d\n", remote_ip, port); - - V3_Send(client, "Welcome!\n", 9); + inv[i++] = '0' + rem; + } while(*ap); - 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; - } + while(i--) { + *rp++ = inv[i]; + } - buf[rc] = '\0'; + *rp++ = '.'; + ap++; + } - PrintDebug( "Read %d bytes: '%s'\n", rc, buf); - } + *--rp = 0; - PrintDebug("TEST END: Sockets API\n"); - return 0; + return str; } -#endif - -#if 0 - -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; - } - 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