inlib  1.2.0
Classes | Functions
inlib::net Namespace Reference

Classes

class  base_socket
class  ftp
class  http
class  inet_socket
class  mail

Functions

bool ip_addresses (std::ostream &a_out, std::vector< std::string > &a_if_names, std::vector< std::string > &a_ip_names, std::vector< unsigned long > &a_ip_addrs)
bool if_address (std::ostream &a_out, const std::string &a_if, std::string &a_sa)
bool dump_ip_addresses (std::ostream &a_out)
std::string serror ()
bool host_name (std::ostream &a_out, std::string &a_host)
bool set_reuse_addr (std::ostream &a_out, int a_socket)
bool set_input_timer (std::ostream &a_out, int a_socket, unsigned int a_micro_secs)
bool is_there_input (const std::vector< int > &a_socks, bool &a_is)
bool wait_input (const std::vector< int > &a_socks)
std::string herror ()
bool inet_addr (std::ostream &a_out, const std::string &a_host, in_addr_t &a_addr)
bool inet_host (std::ostream &a_out, std::string &a_host)

Function Documentation

bool inlib::net::dump_ip_addresses ( std::ostream &  a_out) [inline]

Definition at line 161 of file addresses.

                                                {
  std::vector<std::string> if_names;
  std::vector<std::string> ip_names;
  std::vector<unsigned long> ip_addrs;
  if(!inlib::net::ip_addresses(a_out,if_names,ip_names,ip_addrs)) {
    return false;
  }

  // get interface address. for exa for a_if = "en0".
  std::vector<std::string>::iterator it1 = if_names.begin();
  std::vector<std::string>::iterator it2 = ip_names.begin();
  std::vector<unsigned long>::iterator it3 = ip_addrs.begin();
  for(;it1!=if_names.end();++it1,++it2,++it3) {
    a_out << "--------------------" << std::endl;
    a_out << "if name " << *it1 << std::endl;
    a_out << "ip name " << *it2 << std::endl;
    a_out << "ip addr " << (unsigned int)*it3 << std::endl;
  }

  return true;
}
std::string inlib::net::herror ( ) [inline]

Definition at line 33 of file inet_socket.

                          {
   std::ostringstream strm;
#ifdef WIN32
   strm << (int)WSAGetLastError();
   return std::string(" error : ")+strm.str()+".";
#else
   strm << (int)h_errno;
   return std::string(" error : ")+strm.str()
          + " : " +std::string(hstrerror(h_errno))+".";
#endif
}
bool inlib::net::host_name ( std::ostream &  a_out,
std::string &  a_host 
) [inline]

Definition at line 47 of file base_socket.

                                                          {
  char s[512];
  if(::gethostname(s,sizeof(s))<0) {
    a_out << "inlib::net::host_name :"
          << " gethostname() :" << serror()
          << std::endl;
    a_host.clear();
    return false;
  }
  a_host = s;
  return true;
}
bool inlib::net::if_address ( std::ostream &  a_out,
const std::string &  a_if,
std::string &  a_sa 
) [inline]

Definition at line 130 of file addresses.

                                       {
  std::vector<std::string> if_names;
  std::vector<std::string> ip_names;
  std::vector<unsigned long> ip_addrs;
  if(!inlib::net::ip_addresses(a_out,if_names,ip_names,ip_addrs)) {
    a_sa.clear();
    return false;
  }

  // get interface address. for exa for a_if = "en0".
  std::vector<std::string>::iterator it1 = if_names.begin();
  std::vector<std::string>::iterator it2 = ip_names.begin();
  std::vector<unsigned long>::iterator it3 = ip_addrs.begin();
  for(;it1!=if_names.end();++it1,++it2,++it3) {
    if((*it1)==a_if) { 
      a_sa = *it2;
      return true;
    }
  }

  a_out << "inlib::net::if_address :"
        << " interface \"" << a_if << "\""
        << " not found."
        << std::endl;

  a_sa.clear();
  return false; //not found.
}
bool inlib::net::inet_addr ( std::ostream &  a_out,
const std::string &  a_host,
in_addr_t &  a_addr 
) [inline]

