Debian Codegraph @

src/include/socket.h

raw
1 // ----------------------------------------------------------------------------
2 // socket.h
3 //
4 // Copyright (C) 2008-2009
5 // Stelios Bounanos, M0GLD
6 //
7 // ----------------------------------------------------------------------------
8 // Copyright (C) 2014
9 // David Freese, W1HKJ
10 //
11 // This file is part of flmsg
12 //
13 // flrig is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // flrig is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 // ----------------------------------------------------------------------------
26
27 #ifndef SOCKET_H_
28 #define SOCKET_H_
29
30 #include <config.h>
31
32 #include <sys/time.h>
33 #ifndef __MINGW32__
34 # include <sys/socket.h>
35 # include <netdb.h>
36 # include <netinet/in.h>
37 #else
38 # include <winsock2.h>
39 #endif
40
41 #include <string>
42 #include <exception>
43 #include <cstring>
44
45 class SocketException : public std::exception
46 {
47 public:
48 SocketException(int err_ = 0) : err(err_), msg(err_to_str(err_)) { }
49 SocketException(const char* msg_) : err(1), msg(msg_) { }
50 SocketException(int err_, const std::string& prefix)
51 : err(err_), msg(std::string(prefix).append(": ").append(err_to_str(err_))) { }
52 virtual ~SocketException() throw() { }
53
54 const char* what(void) const throw() { return msg.c_str(); }
55 int error(void) const { return err; }
56
57 protected:
58 const char* err_to_str(int e)
59 {
60 #if HAVE_GETADDRINFO
61 if (e < 0)
62 return gai_strerror(e);
63 else
64 #endif
65 return strerror(e);
66 }
67
68 int err;
69 std::string msg;
70 };
71
72 #if HAVE_GETADDRINFO
73 typedef struct addrinfo addr_info_t;
74 #else
75 struct addr_info_t {
76 int ai_family;
77 int ai_socktype;
78 int ai_protocol;
79 int ai_addrlen;
80 struct sockaddr* ai_addr;
81 };
82 #endif
83
84 class Address
85 {
86
87 public:
88 Address(const char* host = "", int port = 0, const char* proto_name = "tcp");
89 Address(const char* host, const char* port_name, const char* proto_name = "tcp");
90 Address(const Address& addr);
91 ~Address();
92 Address& operator=(const Address& rhs);
93
94 size_t size(void) const;
95 const addr_info_t* get(size_t n = 0) const;
96 static std::string get_str(const addr_info_t* addr);
97
98 private:
99 void lookup(const char* proto_name);
100
101 std::string node;
102 std::string service;
103 int port;
104
105 #if HAVE_GETADDRINFO
106 struct addrinfo* info;
107 #else
108 struct hostent host_entry;
109 struct servent service_entry;
110 mutable addr_info_t addr;
111 mutable struct sockaddr_in saddr;
112 #endif
113 bool copied;
114 };
115
116 class Socket
117 {
118 public:
119 Socket(const Address& addr);
120 Socket(int fd = -1);
121 Socket(const Socket& s);
122 ~Socket();
123 Socket& operator=(const Socket& rhs);
124
125 void open(const Address& addr);
126 void close(void);
127 bool wait(int dir);
128
129 // Server
130 void bind(void);
131 void listen(int backlog = SOMAXCONN);
132 Socket accept(void);
133 Socket accept1(void);
134
135 // Client
136 void connect(const Address& addr);
137 void connect(void);
138
139 // Data Transimission
140 size_t send(const void* buf, size_t len);
141 size_t send(const std::string& buf);
142
143 size_t recv(void* buf, size_t len);
144 size_t recv(std::string& buf);
145
146 // Options
147 int get_bufsize(int dir);
148 void set_bufsize(int dir, int len);
149 void set_nonblocking(bool v = true);
150 void set_nodelay(bool v = true);
151 void set_timeout(const struct timeval& t);
152 void set_timeout(double t);
153 void set_autoclose(bool v) const;
154 void set_close_on_exec(bool v, int fd = -1);
155
156 int fd(void);
157
158 private:
159 int sockfd;
160 Address address;
161 size_t anum;
162 const addr_info_t* ainfo;
163 char* buffer;
164 struct timeval timeout;
165 bool nonblocking;
166 mutable bool autoclose;
167 };
168
169 #endif // SOCKET_H_
170
171 // Local Variables:
172 // mode: c++
173 // c-file-style: "linux"
174 // End: