Debian Codegraph @

src/UdpSocket.cpp

raw
1 // UdpSocket class - implements BaseSocket for INET domain sockets
2
3 // Note: This class is only tested for UDP send at present other functions (such as listen, read etc may not currently work!!! PIP
4
5 // For all support, instructions and copyright go to:
6 // http://e2guardian.org/
7 // Released under the GPL v2, with the OpenSSL exception described in the README file.
8
9 // INCLUDES
10 #ifdef HAVE_CONFIG_H
11 #include "e2config.h"
12 #endif
13
14 #include "UdpSocket.hpp"
15 #include "Logger.hpp"
16
17 #include <string.h>
18 #include <csignal>
19 #include <fcntl.h>
20 #include <sys/time.h>
21 #include <pwd.h>
22 #include <stdexcept>
23 #include <cerrno>
24 #include <unistd.h>
25 #include <netinet/tcp.h>
26 #include "String.hpp"
27
28 extern bool reloadconfig;
29
30
31 // IMPLEMENTATION
32 //
33 // destructor
34 UdpSocket::~UdpSocket() {
35 close();
36 }
37
38 // constructor - create an INET socket & clear address structures
39 UdpSocket::UdpSocket() {
40 sck = socket(AF_INET, SOCK_DGRAM, 0);
41 if (sck < 0) {
42 s_errno = errno;
43 } else {
44 memset(&my_adr, 0, sizeof my_adr);
45 memset(&peer_adr, 0, sizeof peer_adr);
46 my_adr.sin_family = AF_INET;
47 peer_adr.sin_family = AF_INET;
48 peer_adr_length = sizeof(struct sockaddr_in);
49 int f = 1;
50
51 if (sck > 0)
52 setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
53
54 my_port = 0;
55 }
56 }
57
58 // create socket from pre-existing FD (address structs will be invalid!)
59 UdpSocket::UdpSocket(int fd)
60 : BaseSocket(fd) {
61 memset(&my_adr, 0, sizeof my_adr);
62 memset(&peer_adr, 0, sizeof peer_adr);
63 my_adr.sin_family = AF_INET;
64 peer_adr.sin_family = AF_INET;
65 peer_adr_length = sizeof(struct sockaddr_in);
66 int f = 1;
67
68 int res = setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
69 if (res < 0) s_errno = errno;
70 my_port = 0;
71 }
72
73 // create socket from pre-existing FD, storing local & remote IPs
74 UdpSocket::UdpSocket(int newfd, struct sockaddr_in myip, struct sockaddr_in peerip)
75 : BaseSocket(newfd) {
76 memset(&my_adr, 0, sizeof my_adr); // ***
77 memset(&peer_adr, 0, sizeof peer_adr); // ***
78 my_adr.sin_family = AF_INET; // *** Fix suggested by
79 peer_adr.sin_family = AF_INET; // *** Christopher Weimann
80 my_adr = myip;
81 peer_adr = peerip;
82 peer_adr_length = sizeof(struct sockaddr_in);
83 int f = 1;
84
85 int res = setsockopt(sck, IPPROTO_TCP, TCP_NODELAY, &f, sizeof(int));
86 if (res < 0) s_errno = errno;
87 my_port = 0;
88 }
89
90 // find the ip to which the client has connected
91 std::string UdpSocket::getLocalIP() {
92 char res[INET_ADDRSTRLEN];
93 return inet_ntop(AF_INET,&my_adr.sin_addr, res, sizeof(res));
94 }
95
96 // find the ip of the client connecting to us
97 std::string UdpSocket::getPeerIP() {
98 if (!client_addr.empty()) {
99 return client_addr;
100 } else {
101 char res[INET_ADDRSTRLEN];
102 return inet_ntop(AF_INET, &peer_adr.sin_addr, res, sizeof(res));
103 }
104 }
105
106 // find the port of the client connecting to us
107 int UdpSocket::getPeerSourcePort() {
108 return ntohs(peer_adr.sin_port);
109 }
110
111 int UdpSocket::getPort() {
112 return my_port;
113 }
114
115 void UdpSocket::setPort(int port) {
116 my_port = port;
117 }
118
119 // return the address of the client connecting to us
120 unsigned long int UdpSocket::getPeerSourceAddr() {
121 return (unsigned long int) ntohl(peer_adr.sin_addr.s_addr);
122 }
123
124 // close connection & wipe address structs
125 void UdpSocket::reset() {
126 this->baseReset();
127
128 sck = socket(AF_INET, SOCK_DGRAM, 0);
129 if (sck < 0) {
130 s_errno = errno;
131 return;
132 }
133
134 memset(&my_adr, 0, sizeof my_adr);
135 memset(&peer_adr, 0, sizeof peer_adr);
136 my_adr.sin_family = AF_INET;
137 peer_adr.sin_family = AF_INET;
138 peer_adr_length = sizeof(struct sockaddr_in);
139 infds[0].fd = sck;
140 outfds[0].fd = sck;
141
142 }
143
144 // connect to given IP & port (following default constructor)
145 int UdpSocket::connect(const std::string &ip, int port) {
146 reset(); // do it anyway as we need sck to be allocated
147
148 if (sck < 0) // socket creation error
149 {
150 return -1;
151 }
152 bind(39000); // otherwise source port allocated on each send confusing destination server. f/w etc
153
154 int len = sizeof my_adr;
155 peer_adr.sin_port = htons(port);
156 inet_aton(ip.c_str(), &peer_adr.sin_addr);
157 my_port = port;
158 peer_adr_length = sizeof peer_adr;
159 int save_flags = fcntl(sck,F_GETFL);
160 fcntl(sck, F_SETFL, save_flags | O_NONBLOCK);
161 s_errno = 0;
162 errno = 0;
163 int ret = ::connect(sck, (struct sockaddr *) &peer_adr, len);
164 if (ret < 0 && errno == EINPROGRESS) ret = 0;
165 else s_errno = errno;
166 if (ret == 0) {
167 int rc = poll(outfds, 1, timeout);
168 if (rc == 0) {
169 timedout = true;
170 ret = -1;
171 } else if (rc < 0) {
172 s_errno = errno;
173 ret = -1;
174 } else {
175 int so_error;
176 socklen_t len = sizeof so_error;
177 getsockopt(sck, SOL_SOCKET, SO_ERROR, &so_error, &len);
178 if (so_error != 0) {
179 sockerr = true;
180 s_errno = so_error;
181 ret = -1;
182 } else {
183 ret = 0;
184 }
185 }
186 }
187 if (ret < 0) {
188 close();
189 } else {
190 save_flags = fcntl(sck,F_GETFL);
191 fcntl(sck, F_SETFL, save_flags & ~O_NONBLOCK);
192 }
193 return ret;
194 }
195
196 // bind socket to given port
197 int UdpSocket::bind(int port) {
198 int len = sizeof my_adr;
199 int i = 1;
200
201 setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
202
203 my_adr.sin_port = htons(port);
204 my_port = port;
205
206 return ::bind(sck, (struct sockaddr *) &my_adr, len);
207 }
208
209 // bind socket to given port & IP
210 int UdpSocket::bind(const std::string &ip, int port) {
211 int len = sizeof my_adr;
212 int i = 1;
213
214 setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
215
216 my_adr.sin_port = htons(port);
217 my_adr.sin_addr.s_addr = inet_addr(ip.c_str());
218 my_port = port;
219
220 return ::bind(sck, (struct sockaddr *) &my_adr, len);
221 }
222
223 // accept incoming connections & return new UdpSocket
224 UdpSocket *UdpSocket::accept() {
225 peer_adr_length = sizeof(struct sockaddr_in);
226 s_errno = 0;
227 errno = 0;
228 int newfd = ::accept(sck, (struct sockaddr *) &peer_adr, &peer_adr_length);
229
230 if (newfd > 0) {
231 UdpSocket *s = new UdpSocket(newfd, my_adr, peer_adr);
232 s->setPort(my_port);
233 return s;
234 } else {
235 s_errno = errno;
236 return NULL;
237 }
238 }
239
240
241
242 void UdpSocket::close()
243 {
244
245 BaseSocket::close();
246 }
247
248
249
250 // write line to socket
251 bool UdpSocket::writeString(std::string line)
252 {
253 int l = line.length();
254 return writeToSocket(line.c_str(), l, 0, timeout);
255 }
256
257 // write data to socket
258 bool UdpSocket::writeToSocket(const char *buff, int len, unsigned int flags, int timeout)
259 {
260 if (len == 0) // nothing to write
261 return true;
262 if (true) {
263 return BaseSocket::writeToSocket(buff, len, flags, timeout);
264 }
265
266
267 return true;
268 }
269
270 int UdpSocket::readFromSocket(char *buff, int len, unsigned int flags, int timeout, bool ret_part )
271 {
272 if (len == 0) // nothing to read
273 return 0;
274 if (true) {
275 return BaseSocket::readFromSocket(buff, len, flags, timeout, ret_part);
276 }
277
278 // first, return what's left from the previous buffer read, if anything
279 int cnt = len;
280 int tocopy = 0;
281 if ((bufflen - buffstart) > 0) {
282 DEBUG_network("Socket::readFromSocket: data already in buffer; bufflen: ", bufflen, " buffstart: ", buffstart );
283 tocopy = len;
284 if ((bufflen - buffstart) < len)
285 tocopy = bufflen - buffstart;
286 memcpy(buff, buffer + buffstart, tocopy);
287 cnt -= tocopy;
288 buffstart += tocopy;
289 buff += tocopy;
290 if(ret_part) return tocopy;
291 if (cnt == 0)
292 return len;
293 }
294
295 return len;
296 }
297
298
299 bool UdpSocket::getIeof() {
300 return ieof;
301 }
302
303
304
305 void UdpSocket::setClientAddr(std::string ip, int port) {
306 client_addr = ip;
307 client_port = port;
308 }