1 // Socket.C -*- C++ -*-
2 // Copyright (C) 2002 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
34 : remotePort(0), remoteAddress(0),
35 localPort(0), localAddress(0)
38 fd->fd = socket(AF_INET, SOCK_STREAM, 0);
41 Socket::Socket(int newfd, unsigned long newAddress, int newPort)
42 : remotePort(newPort), remoteAddress(newAddress),
43 localPort(0), localAddress(0)
49 Socket::Socket(const Socket &s)
50 : remotePort(s.remotePort), remoteAddress(s.remoteAddress),
51 localPort(s.localPort), localAddress(s.localAddress)
66 Socket::getRemotePort() const
72 Socket::getLocalPort() const
78 Socket::getFileDescriptor() const
90 Socket::setRemoteHostname(String hostname)
94 if ((host = gethostbyname((const char *)hostname)) == 0)
97 memcpy(&remoteAddress, host->h_addr, host->h_length);
98 remoteAddress = ntohl(remoteAddress);
104 Socket::setRemoteIP(unsigned long address)
106 remoteAddress = address;
111 Socket::setRemotePort(int p)
119 Socket::setLocalHostname(String hostname)
121 struct hostent *host;
123 if ((host = gethostbyname((const char *)hostname)) == 0)
126 memcpy(&localAddress, host->h_addr, host->h_length);
128 struct sockaddr_in local_addr;
129 local_addr.sin_family = AF_INET;
130 local_addr.sin_port = htons(localPort);
131 local_addr.sin_addr.s_addr = htonl(localAddress);
132 if (bind(fd->fd, (struct sockaddr *)(&local_addr), sizeof(local_addr)) < 0)
139 Socket::setLocalIP(unsigned long address)
141 localAddress = address;
143 struct sockaddr_in local_addr;
144 local_addr.sin_family = AF_INET;
145 local_addr.sin_port = htons(localPort);
146 local_addr.sin_addr.s_addr = htonl(localAddress);
147 if (bind(fd->fd, (struct sockaddr *)(&local_addr), sizeof(local_addr)) < 0)
154 Socket::setLocalPort(int p)
158 struct sockaddr_in local_addr;
159 local_addr.sin_family = AF_INET;
160 local_addr.sin_port = htons(localPort);
161 local_addr.sin_addr.s_addr = htonl(localAddress);
162 if (bind(fd->fd, (struct sockaddr *)(&local_addr), sizeof(local_addr)) < 0)
169 Socket::setNonBlocking(bool nonblock)
176 // We first get the file descriptor's flags
177 if (!fcntl(fd->fd, F_GETFL, &flags))
181 return fcntl(fd->fd, F_SETFL, flags | O_NONBLOCK);
183 return fcntl(fd->fd, F_SETFL, flags & ~O_NONBLOCK);
189 struct sockaddr_in addr;
190 memset(&addr, 0, sizeof(addr));
191 addr.sin_family = AF_INET;
192 addr.sin_addr.s_addr = htonl(remoteAddress);
193 addr.sin_port = htons(remotePort);
194 if (::connect(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
196 // Bot::logLine (String("Socket Error: ")
197 // + strerror (errno) + String("\n"));
205 Socket::listen(int backlog)
207 return ::listen(fd->fd, backlog) == 0;
213 struct sockaddr_in addr;
214 socklen_t addrlen=sizeof(addr);
215 int newfd = ::accept(fd->fd, (sockaddr *)&addr, &addrlen);
217 return Socket(-1, 0, 0);
218 unsigned long newRemoteAddress = ntohl(addr.sin_addr.s_addr);
219 int newRemotePort = ntohs(addr.sin_port);
220 return Socket(newfd, newRemoteAddress, newRemotePort);
231 Socket::write(String s, bool m)
237 if (::write(fd->fd, (const char *)s, s.length()) +
238 ::write(fd->fd, "\n", 1) != s.length() + 1)
242 if (::write(fd->fd, (const char *)s, s.length()) +
243 ::write(fd->fd, "\r\n", 2) != s.length() + 2)
252 // fixme: this could probably be sped up (use some faster way of
253 // reading from the socket than reading a char at a time)
255 // We allocate the buffer statically to keep from having to overhead
256 // of reallocating a new buffer every time we call this (which is
257 // every time anything happens on IRC so the overhead of
258 // re-allocating a buffer every time is potentially too large).
260 // Since it is static, and the length of lines will differ for each
261 // read, an embedded \0 is inserted after the last character of the
262 // line and then a copy of the the c_str is returned as a String so
263 // that only the line that was just read is returned.
264 static std::string buf (512, ' ');
266 int pos = 0; // pos in buffer
267 int nb; // number of bytes read by ::read
268 char r; // temp var for storing output of read into
269 std::string::size_type length = buf.length ();
273 nb = ::read(fd->fd, &r, 1);
278 if (errno != EINTR && errno != EAGAIN)
288 buf.resize (length * 2);
289 length = buf.length ();
294 if (pos > 1 && buf[pos-2] == '\r')
299 // c_str () is used because the String constructor for std::string
300 // will copy the entire std::string into it when we only want it to
301 // copy up to the first null.
302 return String (buf.c_str ());
310 nb = ::read(fd->fd, &r, 1);
315 if(errno != EINTR && errno != EAGAIN)
324 // // Here we use a circular buffer to read from the socket
326 // Socket::readLine()
328 // // The result string
330 // // Position in the result string
332 // // Number of chars read from the socket
335 // // First, we read from the socket
337 // if (end <= begin) {
338 // nb = ::read(fd->fd, buf+end, begin - end);
340 // nb = ::read(fd->fd, buf+end, 1024 - end);
343 // if (nb == -1 && errno != EINTR && errno != EAGAIN)
344 // return String("");
346 // end = (end + nb) % 1024;
348 // // Fine, now we read our string from the buffer
349 // while (buf[begin] != '\r' && buf[begin] != '\n') {
350 // result[pos++] = buf[begin++];
356 // result[pos] = '\0';
358 // // Now we skip the final '\r' and '\n'
359 // if (buf[begin] == '\r')
360 // begin = (begin + 1) % 1024;
361 // if (buf[begin] == '\n')
362 // begin = (begin + 1) % 1024;
364 // // And we return the result
365 // return String(result);
371 // cout << "DEBUG hasData = " << (begin != end) << endl;
372 // return begin != end;