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.


added network server files
[palacios.git] / misc / network_servers / vtl / util.cc
1
2 #include "util.h"
3 #include <ctype.h> 
4
5 #include <string.h>
6
7
8 #if defined(USE_SSL)
9 //SSL specific include libraries
10 #include <openssl/ssl.h>
11 #include <openssl/crypto.h>
12 #include <openssl/x509.h>
13 #include <openssl/pem.h>
14 #include <openssl/err.h>
15 #include <openssl/rsa.h>
16 #endif
17
18 int compare_nocase(const string& s1, const string& s2)
19 {
20   string::const_iterator p1 = s1.begin();
21   string::const_iterator p2 = s2.begin();
22
23   while((p1 != s1.end()) && (p2 != s2.end())) {
24     if (toupper(*p1) != toupper(*p2)) {
25       return (toupper(*p1) < toupper(*p2)) ? -1 : 1;
26     }
27     
28     p1++;
29     p2++;
30   }
31  
32   /* This is a two layer tri op */
33   return((s2.size() == s1.size()) ? 0 : 
34          (s1.size() < s2.size()) ? -1 : 1);
35 }
36
37 void ConvertHexEthernetAddressToBinary(const char* string, char address[6]) {
38   for(int k = 0; k < 6; k++) {
39       hexbytetobyte(&(string[2 * k]), address + k);
40     }
41
42
43 void ConvertBinaryEthernetAddressToHex(char address[6], char * string) {
44   for (int j = 0; j < 6; j++) { 
45     bytetohexbyte(address[j], &(string[2 * j]));
46   }
47 }
48
49 void string_to_mac(string str, char mac[6]) {
50   for(int k = 0; k < 6; k++) {
51     hexbytetobyte(&(str[(2 * k) + k]), mac + k);
52   }
53 }
54
55 void string_to_mac(const char * str, char mac[6]) {
56   for(int k = 0; k < 6; k++) {
57     hexbytetobyte(&(str[(2 * k) + k]), mac + k);
58   }
59 }
60
61 void mac_to_string(char address[6], char * buf) {
62   for (int i = 0; i < 6; i++) {
63     bytetohexbyte(address[i], &(buf[3 * i]));
64     buf[(3 * i) + 2] = ':';
65   }
66   buf[17] = 0;
67 }
68
69 void mac_to_string(char address[6], string * str) {
70   EthernetAddrString buf;
71
72   mac_to_string(address, buf);  
73   *str = buf;
74 }
75
76 void ip_to_string(unsigned long addr, string * str) {
77   struct in_addr addr_st;
78
79   addr_st.s_addr = htonl(addr);
80   *str = inet_ntoa(addr_st);
81 }
82
83
84 void ip_to_string(unsigned long addr, char * buf) {
85   struct in_addr addr_st;
86   char * tmp_str;
87
88   addr_st.s_addr = htonl(addr);
89   tmp_str = inet_ntoa(addr_st);
90
91   memcpy(buf, tmp_str, strlen(tmp_str));
92 }
93
94 const char * ip_to_string(unsigned long addr) {
95   struct in_addr addr_st;
96
97   addr_st.s_addr = htonl(addr);
98   return inet_ntoa(addr_st);
99 }
100
101
102 #if defined(USE_SSL) 
103 int readall(const int fd, SSL *ssl, char *buf, const int len, const int oneshot, const int awaitblock) {
104   int rc;
105   int left;
106
107
108   left = len;
109
110   while (left > 0) {
111     if (ssl != NULL) {
112       rc = SSL_read(ssl, &(buf[len - left]), left);
113     } else {
114       rc = read(fd, &(buf[len - left]), left);
115     }
116
117     if (oneshot) { 
118       return rc;
119     }
120
121     if (rc <= 0) { 
122       if (errno == EINTR) {
123         continue;
124       }
125       if ((errno == EWOULDBLOCK) && awaitblock) {
126         continue;
127       }
128       return rc;
129     } else {
130       left -= rc;
131     }
132   }
133   return len;
134 }
135
136 int writeall(const int fd, SSL *ssl, const char *buf, const int len, const int oneshot, const int awaitblock)
137 {
138   int rc;
139   int left;
140   
141
142   left = len;
143
144   while (left > 0) {
145     if(ssl != NULL) {
146       rc = SSL_write(ssl, &(buf[len - left]), left);
147     } else {
148       rc = write(fd, &(buf[len - left]), left);
149     }
150     if (oneshot) { 
151       return rc;
152     }
153     if (rc <= 0) { 
154       if (errno == EINTR) {
155         continue;
156       }
157       if ((errno == EWOULDBLOCK) && awaitblock) {
158         continue;
159       }
160       return rc;
161     } else {
162       left -= rc;
163     }
164   }
165   return len;
166 }
167 #endif
168
169 int readall(const int fd, char *buf, const int len, const int oneshot, const int awaitblock) {
170   int rc;
171   int left;
172
173   left = len;
174
175   while (left > 0) {
176     rc = read(fd, &(buf[len - left]), left);
177     
178     if (oneshot) { 
179       return rc;
180     }
181
182     if (rc <= 0) { 
183       if (errno == EINTR) {
184         continue;
185       }
186
187       if ((errno == EWOULDBLOCK) && awaitblock) {
188         continue;
189       }
190
191       return rc;
192     } else {
193       left -= rc;
194     }
195   }
196   return len;
197 }
198
199 int writeall(const int fd, const char *buf, const int len, const int oneshot, const int awaitblock)
200 {
201   int rc;
202   int left;
203   
204
205   left = len;
206   while (left > 0) {
207     rc = write(fd, &(buf[len - left]), left);
208
209     if (oneshot) { 
210       return rc;
211     }
212
213     if (rc <= 0) { 
214       if (errno == EINTR) {
215         continue;
216       }
217       if ((errno == EWOULDBLOCK) && awaitblock) {
218         continue;
219       }
220       return rc;
221     } else {
222       left -= rc;
223     }
224   }
225   return len;
226 }
227
228
229
230 void printhexnybble(FILE *out,const char lower) {
231   fputc( (lower >= 10) ? (lower - 10 + 'A') : (lower + '0'), 
232          out);
233 }
234
235 void printhexbyte(FILE *out,const char h) {
236   char upper=(h >> 4) & 0xf;
237   char lower=h & 0xf;
238
239   printhexnybble(out, upper); 
240   printhexnybble(out, lower);
241 }
242
243 void printhexbuffer(FILE *out, const char *buf, const int len) {
244   int i;
245   for (i = 0; i < len; i++) { 
246     printhexbyte(out, buf[i]);
247   }
248 }
249
250 void printhexshort(FILE *out, const short s) {
251   printhexbuffer(out, (char*)&s, 2);
252 }
253
254 void printhexint(FILE *out, const int i) {
255   printhexbuffer(out, (char*)&i, 4);
256 }
257
258
259 char hexnybbletonybble(const char hexnybble) {
260   char x = toupper(hexnybble);
261   if ((x >= '0') && (x <= '9')) {
262     return x - '0';
263   } else {
264     return 10 + (x - 'A');
265   }
266 }
267
268 void hexbytetobyte(const char hexbyte[2], char *byte) {
269   *byte = ((hexnybbletonybble(hexbyte[0]) << 4) + 
270            (hexnybbletonybble(hexbyte[1]) & 0xf));
271 }
272
273 char nybbletohexnybble(const char nybble) {
274   return (nybble >= 10) ? (nybble - 10 + 'A') : (nybble + '0');
275 }
276
277 void bytetohexbyte(const char byte, char hexbyte[2]) {
278   hexbyte[0] = nybbletohexnybble((byte >> 4) & 0xf);
279   hexbyte[1] = nybbletohexnybble(byte & 0xf);
280 }
281
282 EthernetAddr::EthernetAddr()  {
283   memset(addr, 0, 6);
284 }
285
286
287 EthernetAddr::EthernetAddr(const EthernetAddr &rhs) {
288   memcpy(addr, rhs.addr, 6);
289 }
290
291 EthernetAddr::EthernetAddr(const EthernetAddrString rhs) {
292   SetToString(rhs);
293 }
294
295
296 const EthernetAddr & EthernetAddr::operator=(const EthernetAddr &rhs) {
297   memcpy(addr, rhs.addr, 6);
298   return *this;
299 }
300
301 bool EthernetAddr::operator==(const EthernetAddr &rhs) const {
302   return (memcmp(addr, rhs.addr, 6) == 0);
303 }
304
305
306
307 void EthernetAddr::SetToString(const EthernetAddrString s) {
308   int i,j;
309
310   for (i = 0, j = 0; i < 6; i++, j += 3) {
311     hexbytetobyte(&(s[j]), &(addr[i]));
312   }
313
314 }
315
316 void EthernetAddr::GetAsString(EthernetAddrString s) const {
317   int i,j;
318
319   for (i = 0, j = 0; i < 6; i++, j += 3) {
320     bytetohexbyte(addr[i], &(s[j]));
321     if (i < 5) {
322       s[j + 2] = ':';
323     } else {
324       s[j + 2] = 0;
325     }
326   }
327 }    
328
329
330 ostream & EthernetAddr::Print(ostream &os) const {
331   EthernetAddrString s;
332   
333   GetAsString(s);
334   os << "EthernetAddr(" << (char*)s << ")";
335   return os;
336 }
337
338 #if defined(USE_SSL)
339 void EthernetAddr::Serialize(const int fd, SSL *ssl) const {
340   if (writeall(fd, ssl, addr, 6, 0, 1) != 6) {
341     throw SerializationException();
342   }
343 }
344
345 void EthernetAddr::Unserialize(const int fd, SSL *ssl) {
346   if (readall(fd, ssl, addr, 6, 0, 1) != 6) {
347     throw SerializationException();
348   }    
349 }
350
351 #endif
352 void EthernetAddr::Serialize(const int fd) const {
353   if (writeall(fd, addr, 6, 0, 1) != 6) {
354     throw SerializationException();
355   }
356 }
357
358 void EthernetAddr::Unserialize(const int fd) {
359   if (readall(fd, addr, 6, 0, 1) != 6) {
360     throw SerializationException();
361   }
362 }
363