findcp.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:5k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include "findcp.h"
- list<IPInfo> ipinfo_list;
- list<struct MySession> sessioninfo_list;
- #ifdef __cplusplus
- extern "C"{
- #endif
- int init()
- {
- int ret = readconfig("ip.list");
- return ret;
- }
- int readconfig(char * _filename)
- {
- if(_filename == NULL)
- return -1;
- string filename = string(_filename);
- ifstream file(filename.c_str());
- string line;
- while(1)
- {
- getline(file, line, 'n');
- if(strlen(line.c_str()) == 0 || strcmp(line.c_str(), "r") == 0)
- {
- break;
- }
- IPInfo ipinfo;
- int pos = line.find("/", 0);
- int blank = line.find(" ", 0);
- ipinfo.mask = atoi(line.substr(pos+1, blank).c_str());
- ipinfo.ipaddr = ntohl(inet_addr(line.substr(0, pos).c_str())) >> ipinfo.mask;
- ipinfo.nettype = line.substr(blank+1, strlen(line.c_str())-blank-1);
- if(ipinfo_list.size() == 0)
- {
- ipinfo_list.push_back(ipinfo);
- continue;
- }
- list<IPInfo>::iterator iter = ipinfo_list.begin();
- for(; iter != ipinfo_list.end(); iter ++)
- {
- if(ipinfo.ipaddr < (*iter).ipaddr)
- {
- ipinfo_list.insert(iter, ipinfo);
- break;
- }
- }
- if(iter == ipinfo_list.end())
- ipinfo_list.push_back(ipinfo);
- }
- if(ipinfo_list.size() == 0)
- return -1;
- return 0;
- }
- const char* find_cp_service_type(unsigned long ip)
- {
- const char * retchar = find_ip_from_list(ip);
- if(retchar == NULL)
- {
- PDEBUG("find_cp_service_type: Cant find cp service type. ip=%ld ret=%sn",ip,retchar);
- return NULL;
- }
- string servicetype = string(retchar);
- int pos = servicetype.rfind(".");
- if(pos != -1)
- servicetype = servicetype.substr(pos+1, servicetype.length()-pos-1);
- PDEBUG("find_cp_service_type: return %sn", servicetype.c_str());
- return servicetype.c_str();
- }
- const char* find_ip_from_list(unsigned long ip)
- {
- // PDEBUG("in find_ip_from_listn");
- unsigned long ipaddr = ip;
- // unsigned long ipaddr = htonl(inet_addr(ip.c_str()));
- for(list<IPInfo>::iterator iter = ipinfo_list.begin(); iter != ipinfo_list.end(); iter ++)
- {
- unsigned long ipcode = ipaddr >> (*iter).mask;
- // PDEBUG("ip: %ld mask: %d result: %ldn", ip, (*iter).mask, ipcode);
- if(ipcode == (*iter).ipaddr)
- {
- return (*iter).nettype.c_str();
- }
- }
- return NULL;
- }
- int findcppeers(unsigned long ip, void * _p)
- {
- MySession *p = (MySession*)_p;
- const char * retchar = find_ip_from_list(ip);
- if(retchar == NULL)
- {
- PDEBUG("findcppeers: Cant find cp service type. ip=%ld ret=%sn", ip, retchar);
- return -1;
- }
- else
- {
- PDEBUG("findcppeers: found %sn", retchar);
- }
- string nettype = string(retchar);
- int pos = 0;
- bool bfound = 0;
- if(strlen(p->u.cp.servicetype) == 0)
- return -1;
- return findnettype(nettype.c_str(), p);
- }
- int findnettype(const char* _nettype, void *_p)
- {
- MySession *p = (MySession*)_p;
- if(_nettype == NULL)
- return -1;
- string nettype = string(_nettype);
- int minpriority = 100;
- //check if the cp serves the given nettype
- string servicetype = string(p->u.cp.servicetype);
- int pos = servicetype.find(nettype);
- if(pos == -1)
- return -1;
- if(p->u.cp.maxConn == 1 || p->socket == 0)//无效的CP
- return -1;
- int priority = 0;
- pos = 0;
- //deal with priority
- while(pos != -1)
- {
- priority ++;//sequence in servicetype string
- int dotpos = servicetype.find(".", pos);
- string temp = servicetype.substr(pos, dotpos - pos);
- if(strcmp(temp.c_str(), nettype.c_str()) == 0)
- break;
- pos = servicetype.find(nettype, dotpos);
- }
- return priority;
- }
- void add_cp_to_list(void *_p)
- {
- MySession *p = (MySession *)_p;
- sessioninfo_list.push_back(*p);
- }
- void remove_cp_from_list(void *_p)
- {
- MySession *p = (MySession*)_p;
- list<MySession>::iterator iter = sessioninfo_list.begin();
- for(; iter != sessioninfo_list.end(); iter++)
- {
- if(static_cast<MySession>(*iter).host == p->host)
- {
- sessioninfo_list.erase(iter);
- //sessioninfo_list.remove(static_cast<CPInfo>(*iter));
- break;
- }
- }
- }
- #ifdef __cplusplus
- }
- #endif