~frankencode/drycore/trunk

« back to all changes in this revision

Viewing changes to dry/SocketAddress.hpp

  • Committer: Frank Mertens
  • Date: 2013-02-27 18:43:50 UTC
  • Revision ID: frank@cyblogic.de-20130227184350-ypu14rj5e2r8gwqz
Initial commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
  * Copyright (C) 2007-2013 Frank Mertens.
 
3
  *
 
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.
 
8
  */
 
9
#ifndef DRY_SOCKETADDRESS_HPP
 
10
#define DRY_SOCKETADDRESS_HPP
 
11
 
 
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
 
17
 
 
18
#include "String.hpp"
 
19
#include "List.hpp"
 
20
 
 
21
namespace dry
 
22
{
 
23
 
 
24
DRY_EXCEPTION(NetworkingException, Exception);
 
25
 
 
26
class SocketAddress;
 
27
 
 
28
typedef List< Ref<SocketAddress> > SocketAddressList;
 
29
 
 
30
class SocketAddress: public Instance
 
31
{
 
32
public:
 
33
        inline static Ref<SocketAddress> create() {
 
34
                return new SocketAddress;
 
35
        }
 
36
 
 
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.
 
40
          *
 
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
 
44
          */
 
45
        inline static Ref<SocketAddress> create(int family, String address = String(), int port = 0) {
 
46
                return new SocketAddress(family, address, port);
 
47
        }
 
48
 
 
49
        inline static Ref<SocketAddress> create(struct sockaddr_in *addr) {
 
50
                return new SocketAddress(addr);
 
51
        }
 
52
        inline static Ref<SocketAddress> create(struct sockaddr_in6 *addr) {
 
53
                return new SocketAddress(addr);
 
54
        }
 
55
        inline static Ref<SocketAddress> create(addrinfo *info) {
 
56
                return new SocketAddress(info);
 
57
        }
 
58
 
 
59
        int family() const;
 
60
        int socketType() const;
 
61
        int protocol() const;
 
62
        int port() const;
 
63
        void setPort(int port);
 
64
 
 
65
        String addressString() const;
 
66
        String toString() const;
 
67
 
 
68
        int scope() const;
 
69
        void setScope(int scope);
 
70
 
 
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.
 
81
          */
 
82
        static Ref<SocketAddressList> resolve(String hostName, String serviceName = String(), int family = AF_UNSPEC, int socketType = 0, String *canonicalName = 0);
 
83
 
 
84
        /** Lookup the host name of given address. Usually a reverse DNS
 
85
          * lookup will be issued, which may take several seconds.
 
86
          */
 
87
        String lookupHostName(bool *failed = 0) const;
 
88
 
 
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.
 
91
          */
 
92
        String lookupServiceName() const;
 
93
 
 
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.
 
97
          */
 
98
        static String hostName();
 
99
 
 
100
        struct sockaddr *addr();
 
101
        const struct sockaddr *addr() const;
 
102
        int addrLen() const;
 
103
 
 
104
protected:
 
105
        SocketAddress();
 
106
        SocketAddress(int family, String address, int port);
 
107
        SocketAddress(struct sockaddr_in *addr);
 
108
        SocketAddress(struct sockaddr_in6 *addr);
 
109
        SocketAddress(addrinfo *info);
 
110
 
 
111
        union {
 
112
                struct sockaddr addr_;
 
113
                struct sockaddr_in inet4Address_;
 
114
                struct sockaddr_in6 inet6Address_;
 
115
                struct sockaddr_un localAddress_;
 
116
        };
 
117
 
 
118
        int socketType_;
 
119
        int protocol_;
 
120
};
 
121
 
 
122
} // namespace dry
 
123
 
 
124
#endif // DRY_SOCKETADDRESS_HPP