2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm_socket.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_debug.h>
24 #include <palacios/vmm_stddef.h>
27 struct v3_socket_hooks * sock_hooks = 0;
30 //int v3_socket_api_test(void);
33 void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
34 PrintInfo("Initializing Socket Interface\n");
37 PrintDebug("V3 sockets inited\n");
39 //v3_socket_api_test();
46 void v3_init_sock_set(struct v3_sock_set * sock_set) {
47 sock_set->num_socks = 0;
48 sock_set->socks = NULL;
57 /* This should probably check if the socket is already added */
58 // adds socket to the sockset
59 void v3_set_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
60 struct v3_sock_entry * new_entry = V3_Malloc(sizeof(struct v3_sock_entry));
62 new_entry->sock = sock;
63 new_entry->is_set = 0;
65 if (sock_set->socks) {
66 new_entry->next = sock_set->socks;
69 sock_set->socks = new_entry;
71 sock_set->num_socks++;
75 // deletes socket from sockset
76 void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
77 struct v3_sock_entry * iter, * back_ptr;
79 iter = sock_set->socks;
82 v3_foreach_sock(sock_set, iter) {
83 if (iter->sock == sock) {
84 if (back_ptr == NULL) {
85 sock_set->socks = iter->next;
87 back_ptr->next = iter->next;
92 sock_set->num_socks--;
100 // checks is_set vairable
101 int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
102 struct v3_sock_entry * iter;
104 v3_foreach_sock(sock_set, iter) {
105 if (iter->sock == sock) {
113 // clears all is_set variables.
114 void v3_zero_sockset(struct v3_sock_set * sock_set) {
115 struct v3_sock_entry * iter;
116 v3_foreach_sock(sock_set, iter) {
124 v3_socket_api_test(void)
131 unsigned int remote_ip;
133 PrintDebug("\nIn Palacios: Test V3_Socket Macros\n");
134 sock = V3_Create_TCP_Socket();
135 if( ((int)sock) < 0 ){
136 PrintDebug( "ERROR: tcp_socket() failed!\n");
142 if( V3_Bind_Socket(sock, port) < 0){
143 PrintDebug("bind error\n");
147 if( V3_Listen_Socket(sock, 1) < 0) {
148 PrintDebug("listen error\n" );
152 PrintDebug( "Going into mainloop: server listening on port %d\n", port);
154 client = V3_Accept_Socket(sock, &remote_ip, &port);
156 PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
158 V3_Send(client, "Welcome!\n", 9);
162 V3_Send(client, buf, rc);
163 rc = V3_Recv(client, buf, sizeof(buf)-1);
165 PrintDebug( "Closed connection\n");
166 V3_Close_Socket(client);
172 PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
175 PrintDebug("TEST END: Sockets API\n");
185 socket_api_test(void)
192 unsigned int remote_ip;
194 PrintDebug("\nIn Palacios: TEST BEGIN: Sockets API\n");
195 sock = sock_hooks->tcp_socket(0, 0, 0);
197 PrintDebug( "ERROR: tcp_socket() failed!\n");
203 if( sock_hooks->bind_socket(sock, port) < 0){
204 PrintDebug("bind error\n");
208 if( sock_hooks->listen(sock, 1) < 0) {
209 PrintDebug("listen error\n" );
213 PrintDebug( "Going into mainloop: server listening on port %d\n", port);
215 client = sock_hooks->accept(sock, &remote_ip , &port);
217 PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
219 sock_hooks->send(client, "Welcome!\n", 9);
223 sock_hooks->send(client, buf, rc);
224 rc = sock_hooks->recv(client, buf, sizeof(buf)-1);
226 PrintDebug( "Closed connection\n");
227 sock_hooks->close(client);
233 PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
236 PrintDebug("TEST END: Sockets API\n");