⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servmgr.cpp.svn-base

📁 这是和p2p相关的一份源码
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
		while (s)		{			if (s != src)				if (s->isConnected())
					if (s->type == Servent::T_PGNU)
						if (!s->seenIDs.contains(pack.id))							if (s->seenIDs.contains(routeID))							{								if (src)									if (!src->networkID.isSame(s->networkID))										continue;								if (s->outputPacket(pack,true))									cnt++;							}			s=s->next;		}	}	LOG_NETWORK("route: %s (%d) to %d servents",GNU_FUNC_STR(pack.func),pack.ttl,cnt);	return cnt;}
// -----------------------------------
bool ServMgr::canConnectToMe(Host &h)
{
	if ((firewalled != FW_ON) || h.localIP())
		return true;

	return false;
}
// -----------------------------------
void ServMgr::checkFirewall()
{
	if ((getFirewall() == FW_UNKNOWN) && !servMgr->rootHost.isEmpty())
	{

		LOG_DEBUG("Checking firewall..");
		Host host;
		host.fromStrName(servMgr->rootHost.cstr(),DEFAULT_PORT);

		ClientSocket *sock = sys->createSocket();
		if (!sock)
			throw StreamException("Unable to create socket");
		sock->setReadTimeout(30000);
		sock->open(host);
		sock->connect();

		AtomStream atom(*sock);

		atom.writeInt(PCP_CONNECT,1);

		GnuID remoteID;
		String agent;
		Servent::handshakeOutgoingPCP(atom,sock->host,remoteID,agent);

		atom.writeInt(PCP_QUIT,0);

		sock->close();
		delete sock;
	}
}
// -----------------------------------void ServMgr::setFirewall(FW_STATE state){	if (firewalled != state)	{		char *str;		switch (state)		{			case FW_ON:				str = "ON";				break;			case FW_OFF:				str = "OFF";				break;			case FW_UNKNOWN:			default:				str = "UNKNOWN";				break;		}		LOG_DEBUG("Firewall is set to %s",str);		firewalled = state;	}}// -----------------------------------bool ServMgr::isFiltered(int fl, Host &h){	for(int i=0; i<numFilters; i++)		if (filters[i].flags & fl)			if (h.isMemberOf(filters[i].host))				return true;	return false;}#if 0// -----------------------------------bool ServMgr::canServeHost(Host &h){	if (server)	{		Host sh = server->getHost();		if (sh.globalIP() || (sh.localIP() && h.localIP()))			return true;			}	return false;}#endif// --------------------------------------------------void writeServerSettings(IniFile &iniFile, unsigned int a){	iniFile.writeBoolValue("allowHTML",a & Servent::ALLOW_HTML);	iniFile.writeBoolValue("allowData",a & Servent::ALLOW_DATA);	iniFile.writeBoolValue("allowServent",a & Servent::ALLOW_SERVENT);	iniFile.writeBoolValue("allowBroadcast",a & Servent::ALLOW_BROADCAST);}// --------------------------------------------------void writeFilterSettings(IniFile &iniFile, ServFilter &f){	char ipstr[64];	f.host.IPtoStr(ipstr);	iniFile.writeStrValue("ip",ipstr);	iniFile.writeBoolValue("private",f.flags & ServFilter::F_PRIVATE);	iniFile.writeBoolValue("ban",f.flags & ServFilter::F_BAN);	iniFile.writeBoolValue("network",f.flags & ServFilter::F_NETWORK);	iniFile.writeBoolValue("direct",f.flags & ServFilter::F_DIRECT);}// --------------------------------------------------static void  writeServHost(IniFile &iniFile, ServHost &sh){	iniFile.writeSection("Host");	char ipStr[64];	sh.host.toStr(ipStr);	iniFile.writeStrValue("type",ServHost::getTypeStr(sh.type));	iniFile.writeStrValue("address",ipStr);	iniFile.writeIntValue("time",sh.time);	iniFile.writeLine("[End]");}// --------------------------------------------------bool ServMgr::writeVariable(Stream &out, const String &var){	char buf[128];	String str;	if (var == "version")		strcpy(buf,HTML_VERSTRING);	else if (var == "ip")		serverHost.IPtoStr(buf);	else if (var == "uptime")	{		str.setFromStopwatch(getUptime());		str.convertTo(String::T_HTML);		strcpy(buf,str.cstr());	}else if (var == "totalStreams")		sprintf(buf,"%d",numStreams(false),numStreams(true));	else if (var == "totalConnected")		sprintf(buf,"%d",totalConnected());//	else if (var == "numOutgoing")//		sprintf(buf,"%d",numConnected(Servent::T_OUTGOING));//	else if (var == "numIncoming")//		sprintf(buf,"%d",numConnected(Servent::T_INCOMING));			else if (var == "numServHosts")		sprintf(buf,"%d",numHosts(ServHost::T_SERVENT));			else		return false;	out.writeString(buf);	return true;}// --------------------------------------------------void ServMgr::saveSettings(const char *fn){	IniFile iniFile;	if (!iniFile.openWriteReplace(fn))	{		LOG_ERROR("Unable to open ini file");	}else{		LOG_DEBUG("Saving settings to:  %s",fn);		char idStr[64];		iniFile.writeSection("Server");		iniFile.writeIntValue("serverPort",servMgr->serverHost.port);		iniFile.writeBoolValue("autoServe",servMgr->autoServe);		iniFile.writeStrValue("forceIP",servMgr->forceIP);		iniFile.writeBoolValue("isRoot",servMgr->isRoot);		iniFile.writeIntValue("maxBitrate",servMgr->maxBitrate);		iniFile.writeIntValue("maxStreams",servMgr->maxStreams);		iniFile.writeIntValue("maxStreamsPerChannel",chanMgr->maxStreamsPerChannel);		iniFile.writeIntValue("firewallTimeout",firewallTimeout);		iniFile.writeBoolValue("forceNormal",forceNormal);		iniFile.writeStrValue("rootMsg",rootMsg.cstr());		iniFile.writeStrValue("authType",servMgr->authType==ServMgr::AUTH_COOKIE?"cookie":"http-basic");		iniFile.writeStrValue("cookiesExpire",servMgr->cookieList.neverExpire==true?"never":"session");		networkID.toStr(idStr);		iniFile.writeStrValue("networkID",idStr);

		iniFile.writeSection("Broadcast");
		iniFile.writeIntValue("broadcastMsgInterval",chanMgr->broadcastMsgInterval);
		iniFile.writeStrValue("broadcastMsg",chanMgr->broadcastMsg.cstr());
		iniFile.writeIntValue("icyMetaInterval",chanMgr->icyMetaInterval);
		chanMgr->broadcastID.toStr(idStr);
		iniFile.writeStrValue("broadcastID",idStr);		
		iniFile.writeIntValue("hostUpdateInterval",chanMgr->hostUpdateInterval);
		iniFile.writeIntValue("maxControlConnections",servMgr->maxControl);
		iniFile.writeStrValue("rootHost",servMgr->rootHost.cstr());
		iniFile.writeSection("Client");		iniFile.writeIntValue("refreshHTML",refreshHTML);		iniFile.writeIntValue("relayBroadcast",servMgr->relayBroadcast);		iniFile.writeIntValue("minBroadcastTTL",chanMgr->minBroadcastTTL);		iniFile.writeIntValue("maxBroadcastTTL",chanMgr->maxBroadcastTTL);		iniFile.writeIntValue("pushTries",chanMgr->pushTries);		iniFile.writeIntValue("pushTimeout",chanMgr->pushTimeout);		iniFile.writeIntValue("maxPushHops",chanMgr->maxPushHops);		iniFile.writeIntValue("autoQuery",chanMgr->autoQuery);		iniFile.writeIntValue("queryTTL",servMgr->queryTTL);
		iniFile.writeSection("Privacy");		iniFile.writeStrValue("password",servMgr->password);		iniFile.writeIntValue("maxUptime",chanMgr->maxUptime);				int i;		for(i=0; i<servMgr->numFilters; i++)		{			iniFile.writeSection("Filter");				writeFilterSettings(iniFile,servMgr->filters[i]);			iniFile.writeLine("[End]");		}		iniFile.writeSection("Notify");			iniFile.writeBoolValue("PeerCast",notifyMask&NT_PEERCAST);			iniFile.writeBoolValue("Broadcasters",notifyMask&NT_BROADCASTERS);			iniFile.writeBoolValue("TrackInfo",notifyMask&NT_TRACKINFO);		iniFile.writeLine("[End]");		iniFile.writeSection("Server1");			writeServerSettings(iniFile,allowServer1);		iniFile.writeLine("[End]");		iniFile.writeSection("Server2");			writeServerSettings(iniFile,allowServer2);		iniFile.writeLine("[End]");		iniFile.writeSection("Debug");		iniFile.writeBoolValue("logDebug",(showLog&(1<<LogBuffer::T_DEBUG))!=0);		iniFile.writeBoolValue("logErrors",(showLog&(1<<LogBuffer::T_ERROR))!=0);		iniFile.writeBoolValue("logNetwork",(showLog&(1<<LogBuffer::T_NETWORK))!=0);		iniFile.writeBoolValue("logChannel",(showLog&(1<<LogBuffer::T_CHANNEL))!=0);		iniFile.writeBoolValue("pauseLog",pauseLog);		iniFile.writeIntValue("idleSleepTime",sys->idleSleepTime);		for(i=0; i<ChanMgr::MAX_CHANNELS; i++)		{			char idstr[64];			Channel *c = &chanMgr->channels[i];			if (c->isActive() && c->stayConnected)			{				c->getIDStr(idstr);				iniFile.writeSection("RelayChannel");				iniFile.writeStrValue("name",c->getName());				iniFile.writeStrValue("genre",c->info.genre.cstr());				if (!c->sourceURL.isEmpty())					iniFile.writeStrValue("sourceURL",c->sourceURL.cstr());				iniFile.writeStrValue("sourceProtocol",ChanInfo::getProtocolStr(c->info.srcProtocol));				iniFile.writeStrValue("contentType",ChanInfo::getTypeStr(c->info.contentType));				iniFile.writeIntValue("bitrate",c->info.bitrate);				iniFile.writeStrValue("contactURL",c->info.url.cstr());				iniFile.writeStrValue("id",idstr);				iniFile.writeBoolValue("stayConnected",c->stayConnected);


				ChanHitList *chl = chanMgr->findHitList(c->info);
				if (chl)
				{
					ChanHit best = chl->getHit(NULL,0,false,true);
					if (best.host.ip)
					{
						char ipStr[64];
						best.host.toStr(ipStr);
						iniFile.writeStrValue("tracker",ipStr);
					}
				}
				iniFile.writeLine("[End]");			}		}		#if 0		Servent *s = servents;		while (s)		{			if (s->type == Servent::T_OUTGOING)				if (s->isConnected())
				{					ServHost sh;					Host h = s->getHost();					sh.init(h,ServHost::T_SERVENT,0,s->networkID);					writeServHost(iniFile,sh);				}			s=s->next;		}#endif
		for(i=0; i<ServMgr::MAX_HOSTCACHE; i++)		{			ServHost *sh = &servMgr->hostCache[i];			if (sh->type != ServHost::T_NONE)				writeServHost(iniFile,*sh);		}		iniFile.close();	}}// --------------------------------------------------unsigned int readServerSettings(IniFile &iniFile, unsigned int a){	while (iniFile.readNext())	{		if (iniFile.isName("[End]"))			break;		else if (iniFile.isName("allowHTML"))			a = iniFile.getBoolValue()?a|Servent::ALLOW_HTML:a&~Servent::ALLOW_HTML;		else if (iniFile.isName("allowData"))			a = iniFile.getBoolValue()?a|Servent::ALLOW_DATA:a&~Servent::ALLOW_DATA;		else if (iniFile.isName("allowServent"))			a = iniFile.getBoolValue()?a|Servent::ALLOW_SERVENT:a&~Servent::ALLOW_SERVENT;		else if (iniFile.isName("allowBroadcast"))			a = iniFile.getBoolValue()?a|Servent::ALLOW_BROADCAST:a&~Servent::ALLOW_BROADCAST;	}	return a;}// --------------------------------------------------void readFilterSettings(IniFile &iniFile, ServFilter &sv){	sv.host.init();	while (iniFile.readNext())	{		if (iniFile.isName("[End]"))			break;		else if (iniFile.isName("ip"))			sv.host.fromStrIP(iniFile.getStrValue(),0);		else if (iniFile.isName("private"))			sv.flags = (sv.flags & ~ServFilter::F_PRIVATE) | (iniFile.getBoolValue()?ServFilter::F_PRIVATE:0);		else if (iniFile.isName("ban"))			sv.flags = (sv.flags & ~ServFilter::F_BAN) | (iniFile.getBoolValue()?ServFilter::F_BAN:0);		else if (iniFile.isName("allow") || iniFile.isName("network"))			sv.flags = (sv.flags & ~ServFilter::F_NETWORK) | (iniFile.getBoolValue()?ServFilter::F_NETWORK:0);		else if (iniFile.isName("direct"))			sv.flags = (sv.flags & ~ServFilter::F_DIRECT) | (iniFile.getBoolValue()?ServFilter::F_DIRECT:0);	}}// --------------------------------------------------void ServMgr::loadSettings(const char *fn){	IniFile iniFile;	if (!iniFile.openReadOnly(fn))		saveSettings(fn);	servMgr->numFilters = 0;	showLog = 0;	if (iniFile.openReadOnly(fn))	{		while (iniFile.readNext())		{			// server settings			if (iniFile.isName("serverPort"))				servMgr->serverHost.port = iniFile.getIntValue();			else if (iniFile.isName("autoServe"))				servMgr->autoServe = iniFile.getBoolValue();			else if (iniFile.isName("autoConnect"))				servMgr->autoConnect = iniFile.getBoolValue();			else if (iniFile.isName("icyPassword"))		// depreciated				strcpy(servMgr->password,iniFile.getStrValue());			else if (iniFile.isName("forceIP"))			{				strcpy(servMgr->forceIP,iniFile.getStrValue());				if (strlen(servMgr->forceIP))					servMgr->serverHost.ip = ClientSocket::getIP(servMgr->forceIP);			}			else if (iniFile.isName("isRoot"))				servMgr->isRoot = iniFile.getBoolValue();			else if (iniFile.isName("broadcastID"))				chanMgr->broadcastID.fromStr(iniFile.getStrValue());

			else if (iniFile.isName("maxControlConnections"))
			{
				servMgr->maxControl = iniFile.getIntValue();

			}			else if (iniFile.isName("maxBitrate"))				servMgr->maxBitrate = iniFile.getIntValue();			else if (iniFile.isName("maxStreams"))				servMgr->maxStreams = iniFile.getIntValue();			else if (iniFile.isName("maxStreamsPerChannel"))				chanMgr->maxStreamsPerChannel = iniFile.getIntValue();			else if (iniFile.isName("firewallTimeout"))				firewallTimeout = iniFile.getIntValue();			else if (iniFile.isName("forceNormal"))				forceNormal = iniFile.getBoolValue();			else if (iniFile.isName("broadcastMsgInterval"))				chanMgr->broadcastMsgInterval = iniFile.getIntValue();			else if (iniFile.isName("broadcastMsg"))				chanMgr->broadcastMsg.set(iniFile.getStrValue(),String::T_ASCII);			else if (iniFile.isName("hostUpdateInterval"))
				chanMgr->hostUpdateInterval = iniFile.getIntValue();
			else if (iniFile.isName("icyMetaInterval"))				chanMgr->icyMetaInterval = iniFile.getIntValue();
			else if (iniFile.isName("rootMsg"))				rootMsg.set(iniFile.getStrValue());			else if (iniFile.isName("networkID"))				networkID.fromStr(iniFile.getStrValue());			else if (iniFile.isName("authType"))			{				char *t = iniFile.getStrValue();				if (stricmp(t,"cookie")==0)					servMgr->authType = ServMgr::AUTH_COOKIE;				else if (stricmp(t,"http-basic")==0)					servMgr->authType = ServMgr::AUTH_HTTPBASIC;			}else if (iniFile.isName("cookiesExpire"))			{				char *t = iniFile.getStrValue();				if (stricmp(t,"never")==0)					servMgr->cookieList.neverExpire = true;				else if (stricmp(t,"session")==0)					servMgr->cookieList.neverExpire = false;			}			// privacy settings			else if (iniFile.isName("password"))				strcpy(servMgr->password,iniFile.getStrValue());			else if (iniFile.isName("maxUptime"))				chanMgr->maxUptime = iniFile.getIntValue();			// client settings			else if (iniFile.isName("rootHost"))
				servMgr->rootHost = iniFile.getStrValue();			else if (iniFile.isName("deadHitAge"))				chanMgr->deadHitAge = iniFile.getIntValue();			else if (iniFile.isName("tryoutDelay"))				servMgr->tryoutDelay = iniFile.getIntValue();			else if (iniFile.isName("refreshHTML"))				refreshHTML = iniFile.getIntValue();			else if (iniFile.isName("relayBroadcast"))			{				servMgr->relayBroadcast = iniFile.getIntValue();				if (servMgr->relayBroadcast < 30)					servMgr->relayBroadcast = 30;			}			else if (iniFile.isName("minBroadcastTTL"))				chanMgr->minBroadcastTTL = iniFile.getIntValue();			else if (iniFile.isName("maxBroadcastTTL"))				chanMgr->maxBroadcastTTL = iniFile.getIntValue();			else if (iniFile.isName("pushTimeout"))				chanMgr->pushTimeout = iniFile.getIntValue();			else if (iniFile.isName("pushTries"))				chanMgr->pushTries = iniFile.getIntValue();			else if (iniFile.isName("maxPushHops"))				chanMgr->maxPushHops = iniFile.getIntValue();			else if (iniFile.isName("autoQuery"))			{				chanMgr->autoQuery = iniFile.getIntValue();				if ((chanMgr->autoQuery < 300) && (chanMgr->autoQuery > 0))					chanMgr->autoQuery = 300;			}			else if (iniFile.isName("queryTTL"))			{				servMgr->queryTTL = iniFile.getIntValue();			}			// debug			else if (iniFile.isName("logDebug"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_DEBUG:0;			else if (iniFile.isName("logErrors"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_ERROR:0;			else if (iniFile.isName("logNetwork"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_NETWORK:0;			else if (iniFile.isName("logChannel"))				showLog |= iniFile.getBoolValue() ? 1<<LogBuffer::T_CHANNEL:0;			else if (iniFile.isName("pauseLog"))				pauseLog = iniFile.getBoolValue();			else if (iniFile.isName("idleSleepTime"))				sys->idleSleepTime = iniFile.getIntValue();			else if (iniFile.isName("[Server1]"))				allowServer1 = readServerSettings(iniFile,allowServer1);			else if (iniFile.isName("[Server2]"))				allowServer2 = readServerSettings(iniFile,allowServer2);			else if (iniFile.isName("[Filter]"))			{				readFilterSettings(iniFile,filters[numFilters]);				if (numFilters < (MAX_FILTERS-1))					numFilters++;			}			else if (iniFile.isName("[Notify]"))			{				notifyMask = NT_UPGRADE;				while (iniFile.readNext())				{					if (iniFile.isName("[End]"))						break;					else if (iniFile.isName("PeerCast"))						notifyMask |= iniFile.getBoolValue()?NT_PEERCAST:0;					else if (iniFile.isName("Broadcasters"))						notifyMask |= iniFile.getBoolValue()?NT_BROADCASTERS:0;					else if (iniFile.isName("TrackInfo"))						notifyMask |= iniFile.getBoolValue()?NT_TRACKINFO:0;				}			}			else if (iniFile.isName("[RelayChannel]"))			{				ChanInfo info;				bool stayConnected=false;				String sourceURL;				while (iniFile.readNext())				{					if (iniFile.isName("[End]"))						break;					else if (iniFile.isName("name"))						info.name.set(iniFile.getStrValue());					else if (iniFile.isName("id"))						info.id.fromStr(iniFile.getStrValue());					else if (iniFile.isName("sourceType"))						info.srcProtocol = ChanInfo::getProtocolFromStr(iniFile.getStrValue());					else if (iniFile.isName("contentType"))						info.contentType = ChanInfo::getTypeFromStr(iniFile.getStrValue());					else if (iniFile.isName("stayConnected"))						stayConnected = iniFile.getBoolValue();					else if (iniFile.isName("sourceURL"))						sourceURL.set(iniFile.getStrValue());					else if (iniFile.isName("genre"))						info.genre.set(iniFile.getStrValue());					else if (iniFile.isName("contactURL"))						info.url.set(iniFile.getStrValue());					else if (iniFile.isName("bitrate"))						info.bitrate = atoi(iniFile.getStrValue());					else if (iniFile.isName("tracker"))
					{
						ChanHit hit;
						hit.init();
						hit.tracker = true;
						hit.host.fromStrName(iniFile.getStrValue(),DEFAULT_PORT);
						hit.rhost[0] = hit.host;
						hit.rhost[1] = hit.host;
						chanMgr->addHit(info.id,hit);
					}
				}				if (sourceURL.isEmpty())				{					chanMgr->createRelay(info,stayConnected);				}else				{					Channel *c = chanMgr->createChannel(info,NULL);					if (c)						c->startURL(sourceURL.cstr());				}			} else if (iniFile.isName("[Host]"))			{				Host h;				ServHost::TYPE type=ServHost::T_NONE;
				bool firewalled=false;				unsigned int time=0;				while (iniFile.readNext())				{					if (iniFile.isName("[End]"))						break;					else if (iniFile.isName("address"))						h.fromStrIP(iniFile.getStrValue(),DEFAULT_PORT);					else if (iniFile.isName("type"))						type = ServHost::getTypeFromStr(iniFile.getStrValue());					else if (iniFile.isName("time"))						time = iniFile.getIntValue();				}				servMgr->addHost(h,type,time);			}		}	}	if (!numFilters)		setFilterDefaults();}
// --------------------------------------------------
unsigned int ServMgr::numStreams(GnuID &cid, ChanInfo::PROTOCOL proto)
{
	int cnt = 0;
	Servent *sv = servents;
	while (sv)
	{
		if (sv->isConnected())

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -