int LcReplGroup::connGroupLstnrs() { Addr2ClntConnMap_t::iterator itr; Addr2ClntConnMap_t &lstrMap = m_pSockConnMgr->getClntConnMap(); const char* sockAddr; ClientConn * pConn; for (itr = lstrMap.begin(); itr != lstrMap.end(); itr = lstrMap.next ( itr )) { sockAddr = itr.first(); pConn = itr.second(); LS_DBG_M("LcReplGroup::connAllListenSvr addr:%s", sockAddr); if (!pConn->isActive() ) { LS_DBG_M("LcReplGroup::connAllListenSvr is not active, pMultiplexer:%p", getMultiplexer()); pConn->closeConnection(); pConn->SetMultiplexer(getMultiplexer() ); GSockAddr serverAddr; serverAddr.set(sockAddr, AF_INET ); if (pConn->connectTo (&serverAddr) != -1) { LS_INFO ("node[%s] addr=%p, is connecting to master[%s]" ,pConn->getLocalAddr(), pConn, sockAddr); LcReplSender::getInstance().clientJoinListenSvrReq(pConn); } else LS_INFO ("failed to connect to master %d", pConn->isActive()); } } return LS_OK; }
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::listen(const char *pURL, int backlog, int *fd, int sndBuf, int rcvBuf) { int ret; GSockAddr server; ret = server.set(pURL, 0); if (ret != 0) return LS_FAIL; return listen(server, backlog, fd, sndBuf, rcvBuf); }
int CoreSocket::connect(const char *pURL, int iFLTag, int *fd, int dnslookup, int nodelay) { int ret; GSockAddr server; *fd = -1; int tag = NO_ANY; if (dnslookup) tag |= DO_NSLOOKUP; ret = server.set(pURL, tag); if (ret != 0) return LS_FAIL; return connect(server, iFLTag, fd, nodelay); }
int HttpListener::start() { GSockAddr addr; if ( addr.set( getAddrStr(), 0 ) ) return errno; int fd; int ret = CoreSocket::listen( addr, SmartSettings::getSockBacklog(), &fd, m_iSockSendBufSize, m_iSockRecvBufSize ); if ( ret != 0 ) { return ret; } return setSockAttr( fd, addr ); }
int HttpListener::setSockAttr( int fd, GSockAddr &addr ) { setfd( fd ); ::fcntl( fd, F_SETFD, FD_CLOEXEC ); ::fcntl( fd, F_SETFL, HttpGlobals::getMultiplexer()->getFLTag()); int nodelay = 1; //::setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof( int ) ); #ifdef TCP_DEFER_ACCEPT ::setsockopt( fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &nodelay, sizeof( int ) ); #endif //int tos = IPTOS_THROUGHPUT; //setsockopt( fd, IPPROTO_IP, IP_TOS, &tos, sizeof( tos )); m_pMapVHost->setPort( addr.getPort() ); #ifdef SO_ACCEPTFILTER /* * FreeBSD accf_http filter */ struct accept_filter_arg arg; memset( &arg, 0, sizeof(arg) ); strcpy( arg.af_name, "httpready" ); if ( setsockopt(fd, SOL_SOCKET, SO_ACCEPTFILTER, &arg, sizeof(arg)) < 0) { if (errno != ENOENT) { LOG_NOTICE(( "Failed to set accept-filter 'httpready': %s", strerror(errno) )); } } #endif return HttpGlobals::getMultiplexer()->add( this, POLLIN|POLLHUP|POLLERR ); }
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; }
int CoreSocket::listen(const GSockAddr &server, int backLog, int *fd, int sndBuf, int rcvBuf) { int ret; ret = bind(server, SOCK_STREAM, fd); if (ret) return ret; if (sndBuf > 4096) ::setsockopt(*fd, SOL_SOCKET, SO_SNDBUF, &sndBuf, sizeof(int)); if (rcvBuf > 4096) ::setsockopt(*fd, SOL_SOCKET, SO_RCVBUF, &rcvBuf, sizeof(int)); if (((server.family() == AF_INET) || (server.family() == AF_INET6))) { int nodelay = 1; ::setsockopt(*fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(int)); #ifdef TCP_FASTOPEN nodelay = backLog / 2; if (nodelay > 256) nodelay = 256; ::setsockopt(*fd, IPPROTO_TCP, TCP_FASTOPEN, &nodelay, sizeof(int)); #endif } ret = ::listen(*fd, backLog); if (ret == 0) return 0; ret = errno; ::close(*fd); *fd = -1; return ret; }
bool LcReplConf::parse(const char *szFile) { char pSockAddr[64]; const char* pAddr; StringList lbAddrs; AutoStr str; if (!m_confParser.loadConfig(szFile, "=")) { initLogger("stderr", "DBG"); LS_ERROR ( "LcReplConf unable to load config %s, error:%s" , szFile, m_confParser.getErrorMessage()); return false; } initLogger(getLogFile(), getLogLevel()); const char *pEntry = m_confParser.getConfig("REPL.LBADDRS"); setLBAddrs(pEntry); GSockAddr sockAddr; //LISTENSVRADDR pAddr = m_confParser.getConfig("REPL.LISTENSVRADDR"); if (!setLisenSvrAddr(pAddr)) { LS_ERROR ( "LcReplConf fails to load Repl ListenSvrAddr %s", pAddr); return false; } m_lsntnrSvrIp = getIpOfAddr(pAddr, str); if ( !isAllowedIP(getLocalLsntnrIp() )) { LS_ERROR ( "Repl ListenSvrIp %s is not in LB Addrs" , getLocalLsntnrIp()); return false; } //MEMCACHEDADDR pAddr = m_confParser.getConfig("CACHED.ADDR"); if (pAddr == NULL || sockAddr.set(pAddr, 0)) { LS_ERROR ( "LcReplConf fails to load Repl MemCached SvrAddr %s", pAddr); return false; } sockAddr.toString(pSockAddr, 64); m_cachedAddr = pSockAddr; //GzipStream const char *ptr = m_confParser.getConfig("REPL.GZIPSTREAM"); if(ptr != NULL && !strcasecmp(ptr, "YES")) m_bGzipStream = true; //SubFileNum int v; ptr = m_confParser.getConfig("CACHED.SLICES"); if ((ptr != NULL) && ((v = atoi(ptr)) > 0)) m_iSubFileNum = v; if ( m_confParser.getConfig("CACHED.SHMDIR")) m_shmDir = m_confParser.getConfig("CACHED.SHMDIR"); if ( m_confParser.getConfig("CACHED.SHMNAME")) m_shmName = m_confParser.getConfig("CACHED.SHMNAME"); if (m_confParser.getConfig("CACHED.SHMHASHNAME")) m_shmHashName = m_confParser.getConfig("CACHED.SHMHASHNAME"); ptr = m_confParser.getConfig("REPL.MAXTIDPACKET"); m_maxTidPacket = ptr ? atoi(ptr) : DEF_MAXTIDPACKET; ptr = m_confParser.getConfig("REPL.VALMAXSZ"); m_valMaxSz = ptr ? atoi(ptr) : DEF_VALMAXSZ; ptr = m_confParser.getConfig("REPL.MEMMAXSZ"); m_memMaxSz = ptr ? atoi(ptr) : DEF_MEMMAXSZ; if ( (ptr = m_confParser.getConfig("CACHED.USECAS")) != NULL) { m_useCas = strcmp(ptr, "TRUE") ? false : true; } if ((ptr = m_confParser.getConfig("CACHED.USESASL")) != NULL ) { if ( !strcmp(ptr, "TRUE")) m_useSasl = true; } if ((ptr = m_confParser.getConfig("CACHED.NOMEMFAIL")) != NULL ) { if ( !strcmp(ptr, "TRUE")) m_noMemFail = true; } char pBuf[1024], pBuf2[1024]; uint16_t num = getSubFileNum(); m_pPriorities = new int[num]; m_pShmFiles = new char *[num]; for (int i=0; i < num; ++i) { //Cached.Slice. snprintf(pBuf, sizeof(pBuf), "CACHED.SLICE.PRIORITY.%d", i); const char *ptr = m_confParser.getConfig(pBuf); if ((ptr != NULL) && ((v = atoi(ptr)) >= 0)) { LS_DBG_M ( "%s=%d", pBuf, v); m_pPriorities[i] = v; } snprintf(pBuf, sizeof(pBuf), "CACHED.SHMFILEPATH.%d", i); ptr = m_confParser.getConfig(pBuf); if (ptr != NULL) { snprintf(pBuf2, sizeof(pBuf2), "%s/%s", m_shmDir.c_str(), ptr); m_confParser.setConfig(pBuf, pBuf2); m_pShmFiles[i] = (char*)m_confParser.getConfig(pBuf); LS_DBG_M("%s=%s", pBuf, pBuf2); } else { LS_ERROR ( "[ERROR] [Configure]: LcReplConf config file, missing required: %s\n", pBuf); return false; } } return true; }