X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_socket.c;h=ee5bc92f409c6c8f0877ca4a9e38410222ae5810;hb=ae2948e2c649d8e63252d4b665f29b268858294e;hp=854668809315268823ebf92f51f6e6d16633e5ce;hpb=a3a2224fa5e7d5c9d0ef2fe29bae0ccfb373ba26;p=palacios.git diff --git a/palacios/src/palacios/vmm_socket.c b/palacios/src/palacios/vmm_socket.c index 8546688..ee5bc92 100644 --- a/palacios/src/palacios/vmm_socket.c +++ b/palacios/src/palacios/vmm_socket.c @@ -19,13 +19,220 @@ #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; + + PrintDebug("V3 sockets inited\n"); + + v3_socket_api_test(); + + return; +} + + + +void v3_init_sock_set(struct v3_sock_set * sock_set) { + sock_set->num_socks = 0; + sock_set->socks = NULL; + + return; +} + + + + + +/* 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++; } + + +// deletes socket from sockset +void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock) { + struct v3_sock_entry * iter, * back_ptr; + + 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; + } + + V3_Free(iter); + + sock_set->num_socks--; + 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; + } + } + 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 +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; + + if( V3_Bind_Socket(sock, port) < 0){ + PrintDebug("bind error\n"); + return -1; + } + + if( V3_Listen_Socket(sock, 1) < 0) { + PrintDebug("listen error\n" ); + return -1; + } + + PrintDebug( "Going into mainloop: server listening on port %d\n", port); + + client = V3_Accept_Socket(sock, &remote_ip , &port); + + PrintDebug(" New connection from %d port: %d\n", remote_ip, port); + + V3_Send(client, "Welcome!\n", 9); + + 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; + } + + buf[rc] = '\0'; + + PrintDebug( "Read %d bytes: '%s'\n", rc, buf); + } + + PrintDebug("TEST END: Sockets API\n"); + return 0; +} + +#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); + + 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); + + 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'; + + PrintDebug( "Read %d bytes: '%s'\n", rc, buf); + } + + PrintDebug("TEST END: Sockets API\n"); + return 0; +} + +#endif