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.


V3_Sockets Macros tested
[palacios.git] / palacios / src / palacios / vmm_socket.c
1 /*
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21 #include <palacios/vmm_socket.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_debug.h>
24 #include <palacios/vmm_stddef.h>
25
26
27 struct v3_socket_hooks * sock_hooks = 0;
28
29
30 static int v3_socket_api_test(void);
31
32
33 void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
34   PrintInfo("Initializing Socket Interface\n");
35   sock_hooks = hooks;
36
37   PrintDebug("V3 sockets inited\n");
38
39   v3_socket_api_test();
40   
41   return;
42 }
43
44
45
46 void v3_init_sock_set(struct v3_sock_set * sock_set) {
47   sock_set->num_socks = 0;
48   sock_set->socks = NULL;
49
50   return;
51 }
52
53
54
55
56
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));
61
62   new_entry->sock = sock;
63   new_entry->is_set = 0;
64
65   if (sock_set->socks) {
66     new_entry->next = sock_set->socks;
67   }
68
69   sock_set->socks = new_entry;
70
71   sock_set->num_socks++;
72 }
73
74
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;
78
79   iter = sock_set->socks;
80   back_ptr = NULL;
81
82   v3_foreach_sock(sock_set, iter) {
83     if (iter->sock == sock) {
84       if (back_ptr == NULL) {
85         sock_set->socks = iter->next;
86       } else {
87         back_ptr->next = iter->next;
88       }
89
90       V3_Free(iter);
91
92       sock_set->num_socks--;
93       break;
94     }
95
96     back_ptr = iter;
97   }
98 }
99
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;
103
104   v3_foreach_sock(sock_set, iter) {
105     if (iter->sock == sock) {
106       return iter->is_set;
107     }
108   }
109   return -1;
110 }
111
112
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) {
117     iter->is_set = 0;
118   }
119 }
120
121 #if 1
122 static int
123 v3_socket_api_test(void)
124 {
125         unsigned int port;
126         char buf[1024];
127         int rc = 0;
128         V3_SOCK sock; 
129         V3_SOCK client;
130         unsigned int remote_ip;
131         
132         PrintDebug("\nIn Palacios: Test V3_Socket Macros\n");
133         sock = V3_Create_TCP_Socket();
134         if( sock == NULL ){
135                 PrintDebug( "ERROR: tcp_socket() failed!\n");
136                 return -1;
137         }
138
139         port = 80;
140
141         if( V3_Bind_Socket(sock, port) < 0){
142                 PrintDebug("bind error\n");
143                 return -1;
144         }
145
146         if( V3_Listen_Socket(sock, 1) < 0) {
147                 PrintDebug("listen error\n" );
148                 return -1;
149         }
150
151         PrintDebug( "Going into mainloop: server listening on port %d\n", port);
152
153         client = V3_Accept_Socket(sock, &remote_ip , &port);
154
155         PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
156              
157         V3_Send(client, "Welcome!\n", 9);
158
159         while(1)
160         {               
161              V3_Send(client, buf, rc);
162             rc = V3_Recv(client, buf, sizeof(buf)-1);
163              if( rc <= 0 ){
164                                 PrintDebug( "Closed connection\n");
165                                 V3_Close_Socket(client);
166                                 break;
167              }
168
169              buf[rc] = '\0';
170
171              PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
172          }
173
174         PrintDebug("TEST END: Sockets API\n");
175         return 0;
176 }
177
178 #endif
179
180 #if 0
181
182 static int
183 socket_api_test(void)
184 {
185         unsigned int port;
186         char buf[1024];
187         int rc = 0;
188         V3_SOCK sock; 
189         V3_SOCK client;
190         unsigned int remote_ip;
191         
192         PrintDebug("\nIn Palacios: TEST BEGIN: Sockets API\n");
193         sock = sock_hooks->tcp_socket(0, 0, 0);
194         if( sock == NULL ){
195                 PrintDebug( "ERROR: tcp_socket() failed!\n");
196                 return -1;
197         }
198
199         port = 80;
200
201         if( sock_hooks->bind_socket(sock, port) < 0){
202                 PrintDebug("bind error\n");
203                 return -1;
204         }
205
206         if( sock_hooks->listen(sock, 1) < 0) {
207                 PrintDebug("listen error\n" );
208                 return -1;
209         }
210
211         PrintDebug( "Going into mainloop: server listening on port %d\n", port);
212
213         client = sock_hooks->accept(sock, &remote_ip , &port);
214
215         PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
216              
217         sock_hooks->send(client, "Welcome!\n", 9);
218
219         while(1)
220         {               
221              sock_hooks->send(client, buf, rc);
222             rc = sock_hooks->recv(client, buf, sizeof(buf)-1);
223              if( rc <= 0 ){
224                                 PrintDebug( "Closed connection\n");
225                                 sock_hooks->close(client);
226                                 break;
227              }
228
229              buf[rc] = '\0';
230
231              PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
232          }
233
234         PrintDebug("TEST END: Sockets API\n");
235         return 0;
236 }
237
238 #endif