void TCPSocket::connect(const IPv4Address &addr, int port){ struct sockaddr_in name; if(_socket == -1) create(); name.sin_family = AF_INET; name.sin_port = htons(port); name.sin_addr = *addr.address(); if(::connect(_socket, (struct sockaddr *)&name, sizeof(name)) != 0){ int erx = errno; close(); _socket = -1; switch(erx){ case ETIMEDOUT: throw ConnectionTimeout(); break; case ECONNREFUSED: throw ConnectionRefused(); break; case ENETUNREACH: throw NetworkUnreachable(); break; default: throw NetworkException(erx); } } in.open(_socket); out.open(_socket); }
void Socket::bind(const IPv4Address &ip){ sockaddr_in name; name.sin_family = AF_INET; name.sin_addr = *ip.address(); if(::bind(_socket, (struct sockaddr *)&name, sizeof(sockaddr_in)) != 0){ switch(errno){ default: NetworkException(errno); } } }
void IPForensics::update_host(std::set<Host>::iterator it, IPv4Address ipv4, IPv6Address ipv6) { Host h = *it; if (h.ipv4().address().empty() && !ipv4.address().empty()) { h.set_ipv4(ipv4); } if (h.ipv6().address().empty() && !ipv6.address().empty()) { h.set_ipv6(ipv6); } // replace previous IPv6 address if it is link-local if (!h.ipv6().address().empty() && !ipv6.address().empty()) { if (h.ipv6().address()[0] == ipf::kLinkLocalIPv6[0] && h.ipv6().address()[1] == ipf::kLinkLocalIPv6[1] && ipv6.address()[0] != ipf::kLinkLocalIPv6[0] && ipv6.address()[1] != ipf::kLinkLocalIPv6[1]) { h.set_ipv6(ipv6); } } hosts_.erase(it); hosts_.insert(h); }
OldUDPSocket::OldUDPSocket(const IPv4Address &addr, int _port){ #ifdef USE_EMBEDDED_CLASSNAMES setClassName(__xvr2_Net_OldUDPSocket); #endif flags = MSG_NOSIGNAL; port = _port; bzero(&ipv4addr, sizeof(struct ::sockaddr_in)); ipv4addr.sin_family = AF_INET; ipv4addr.sin_port = htons(_port); ipv4addr.sin_addr = *addr.address(); tsock = socket(ipv4addr.sin_family, SOCK_DGRAM, 0); if(tsock < 0){ switch(errno){ case EMFILE: throw ProcOutOfFileDescriptors(); break; case ENFILE: throw SysOutOfFileDescriptors(); break; default: throw IOException(); } } }