?? tracker.cpp
字號:
DEBUG("%s\n", "write is set");
}
if(T_FREE == m_stat_list[i])
{
DEBUG("%s\n", "state : T_FREE");
ret = Connect(i);
if(-2 == ret)
{
printf("\tHost: %s is not available\n", m_host_list[i]);
CloseSocket(i);
}
else if(ret == -1)
{
DEBUG("\t%s\n", "socket is being connected");
m_stat_list[i] = T_CONNECTING;
FD_SET(m_sock_list[i], next_rset);
FD_SET(m_sock_list[i], next_wset);
}
else
{
if(0 == (ret = SendRequest(i, 0))) //send
{
DEBUG("\t%s\n", "socket has been connected");
m_stat_list[i] = T_READY;
FD_SET(m_sock_list[i], next_rset);
}
else
{
close(m_sock_list[i]);
m_stat_list[i] = T_FREE;
continue;
/*
DEBUG("\tHost : %s socket is being canceled\n", m_host_list[i]);
CloseSocket(i);*/
}
}
}
else if(INVALID_SOCK != m_sock_list[i]
&& FD_ISSET(m_sock_list[i], wset)
&& m_stat_list[i] == T_CONNECTING)
{
int error;
socklen_t len;
DEBUG("%s\n", "state : T_CONNECTING");
len = sizeof(error);
FD_CLR(m_sock_list[i], wset);
if(FD_ISSET(m_sock_list[i], rset))
{
FD_CLR(m_sock_list[i], rset);
(*nfds)--;
}
(*nfds)--;
if(getsockopt(m_sock_list[i], SOL_SOCKET, SO_ERROR, &error, &len)< 0
|| error != 0)
{
DEBUG("\t%s\n", "error is occured");
CloseSocket(i);
}
else if(0 == SendRequest(i, 0)) //send immediately
{
DEBUG("\t%s\n", "request has been sent out");
FD_SET(m_sock_list[i], next_rset);
m_stat_list[i] = T_READY;
}
else
{
close(m_sock_list[i]);
m_stat_list[i] = T_FREE;
continue;
/*
DEBUG("\t%s\n", "has been disconnected");
CloseSocket(i);*/
}
}
else if(INVALID_SOCK != m_sock_list[i]
&& m_stat_list[i] == T_READY && FD_ISSET(m_sock_list[i], rset))
{
int error;
socklen_t len;
len = sizeof(error);
(*nfds)--;
FD_CLR(m_sock_list[i], rset);
DEBUG("%s\n", "state : T_READY");
if(getsockopt(m_sock_list[i], SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error != 0)
{
DEBUG("\t%s\n", "error is occured\n");
CloseSocket(i);
}
else
{
if(-1 ==(n = CheckResponse(i))) //need reconnect
{
DEBUG("\t%s\n", "has been redirected");
m_stat_list[i] = T_FREE;
close(m_sock_list[i]);
continue;
}
else if(0 == n) //read successfully
{
DEBUG("\t%s\n", "read successfully");
FD_SET(m_sock_list[i], next_rset);
}
else
{
DEBUG("\t%s\n", "has been closed");
CloseSocket(i);
}
}
}
else if(T_CONNECTED != m_stat_list[i]
&&(FD_ISSET(m_sock_list[i], rset) || FD_ISSET(m_sock_list[i], wset)))
{
DEBUG("%s\n", "state : abnormal");
CloseSocket(i); // abnormally close socket
}
if(T_CONNECTED != m_stat_list[i])
{
nactive++;
maxfd = (m_sock_list[i] > maxfd) ? m_sock_list[i] : maxfd;
}
i++;
cout << endl << endl;
}
for(i = 0; i < m_tracker_count; i++)
{
if(m_stat_list[i] != T_CONNECTED)
cout << "socket is read && write"<<endl;
else
cout<< "socket has been cancled"<<endl;
}
cout<<"active: "<<nactive<<endl;
return nactive ? maxfd : -1;
}
int btTracker::CheckResponse(int idx)
{
char read_buffer[MAXPATHNAMELEN], *pdata, *p;
int r;
size_t dlen, hlen;
r = read(m_sock_list[idx], read_buffer, 1023);
if(r <= 0)
return -2;
read_buffer[r] = '\0';
hlen = Http_split(read_buffer, r, &pdata, &dlen);
if (hlen <= 0)
return -2;
r = Http_reponse_code(read_buffer, hlen);
if (r <= 0)
return -2;
else if (r != 200) {
if (r == 301 || r == 302) {
char redirect[MAXHOSTNAMELEN];
if (Http_get_header(read_buffer, hlen,
"Location", redirect) < 0)
{
DEBUG("%s\n", "get http header error!");
return -2;
}
if (Http_url_analyse(redirect, m_host, (int*)&m_port, m_path)
< 0)
{
DEBUG("%s\n", "analyze the url error!");
return -2;
}
delete [] m_host_list[idx];
delete [] m_path_list[idx];
if(NULL != (p = strchr(m_path, '?')))
*p++ = '\0';
if(NULL == (m_host_list[idx] = new char [strlen(m_host) + 1])
|| NULL == (m_path_list[idx] = new char[strlen(m_path) + 1]))
delete [] m_host_list[idx];
strcpy(m_host_list[idx], m_host);
strcpy(m_path_list[idx], m_path);
m_port_list[idx] = m_port;
printf("%s\n", m_host_list[idx]);
printf("%s\n", m_path_list[idx]);
printf("%d\n", m_port_list[idx]);
return -1;
}
else if (r >= 400)
{
DEBUG("%s\n", "the file has been deleted or removed");
return -2;
}
}
return UpdatePeerList(pdata, dlen) == -1 ? -2 : 0;
}
int btTracker::UpdatePeerList(char* buffer, size_t len)
{
BencodeDict* pdict = NULL;
Bencode* pcode;
std::string keylist;
char *p = NULL;
int n, ret = -1;
if((NULL == (pdict = new BencodeDict)) || !(pdict->Decode(buffer, len + 1)))
{
DEBUG("%s\n", "Create dictionary error!\n");
delete pdict;
return -1;
}
keylist = "failure reason";
if(search_dictionary(pdict, keylist, &p, NULL))
{
DEBUG("錯誤 : %s\n", p);
delete []p;
return -1;
}
keylist = "warning message";
if(search_dictionary(pdict, keylist, &p, NULL))
{
DEBUG("警告 : %s\n", p);
delete []p;
}
keylist = "tracker id";
if(search_dictionary(pdict, keylist, &p, NULL))
{
if(strlen(p) == 20)
memcpy(m_tracker_id, p, 20);
delete []p;
}
keylist = "interval";
if(search_dictionary(pdict, keylist, NULL, &n))
m_conn_interval = n;
keylist = "done peers";
if(search_dictionary(pdict, keylist, NULL, &n))
m_seeds_count = n;
keylist = "complete";
if(search_dictionary(pdict, keylist, NULL, &n))
m_seeds_count = n;
keylist = "incomplete";
if(search_dictionary(pdict, keylist, NULL, &n))
m_peers_count = m_seeds_count + n;
keylist = "num peers";
if(search_dictionary(pdict, keylist, NULL, &n))
m_peers_count = n;
keylist = "peers";
if( NULL == (pcode = query_dict(pdict, keylist)) || GetPeerInfo(pcode) <= 0)
return -1;
delete_dictionary(pdict);
return ret;
}
int btTracker::GetPeerInfo(Bencode* p)
{
printf("begining to analyze peers\n");
int n = 0;
if(bencode_str == p->m_enumType) //compact mode
{
const char* dt;
struct in_addr adr;
n = ((BencodeString*)p)->m_strValue.length();
if(0 != (n % 6))
return -1;
n = n / 6;
printf("peer number is : %d\n", n);
dt = ((BencodeString*)p)->m_strValue.data();
int fd = open("txt", O_RDWR);
write(fd, dt, ((BencodeString*)p)->m_strValue.length());
for(int i = 0; i < n; i++)
{
unsigned char ip[4];
unsigned short port;
memcpy(&ip, dt, 4);
dt += 4;
memcpy(&port, dt, 2);
dt +=2;
port = ntohs(port);
printf("%u.%u.%u.%u\n", ip[0],ip[1],ip[2],ip[3]);
printf("peer port : %d\n", port);
}
return n;
}
else if(bencode_list == p->m_enumType) //normal mode
{
vector<Bencode*>::iterator iter, iter_end;
iter = ((BencodeList*)p)->m_list.begin();
iter_end =((BencodeList*)p)->m_list.end();
for(; iter != iter_end; iter++)
{
char*ip;
int port;
string keylist;
BencodeDict* pdict;
pdict =(BencodeDict*) (*iter);
if(search_dictionary(pdict, keylist, &ip, NULL))
{
printf("peer ip : %s\n", ip);
delete [] ip;
}
if(search_dictionary(pdict, keylist, NULL, &port))
{
printf("peer port : %d\n", port);
}
n++;
}
}
return n;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -