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.


eeb38ae307578a278e689b42b328283bd34fce82
[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
26
27 struct v3_socket_hooks * sock_hooks = 0;
28
29 void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
30     PrintInfo("Initializing Socket Interface\n");
31     sock_hooks = hooks;
32     PrintDebug("V3 sockets inited\n");
33
34     return;
35 }
36
37
38
39 uint32_t v3_inet_addr(const char * ip_str) {
40     uint32_t val;
41     int base, n, c;
42     uint32_t parts[4];
43     uint32_t * pp = parts;
44
45     c = *ip_str;
46     for (;;) {
47         /*
48          * Collect number up to ``.''.
49          * Values are specified as for C:
50          * 0x=hex, 0=octal, 1-9=decimal.
51          */
52         if (!isdigit(c)) {
53             return (0);
54         }
55
56         val = 0;
57         base = 10;
58
59         if (c == '0') {
60             c = *++ip_str;
61             if ((c == 'x') || (c == 'X')) {
62                 base = 16;
63                 c = *++ip_str;
64             } else {
65                 base = 8;
66             }
67         }
68
69         for (;;) {
70             if (isdigit(c)) {
71                 val = (val * base) + (int)(c - '0');
72                 c = *++ip_str;
73             } else if ((base == 16) && (isxdigit(c))) {
74                 val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
75                 c = *++ip_str;
76             } else {
77                 break;
78             }
79         }
80
81         if (c == '.') {
82             /*
83              * Internet format:
84              *  a.b.c.d
85              *  a.b.c   (with c treated as 16 bits)
86              *  a.b (with b treated as 24 bits)
87              */
88             if (pp >= parts + 3) {
89                 return 0;
90             }
91
92             *pp++ = val;
93             c = *++ip_str;
94         } else {
95             break;
96         }
97     }
98
99     /*
100      * Check for trailing characters.
101      */
102     if ( (c != '\0') && 
103          ( (!isprint(c)) || (!isspace(c)) ) ) {
104         return 0;
105     }
106
107     /*
108      * Concoct the address according to
109      * the number of parts specified.
110      */
111     n = pp - parts + 1;
112
113     switch (n) {
114         case 0:
115             return 0;       /* initial nondigit */
116
117         case 1:             /* a -- 32 bits */
118             break;
119
120         case 2:             /* a.b -- 8.24 bits */
121             if (val > 0xffffffUL) {
122                 return 0;
123             }
124
125             val |= parts[0] << 24;
126             break;
127
128         case 3:             /* a.b.c -- 8.8.16 bits */
129             if (val > 0xffff) {
130                 return 0;
131             }
132
133             val |= (parts[0] << 24) | (parts[1] << 16);
134             break;
135
136         case 4:             /* a.b.c.d -- 8.8.8.8 bits */
137             if (val > 0xff) {
138                 return 0;
139             }
140
141             val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
142             break;
143     }
144   
145     if (val) {
146         return v3_htonl(val);
147     }
148   
149     return -1;
150 }
151
152
153 char * v3_inet_ntoa(uint32_t addr) {
154     static char str[16];
155     char inv[3];
156     char * rp;
157     uint8_t * ap;
158     uint8_t rem;
159     uint8_t n;
160     uint8_t i;
161
162     rp = str;
163     ap = (uint8_t *)&addr;
164
165     for(n = 0; n < 4; n++) {
166         i = 0;
167
168         do {
169             rem = *ap % (uint8_t)10;
170
171             *ap /= (uint8_t)10;
172
173             inv[i++] = '0' + rem;
174         } while(*ap);
175
176         while(i--) {
177             *rp++ = inv[i];
178         }
179
180         *rp++ = '.';
181         ap++;
182     }
183
184     *--rp = 0;
185
186     return str;
187 }
188
189
190
191
192
193 uint16_t v3_htons(uint16_t n) {
194     return (((n & 0xff) << 8) | ((n & 0xff00) >> 8));
195 }
196
197
198 uint16_t v3_ntohs(uint16_t n) {
199     return v3_htons(n);
200 }
201
202
203 uint32_t v3_htonl(uint32_t n) {
204     return (((n & 0xff) << 24) |
205             ((n & 0xff00) << 8) |
206             ((n & 0xff0000UL) >> 8) |
207             ((n & 0xff000000UL) >> 24));
208 }
209
210
211 uint32_t v3_ntohl(uint32_t n) {
212   return v3_htonl(n);
213 }