CPN
Computational Process Networks
SocketAddress.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #ifndef SOCKETADDRESS_H
24 #define SOCKETADDRESS_H
25 #pragma once
26 #include <cpn/common.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <sys/un.h>
31 #include <netdb.h>
32 #include <vector>
33 #include <string>
34 
36 
37 typedef std::vector<SocketAddress> SockAddrList;
38 
43 public:
44 
45  enum Type_t {
49  };
50 
56  static SockAddrList CreateIPFromServ(const char* servname);
57  static SockAddrList CreateIPFromServ(const std::string &servname);
58  static SockAddrList CreateIPFromHost(const char* hostname);
59  static SockAddrList CreateIPFromHost(const std::string &hostname);
62  static SockAddrList CreateIP(unsigned serv);
68  static SockAddrList CreateIP(const char* hostname, const char* servname);
69  static SockAddrList CreateIP(const std::string &hostname, const std::string &servname);
75  static SockAddrList CreateIP(const char* hostname, unsigned serv);
76  static SockAddrList CreateIP(const std::string &hostname, unsigned serv);
77 
83  static SockAddrList CreateIP(const std::string &hostname);
84 
85  SocketAddress();
86  SocketAddress(addrinfo *info);
87  SocketAddress(addrinfo *info, unsigned portnum);
93  SocketAddress(sockaddr *addr, socklen_t len);
95 
100  std::string GetHostName(bool numerichost = true) const;
104  std::string GetServName() const;
108  unsigned GetServ() const;
109 
110  Type_t GetType() const;
111 
112 
113  sockaddr *GetAddr() { return &address.addr; }
114  socklen_t &GetLen() { return length; }
115  sa_family_t &Family() { return address.storage.ss_family; }
116  sa_family_t Family() const { return address.storage.ss_family; }
117 
123  void SetFromSockName(int fd);
129  void SetFromPeerName(int fd);
130 
131 private:
132  static SockAddrList Lookup(const char* const hostname, const char* const port,
133  int family, unsigned portnum);
134 
135  socklen_t length;
136 
137  union address_ {
138  sockaddr addr;
139  sockaddr_in in;
140  sockaddr_in6 in6;
141  sockaddr_un un;
142  sockaddr_storage storage;
143  } address;
144 };
145 
146 #endif
static SockAddrList Lookup(const char *const hostname, const char *const port, int family, unsigned portnum)
An abstraction of a socket address with convenience methods.
Definition: SocketAddress.h:42
socklen_t & GetLen()
unsigned GetServ() const
std::string GetHostName(bool numerichost=true) const
void SetFromPeerName(int fd)
Fill this SocketAddress with data from the other side of the connection represented by fd...
Type_t GetType() const
sa_family_t Family() const
socklen_t length
static SockAddrList CreateIPFromServ(const char *servname)
Return a list of valid socket addresses for the given service name All CreateIP functions create IP a...
sa_family_t & Family()
sockaddr * GetAddr()
void SetFromSockName(int fd)
Fill this SocketAddress with data from this side of the connection represented by fd...
sockaddr_storage storage
std::string GetServName() const
static SockAddrList CreateIPFromHost(const char *hostname)
static SockAddrList CreateIP(unsigned serv)
Return a list of valid socket address for the given service number or port number.
std::vector< SocketAddress > SockAddrList
Definition: SocketAddress.h:35
union SocketAddress::address_ address