MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Core/MOOSLIB/XPCGetHostInfo.h
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS CORE Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program is distributed in the hope that it will be useful, 
00020 //   but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00029 //   The XPC classes in MOOS are modified versions of the source provided 
00030 //   in "Making UNIX and Windows NT Talk" by Mark Nadelson and Thomas Haga 
00031 //
00033 #ifndef _XPCGetHostInfo
00034 #define _XPCGetHostInfo
00035 
00036 #include "XPCException.h"
00037 #ifdef UNIX
00038     #include <arpa/inet.h>
00039     #include <netdb.h>
00040     #include <netinet/in.h>
00041     #include <sys/socket.h>
00042 #elif _WIN32
00043     #include <winsock2.h>
00044 #else
00045     #error "Looks like the build scripts didn't set the platform type"
00046 #endif
00047     
00048 #ifndef in_addr_t
00049     #define in_addr_t unsigned long
00050 #endif
00051 
00052 enum hostType {NAME, ADDRESS};
00053 
00054 class XPCGetHostInfo
00055 {
00056 #ifdef UNIX
00057     char cIteratorFlag;        // Host database iteration flag
00058 #endif
00059     struct hostent *hostPtr;    // Entry within the host address database
00060 public:
00061 #ifdef UNIX
00062     // Default constructor.  Opens the host entry database
00063     XPCGetHostInfo()
00064     {
00065         vOpenHostDb();
00066     }
00067 #endif
00068     // Retrieves the host entry based on the host name or address
00069     XPCGetHostInfo(const char *_sHostName, hostType _type);
00070 
00071     XPCGetHostInfo(in_addr_t *_netAddr);
00072 
00073     // Destructor.  Closes the host entry database.
00074     ~XPCGetHostInfo()
00075     {
00076 #ifdef UNIX
00077         endhostent();
00078 #endif
00079     }
00080 #ifdef UNIX
00081     // Retrieves the next host entry in the database
00082     char cGetNextHost();
00083 
00084     // Opens the host entry database
00085     void vOpenHostDb()
00086     {
00087         endhostent();
00088         cIteratorFlag = 1;
00089         sethostent(1);
00090     }
00091 #endif
00092     // Retrieves the hosts IP address
00093     char *sGetHostAddress() 
00094     {
00095         struct in_addr *addr_ptr;
00096         addr_ptr = (struct in_addr *)*hostPtr->h_addr_list;
00097         return inet_ntoa(*addr_ptr);
00098     }    
00099     
00100     // Retrieves the hosts name
00101     char *sGetHostName()
00102     {
00103         return hostPtr->h_name;
00104     }
00105 };
00106 
00107 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines