int CoreSocket::connect(const GSockAddr &server, int iFLTag, int *fd, int nodelay) { int type = SOCK_STREAM; int ret; *fd = ::socket(server.family(), type, 0); if (*fd == -1) return LS_FAIL; if (iFLTag) ::fcntl(*fd, F_SETFL, iFLTag); if ((nodelay) && ((server.family() == AF_INET) || (server.family() == AF_INET6))) ::setsockopt(*fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(int)); ret = ::connect(*fd, server.get(), server.len()); if (ret != 0) { if (!((iFLTag & O_NONBLOCK) && (errno == EINPROGRESS))) { ::close(*fd); *fd = -1; } return LS_FAIL; } return ret; }
int CoreSocket::bind(const GSockAddr &server, int type, int *fd) { int ret; if (!server.get()) return EINVAL; *fd = ::socket(server.family(), type, 0); if (*fd == -1) return errno; int flag = 1; if (setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, (char *)(&flag), sizeof(flag)) == 0) { ret = ::bind(*fd, server.get(), server.len()); if (!ret) return ret; } ret = errno; ::close(*fd); *fd = -1; return ret; }
VHostMap *SubIpMap::addIP(const char *pIP) { GSockAddr addr; VHostMap *pMap; uint32_t ipv4; if (addr.parseAddr(pIP) == -1) return NULL; struct sockaddr *pAddr = addr.get(); pMap = getMap(pAddr); if (pMap) return pMap; pMap = new VHostMap(); if (pMap) { pMap->setAddrStr(pIP); ipv4 = addrConvert(pAddr); m_map.insert((void *)(unsigned long)ipv4, pMap); } return pMap; }