Definition at line 45 of file inet_socket.

                                                                                   {
  a_addr = ::inet_addr(a_host.c_str());
  if(a_addr!=INADDR_NONE) {
    //a_out << "inlib::net::inet_addr :"
    //      << " for " << sout(a_host)
    //      << " numeric."
    //      << std::endl;
    return true;
  }
  //perhaps not numeric form.
  hostent* host_p = ::gethostbyname(a_host.c_str());
  if(!host_p) {
    a_out << "inlib::net::inet_addr :"
          << " for " << sout(a_host)
          << " gethostbyname() : " << herror()
          << std::endl;
    a_addr = 0;      
    return false;
  }
  if(host_p->h_length!=sizeof(in_addr_t)) {
    a_out << "inlib::inet_socket:::connect :"
          << " for " << sout(a_host)
          << " gethostbyname() : bad length."
          << std::endl;
    a_addr = 0;      
    return false;
  }
  ::memcpy((char*)&a_addr,(char*)(host_p->h_addr),host_p->h_length);
  return true;
}
bool inlib::net::inet_host ( std::ostream &  a_out,
std::string &  a_host 
) [inline]

Definition at line 76 of file inet_socket.

                                                          {
#if TARGET_OS_EMBEDDED
  if(!inlib::net::if_address(a_out,"en0",a_host)) return false;
#elif ANDROID_NDK
  inlib::net::dump_ip_addresses(a_out);    
  if(!inlib::net::if_address(a_out,"eth0",a_host)) return false;
#else
  if(!host_name(a_out,a_host)) return false;
#endif
  return true;
}
bool inlib::net::ip_addresses ( std::ostream &  a_out,
std::vector< std::string > &  a_if_names,
std::vector< std::string > &  a_ip_names,
std::vector< unsigned long > &  a_ip_addrs 
) [inline]

Definition at line 28 of file addresses.

                                        {

  a_if_names.clear();
  a_ip_names.clear();
  a_ip_addrs.clear();

#ifdef WIN32
  a_out << "inlib::net::ip_addresses :"
        << " not yet ported on this platform."
        << std::endl;
  return false;
#else
  int sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0) {
    a_out << "inlib::net::ip_addresses :"
          << " socket() : error : " << (int)errno
          << " : " << std::string(strerror(errno)) << "."
          << std::endl;
    return false;
  }
        
  const int BUFFERSIZE = 4000;
  char buffer[BUFFERSIZE];
  struct ifconf ifc;
  ifc.ifc_len = BUFFERSIZE;
  ifc.ifc_buf = buffer;
        
  if (::ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
    a_out << "inlib::net::ip_addresses :"
          << " ioctl() : error : " << (int)errno
          << " : " << std::string(strerror(errno)) << "."
          << std::endl;
    return false;
  }
        
  char lastname[IFNAMSIZ];
  lastname[0] = 0;
        
  char* ptr;
  for (ptr = buffer; ptr < buffer + ifc.ifc_len; )  {
    struct ifreq* ifr = (struct ifreq*)ptr;

#if defined(__APPLE__)
    int len = sizeof(struct sockaddr)  >  ifr->ifr_addr.sa_len?
              sizeof(struct sockaddr)  :  ifr->ifr_addr.sa_len;
#else
    int len = sizeof(struct sockaddr);
#endif
    ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer
        
    if (ifr->ifr_addr.sa_family != AF_INET) {
      continue; // ignore if not desired address family
    }
        
    char* cptr;
    if ((cptr = (char *)::strchr(ifr->ifr_name, ':')) != NULL) {
      *cptr = 0; // replace colon will null
    }
        
    if (::strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
      continue; /* already processed this interface */
    }
        
    ::memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
        
    struct ifreq ifrcopy = *ifr;
    ::ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
    int flags = ifrcopy.ifr_flags;
    if ((flags & IFF_UP) == 0) {
      continue; // ignore if interface not up
    }
        
    a_if_names.push_back(ifr->ifr_name);
        
    struct sockaddr_in*  sin = (struct sockaddr_in*)&ifr->ifr_addr;

    a_ip_names.push_back(inet_ntoa(sin->sin_addr));
    a_ip_addrs.push_back(sin->sin_addr.s_addr);

  }
        
  ::close(sockfd);

