1 // Socket.C -*- C++ -*-
2 // Copyright (C) 2002,2005 Clinton Ebadi
3 // Copyright (c) 1997, 1998 Etienne BERNARD
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
10 // This program 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
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
35 : remotePort(0), remoteAddress(0),
36 localPort(0), localAddress(0)
39 fd->fd = socket(AF_INET, SOCK_STREAM, 0);
42 Socket::Socket(int newfd, unsigned long newAddress, int newPort)
43 : remotePort(newPort), remoteAddress(newAddress),
44 localPort(0), localAddress(0)
50 Socket::Socket(const Socket &s)
51 : remotePort(s.remotePort), remoteAddress(s.remoteAddress),
52 localPort(s.localPort), localAddress(s.localAddress)
67 Socket::getRemotePort() const
73 Socket::getLocalPort() const
79 Socket::getFileDescriptor() const
91 Socket::setRemoteHostname(String hostname)
95 if ((host = gethostbyname((const char *)hostname)) == 0)
98 memcpy(&remoteAddress, host->h_addr, host->h_length);
99 remoteAddress = ntohl(remoteAddress);
105 Socket::setRemoteIP(unsigned long address)
107 remoteAddress = address;
112 Socket::setRemotePort(int p)
120 Socket::setLocalHostname(String hostname)
122 struct hostent *host;
124 if ((host = gethostbyname((const char *)hostname)) == 0)
127 memcpy(&localAddress, host->h_addr, host->h_length);
129 struct sockaddr_in local_addr;
130 local_addr.sin_family = AF_INET;
131 local_addr.sin_port = htons(localPort);
132 local_addr.sin_addr.s_addr = htonl(localAddress);
133 if (bind(fd->fd, (struct sockaddr *)(&local_addr), sizeof(local_addr)) < 0)
140 Socket::setLocalIP(unsigned long address)
142 localAddress = address;
144 struct sockaddr_in local_addr;
145 local_addr.sin_family = AF_INET;
146 local_addr.sin_port = htons(localPort);
147 local_addr.sin_addr.s_addr = htonl(localAddress);
148 if (bind(fd->fd, (struct sockaddr *)(&local_addr), sizeof(local_addr)) < 0)
155 Socket::setLocalPort(int p)
159 struct sockaddr_in local_addr;
160 local_addr.sin_family = AF_INET;
161 local_addr.sin_port = htons(localPort);
162 local_addr.sin_addr.s_addr = htonl(localAddress);
163 if (bind(fd->fd, (struct sockaddr *)(&local_addr), sizeof(local_addr)) < 0)
170 Socket::setNonBlocking(bool nonblock)
177 // We first get the file descriptor's flags
178 if (!fcntl(fd->fd, F_GETFL, &flags))
182 return fcntl(fd->fd, F_SETFL, flags | O_NONBLOCK);
184 return fcntl(fd->fd, F_SETFL, flags & ~O_NONBLOCK);
190 struct sockaddr_in addr;
191 memset(&addr, 0, sizeof(addr));
192 addr.sin_family = AF_INET;
193 addr.sin_addr.s_addr = htonl(remoteAddress);
194 addr.sin_port = htons(remotePort);
195 if (::connect(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
197 // Bot::logLine (String("Socket Error: ")
198 // + strerror (errno) + String("\n"));
206 Socket::listen(int backlog)
208 return ::listen(fd->fd, backlog) == 0;
214 struct sockaddr_in addr;
215 socklen_t addrlen=sizeof(addr);
216 int newfd = ::accept(fd->fd, (sockaddr *)&addr, &addrlen);
218 return Socket(-1, 0, 0);
219 unsigned long newRemoteAddress = ntohl(addr.sin_addr.s_addr);
220 int newRemotePort = ntohs(addr.sin_port);
221 return Socket(newfd, newRemoteAddress, newRemotePort);
232 Socket::write(String s, bool m)
238 if (::write(fd->fd, (const char *)s, s.length()) +
239 ::write(fd->fd, "\n", 1) != s.length() + 1)
243 if (::write(fd->fd, (const char *)s, s.length()) +
244 ::write(fd->fd, "\r\n", 2) != s.length() + 2)
253 // fixme: this could probably be sped up (use some faster way of
254 // reading from the socket than reading a char at a time)
256 // We allocate the buffer statically to keep from having to overhead
257 // of reallocating a new buffer every time we call this (which is
258 // every time anything happens on IRC so the overhead of
259 // re-allocating a buffer every time is potentially too large).
261 // Since it is static, and the length of lines will differ for each
262 // read, an embedded \0 is inserted after the last character of the
263 // line and then a copy of the the c_str is returned as a String so
264 // that only the line that was just read is returned.
265 static std::string buf (512, ' ');
267 std::string::size_type pos = 0; // pos in buffer
268 int nb; // number of bytes read by ::read
269 char r; // temp var for storing output of read into
270 std::string::size_type length = buf.length ();
274 nb = ::read(fd->fd, &r, 1);
279 if (errno != EINTR && errno != EAGAIN)
289 buf.resize (length * 2);
290 length = buf.length ();
295 if (pos > 1 && buf[pos-2] == '\r')
298 return String (buf.substr (0, pos - 2));
303 return String (buf.substr (0, pos - 1));
312 nb = ::read(fd->fd, &r, 1);
317 if(errno != EINTR && errno != EAGAIN)
326 // // Here we use a circular buffer to read from the socket
328 // Socket::readLine()
330 // // The result string
332 // // Position in the result string
334 // // Number of chars read from the socket
337 // // First, we read from the socket
339 // if (end <= begin) {
340 // nb = ::read(fd->fd, buf+end, begin - end);
342 // nb = ::read(fd->fd, buf+end, 1024 - end);
345 // if (nb == -1 && errno != EINTR && errno != EAGAIN)
346 // return String("");
348 // end = (end + nb) % 1024;
350 // // Fine, now we read our string from the buffer
351 // while (buf[begin] != '\r' && buf[begin] != '\n') {
352 // result[pos++] = buf[begin++];
358 // result[pos] = '\0';
360 // // Now we skip the final '\r' and '\n'
361 // if (buf[begin] == '\r')
362 // begin = (begin + 1) % 1024;
363 // if (buf[begin] == '\n')
364 // begin = (begin + 1) % 1024;
366 // // And we return the result
367 // return String(result);
373 // cout << "DEBUG hasData = " << (begin != end) << endl;
374 // return begin != end;