Google
Web alhem.net

TrackerSock.cpp

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004  grymse@alhem.net
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 */
00022 //#include <stdio.h>
00023 #include <map>
00024 #include <Parse.h>
00025 #include "MyHandler.h"
00026 #include "TrackerSock.h"
00027 
00028 
00029 
00030 
00031 TrackerSock::TrackerSock(ISocketHandler& h)
00032 :HTTPSocket(h)
00033 {
00034         SetLineProtocol();
00035 }
00036 
00037 
00038 TrackerSock::~TrackerSock()
00039 {
00040 //printf("~TrackerSock\n");
00041 }
00042 
00043 
00044 void TrackerSock::OnFirst()
00045 {
00046         if (GetMethod() == "GET")
00047         {
00048                 std::string url = GetUrl();
00049                 std::string params;
00050                 
00051                 {
00052                         Parse pa(url,"?");
00053                         m_uri = pa.getword();
00054                         params = pa.getrest();
00055                 }
00056                 Parse pa(params,"&");
00057                 std::string tmp = pa.getword();
00058                 while (tmp.size())
00059                 {
00060                         {
00061                                 Parse pa(tmp,"=");
00062                                 std::string key = pa.getword();
00063                                 std::string val = pa.getrest();
00064                                 m_par[key] = val;
00065                         }
00066                         tmp = pa.getword();
00067                 }
00068                 m_info_hash = static_cast<MyHandler&>(Handler()).escape(m_par["info_hash"]);
00069                 std::string ip = m_par["ip"];
00070                 ip = ip.size() ? ip : GetRemoteAddress();
00071                 MyHandler::PEER *p = static_cast<MyHandler&>(Handler()).reg_peer(
00072                         m_info_hash,
00073                         m_par["peer_id"],
00074                         ip,
00075                         atoi(m_par["port"].c_str()));
00076                 if (m_par["event"] == "completed")
00077                 {
00078                         p -> complete();
00079                 }
00080                 else
00081                 if (m_par["event"] == "stopped")
00082                 {
00083                         p -> stop();
00084                 }
00085                 else
00086                 {
00087                         p -> update(m_par["uploaded"],m_par["downloaded"],m_par["left"]);
00088                 }
00089         }
00090 }
00091 
00092 
00093 void TrackerSock::OnHeaderComplete()
00094 {
00095         AddResponseHeader("Connection", "close");
00096         AddResponseHeader("Server", static_cast<MyHandler&>(Handler()).GetString("server/identity"));
00097 
00098         if (m_uri == "/" || m_uri == "/index.html")
00099         {
00100                 SetHttpVersion("HTTP/1.0");
00101                 SetStatus("501");
00102                 SetStatusText("Not Implemented");
00103                 SendResponse();
00104                 SetCloseAndDelete();
00105         }
00106         else
00107         if (m_uri == "/scrape")
00108         {
00109                 SetHttpVersion("HTTP/1.0");
00110                 SetStatus("501");
00111                 SetStatusText("Not Implemented");
00112                 SendResponse();
00113                 SetCloseAndDelete();
00114         }
00115         else
00116         if (m_uri == "/file")
00117         {
00118                 SetHttpVersion("HTTP/1.0");
00119                 SetStatus("501");
00120                 SetStatusText("Not Implemented");
00121                 SendResponse();
00122                 SetCloseAndDelete();
00123         }
00124         else
00125         if (m_uri == "/favicon.ico")
00126         {
00127                 SetHttpVersion("HTTP/1.0");
00128                 SetStatus("501");
00129                 SetStatusText("Not Implemented");
00130                 SendResponse();
00131                 SetCloseAndDelete();
00132         }
00133         else
00134         if (m_uri != "/announce")
00135         {
00136                 SetHttpVersion("HTTP/1.0");
00137                 SetStatus("404");
00138                 SetStatusText("Not Found");
00139                 SendResponse();
00140                 SetCloseAndDelete();
00141                 return;
00142         }
00143 
00144         SetHttpVersion("HTTP/1.0");
00145         SetStatus("200");
00146         SetStatusText("OK");
00147         SendResponse();
00148 
00149         {
00150                 time_t t = time(NULL);
00151                 struct tm *tp = localtime(&t);
00152                 printf("%d-%02d-%02d %02d:%02d:%02d :: %s: %s %s %s\n",tp -> tm_year + 1900,
00153                         tp -> tm_mon + 1,tp -> tm_mday,
00154                         tp -> tm_hour,tp -> tm_min,tp -> tm_sec,
00155                         GetRemoteAddress().c_str(),GetMethod().c_str(),GetUrl().c_str(),GetHttpVersion().c_str());
00156         }
00157 
00158         std::string interval = "i" + static_cast<MyHandler&>(Handler()).GetString("server/reannounce_interval") + "e";
00159         size_t numwant = atoi(m_par["numwant"].c_str());
00160         size_t response_size = static_cast<MyHandler&>(Handler()).GetInt("server/response_size");
00161 
00162         Send("d");
00163         Send("8:interval");
00164         Send(interval);
00165         Send("5:peers" "l");
00166         static_cast<MyHandler&>(Handler()).CreatePeers(this, m_info_hash, numwant ? numwant : response_size);
00167         // end peers list
00168         Send("e");
00169         // end dictionary
00170         Send("e");
00171 
00172         SetCloseAndDelete();
00173 #ifdef ENABLE_DETACH
00174         Detach();
00175 #endif
00176 }
00177 
00178 

Generated on Thu Sep 27 12:58:27 2007 for distributed bittorrent tracker by  doxygen 1.5.2