1 /* 2 * sockprot.h - C++ header for sockprot.cpp 3 * socket addresses handling 4 * $Id: sockprot.h 175 2005-07-01 18:07:39Z rdenisc $ 5 */ 6 7 /*********************************************************************** 8 * Copyright (C) 2002-2003 Remi Denis-Courmont. * 9 * This program is free software; you can redistribute and/or modify * 10 * it under the terms of the GNU General Public License as published * 11 * by the Free Software Foundation; version 2 of the license. * 12 * * 13 * This program is distributed in the hope that it will be useful, * 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 16 * See the GNU General Public License for more details. * 17 * * 18 * You should have received a copy of the GNU General Public License * 19 * along with this program; if not, you can get it from: * 20 * http://www.gnu.org/copyleft/gpl.html * 21 ***********************************************************************/ 22 23 #ifndef __TCPREEN_SOCKPROT_H 24 # define __TCPREEN_SOCKPROT_H 25 26 # include <stddef.h> 27 # include <sys/types.h> 28 # if HAVE_SYS_SOCKET_H 29 # include <sys/socket.h> // SOCK_STREAM, AF_UNSPEC 30 # endif 31 # include "getaddrinfo.h" // struct addrinfo 32 33 # ifndef AF_UNSPEC 34 # define AF_UNSPEC 0 35 # endif 36 37 class SocketAddress 38 { 39 private: 40 int err_ai, err_sys; 41 char myhost[1024], myserv[128]; 42 struct addrinfo *res; 43 44 int SetFromSocket (int fd, int flags, int side); 45 46 public: SocketAddress(void)47 SocketAddress (void) : err_ai (0), err_sys (0), res (NULL) 48 { 49 } 50 51 int GetSockName (int fd, int flags = 0) 52 { 53 return SetFromSocket (fd, flags, 0); 54 } 55 56 int GetPeerName (int fd, int flags = 0) 57 { 58 return SetFromSocket (fd, flags, 1); 59 } 60 /* Creates a socket address from a bound socket. */ 61 62 int SetByName (const char *host, const char *service, 63 int flags = 0, int af = AF_UNSPEC, 64 int type = SOCK_STREAM, int proto = 0); 65 /* Resolves a socket address. */ 66 67 SocketAddress& operator= (const SocketAddress& src); 68 SocketAddress (const SocketAddress& src); 69 /* Copy constructor. */ 70 71 ~SocketAddress (void); 72 73 int SetError (int ai = EAI_SYSTEM); ClearError(void)74 void ClearError (void) 75 { 76 SetError (0); 77 } 78 GetError(void)79 int GetError (void) const 80 { 81 return err_ai; 82 } 83 84 int Bind (void); 85 int Connect (int nonblock = 0); 86 void CleanUp (void) const; 87 88 int operator! (void) const 89 { 90 return err_ai; 91 } 92 93 const char *StrError (void) const; Node(void)94 const char *Node (void) const 95 { 96 return myhost; 97 } Service(void)98 const char *Service (void) const 99 { 100 return myserv; 101 } 102 }; 103 104 #endif 105