2
* Copyright (C) 2007-2013 Frank Mertens.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version
7
* 2 of the License, or (at your option) any later version.
9
#ifndef DRY_SOCKETADDRESS_HPP
10
#define DRY_SOCKETADDRESS_HPP
12
#include <arpa/inet.h>
13
#include <netinet/in.h> // sockaddr_in, etc.
14
#include <netdb.h> // addrinfo
15
#include <sys/socket.h> // connect
16
#include <sys/un.h> // sockaddr_un
24
DRY_EXCEPTION(NetworkingException, Exception);
28
typedef List< Ref<SocketAddress> > SocketAddressList;
30
class SocketAddress: public Instance
33
inline static Ref<SocketAddress> create() {
34
return new SocketAddress;
37
/** Initialize object with protocol family and numerical address.
38
* Providing the wildcard address "*" allows to specify an address for
39
* a TCP server, which will listen on all interfaces of the serving host.
41
* \param family protocol family (AF_UNSPEC, AF_INET, AF_INET6 or AF_LOCAL)
42
* \param address numerical host address, wildcard ("*") or file path
43
* \param port service port
45
inline static Ref<SocketAddress> create(int family, String address = String(), int port = 0) {
46
return new SocketAddress(family, address, port);
49
inline static Ref<SocketAddress> create(struct sockaddr_in *addr) {
50
return new SocketAddress(addr);
52
inline static Ref<SocketAddress> create(struct sockaddr_in6 *addr) {
53
return new SocketAddress(addr);
55
inline static Ref<SocketAddress> create(addrinfo *info) {
56
return new SocketAddress(info);
60
int socketType() const;
63
void setPort(int port);
65
String addressString() const;
66
String toString() const;
69
void setScope(int scope);
71
/** Query the complete connection information for given host name, service name and
72
* protocol family. The call blocks until the local resolver has resolved the
73
* host name. This may take several seconds.
74
* Depending on supported protocol stacks and service availability in
75
* different protocols (UDP/TCP) and number of network addresses of the
76
* queried host multiple SocketAddress objects will be returned.
77
* An empty list is returned, if the host name is unknown, service is
78
* not available or protocol family is not supported by the host.
79
* The host name can be a short name relative to the local domain.
80
* The fully qualified domain name (aka canonical name) can be optionally retrieved.
82
static Ref<SocketAddressList> resolve(String hostName, String serviceName = String(), int family = AF_UNSPEC, int socketType = 0, String *canonicalName = 0);
84
/** Lookup the host name of given address. Usually a reverse DNS
85
* lookup will be issued, which may take several seconds.
87
String lookupHostName(bool *failed = 0) const;
89
/** Lookup the service name. In most setups the service name will be looked up
90
* in a local file (/etc/services) and therefore the call returns immediately.
92
String lookupServiceName() const;
94
/** Returns the name of this host.
95
* On a properly configured server the host name returned should be a fully
96
* qualified domain name.
98
static String hostName();
100
struct sockaddr *addr();
101
const struct sockaddr *addr() const;
106
SocketAddress(int family, String address, int port);
107
SocketAddress(struct sockaddr_in *addr);
108
SocketAddress(struct sockaddr_in6 *addr);
109
SocketAddress(addrinfo *info);
112
struct sockaddr addr_;
113
struct sockaddr_in inet4Address_;
114
struct sockaddr_in6 inet6Address_;
115
struct sockaddr_un localAddress_;
124
#endif // DRY_SOCKETADDRESS_HPP