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.


5ac5c15a0bd30740f38957363e9ae0a724f0f34f
[palacios.git] / palacios / src / interfaces / 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 <interfaces/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     void * priv_data = NULL;
211
212     if (vm) {
213         priv_data = vm->host_priv_data;
214     }
215     
216     return sock_hooks->udp_socket(0, 0, priv_data);
217 }
218
219 v3_sock_t v3_create_tcp_socket(struct v3_vm_info * vm) {
220     V3_ASSERT(sock_hooks);
221     V3_ASSERT(sock_hooks->tcp_socket);
222     void * priv_data = NULL;
223
224     if (vm) {
225         priv_data = vm->host_priv_data;
226     }
227
228     return sock_hooks->tcp_socket(0, 1, 0, priv_data);
229 }
230
231 void v3_socket_close(v3_sock_t sock) {
232     V3_ASSERT(sock_hooks);
233     V3_ASSERT(sock_hooks->close);
234
235     sock_hooks->close(sock);
236 }
237
238 int v3_socket_bind(const v3_sock_t sock, uint16_t port) {
239     V3_ASSERT(sock_hooks);
240     V3_ASSERT(sock_hooks->bind);
241
242     return sock_hooks->bind(sock, port);
243 }
244
245 int v3_socket_listen(const v3_sock_t sock, int backlog) {
246     V3_ASSERT(sock_hooks);
247     V3_ASSERT(sock_hooks->listen);
248
249     return sock_hooks->listen(sock, backlog);
250 }
251
252 v3_sock_t v3_socket_accept(const v3_sock_t sock, uint32_t * remote_ip, uint32_t * port) {
253     V3_ASSERT(sock_hooks);
254     V3_ASSERT(sock_hooks->accept);
255
256     return sock_hooks->accept(sock, remote_ip, port);
257 }
258
259 int v3_connect_to_ip(const v3_sock_t sock, const uint32_t hostip, const uint16_t port) {
260     V3_ASSERT(sock_hooks);
261     V3_ASSERT(sock_hooks->connect_to_ip);
262     
263     return sock_hooks->connect_to_ip(sock, hostip, port);
264 }
265
266 int v3_connect_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port) {
267     V3_ASSERT(sock_hooks);
268     V3_ASSERT(sock_hooks->connect_to_host);
269
270     return sock_hooks->connect_to_host(sock, hostname, port);
271 }
272
273 int v3_socket_send(const v3_sock_t sock, const uint8_t * buf, const uint32_t len) {
274     V3_ASSERT(sock_hooks);
275     V3_ASSERT(sock_hooks->send);
276
277     return sock_hooks->send(sock, buf, len);
278 }
279
280 int v3_socket_recv(const v3_sock_t sock, uint8_t * buf, const uint32_t len) {
281     V3_ASSERT(sock_hooks);
282     V3_ASSERT(sock_hooks->recv);
283
284     return sock_hooks->recv(sock, buf, len);
285 }
286
287 int v3_socket_send_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port, 
288                            const uint8_t * buf, const uint32_t len) {
289     V3_ASSERT(sock_hooks);
290     V3_ASSERT(sock_hooks->sendto_host);
291
292     return sock_hooks->sendto_host(sock, hostname, port, buf, len);
293 }
294
295 int v3_socket_send_to_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port, 
296                          const uint8_t * buf, const uint32_t len) {
297     V3_ASSERT(sock_hooks);
298     V3_ASSERT(sock_hooks->sendto_ip);
299
300     return sock_hooks->sendto_ip(sock, ip, port, buf, len);
301 }
302
303 int v3_socket_recv_from_host(const v3_sock_t sock, const char * hostname, const uint16_t port, 
304                              uint8_t * buf, const uint32_t len) {
305     V3_ASSERT(sock_hooks);
306     V3_ASSERT(sock_hooks->recvfrom_host);
307
308     return sock_hooks->recvfrom_host(sock, hostname, port, buf, len);
309 }
310
311 int v3_socket_recv_from_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port, 
312                            uint8_t * buf, const uint32_t len) {
313     V3_ASSERT(sock_hooks);
314     V3_ASSERT(sock_hooks->recvfrom_ip);
315
316     return sock_hooks->recvfrom_ip(sock, ip, port, buf, len);
317 }
318
319
320
321
322 void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
323     sock_hooks = hooks;
324     PrintDebug("V3 sockets inited\n");
325
326     return;
327 }