1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* This file is part of Magellan <http://www.kAlliance.org/Magellan>
*
* Copyright (c) 1998-2000 Teodor Mihai <teddy@ireland.com>
* Copyright (c) 1998-2000 Laur Ivan <laur.ivan@ul.ie>
* Copyright (c) 1999-2000 Virgil Palanciuc <vv@ulise.cs.pub.ro>
*
* Requires the Qt widget libraries, available at no cost at
* http://www.troll.no/
*
* Also requires the KDE libraries, available at no cost at
* http://www.kde.org/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <netgroup.h>
#include <netsocket.h>
NetSocket::NetSocket(int size)
{
// create socket
sockfd=socket(AF_INET, SOCK_STREAM, 0);
// set socket non-blocking (this seems to cause several problems in Linux)
if(fcntl(sockfd, F_SETFL, O_NONBLOCK)!=0) printf("warning: could not set socket non-blocking\n");
// set last error to an empty string
lasterror="not connected";
// set properties
sr_size=size;
read_timeout=NetGroup::getDefaultReadTimeout();
write_timeout=NetGroup::getDefaultWriteTimeout();
// not connected yet
conn=false;
}
NetSocket::~NetSocket()
{
// close first if connected
if(isConnected()) close(sockfd);
}
void NetSocket::setReadTimeout(int timeout)
{
read_timeout=timeout;
}
int NetSocket::getReadTimeout()
{
return read_timeout;
}
void NetSocket::setBufferSize(int size)
{
sr_size=size;
}
int NetSocket::getBufferSize()
{
return sr_size;
}
bool NetSocket::connect(const char *host, int port, int timeout)
{
struct sockaddr_in dest_addr;
struct hostent *h;
struct timeval tv;
fd_set fdread, fdwrite, fdexcept;
// set address options and select descriptors
h=gethostbyname(host);
if(!h)
{
// handle error
switch(h_errno)
{
case HOST_NOT_FOUND:
lasterror="host not found";
break;
case NO_ADDRESS:
lasterror="host found, but no address was returned";
break;
case NO_RECOVERY:
lasterror="a name server error has occured";
break;
case TRY_AGAIN:
lasterror="a temporary name server error has occured, try again";
break;
default:
lasterror="unknown gethostbyname() error";
}
return false;
}
dest_addr.sin_family=AF_INET;
dest_addr.sin_port=htons(port);
dest_addr.sin_addr=*((struct in_addr *)h->h_addr);
bzero(&(dest_addr.sin_zero), 8);
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcept);
FD_SET(sockfd, &fdread);
FD_SET(sockfd, &fdwrite);
FD_SET(sockfd, &fdexcept);
tv.tv_sec=timeout;
tv.tv_usec=0;
// connect
int iError = 0; //no error
unsigned int uErrorSize = sizeof(int);
::connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
::select(sockfd+1, &fdread, &fdwrite, 0, &tv);
// getsockopt will get us the connecting error
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &iError, &uErrorSize);
// if(FD_ISSET(sockfd, &fdread) || FD_ISSET(sockfd, &fdwrite))
if( !iError )
{
conn=true;
return true;
}
close(sockfd);
lasterror="connection could not be established";
return false;
}
void NetSocket::disconnect()
{
close(sockfd);
conn=false;
}
bool NetSocket::isConnected()
{
if(!conn)
return false;
// reading 0 bytes should either return success (bytes available) or EAGAIN (no bytes available), otherwise it's not connected
int res=recv(sockfd, 0, 0, 0);
if(res==0 || (res==-1 && errno==EAGAIN)) return true;
lasterror="not connected";
return false;
}
bool NetSocket::isValid()
{
if(sockfd!=-1)
return true;
return false;
}
bool NetSocket::bytesAvailable()
{
fd_set fdread;
struct timeval tv;
FD_ZERO(&fdread);
FD_SET(sockfd, &fdread);
tv.tv_sec=read_timeout;
tv.tv_usec=0;
// using select() we are able to timeout on read, handy for slow connections
if(select(sockfd+1, &fdread, 0, 0, &tv)>0)
return true;
return false;
}
bool NetSocket::canWrite()
{
fd_set fdwrite;
struct timeval tv;
int iErr;
FD_ZERO(&fdwrite);
FD_SET(sockfd, &fdwrite);
tv.tv_sec=write_timeout;
tv.tv_usec=0;
// using select() we are able to timeout on read, handy for slow connections
if( (iErr=select(sockfd+1, 0, &fdwrite, 0, &tv))>0)
return true;
return false;
}
int NetSocket::write(const char *buffer)
{
int offset=0, remaining=strlen(buffer), chunk=0;
while(remaining!=0 && chunk!=-1)
{
chunk=send(sockfd, buffer+offset, remaining<sr_size?remaining:sr_size, 0);
if(chunk!=-1)
{
offset+=chunk;
remaining-=chunk;
}
// a small delay for controling the flood
usleep(10000);
}
return offset;
}
int NetSocket::read(char *buffer, int maxlength)
{
int received=0;
if( bytesAvailable() )
received=recv(sockfd, buffer+received, maxlength<sr_size?maxlength:sr_size, 0);
if( received==-1 )
return -1;
buffer[received]=0;
return received;
}
int NetSocket::readLine(char *buffer, int maxlength)
{
int received=0, last=1;
char lastchar=0;
while(lastchar!='\n' && received<=maxlength && last)
{
last=read(buffer+received, 1);
lastchar=buffer[received];
received+=last;
}
return received;
}
const char *NetSocket::error()
{
return lasterror;
}
void NetSocket::setSRSize(const int iSRSize)
{
sr_size=iSRSize;
}
int NetSocket::srSize() const
{
return sr_size;
}
|