OSDN Git Service

IM0035
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast / core / common / servent.cpp
index bd4c171..0d53331 100644 (file)
@@ -40,6 +40,8 @@
 #define new DEBUG_NEW
 #endif
 
+#include "win32/seh.h"
+
 
 const int DIRECT_WRITE_TIMEOUT = 60;
 
@@ -878,20 +880,24 @@ bool Servent::handshakeStream(ChanInfo &chanInfo)
                }
 
                chanID = chanInfo.id;
-               serventHit.rhost[0].ip = getHost().ip;
-               serventHit.rhost[0].port = listenPort;
-               serventHit.host = serventHit.rhost[0];
+               serventHit.host.ip = getHost().ip;
+               serventHit.host.port = listenPort;
+               if (serventHit.host.globalIP())
+                       serventHit.rhost[0] = serventHit.host;
+               else
+                       serventHit.rhost[1] = serventHit.host;
                serventHit.chanID = chanID;
 
                canStreamLock.on();
                chanReady = canStream(ch);
-               if (/*0 && */!chanReady)
+               if (0 && !chanReady && ch->isPlaying())
                {
-                       if (servMgr->numStreams(chanID, Servent::T_RELAY, false) == 0)
+                       if (ch->info.getUptime() > 60
+                               && servMgr->numStreams(chanID, Servent::T_RELAY, false) == 0)
                        {
                                sourceHit = &ch->sourceHost;  // send source host info
 
-                               if (listenPort && ch->info.getUptime() > 60)  // if stable
+                               if (listenPort)
                                {
                                        // connect "this" host later
                                        chanMgr->addHit(serventHit);
@@ -1054,11 +1060,18 @@ bool Servent::handshakeStream(ChanInfo &chanInfo)
 
                        int error = PCP_ERROR_QUIT+PCP_ERROR_UNAVAILABLE;
 
+                       if (sourceHit) {
+                               sourceHit->writeAtoms(atom2,chanInfo.id);       
+                               char tmp[50];
+                               sourceHit->host.toStr(tmp);
+                               LOG_DEBUG("relay info(sourceHit): %s", tmp);
+                       }
+
                        chanMgr->hitlistlock.on();
 
                        chl = chanMgr->findHitList(chanInfo);
 
-                       if (chl)
+                       if (chl && !sourceHit)
                        {
                                ChanHit best;
                                
@@ -1117,25 +1130,18 @@ bool Servent::handshakeStream(ChanInfo &chanInfo)
                                        cnt++;
                                }
 
-                               if (sourceHit) {
-                                       char tmp[50];
-                                       sourceHit->writeAtoms(atom2, chanInfo.id);
-                                       sourceHit->host.toStr(tmp);
-                                       LOG_DEBUG("relay info(sourceHit): %s", tmp);
-                                       best.host.ip = sourceHit->host.ip;
-                               }
-
                                if (!best.host.ip){
                                        char tmp[50];
 //                                     chanMgr->hitlistlock.on();
-                                       int cnt = chs.getRelayHost(servMgr->serverHost, rhost, remoteID, chl);
+                                       int rhcnt = chs.getRelayHost(servMgr->serverHost, rhost, remoteID, chl);
 //                                     chanMgr->hitlistlock.off();
-                                       for (int i = 0; i < cnt; i++){
+                                       for (int i = 0; i < rhcnt; i++){
                                                chs.best[i].writeAtoms(atom2, chanInfo.id);
                                                chs.best[i].host.toStr(tmp);
                                                LOG_DEBUG("relay info: %s hops = %d", tmp, chs.best[i].numHops);
                                                best.host.ip = chs.best[i].host.ip;
                                        }
+                                       cnt += rhcnt;
                                }
 
                                if (cnt)
@@ -1352,6 +1358,13 @@ bool Servent::handshakeStream(ChanInfo &chanInfo)
                {
                        handshakeIncomingPCP(atom,rhost,remoteID,agent);
                        atom.writeInt(PCP_OK,0);
+                       if (rhost.globalIP())
+                               serventHit.rhost[0] = rhost;
+                       else
+                               serventHit.rhost[1] = rhost;
+                       serventHit.sessionID = remoteID;
+                       serventHit.numHops = 1;
+                       chanMgr->addHit(serventHit);
                }
 
        }
@@ -1657,7 +1670,7 @@ void Servent::processRoot()
 }      
 
 // -----------------------------------
-int Servent::givProc(ThreadInfo *thread)
+int Servent::givProcMain(ThreadInfo *thread)
 {
 //     thread->lock();
        Servent *sv = (Servent*)thread->data;
@@ -1677,6 +1690,12 @@ int Servent::givProc(ThreadInfo *thread)
 }
 
 // -----------------------------------
+int Servent::givProc(ThreadInfo *thread)
+{
+       SEH_THREAD(givProcMain, Servent::givProc);
+}
+
+// -----------------------------------
 void Servent::handshakeOutgoingPCP(AtomStream &atom, Host &rhost, GnuID &rid, String &agent, bool isTrusted)
 {
 
@@ -2117,7 +2136,7 @@ void Servent::processIncomingPCP(bool suggestOthers)
 }
 
 // -----------------------------------
-int Servent::outgoingProc(ThreadInfo *thread)
+int Servent::outgoingProcMain(ThreadInfo *thread)
 {
 //     thread->lock();
        LOG_DEBUG("COUT started");
@@ -2298,7 +2317,12 @@ int Servent::outgoingProc(ThreadInfo *thread)
        return 0;
 }
 // -----------------------------------
-int Servent::incomingProc(ThreadInfo *thread)
+int Servent::outgoingProc(ThreadInfo *thread)
+{
+       SEH_THREAD(outgoingProcMain, Servent::outgoingProc);
+}
+// -----------------------------------
+int Servent::incomingProcMain(ThreadInfo *thread)
 {
 //     thread->lock();
 
@@ -2331,6 +2355,11 @@ int Servent::incomingProc(ThreadInfo *thread)
        return 0;
 }
 // -----------------------------------
+int Servent::incomingProc(ThreadInfo *thread)
+{
+       SEH_THREAD(incomingProcMain, Servent::incomingProc);
+}
+// -----------------------------------
 void Servent::processServent()
 {
        setStatus(S_HANDSHAKE);
@@ -2975,7 +3004,7 @@ void Servent::sendPCPChannel()
 }
 
 // -----------------------------------
-int Servent::serverProc(ThreadInfo *thread)
+int Servent::serverProcMain(ThreadInfo *thread)
 {
 //     thread->lock();
 
@@ -3004,6 +3033,16 @@ int Servent::serverProc(ThreadInfo *thread)
                        if (servMgr->numActiveOnPort(sv->sock->host.port) < servMgr->maxServIn)
                        {
                                ClientSocket *cs = sv->sock->accept();
+
+                               // \95s\90³\82È\83\\81[\83X\83A\83h\83\8c\83X(IPv4\83}\83\8b\83`\83L\83\83\83X\83g)\82ð\8f\9c\8aO
+                               if (cs && (((cs->host.ip >> 24) & 0xF0) == 0xE0))
+                               {
+                                       char ip[64];
+                                       cs->host.toStr(ip);
+                                       cs->close();
+                                       LOG_ERROR("reject incoming multicast address: %s", ip);
+                                       peercastApp->notifyMessage(ServMgr::NT_PEERCAST, "reject multicast address");
+                               } else
                                if (cs)
                                {       
                                        LOG_DEBUG("accepted incoming");
@@ -3032,6 +3071,12 @@ int Servent::serverProc(ThreadInfo *thread)
        sys->endThread(thread);
        return 0;
 }
+
+// -----------------------------------
+int Servent::serverProc(ThreadInfo *thread)
+{
+       SEH_THREAD(serverProcMain, Servent::serverProc);
+}
  
 // -----------------------------------
 bool   Servent::writeVariable(Stream &s, const String &var)
@@ -3147,7 +3192,7 @@ bool      Servent::writeVariable(Stream &s, const String &var)
                        }
                        strcat(buf,h_ip);
                        char h_name[128];
-                       if (ClientSocket::getHostname(h_name,h.ip))
+                       if (ClientSocket::getHostname(h_name,sizeof(h_name),h.ip)) //JP-MOD(BOF\91Î\8dô)
                        {
                                strcat(buf,"[");
                                strcat(buf,h_name);