diff -ur ctorrent-1.3.4/peer.cpp new/peer.cpp --- ctorrent-1.3.4/peer.cpp 2004-09-09 00:10:51.000000000 +0100 +++ new/peer.cpp 2005-01-25 01:23:51.000000000 +0000 @@ -3,11 +3,32 @@ #include #include +#include "btstream.h" #include "./btcontent.h" #include "./msgencode.h" #include "./peerlist.h" #include "./btconfig.h" +size_t get_nl(char *sfrom) +{ + unsigned char *from = (unsigned char *)sfrom; + size_t t; + t = (*from++) << 24; + t |= (*from++) << 16; + t |= (*from++) << 8; + t |= *from; + return t; +} + +void set_nl(char *sto, size_t from) +{ + unsigned char *to = (unsigned char *)sto; + *to++ = (from >> 24) & 0xff; + *to++ = (from >> 16) & 0xff; + *to++ = (from >> 8) & 0xff; + *to = from & 0xff; +} + btBasic Self; void btBasic::SetIp(struct sockaddr_in addr) @@ -152,7 +173,8 @@ char *msgbuf = stream.in_buffer.BasePointer(); - r = ntohl(*(size_t*) msgbuf); + r = get_nl(msgbuf); + if( 0 == r ){ time(&m_last_timestamp); @@ -193,7 +215,7 @@ case M_HAVE: if(H_HAVE_LEN != r){return -1;} - idx = ntohl(*(size_t*) (msgbuf + 5)); + idx = get_nl(msgbuf + 5); if( idx >= BTCONTENT.GetNPieces() || bitfield.IsSet(idx)) return -1; @@ -208,12 +230,12 @@ case M_REQUEST: if(H_REQUEST_LEN != r || !m_state.remote_interested){ return -1; } - idx = ntohl(*(size_t*)(msgbuf + 5)); + idx = get_nl(msgbuf + 5); if( !BTCONTENT.pBF->IsSet(idx) ) return -1; - off = ntohl(*(size_t*)(msgbuf + 9)); - len = ntohl(*(size_t*)(msgbuf + 13)); + off = get_nl(msgbuf + 9); + len = get_nl(msgbuf + 13); if( !reponse_q.IsValidRequest(idx, off, len) ) return -1; @@ -235,9 +257,9 @@ case M_CANCEL: if(r != H_CANCEL_LEN || !m_state.remote_interested) return -1; - idx = ntohl(*(size_t*)(msgbuf + 5)); - off = ntohl(*(size_t*)(msgbuf + 9)); - len = ntohl(*(size_t*)(msgbuf + 13)); + idx = get_nl(msgbuf + 5); + off = get_nl(msgbuf + 9); + len = get_nl(msgbuf + 13); if( reponse_q.Remove(idx,off,len) < 0 ){ m_err_count++; return 0; @@ -312,8 +334,8 @@ size_t idx,off,len; char *msgbuf = stream.in_buffer.BasePointer(); - idx = ntohl(*(size_t*) (msgbuf + 5)); - off = ntohl(*(size_t*) (msgbuf + 9)); + idx = get_nl(msgbuf + 5); + off = get_nl(msgbuf + 9); len = mlen - 9; if( request_q.Remove(idx,off,len) < 0 ){ diff -ur ctorrent-1.3.4/peer.h new/peer.h --- ctorrent-1.3.4/peer.h 2004-09-09 00:10:51.000000000 +0100 +++ new/peer.h 2005-01-25 01:23:01.000000000 +0000 @@ -34,6 +34,9 @@ unsigned char reserved:4; /* unused */ }BTSTATUS; +size_t get_nl(char *from); +void set_nl(char *to, size_t from); + class btBasic { private: diff -ur ctorrent/btcontent.cpp ctorrent.new/btcontent.cpp --- ctorrent/btcontent.cpp 2004-09-09 00:10:51.000000000 +0100 +++ ctorrent.new/btcontent.cpp 2005-02-03 01:32:24.000000000 +0000 @@ -226,6 +226,7 @@ if( m_btfiles.BuildFromMI(b, flen, saveas) < 0) ERR_RETURN(); delete []b; + b = (char *)0; PrintOut(); if( arg_flg_exam_only ) return 0; diff -ur ctorrent/iplist.cpp ctorrent.new/iplist.cpp --- ctorrent/iplist.cpp 2004-09-09 00:10:51.000000000 +0100 +++ ctorrent.new/iplist.cpp 2005-02-08 13:02:45.000000000 +0000 @@ -8,8 +8,8 @@ IPLIST *node = ipl_head; for(; ipl_head;){ node = ipl_head; - delete ipl_head; ipl_head = node->next; + delete node; } count = 0; } diff -ur new.x86/httpencode.h new/httpencode.h --- new.x86/httpencode.h 2004-09-09 00:10:51.000000000 +0100 +++ new/httpencode.h 2005-02-01 18:13:59.936139832 +0000 @@ -2,8 +2,8 @@ #define HTTPENCODE_H #define REQ_URL_P1_FMT "GET %s?info_hash=%s&peer_id=%s&port=%d" -#define REQ_URL_P2_FMT "%s&uploaded=%d&downloaded=%d&left=%d&event=%s&compact=1 HTTP/1.0" -#define REQ_URL_P3_FMT "%s&uploaded=%d&downloaded=%d&left=%d&compact=1 HTTP/1.0" +#define REQ_URL_P2_FMT "%s&uploaded=%llu&downloaded=%llu&left=%llu&event=%s&compact=1 HTTP/1.0" +#define REQ_URL_P3_FMT "%s&uploaded=%llu&downloaded=%llu&left=%llu&compact=1 HTTP/1.0" char* Http_url_encode(char *s,char *b,size_t n); int Http_url_analyse(char *url,char *host,int *port,char *path); diff -ur new.x86/tracker.cpp new/tracker.cpp --- new.x86/tracker.cpp 2005-02-01 17:34:43.588359144 +0000 +++ new/tracker.cpp 2005-02-01 18:14:58.632216672 +0000 @@ -360,18 +345,18 @@ if(event){ if(MAXPATHLEN < snprintf(REQ_BUFFER,MAXPATHLEN,REQ_URL_P2_FMT, m_path, - (size_t)Self.TotalUL(), - (size_t)Self.TotalDL(), - (size_t)BTCONTENT.GetLeftBytes(), + Self.TotalUL(), + Self.TotalDL(), + BTCONTENT.GetLeftBytes(), event)){ return -1; } }else{ if(MAXPATHLEN < snprintf(REQ_BUFFER,MAXPATHLEN,REQ_URL_P3_FMT, m_path, - (size_t)Self.TotalUL(), - (size_t)Self.TotalDL(), - (size_t)BTCONTENT.GetLeftBytes() + Self.TotalUL(), + Self.TotalDL(), + BTCONTENT.GetLeftBytes() )){ return -1; } --- ctorrent-1.3.4.orig/btfiles.cpp 2004-09-08 18:10:51.000000000 -0500 +++ ctorrent-1.3.4/btfiles.cpp 2005-02-10 17:27:55.000000000 -0600 @@ -169,7 +169,7 @@ DIR *dp; BTFILE *pbf; - if( !getwd(full_cur) ) return -1; + if( !getcwd(full_cur, MAXPATHLEN) ) return -1; if( cur_path ){ strcpy(fn, full_cur); @@ -293,7 +293,7 @@ m_btfhead = pbf; }else if( S_IFDIR & sb.st_mode ){ char wd[MAXPATHLEN]; - if( !getwd(wd) ) return -1; + if( !getcwd(wd, MAXPATHLEN) ) return -1; m_directory = new char[strlen(pathname) + 1]; #ifndef WINDOWS if( !m_directory ) return -1; --- ctorrent-1.3.4.orig/configure.ac 2004-09-08 18:10:51.000000000 -0500 +++ ctorrent-1.3.4/configure.ac 2005-02-10 17:28:03.000000000 -0600 @@ -32,6 +32,6 @@ AC_FUNC_MEMCMP AC_TYPE_SIGNAL AC_FUNC_STAT -AC_CHECK_FUNCS([ftruncate gethostbyname gettimeofday getwd inet_ntoa memchr memmove memset mkdir select socket strchr strerror strncasecmp strstr strtol strnstr]) +AC_CHECK_FUNCS([ftruncate gethostbyname gettimeofday getcwd inet_ntoa memchr memmove memset mkdir select socket strchr strerror strncasecmp strstr strtol strnstr]) AC_OUTPUT(Makefile) diff --exclude '*Po' -ur ctorrent/peer.cpp ctorrent.new/peer.cpp --- ctorrent/peer.cpp 2005-02-10 18:27:44.980091472 +0000 +++ ctorrent.new/peer.cpp 2005-02-03 17:55:01.000000000 +0000 @@ -252,7 +252,8 @@ if( (r - 1) != bitfield.NBytes() || !bitfield.IsEmpty()) return -1; bitfield.SetReferBuffer(msgbuf + 5); if(bitfield.IsFull() && BTCONTENT.pBF->IsFull()) return -2; - return 0; + + return RequestCheck(); case M_CANCEL: if(r != H_CANCEL_LEN || !m_state.remote_interested) return -1; diff --exclude '*Po' -ur ctorrent/peerlist.cpp ctorrent.new/peerlist.cpp --- ctorrent/peerlist.cpp 2004-09-09 00:10:51.000000000 +0100 +++ ctorrent.new/peerlist.cpp 2005-02-02 00:23:04.000000000 +0000 @@ -495,7 +496,9 @@ if(peer->GetStatus() == P_HANDSHAKE){ if( peer->HandShake() < 0 ) peer->CloseConnection(); }else{ - if( peer->RecvModule() < 0 ) peer->CloseConnection(); + if(peer->GetStatus() == P_SUCCESS) { + if( peer->RecvModule() < 0 ) peer->CloseConnection(); + } } }else if(PEER_IS_SUCCESS(peer) && FD_ISSET(sk,wfdp)){ p->click++; diff -ur ctorrent-1.3.4/ctorrent.cpp new/ctorrent.cpp --- ctorrent-1.3.4/ctorrent.cpp 2005-01-26 00:40:07.747876016 +0000 +++ new/ctorrent.cpp 2005-01-25 01:34:16.000000000 +0000 @@ -87,9 +87,13 @@ Tracker.Initial(); signal(SIGPIPE,SIG_IGN); - signal(SIGINT,sigint_catch); + signal(SIGINT,sig_catch); + signal(SIGTERM,sig_catch); Downloader(); } + if( cfg_cache_size ) BTCONTENT.FlushCache(); + if( arg_bitfield_file ) BTCONTENT.pBF->WriteToFile(arg_bitfield_file); + WORLD.CloseAll(); exit(0); } diff -ur ctorrent-1.3.4/downloader.cpp new/downloader.cpp --- ctorrent-1.3.4/downloader.cpp 2005-01-26 00:40:07.748875864 +0000 +++ new/downloader.cpp 2005-01-24 19:29:18.000000000 +0000 @@ -30,9 +30,9 @@ fd_set rfd; fd_set wfd; - for(;;){ + do{ time(&now); - if( BTCONTENT.SeedTimeout(&now) ) break; + if( BTCONTENT.SeedTimeout(&now) ) Tracker.SetStoped(); FD_ZERO(&rfd); FD_ZERO(&wfd); maxfd = Tracker.IntervalCheck(&now,&rfd, &wfd); @@ -48,5 +48,5 @@ if(T_FREE != Tracker.GetStatus()) Tracker.SocketReady(&rfd,&wfd,&nfds); if( nfds ) WORLD.AnyPeerReady(&rfd,&wfd,&nfds); } - }/* end for(;;) */ + } while(Tracker.GetStatus() != T_FINISHED); } diff -ur ctorrent-1.3.4/sigint.cpp new/sigint.cpp --- ctorrent-1.3.4/sigint.cpp 2005-01-26 00:40:07.749875712 +0000 +++ new/sigint.cpp 2005-01-26 00:39:48.175851416 +0000 @@ -4,17 +4,27 @@ #include #include "btcontent.h" +#include "tracker.h" #include "peerlist.h" #include "btconfig.h" +#include "sigint.h" -void sigint_catch(int sig_no) +void sig_catch(int sig_no) { - if(SIGINT == sig_no){ + if(SIGINT == sig_no || SIGTERM == sig_no){ + Tracker.SetStoped(); + signal(sig_no,sig_catch2); + } +} + +static void sig_catch2(int sig_no) +{ + if(SIGINT == sig_no || SIGTERM == sig_no){ if( cfg_cache_size ) BTCONTENT.FlushCache(); if( arg_bitfield_file ) BTCONTENT.pBF->WriteToFile(arg_bitfield_file); WORLD.CloseAll(); - signal(SIGINT,SIG_DFL); - raise(SIGINT); + signal(sig_no,SIG_DFL); + raise(sig_no); } } diff -ur ctorrent-1.3.4/sigint.h new/sigint.h --- ctorrent-1.3.4/sigint.h 2005-01-26 00:40:07.749875712 +0000 +++ new/sigint.h 2005-01-25 01:30:11.000000000 +0000 @@ -2,7 +2,8 @@ #define SIGINT_H #ifndef WINDOWS -void sigint_catch(int sig_no); +void sig_catch(int sig_no); +static void sig_catch2(int sig_no); #endif #endif diff -ur ctorrent-1.3.4/tracker.cpp new/tracker.cpp --- ctorrent-1.3.4/tracker.cpp 2005-01-26 00:40:07.751875408 +0000 +++ new/tracker.cpp 2005-01-26 00:38:52.828265528 +0000 @@ -31,7 +31,7 @@ m_sock = INVALID_SOCKET; m_port = 80; m_status = T_FREE; - m_f_started = m_f_stoped = m_f_pause = 0; + m_f_started = m_f_stoped = m_f_pause = m_f_completed = 0; m_interval = 15; m_connect_refuse_click = 0; @@ -54,7 +54,8 @@ m_reponse_buffer.Reset(); time(&m_last_timestamp); - m_status = T_FREE; + if (m_f_stoped) m_status = T_FINISHED; + else m_status = T_FREE; } int btTracker:: _IPsin(char *h, int p, struct sockaddr_in *psin) @@ -329,14 +332,15 @@ // fprintf(stdout,"Old Set Self:"); // fprintf(stdout,"%s\n", inet_ntoa(Self.m_sin.sin_addr)); - if( m_f_stoped ) /* stopped */ - event = str_event[1]; - else if( BTCONTENT.pBF->IsFull()) /* download complete */ - event = str_event[2]; - else if( m_f_started ) /* interval */ - event = (char*) 0; - else + if( m_f_stoped ) + event = str_event[1]; /* stopped */ + else if( m_f_started == 0 ) event = str_event[0]; /* started */ + else if( BTCONTENT.pBF->IsFull() && !m_f_completed){ + event = str_event[2]; /* download complete */ + m_f_completed = 1; /* only send download complete once */ + } else + event = (char*) 0; /* interval */ if(event){ if(MAXPATHLEN < snprintf(REQ_BUFFER,MAXPATHLEN,REQ_URL_P2_FMT, @@ -380,8 +390,7 @@ { /* tracker communication */ if( T_FREE == m_status ){ - if((*pnow - m_last_timestamp >= m_interval) && - (cfg_min_peers > WORLD.TotalPeers())){ + if(*pnow - m_last_timestamp >= m_interval){ if(Connect() < 0){ Reset(15); return -1; } diff -ur ctorrent-1.3.4/tracker.h new/tracker.h --- ctorrent-1.3.4/tracker.h 2005-01-26 00:40:07.752875256 +0000 +++ new/tracker.h 2005-01-26 00:38:21.003103688 +0000 @@ -21,6 +21,7 @@ #define T_FREE 0 #define T_CONNECTING 1 #define T_READY 2 +#define T_FINISHED 3 class btTracker { @@ -34,9 +35,10 @@ unsigned char m_status:2; unsigned char m_f_started:1; unsigned char m_f_stoped:1; + unsigned char m_f_completed:1; unsigned char m_f_pause:1; - unsigned char m_f_reserved:3; + unsigned char m_f_reserved:2; time_t m_interval; // 与Tracker通信的时间间隔 @@ -66,6 +68,8 @@ void SetPause() { m_f_pause = 1; } void ClearPause() { m_f_pause = 0; } + void SetStoped() { Reset(15); m_f_stoped = 1;} + int Connect(); int SendRequest(); int CheckReponse();