ESA JPIP server  0.1
address.h
Go to the documentation of this file.
1 #ifndef _NET_ADDRESS_H_
2 #define _NET_ADDRESS_H_
3 
4 
5 #include <string>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <netdb.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/un.h>
13 #include <unistd.h>
14 
15 namespace net
16 {
17  using namespace std;
18 
19 
28  class Address
29  {
30  public:
35  {
36  }
37 
41  virtual sockaddr *GetSockAddr() const = 0;
42 
47  virtual int GetSize() const = 0;
48 
52  virtual ~Address()
53  {
54  }
55  };
56 
57 
64  class InetAddress :public Address
65  {
66  private:
67  sockaddr_in sock_addr;
68 
69  public:
74  {
75  memset(&sock_addr, 0, sizeof(sock_addr));
76 
77  sock_addr.sin_family = AF_INET;
78  }
79 
83  InetAddress(const InetAddress& address)
84  {
85  memcpy(&sock_addr, &(address.sock_addr), sizeof(sock_addr));
86  }
87 
93  InetAddress(int port)
94  {
95  memset(&sock_addr, 0, sizeof(sock_addr));
96 
97  sock_addr.sin_family = AF_INET;
98  sock_addr.sin_addr.s_addr = INADDR_ANY;
99  sock_addr.sin_port = htons((u_short)port);
100  }
101 
107  InetAddress(const char *path, int port)
108  {
109  memset(&sock_addr, 0, sizeof(sock_addr));
110 
111  hostent *hp = NULL;
112  unsigned long addr;
113 
114  if(inet_addr(path) == INADDR_NONE)
115  hp = gethostbyname(path);
116  else {
117  addr = inet_addr(path);
118  hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
119  }
120 
121  if(hp != NULL) {
122  sock_addr.sin_family = AF_INET;
123  sock_addr.sin_port = htons(port);
124  sock_addr.sin_addr.s_addr = *((unsigned long *)hp->h_addr);
125  }
126  }
127 
132  {
133  memcpy(&sock_addr, &(address.sock_addr), sizeof(sock_addr));
134  return *this;
135  }
136 
141  virtual sockaddr *GetSockAddr() const
142  {
143  return (sockaddr *)&sock_addr;
144  }
145 
150  virtual int GetSize() const
151  {
152  return sizeof(sock_addr);
153  }
154 
158  string GetPath() const
159  {
160  return inet_ntoa(sock_addr.sin_addr);
161  }
162 
166  int GetPort() const
167  {
168  return ntohs(sock_addr.sin_port);
169  }
170  };
171 
172 
179  class UnixAddress :public Address
180  {
181  private:
182  sockaddr_un sock_addr;
183 
184  public:
189  {
190  memset(&sock_addr, 0, sizeof(sock_addr));
191 
192  sock_addr.sun_family = AF_UNIX;
193  }
194 
198  UnixAddress(const UnixAddress& address)
199  {
200  memcpy(&sock_addr, &(address.sock_addr), sizeof(sock_addr));
201  }
202 
207  UnixAddress(const char *path)
208  {
209  memset(&sock_addr, 0, sizeof(sock_addr));
210 
211  sock_addr.sun_family = AF_UNIX;
212  strncpy(sock_addr.sun_path, path, sizeof(sock_addr.sun_path) - 1);
213  }
214 
219  {
220  memcpy(&sock_addr, &(address.sock_addr), sizeof(sock_addr));
221  return *this;
222  }
223 
228  {
229  unlink(sock_addr.sun_path);
230  return *this;
231  }
232 
237  virtual sockaddr *GetSockAddr() const
238  {
239  return (sockaddr *)&sock_addr;
240  }
241 
246  virtual int GetSize() const
247  {
248  return sizeof(sock_addr);
249  }
250 
254  string GetPath() const
255  {
256  return sock_addr.sun_path;
257  }
258  };
259 
260 }
261 
262 
263 #endif /* _NET_ADDRESS_H_ */
virtual sockaddr * GetSockAddr() const
Overloaded from the base class to use the internal address structure.
Definition: address.h:141
UnixAddress & operator=(const UnixAddress &address)
Copy assignment.
Definition: address.h:218
string GetPath() const
Returns the address path.
Definition: address.h:254
virtual ~Address()
Empty destructor.
Definition: address.h:52
UnixAddress & Reset()
Removes the file associated to the UNIX address.
Definition: address.h:227
int GetPort() const
Returns the port number.
Definition: address.h:166
sockaddr_un sock_addr
Internal address structure.
Definition: address.h:182
UnixAddress(const char *path)
Initializes the address with given path.
Definition: address.h:207
sockaddr_in sock_addr
Internal address structure.
Definition: address.h:67
InetAddress(const char *path, int port)
Initializes the address with the given path and port.
Definition: address.h:107
UnixAddress(const UnixAddress &address)
Copy constructor.
Definition: address.h:198
virtual int GetSize() const
Overloaded from the base class to use the internal address structure.
Definition: address.h:246
InetAddress(const InetAddress &address)
Copy constructor.
Definition: address.h:83
InetAddress & operator=(const InetAddress &address)
Copy assignment.
Definition: address.h:131
virtual sockaddr * GetSockAddr() const
Overloaded from the base class to use the internal address structure.
Definition: address.h:237
Class to identify and handle an UNIX address.
Definition: address.h:179
string GetPath() const
Returns the address path.
Definition: address.h:158
Abstract base class to wrap the sockaddr derived structures.
Definition: address.h:28
UnixAddress()
Initializes the address to zero.
Definition: address.h:188
virtual int GetSize() const
Overloaded from the base class to use the internal address structure.
Definition: address.h:150
Address()
Empty constructor.
Definition: address.h:34
Class to identify and handle an Internet address.
Definition: address.h:64
InetAddress(int port)
Initializes the address with given port.
Definition: address.h:93
InetAddress()
Initializes the address to zero.
Definition: address.h:73