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.h
1 #ifndef _util
2 #define _util
3
4
5 #include <stdio.h>
6 #include <iostream>
7 #include <string>
8
9
10 #ifdef linux
11 #include <sys/socket.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
17 #elif defined(WIN32)
18
19 #include <io.h>
20 #include <Winsock2.h>
21 #define read _read
22 #define write _write
23
24 #define EWOULDBLOCK WSAEWOULDBLOCK
25
26 typedef int socklen_t;
27 #endif
28
29
30
31
32
33 #define ETHERNET_HEADER_LEN 14
34 #define ETHERNET_DATA_MIN   46
35 #define ETHERNET_DATA_MAX   1500
36
37 #define ETHERNET_PACKET_LEN (ETHERNET_HEADER_LEN+ETHERNET_DATA_MAX)
38
39
40 #if defined(USE_SSL)
41 extern "C" {
42 #define OPENSSL_NO_KRB5
43 #include <openssl/ssl.h>
44 }
45 #endif
46
47 using namespace std;
48
49 #if defined(USE_SSL)
50 int readall(const int fd, SSL * ssl, char * buf, const int len, const int oneshot = 0, const int awaitblock = 1);
51 int writeall(const int fd, SSL * ssl, const char * buf, const int len, const int oneshot = 0, const int awaitblock = 1);
52 #endif
53 int readall(const int fd, char * buf, const int len, const int oneshot = 0, const int awaitblock = 1);
54 int writeall(const int fd, const char * buf, const int len, const int oneshot = 0, const int awaitblock = 1);
55
56
57
58 #if defined(WIN32) 
59 #define snprintf _snprintf
60 #endif
61
62
63 int compare_nocase(const string &s1, const string &s2);
64
65 struct SerializationException {};
66
67 void printhexnybble(FILE * out,const char lower);
68 void printhexbyte(FILE * out,const char h);
69 void printhexshort(FILE * out,const short h);
70 void printhexint(FILE * out,const int h);
71 void printhexbuffer(FILE * out, const char * buf, const int len);
72
73 void hexbytetobyte(const char hexbyte[2], char * byte);
74 void bytetohexbyte(const char byte, char hexbyte[2]);
75
76 void ConvertHexEthernetAddressToBinary(const char * string, char address[6]);
77 void ConvertBinaryEthernetAddressToHex(char address[6], char * string);
78
79 // How about a function that you might actually use...
80 void mac_to_string(char address[6], char * buf);
81 void mac_to_string(char address[6], string * str);
82 void string_to_mac(string str, char mac[6]);
83 void string_to_mac(const char * str, char mac[6]);
84
85 void ip_to_string(unsigned long addr, string * str);
86 void ip_to_string(unsigned long addr, char * buf);
87 const char * ip_to_string(unsigned long addr);
88
89 typedef char EthernetAddrString[(2 * 6) + 6];
90
91
92 struct EthernetAddr {
93   char addr[6];
94
95   EthernetAddr();
96   EthernetAddr(const EthernetAddr &rhs);
97   EthernetAddr(const EthernetAddrString rhs);
98   const EthernetAddr & operator=(const EthernetAddr &rhs);
99
100   bool operator==(const EthernetAddr &rhs) const;
101  
102   void SetToString(const EthernetAddrString s);
103   void GetAsString(EthernetAddrString s) const;
104   
105 #if defined(USE_SSL)
106   void Serialize(const int fd, SSL * ssl) const;
107   void Unserialize(const int fd,SSL * ssl);
108 #endif
109   void Serialize(const int fd) const;
110   void Unserialize(const int fd);
111
112
113   ostream & Print(ostream &os) const;
114 };
115
116 inline ostream & operator<<(ostream &os, const EthernetAddr &e)
117 {
118   return e.Print(os);
119 }
120
121 #endif