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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/***************************************
$Header: /home/amb/wwwoffle/RCS/sockets.c 2.7 1998/04/06 18:11:59 amb Exp $
WWWOFFLE - World Wide Web Offline Explorer - Version 2.1b.
Socket manipulation routines.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1996,97,98 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "errors.h"
#include "sockets.h"
/*++++++++++++++++++++++++++++++++++++++
Opens a socket for a client.
int OpenClientSocket Returns the socket file descriptor.
char* host The name of the remote host.
int port The socket number.
++++++++++++++++++++++++++++++++++++++*/
int OpenClientSocket(char* host, int port)
{
int s;
int retval,err=0;
struct sockaddr_in server;
struct hostent* hp;
int retries=4;
server.sin_family=AF_INET;
server.sin_port=htons((unsigned short)port);
hp=gethostbyname(host);
if(!hp)
{
unsigned long int addr=inet_addr(host);
if(addr!=-1)
hp=gethostbyaddr((char*)addr,sizeof(addr),AF_INET);
if(!hp)
{errno=-1;PrintMessage(Warning,"Unknown host '%s' for server [%!s].",host);return(-1);}
}
memcpy((char*)&server.sin_addr,(char*)hp->h_addr,sizeof(server.sin_addr));
do{
s=socket(PF_INET,SOCK_STREAM,0);
if(s==-1)
{PrintMessage(Warning,"Cannot create client socket [%!s].");return(-1);}
retval=connect(s,(struct sockaddr *)&server,sizeof(server));
if(retval==-1)
{
err=errno;
if(--retries && errno==ECONNREFUSED)
PrintMessage(Inform,"Connect fail [%!s]; trying again.");
else
PrintMessage(Warning,"Connect fail [%!s].");
close(s);
s=-1;
sleep(1);
}
}
while(retval==-1 && retries && err==ECONNREFUSED);
return(s);
}
/*++++++++++++++++++++++++++++++++++++++
Opens a socket for a server.
int OpenServerSocket Returns a socket to be a server.
int port The port number to use.
++++++++++++++++++++++++++++++++++++++*/
int OpenServerSocket(int port)
{
int s;
int retval;
struct sockaddr_in server;
int reuse_addr=1;
s=socket(PF_INET,SOCK_STREAM,0);
if(s==-1)
{PrintMessage(Warning,"Cannot create server socket [%!s].");return(-1);}
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&reuse_addr,sizeof(reuse_addr));
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons((unsigned int)port);
retval=bind(s,(struct sockaddr*)&server,sizeof(server));
if(retval==-1)
{PrintMessage(Warning,"Failed to bind server socket [%!s].");return(-1);}
listen(s,4);
return(s);
}
/*++++++++++++++++++++++++++++++++++++++
Waits for a connection on the socket.
int AcceptConnect returns a socket with a remote connection on the end.
int socket Specifies the socket to check for.
++++++++++++++++++++++++++++++++++++++*/
int AcceptConnect(int socket)
{
int s;
s=accept(socket,(struct sockaddr*)0,(int*)0);
if(s==-1)
PrintMessage(Warning,"Accept on server socket failed [%!s].");
return(s);
}
/*++++++++++++++++++++++++++++++++++++++
Determines the hostname and port number used for a socket on the other end.
int SocketRemoteName Returns 0 on success and -1 on failure.
int socket Specifies the socket to check.
char **name Returns the hostname.
char **ipname Returns the hostname as an IP address.
int *port Returns the port number.
++++++++++++++++++++++++++++++++++++++*/
int SocketRemoteName(int socket,char **name,char **ipname,int *port)
{
struct sockaddr_in server;
int length=sizeof(server),retval;
static char host[MAXHOSTNAMELEN],ip[16];
struct hostent* hp=NULL;
retval=getpeername(socket,(struct sockaddr*)&server,&length);
if(retval==-1)
PrintMessage(Warning,"Failed to get socket peername [%!s].");
else
{
hp=gethostbyaddr((char*)&server.sin_addr,sizeof(server.sin_addr),AF_INET);
if(hp)
strcpy(host,hp->h_name);
else
strcpy(host,inet_ntoa(server.sin_addr));
strcpy(ip,inet_ntoa(server.sin_addr));
if(name)
*name=host;
if(ip)
*ipname=ip;
if(port)
*port=ntohs(server.sin_port);
}
return(retval);
}
/*++++++++++++++++++++++++++++++++++++++
Determines the hostname and port number used for a socket on this end.
int SocketLocalName Returns 0 on success and -1 on failure.
int socket Specifies the socket to check.
char **name Returns the hostname.
char **ipname Returns the hostname as an IP address.
int *port Returns the port number.
++++++++++++++++++++++++++++++++++++++*/
int SocketLocalName(int socket,char **name,char **ipname,int *port)
{
struct sockaddr_in server;
int length=sizeof(server),retval;
static char host[MAXHOSTNAMELEN],ip[16];
struct hostent* hp=NULL;
retval=getsockname(socket,(struct sockaddr*)&server,&length);
if(retval==-1)
PrintMessage(Warning,"Failed to get socket name [%!s].");
else
{
hp=gethostbyaddr((char*)&server.sin_addr,sizeof(server.sin_addr),AF_INET);
if(hp)
strcpy(host,hp->h_name);
else
strcpy(host,inet_ntoa(server.sin_addr));
strcpy(ip,inet_ntoa(server.sin_addr));
if(name)
*name=host;
if(ipname)
*ipname=ip;
if(port)
*port=ntohs(server.sin_port);
}
return(retval);
}
/*++++++++++++++++++++++++++++++++++++++
Closes a previously opened socket.
int CloseSocket Returns 0 on success, -1 on error.
int socket The socket to close
++++++++++++++++++++++++++++++++++++++*/
int CloseSocket(int socket)
{
int retval=close(socket);
if(retval==-1)
PrintMessage(Warning,"Socket close failed [%!s].");
return(retval);
}
/*++++++++++++++++++++++++++++++++++++++
Get the Fully Qualified Domain Name for the local host.
char *GetFQDN Return the FQDN.
++++++++++++++++++++++++++++++++++++++*/
char *GetFQDN(void)
{
static char fqdn[256],**h;
struct hostent* hp;
int type=hp->h_addrtype;
int length=hp->h_length;
char addr[sizeof(struct in_addr)];
/* Try gethostname(). */
if(gethostname(fqdn,255)==-1)
{PrintMessage(Warning,"Failed to get hostname [%!s].");return(NULL);}
if(strchr(fqdn,'.'))
return(fqdn);
/* Try gethostbyname(). */
hp=gethostbyname(fqdn);
if(!hp)
{errno=-1;PrintMessage(Warning,"Can't get IP address for host [%!s].");return(NULL);}
if(strchr(hp->h_name,'.'))
{
strcpy(fqdn,hp->h_name);
return(fqdn);
}
for(h=hp->h_aliases;*h;h++)
if(strchr(*h,'.'))
{
strcpy(fqdn,*h);
return(fqdn);
}
/* Try gethostbyaddr(). */
type=hp->h_addrtype;
length=hp->h_length;
memcpy(addr,(char*)hp->h_addr,sizeof(struct in_addr));
hp=gethostbyaddr(addr,length,type);
if(!hp)
{errno=-1;PrintMessage(Warning,"Can't get hostname for IP address [%!s].");return(NULL);}
if(strchr(hp->h_name,'.'))
{
strcpy(fqdn,hp->h_name);
return(fqdn);
}
for(h=hp->h_aliases;*h;h++)
if(strchr(*h,'.'))
{
strcpy(fqdn,*h);
return(fqdn);
}
strcpy(fqdn,hp->h_name);
return(fqdn);
}
|