/*
 {std::vector<std::string>::iterator it1 = a_if_names.begin();
  std::vector<std::string>::iterator it2 = a_ip_names.begin();
  std::vector<unsigned long>::iterator it3 = a_ip_addrs.begin();
  for(;it1!=a_if_names.end();++it1,++it2,++it3) {
    a_out << "--------------------" << std::endl;
    a_out << "if name " << *it1 << std::endl;
    a_out << "ip name " << *it2 << std::endl;
    a_out << "ip addr " << (unsigned int)*it3 << std::endl;
  }}
*/

  return true;
#endif
}
bool inlib::net::is_there_input ( const std::vector< int > &  a_socks,
bool &  a_is 
) [inline]

Definition at line 86 of file base_socket.

                                                                     {
  a_is = false;
  if(a_socks.empty()) return false;
  
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 10; //microsec
  
  fd_set mask;
  FD_ZERO(&mask);
  int nfds = 0;
 {std::vector<int>::const_iterator it;
  for(it=a_socks.begin();it!=a_socks.end();++it) {
    FD_SET(*it,&mask);
    nfds = mx(nfds,*it);
  }}
  nfds++;

  if(::select(nfds,&mask,0,0,&timeout)==(-1)) {
    a_is = false;
    return false;
  }
  
 {std::vector<int>::const_iterator it;
  for(it=a_socks.begin();it!=a_socks.end();++it) {
    if(FD_ISSET(*it,&mask)) {
      a_is = true;
      break;
    }
  }}

  return true;
}
std::string inlib::net::serror ( ) [inline]

Definition at line 35 of file base_socket.

                          {
   std::ostringstream strm;
#ifdef WIN32
   strm << (int)WSAGetLastError();
   return std::string(" error : ")+strm.str()+".";
#else
   strm << (int)errno;
   return std::string(" error : ")+strm.str()
          + " : " +std::string(strerror(errno))+".";
#endif
}
bool inlib::net::set_input_timer ( std::ostream &  a_out,
int  a_socket,
unsigned int  a_micro_secs 
) [inline]

Definition at line 72 of file base_socket.

                                                                                      {
  struct timeval val;
  val.tv_sec = 0;
  val.tv_usec = a_micro_secs;
  if(::setsockopt(a_socket,SOL_SOCKET,
                  SO_RCVTIMEO,(char*)&val,sizeof(val))<0) {
    a_out << "inlib::base_socket::set_input_timer :"
          << " setsockopt():" << serror()
          << std::endl;
    return false;
  }
  return true;
}
bool inlib::net::set_reuse_addr ( std::ostream &  a_out,
int  a_socket 
) [inline]

Definition at line 60 of file base_socket.

                                                           {
  int val = 1;
  if(::setsockopt(a_socket,SOL_SOCKET,
                  SO_REUSEADDR,(char*)&val,sizeof(val))<0) {
    a_out << "inlib::net::set_reuse_addr :"
          << " setsockopt() :" << serror()
          << std::endl;
    return false;
  }
  return true;
}
bool inlib::net::wait_input ( const std::vector< int > &  a_socks) [inline]

Definition at line 120 of file base_socket.

                                                      {
  if(a_socks.empty()) return false;
  
  fd_set mask;
  FD_ZERO(&mask);
  int nfds = 0;
 {std::vector<int>::const_iterator it;
  for(it=a_socks.begin();it!=a_socks.end();++it) {
    FD_SET(*it,&mask);
    nfds = mx(nfds,*it);
  }}
  nfds++;

  if(::select(nfds,&mask,0,0,0)==(-1)) return false;
  
  return true;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines