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
|
/*
* sockprot.h - C++ header for sockprot.cpp
* socket addresses handling
* $Id: sockprot.h 175 2005-07-01 18:07:39Z rdenisc $
*/
/***********************************************************************
* Copyright (C) 2002-2003 Remi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
***********************************************************************/
#ifndef __TCPREEN_SOCKPROT_H
# define __TCPREEN_SOCKPROT_H
# include <stddef.h>
# include <sys/types.h>
# if HAVE_SYS_SOCKET_H
# include <sys/socket.h> // SOCK_STREAM, AF_UNSPEC
# endif
# include "getaddrinfo.h" // struct addrinfo
# ifndef AF_UNSPEC
# define AF_UNSPEC 0
# endif
class SocketAddress
{
private:
int err_ai, err_sys;
char myhost[1024], myserv[128];
struct addrinfo *res;
int SetFromSocket (int fd, int flags, int side);
public:
SocketAddress (void) : err_ai (0), err_sys (0), res (NULL)
{
}
int GetSockName (int fd, int flags = 0)
{
return SetFromSocket (fd, flags, 0);
}
int GetPeerName (int fd, int flags = 0)
{
return SetFromSocket (fd, flags, 1);
}
/* Creates a socket address from a bound socket. */
int SetByName (const char *host, const char *service,
int flags = 0, int af = AF_UNSPEC,
int type = SOCK_STREAM, int proto = 0);
/* Resolves a socket address. */
SocketAddress& operator= (const SocketAddress& src);
SocketAddress (const SocketAddress& src);
/* Copy constructor. */
~SocketAddress (void);
int SetError (int ai = EAI_SYSTEM);
void ClearError (void)
{
SetError (0);
}
int GetError (void) const
{
return err_ai;
}
int Bind (void);
int Connect (int nonblock = 0);
void CleanUp (void) const;
int operator! (void) const
{
return err_ai;
}
const char *StrError (void) const;
const char *Node (void) const
{
return myhost;
}
const char *Service (void) const
{
return myserv;
}
};
#endif
|