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.


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