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>
18 int compare_nocase(const string& s1, const string& s2)
20 string::const_iterator p1 = s1.begin();
21 string::const_iterator p2 = s2.begin();
23 while((p1 != s1.end()) && (p2 != s2.end())) {
24 if (toupper(*p1) != toupper(*p2)) {
25 return (toupper(*p1) < toupper(*p2)) ? -1 : 1;
32 /* This is a two layer tri op */
33 return((s2.size() == s1.size()) ? 0 :
34 (s1.size() < s2.size()) ? -1 : 1);
37 void ConvertHexEthernetAddressToBinary(const char* string, char address[6]) {
38 for(int k = 0; k < 6; k++) {
39 hexbytetobyte(&(string[2 * k]), address + k);
43 void ConvertBinaryEthernetAddressToHex(char address[6], char * string) {
44 for (int j = 0; j < 6; j++) {
45 bytetohexbyte(address[j], &(string[2 * j]));
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);
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);
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] = ':';
69 void mac_to_string(char address[6], string * str) {
70 EthernetAddrString buf;
72 mac_to_string(address, buf);
76 void ip_to_string(unsigned long addr, string * str) {
77 struct in_addr addr_st;
79 addr_st.s_addr = htonl(addr);
80 *str = inet_ntoa(addr_st);
84 void ip_to_string(unsigned long addr, char * buf) {
85 struct in_addr addr_st;
88 addr_st.s_addr = htonl(addr);
89 tmp_str = inet_ntoa(addr_st);
91 memcpy(buf, tmp_str, strlen(tmp_str));
94 const char * ip_to_string(unsigned long addr) {
95 struct in_addr addr_st;
97 addr_st.s_addr = htonl(addr);
98 return inet_ntoa(addr_st);
103 int readall(const int fd, SSL *ssl, char *buf, const int len, const int oneshot, const int awaitblock) {
112 rc = SSL_read(ssl, &(buf[len - left]), left);
114 rc = read(fd, &(buf[len - left]), left);
122 if (errno == EINTR) {
125 if ((errno == EWOULDBLOCK) && awaitblock) {
136 int writeall(const int fd, SSL *ssl, const char *buf, const int len, const int oneshot, const int awaitblock)
146 rc = SSL_write(ssl, &(buf[len - left]), left);
148 rc = write(fd, &(buf[len - left]), left);
154 if (errno == EINTR) {
157 if ((errno == EWOULDBLOCK) && awaitblock) {
169 int readall(const int fd, char *buf, const int len, const int oneshot, const int awaitblock) {
176 rc = read(fd, &(buf[len - left]), left);
183 if (errno == EINTR) {
187 if ((errno == EWOULDBLOCK) && awaitblock) {
199 int writeall(const int fd, const char *buf, const int len, const int oneshot, const int awaitblock)
207 rc = write(fd, &(buf[len - left]), left);
214 if (errno == EINTR) {
217 if ((errno == EWOULDBLOCK) && awaitblock) {
230 void printhexnybble(FILE *out,const char lower) {
231 fputc( (lower >= 10) ? (lower - 10 + 'A') : (lower + '0'),
235 void printhexbyte(FILE *out,const char h) {
236 char upper=(h >> 4) & 0xf;
239 printhexnybble(out, upper);
240 printhexnybble(out, lower);
243 void printhexbuffer(FILE *out, const char *buf, const int len) {
245 for (i = 0; i < len; i++) {
246 printhexbyte(out, buf[i]);
250 void printhexshort(FILE *out, const short s) {
251 printhexbuffer(out, (char*)&s, 2);
254 void printhexint(FILE *out, const int i) {
255 printhexbuffer(out, (char*)&i, 4);
259 char hexnybbletonybble(const char hexnybble) {
260 char x = toupper(hexnybble);
261 if ((x >= '0') && (x <= '9')) {
264 return 10 + (x - 'A');
268 void hexbytetobyte(const char hexbyte[2], char *byte) {
269 *byte = ((hexnybbletonybble(hexbyte[0]) << 4) +
270 (hexnybbletonybble(hexbyte[1]) & 0xf));
273 char nybbletohexnybble(const char nybble) {
274 return (nybble >= 10) ? (nybble - 10 + 'A') : (nybble + '0');
277 void bytetohexbyte(const char byte, char hexbyte[2]) {
278 hexbyte[0] = nybbletohexnybble((byte >> 4) & 0xf);
279 hexbyte[1] = nybbletohexnybble(byte & 0xf);
282 EthernetAddr::EthernetAddr() {
287 EthernetAddr::EthernetAddr(const EthernetAddr &rhs) {
288 memcpy(addr, rhs.addr, 6);
291 EthernetAddr::EthernetAddr(const EthernetAddrString rhs) {
296 const EthernetAddr & EthernetAddr::operator=(const EthernetAddr &rhs) {
297 memcpy(addr, rhs.addr, 6);
301 bool EthernetAddr::operator==(const EthernetAddr &rhs) const {
302 return (memcmp(addr, rhs.addr, 6) == 0);
307 void EthernetAddr::SetToString(const EthernetAddrString s) {
310 for (i = 0, j = 0; i < 6; i++, j += 3) {
311 hexbytetobyte(&(s[j]), &(addr[i]));
316 void EthernetAddr::GetAsString(EthernetAddrString s) const {
319 for (i = 0, j = 0; i < 6; i++, j += 3) {
320 bytetohexbyte(addr[i], &(s[j]));
330 ostream & EthernetAddr::Print(ostream &os) const {
331 EthernetAddrString s;
334 os << "EthernetAddr(" << (char*)s << ")";
339 void EthernetAddr::Serialize(const int fd, SSL *ssl) const {
340 if (writeall(fd, ssl, addr, 6, 0, 1) != 6) {
341 throw SerializationException();
345 void EthernetAddr::Unserialize(const int fd, SSL *ssl) {
346 if (readall(fd, ssl, addr, 6, 0, 1) != 6) {
347 throw SerializationException();
352 void EthernetAddr::Serialize(const int fd) const {
353 if (writeall(fd, addr, 6, 0, 1) != 6) {
354 throw SerializationException();
358 void EthernetAddr::Unserialize(const int fd) {
359 if (readall(fd, addr, 6, 0, 1) != 6) {
360 throw SerializationException();