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.


updated the socket interface
[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_types.h>
25 #include <palacios/vm_guest.h>
26
27 static struct v3_socket_hooks * sock_hooks = 0;
28
29
30 uint32_t v3_inet_addr(const char * ip_str) {
31     uint32_t val;
32     int base, n, c;
33     uint32_t parts[4];
34     uint32_t * pp = parts;
35
36     c = *ip_str;
37     for (;;) {
38         /*
39          * Collect number up to ``.''.
40          * Values are specified as for C:
41          * 0x=hex, 0=octal, 1-9=decimal.
42          */
43         if (!isdigit(c)) {
44             return (0);
45         }
46
47         val = 0;
48         base = 10;
49
50         if (c == '0') {
51             c = *++ip_str;
52             if ((c == 'x') || (c == 'X')) {
53                 base = 16;
54                 c = *++ip_str;
55             } else {
56                 base = 8;
57             }
58         }
59
60         for (;;) {
61             if (isdigit(c)) {
62                 val = (val * base) + (int)(c - '0');
63                 c = *++ip_str;
64             } else if ((base == 16) && (isxdigit(c))) {
65                 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
66                 c = *++ip_str;
67             } else {
68                 break;
69             }
70         }
71
72         if (c == '.') {
73             /*
74              * Internet format:
75              *  a.b.c.d
76              *  a.b.c   (with c treated as 16 bits)
77              *  a.b (with b treated as 24 bits)
78              */
79             if (pp >= parts + 3) {
80                 return 0;
81             }
82
83             *pp++ = val;
84             c = *++ip_str;
85         } else {
86             break;
87         }
88     }
89
90     /*
91      * Check for trailing characters.
92      */
93     if ( (c != '\0') && 
94          ( (!isprint(c)) || (!isspace(c)) ) ) {
95         return 0;
96     }
97
98     /*
99      * Concoct the address according to
100      * the number of parts specified.
101      */
102     n = pp - parts + 1;
103
104     switch (n) {
105         case 0:
106             return 0;       /* initial nondigit */
107
108         case 1:             /* a -- 32 bits */
109             break;
110
111         case 2:             /* a.b -- 8.24 bits */
112             if (val > 0xffffffUL) {
113                 return 0;
114             }
115
116             val |= parts[0] << 24;
117             break;
118
119         case 3:             /* a.b.c -- 8.8.16 bits */
120             if (val > 0xffff) {
121                 return 0;
122             }
123
124             val |= (parts[0] << 24) | (parts[1] << 16);
125             break;
126
127         case 4:             /* a.b.c.d -- 8.8.8.8 bits */
128             if (val > 0xff) {
129                 return 0;
130             }
131
132             val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
133             break;
134     }
135   
136     if (val) {
137         return v3_htonl(val);
138     }
139   
140     return -1;
141 }
142
143
144 char * v3_inet_ntoa(uint32_t addr) {
145     static char str[16];
146     char inv[3];
147     char * rp;
148     uint8_t * ap;
149     uint8_t rem;
150     uint8_t n;
151     uint8_t i;
152
153     rp = str;
154     ap = (uint8_t *)&addr;
155
156     for (n = 0; n < 4; n++) {
157         i = 0;
158
159         do {
160             rem = *ap % (uint8_t)10;
161
162             *ap /= (uint8_t)10;
163
164             inv[i++] = '0' + rem;
165         } while(*ap);
166
167         while(i--) {
168             *rp++ = inv[i];
169         }
170
171         *rp++ = '.';
172         ap++;
173     }
174
175     *--rp = 0;
176
177     return str;
178 }
179
180
181
182
183
184 uint16_t v3_htons(uint16_t n) {
185     return (((n & 0xff) << 8) | ((n & 0xff00) >> 8));
186 }
187
188
189 uint16_t v3_ntohs(uint16_t n) {
190     return v3_htons(n);
191 }
192
193
194 uint32_t v3_htonl(uint32_t n) {
195     return (((n & 0xff) << 24) |
196             ((n & 0xff00) << 8) |
197             ((n & 0xff0000UL) >> 8) |
198             ((n & 0xff000000UL) >> 24));
199 }
200
201
202 uint32_t v3_ntohl(uint32_t n) {
203   return v3_htonl(n);
204 }
205
206
207 v3_sock_t v3_create_udp_socket(struct v3_vm_info * vm) {
208     V3_ASSERT(sock_hooks);
209     V3_ASSERT(sock_hooks->udp_socket);
210     
211     return sock_hooks->udp_socket(0, 0, vm->host_priv_data);
212 }
213
214 v3_sock_t v3_create_tcp_socket(struct v3_vm_info * vm) {
215     V3_ASSERT(sock_hooks);
216     V3_ASSERT(sock_hooks->tcp_socket);
217     
218     return sock_hooks->tcp_socket(0, 1, 0, vm->host_priv_data);
219 }
220
221 void v3_socket_close(v3_sock_t sock) {
222     V3_ASSERT(sock_hooks);
223     V3_ASSERT(sock_hooks->close);
224
225     sock_hooks->close(sock);
226 }
227
228 int v3_socket_bind(const v3_sock_t sock, uint16_t port) {
229     V3_ASSERT(sock_hooks);
230     V3_ASSERT(sock_hooks->bind);
231
232     return sock_hooks->bind(sock, port);
233 }
234
235 int v3_socket_listen(const v3_sock_t sock, int backlog) {
236     V3_ASSERT(sock_hooks);
237     V3_ASSERT(sock_hooks->listen);
238
239     return sock_hooks->listen(sock, backlog);
240 }
241
242 v3_sock_t v3_socket_accept(const v3_sock_t sock, uint32_t * remote_ip, uint32_t * port) {
243     V3_ASSERT(sock_hooks);
244     V3_ASSERT(sock_hooks->accept);
245
246     return sock_hooks->accept(sock, remote_ip, port);
247 }
248
249 int v3_connect_to_ip(const v3_sock_t sock, const uint32_t hostip, const uint16_t port) {
250     V3_ASSERT(sock_hooks);
251     V3_ASSERT(sock_hooks->connect_to_ip);
252     
253     return sock_hooks->connect_to_ip(sock, hostip, port);
254 }
255
256 int v3_connect_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port) {
257     V3_ASSERT(sock_hooks);
258     V3_ASSERT(sock_hooks->connect_to_host);
259
260     return sock_hooks->connect_to_host(sock, hostname, port);
261 }
262
263 int v3_socket_send(const v3_sock_t sock, const uint8_t * buf, const uint32_t len) {
264     V3_ASSERT(sock_hooks);
265     V3_ASSERT(sock_hooks->send);
266
267     return sock_hooks->send(sock, buf, len);
268 }
269
270 int v3_socket_recv(const v3_sock_t sock, uint8_t * buf, const uint32_t len) {
271     V3_ASSERT(sock_hooks);
272     V3_ASSERT(sock_hooks->recv);
273
274     return sock_hooks->recv(sock, buf, len);
275 }
276
277 int v3_socket_send_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port, 
278                            const uint8_t * buf, const uint32_t len) {
279     V3_ASSERT(sock_hooks);
280     V3_ASSERT(sock_hooks->sendto_host);
281
282     return sock_hooks->sendto_host(sock, hostname, port, buf, len);
283 }
284
285 int v3_socket_send_to_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port, 
286                          const uint8_t * buf, const uint32_t len) {
287     V3_ASSERT(sock_hooks);
288     V3_ASSERT(sock_hooks->sendto_ip);
289
290     return sock_hooks->sendto_ip(sock, ip, port, buf, len);
291 }
292
293 int v3_socket_recv_from_host(const v3_sock_t sock, const char * hostname, const uint16_t port, 
294                              uint8_t * buf, const uint32_t len) {
295     V3_ASSERT(sock_hooks);
296     V3_ASSERT(sock_hooks->recvfrom_host);
297
298     return sock_hooks->recvfrom_host(sock, hostname, port, buf, len);
299 }
300
301 int v3_socket_recv_from_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port, 
302                            uint8_t * buf, const uint32_t len) {
303     V3_ASSERT(sock_hooks);
304     V3_ASSERT(sock_hooks->recvfrom_ip);
305
306     return sock_hooks->recvfrom_ip(sock, ip, port, buf, len);
307 }
308
309
310
311
312 void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
313     sock_hooks = hooks;
314     PrintDebug("V3 sockets inited\n");
315
316     return;
317 }