1 // ------------------------------------------------
6 // Servent handshaking, TODO: should be in its own class
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
31 #include "chkMemoryLeak.h"
32 #define DEBUG_NEW new(__FILE__, __LINE__)
36 // -----------------------------------
37 static void termArgs(char *str)
41 size_t slen = strlen(str);
42 for(size_t i=0; i<slen; i++)
43 if (str[i]=='&') str[i] = 0;
46 // -----------------------------------
47 char *nextCGIarg(char *cp, char *cmd, char *arg)
64 if (cnt >= (MAX_CGI_LEN-1))
80 if (cnt >= (MAX_CGI_LEN-1))
87 // -----------------------------------
88 bool getCGIargBOOL(char *a)
90 return (strcmp(a,"1")==0);
92 // -----------------------------------
93 int getCGIargINT(char *a)
98 // -----------------------------------
99 void Servent::handshakeHTTP(HTTP &http, bool isHTTP)
101 char *in = http.cmdLine;
103 if (http.isRequest("GET /"))
107 char *pt = strstr(fn,HTTP_PROTO1);
111 if (strncmp(fn,"/admin?",7)==0)
113 if (!isAllowed(ALLOW_HTML))
114 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
117 LOG_DEBUG("Admin client");
120 }else if (strncmp(fn,"/http/",6)==0)
122 String dirName = fn+6;
124 if (!isAllowed(ALLOW_HTML))
125 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
127 if (!handshakeAuth(http,fn,false))
128 throw HTTPException(HTTP_SC_UNAUTHORIZED,401);
131 handshakeRemoteFile(dirName);
134 }else if (strncmp(fn,"/html/",6)==0)
136 String dirName = fn+1;
138 if (!isAllowed(ALLOW_HTML))
139 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
141 if (handshakeAuth(http,fn,true))
142 handshakeLocalFile(dirName);
145 }else if (strncmp(fn,"/admin/?",8)==0)
147 if (!isAllowed(ALLOW_HTML))
148 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
150 LOG_DEBUG("Admin client");
153 }else if (strncmp(fn,"/admin.cgi",10)==0)
155 if (!isAllowed(ALLOW_BROADCAST))
156 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
158 char *pwdArg = getCGIarg(fn,"pass=");
159 char *songArg = getCGIarg(fn,"song=");
160 char *mountArg = getCGIarg(fn,"mount=");
161 char *urlArg = getCGIarg(fn,"url=");
163 if (pwdArg && songArg)
166 size_t slen = strlen(fn);
167 for(i=0; i<slen; i++)
168 if (fn[i]=='&') fn[i] = 0;
170 Channel *c=chanMgr->channel;
173 if ((c->status == Channel::S_BROADCASTING) &&
174 (c->info.contentType == ChanInfo::T_MP3) )
176 // if we have a mount point then check for it, otherwise update all channels.
180 match = strcmp(c->mount,mountArg)==0;
184 ChanInfo newInfo = c->info;
185 newInfo.track.title.set(songArg,String::T_ESC);
186 newInfo.track.title.convertTo(String::T_UNICODE);
190 newInfo.track.contact.set(urlArg,String::T_ESC);
191 LOG_CHANNEL("Channel Shoutcast update: %s",songArg);
192 c->updateInfo(newInfo);
199 }else if (strncmp(fn,"/pls/",5)==0)
202 if (!sock->host.isLocalhost())
203 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
204 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
208 if (servMgr->getChannel(fn+5,info,isPrivate()))
209 handshakePLS(info,false);
211 throw HTTPException(HTTP_SC_NOTFOUND,404);
213 }else if (strncmp(fn,"/stream/",8)==0)
216 if (!sock->host.isLocalhost())
217 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
218 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
220 triggerChannel(fn+8,ChanInfo::SP_HTTP,isPrivate());
223 }else if (strncmp(fn,"/channel/",9)==0)
226 if (!sock->host.isLocalhost())
227 if (!isAllowed(ALLOW_NETWORK) || !isFiltered(ServFilter::F_NETWORK))
228 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
230 triggerChannel(fn+9,ChanInfo::SP_PCP,false);
234 while (http.nextHeader());
235 http.writeLine(HTTP_SC_FOUND);
236 http.writeLineF("Location: /%s/index.html",servMgr->htmlPath);
240 else if (http.isRequest("POST /"))
244 char *pt = strstr(fn,HTTP_PROTO1);
248 if (strncmp(fn,"/pls/",5)==0)
251 if (!sock->host.isLocalhost())
252 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
253 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
257 if (servMgr->getChannel(fn+5,info,isPrivate()))
258 handshakePLS(info,false);
260 throw HTTPException(HTTP_SC_NOTFOUND,404);
262 }else if (strncmp(fn,"/stream/",8)==0)
265 if (!sock->host.isLocalhost())
266 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
267 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
269 triggerChannel(fn+8,ChanInfo::SP_HTTP,isPrivate());
271 }else if (strncmp(fn,"/admin",7)==0)
273 if (!isAllowed(ALLOW_HTML))
274 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
277 LOG_DEBUG("Admin client");
278 while(http.nextHeader()){
279 LOG_DEBUG("%s",http.cmdLine);
282 if (sock->readLine(buf, sizeof(buf)) != 0){
288 while (http.nextHeader());
289 http.writeLine(HTTP_SC_FOUND);
290 http.writeLineF("Location: /%s/index.html",servMgr->htmlPath);
293 }else if (http.isRequest("GIV"))
297 while (http.nextHeader()) ;
299 if (!isAllowed(ALLOW_NETWORK))
300 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
305 char *idstr = strstr(in,"/");
310 sock->host.toStr(ipstr);
314 // at the moment we don`t really care where the GIV came from, so just give to chan. no. if its waiting.
315 Channel *ch = chanMgr->findChannelByID(id);
318 throw HTTPException(HTTP_SC_NOTFOUND,404);
320 if (!ch->acceptGIV(sock))
321 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
324 LOG_DEBUG("Accepted GIV channel %s from: %s",idstr,ipstr);
326 sock=NULL; // release this servent but dont close socket.
330 if (!servMgr->acceptGIV(sock))
331 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
333 LOG_DEBUG("Accepted GIV PCP from: %s",ipstr);
334 sock=NULL; // release this servent but dont close socket.
337 }else if (http.isRequest(PCX_PCP_CONNECT))
340 if (!isAllowed(ALLOW_NETWORK) || !isFiltered(ServFilter::F_NETWORK))
341 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
343 processIncomingPCP(true);
345 }else if (http.isRequest("PEERCAST CONNECT"))
347 if (!isAllowed(ALLOW_NETWORK) || !isFiltered(ServFilter::F_NETWORK))
348 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
350 LOG_DEBUG("PEERCAST client");
353 }else if (http.isRequest("SOURCE"))
355 if (!isAllowed(ALLOW_BROADCAST))
356 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
361 if (ps=strstr(in,"ICE/1.0"))
365 LOG_DEBUG("ICE 1.0 client to %s",mount?mount:"unknown");
367 mount = in+strlen(in);
371 mount[-1] = 0; // password preceeds
374 loginPassword.set(in+7);
376 LOG_DEBUG("ICY client: %s %s",loginPassword.cstr(),mount?mount:"unknown");
380 loginMount.set(mount);
382 handshakeICY(Channel::SRC_ICECAST,isHTTP);
383 sock = NULL; // socket is taken over by channel, so don`t close it
385 }else if (http.isRequest(servMgr->password))
387 if (!isAllowed(ALLOW_BROADCAST))
388 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
390 loginPassword.set(servMgr->password); // pwd already checked
392 sock->writeLine("OK2");
393 sock->writeLine("icy-caps:11");
395 LOG_DEBUG("ShoutCast client");
397 handshakeICY(Channel::SRC_SHOUTCAST,isHTTP);
398 sock = NULL; // socket is taken over by channel, so don`t close it
402 throw HTTPException(HTTP_SC_BADREQUEST,400);
406 // -----------------------------------
407 bool Servent::canStream(Channel *ch)
412 if (servMgr->isDisabled)
417 if (!ch->isPlaying() || ch->isFull() || ((type == T_DIRECT) && servMgr->directFull()))
420 if (!isIndexTxt(ch) && (type == T_RELAY) && (servMgr->relaysFull()))
423 Channel *c = chanMgr->channel;
425 unsigned int needRate = 0;
426 unsigned int allRate = 0;
429 int nlr = c->localRelays();
430 allRate += c->info.bitrate * nlr;
431 if ((c != ch) && (nlr == 0)){
432 if(!isIndexTxt(c)) // for PCRaw (relay)
434 needRate+=c->info.bitrate;
439 unsigned int numRelay = servMgr->numStreams(Servent::T_RELAY,false);
440 int diff = servMgr->maxRelays - numRelay;
441 if (ch->localRelays()){
450 LOG_DEBUG("Relay check: Max=%d Now=%d Need=%d ch=%d",
451 servMgr->maxBitrateOut, allRate, needRate, ch->info.bitrate);
452 // if ( !ch->isPlaying()
454 // || (allRate + needRate + ch->info.bitrate > servMgr->maxBitrateOut)
455 // || ((type == T_RELAY) && servMgr->relaysFull() && force_off) // for PCRaw (relay) (force_off)
456 // || ((type == T_RELAY) && (((servMgr->numStreams(Servent::T_RELAY,false) + noRelay) >= servMgr->maxRelays)) && force_off) // for PCRaw (relay) (force_off)
457 // || ((type == T_DIRECT) && servMgr->directFull())
460 if (allRate + needRate + ch->info.bitrate > servMgr->maxBitrateOut)
462 LOG_DEBUG("Relay check: NG");
466 if (!isIndexTxt(ch) && (type == T_RELAY) && (numRelay + noRelay >= servMgr->maxRelays))
468 LOG_DEBUG("Relay check: NG");
473 LOG_DEBUG("Relay check: OK");
476 // -----------------------------------
477 void Servent::handshakeIncoming()
480 setStatus(S_HANDSHAKE);
483 sock->readLine(buf,sizeof(buf));
486 sock->host.toStr(sb);
489 if (stristr(buf,RTSP_PROTO1))
491 LOG_DEBUG("RTSP from %s '%s'",sb,buf);
493 rtsp.initRequest(buf);
495 }else if (stristr(buf,HTTP_PROTO1))
497 LOG_DEBUG("HTTP from %s '%s'",sb,buf);
499 http.initRequest(buf);
500 handshakeHTTP(http,true);
503 LOG_DEBUG("Connect from %s '%s'",sb,buf);
505 http.initRequest(buf);
506 handshakeHTTP(http,false);
510 // -----------------------------------
511 void Servent::triggerChannel(char *str, ChanInfo::PROTOCOL proto,bool relay)
515 // WLockBlock lb(&(chanMgr->channellock));
517 // LOG_DEBUG("----------triggerChannel LOCK ON");
519 servMgr->getChannel(str,info,relay);
520 // LOG_DEBUG("==========triggerChannel LOCK OFF");
523 if (proto == ChanInfo::SP_PCP)
528 outputProtocol = proto;
530 processStream(false,info);
533 // -----------------------------------
534 void writePLSHeader(Stream &s, PlayList::TYPE type)
536 s.writeLine(HTTP_SC_OK);
537 s.writeLineF("%s %s",HTTP_HS_SERVER,PCX_AGENT);
542 case PlayList::T_PLS:
545 case PlayList::T_ASX:
548 case PlayList::T_RAM:
555 s.writeLineF("%s %s",HTTP_HS_CONTENT,content);
556 s.writeLine("Content-Disposition: inline");
557 s.writeLine("Cache-Control: private" );
558 s.writeLineF("%s %s",HTTP_HS_CONNECTION,"close");
563 // -----------------------------------
564 void Servent::handshakePLS(ChanInfo &info, bool doneHandshake)
571 while (sock->readLine(in,128));
574 if (getLocalTypeURL(url,info.contentType))
579 if ((info.contentType == ChanInfo::T_WMA) || (info.contentType == ChanInfo::T_WMV))
580 type = PlayList::T_ASX;
581 else if (info.contentType == ChanInfo::T_OGM)
582 type = PlayList::T_RAM;
584 type = PlayList::T_PLS;
586 writePLSHeader(*sock,type);
590 pls = new PlayList(type,1);
592 pls->addChannel(url,info);
599 // -----------------------------------
600 void Servent::handshakePLS(ChanHitList **cl, int num, bool doneHandshake)
606 while (sock->readLine(in,128));
608 if (getLocalURL(url))
610 writePLSHeader(*sock,PlayList::T_SCPLS);
614 pls = new PlayList(PlayList::T_SCPLS,num);
616 for(int i=0; i<num; i++)
617 pls->addChannel(url,cl[i]->info);
624 // -----------------------------------
625 bool Servent::getLocalURL(char *str)
628 throw StreamException("Not connected");
635 if (sock->host.localIP())
636 h = sock->getLocalHost();
638 h = servMgr->serverHost;
640 h.port = servMgr->serverHost.port;
644 sprintf(str,"http://%s",ipStr);
648 // -----------------------------------
649 bool Servent::getLocalTypeURL(char *str, ChanInfo::TYPE type)
652 throw StreamException("Not connected");
659 if (sock->host.localIP())
660 h = sock->getLocalHost();
662 h = servMgr->serverHost;
664 h.port = servMgr->serverHost.port;
668 case ChanInfo::T_WMA:
669 case ChanInfo::T_WMV:
670 sprintf(str,"mms://%s",ipStr);
673 sprintf(str,"http://%s",ipStr);
677 // -----------------------------------
678 // Warning: testing RTSP/RTP stuff below.
679 // .. moved over to seperate app now.
680 // -----------------------------------
681 void Servent::handshakePOST()
684 while (sock->readLine(tmp,sizeof(tmp)))
685 LOG_DEBUG("POST: %s",tmp);
687 throw HTTPException(HTTP_SC_BADREQUEST,400);
691 // -----------------------------------
692 void Servent::handshakeRTSP(RTSP &rtsp)
694 throw HTTPException(HTTP_SC_BADREQUEST,400);
696 // -----------------------------------
697 bool Servent::handshakeAuth(HTTP &http,const char *args,bool local)
699 char user[1024],pass[1024];
700 user[0] = pass[0] = 0;
702 char *pwd = getCGIarg(args, "pass=");
704 if ((pwd) && strlen(servMgr->password))
707 char *as = strstr(tmp.cstr(),"&");
709 if (strcmp(tmp,servMgr->password)==0)
711 while (http.nextHeader());
719 while (http.nextHeader())
721 char *arg = http.getArgStr();
725 switch (servMgr->authType)
727 case ServMgr::AUTH_HTTPBASIC:
728 if (http.isHeader("Authorization"))
729 http.getAuthUserPass(user, pass, sizeof(user), sizeof(pass));
731 case ServMgr::AUTH_COOKIE:
732 if (http.isHeader("Cookie"))
734 LOG_DEBUG("Got cookie: %s",arg);
736 while ((idp = strstr(idp,"id=")))
739 gotCookie.set(idp,sock->host.ip);
740 if (servMgr->cookieList.contains(gotCookie))
742 LOG_DEBUG("Cookie found");
753 if (sock->host.isLocalhost())
757 switch (servMgr->authType)
759 case ServMgr::AUTH_HTTPBASIC:
761 if ((strcmp(pass,servMgr->password)==0) && strlen(servMgr->password))
764 case ServMgr::AUTH_COOKIE:
765 if (servMgr->cookieList.contains(cookie))
772 if (servMgr->authType == ServMgr::AUTH_HTTPBASIC)
774 http.writeLine(HTTP_SC_UNAUTHORIZED);
775 http.writeLine("WWW-Authenticate: Basic realm=\"PeerCast Admin\"");
776 }else if (servMgr->authType == ServMgr::AUTH_COOKIE)
778 String file = servMgr->htmlPath;
779 file.append("/login.html");
781 handshakeLocalFile(file);
783 handshakeRemoteFile(file);
790 // -----------------------------------
791 void Servent::handshakeCMD(char *cmd)
793 char result[MAX_CGI_LEN];
794 char arg[MAX_CGI_LEN];
795 char curr[MAX_CGI_LEN];
806 if (!handshakeAuth(http,cmd,true))
811 if (cmpCGIarg(cmd,"cmd=","redirect"))
813 char *j = getCGIarg(cmd,"url=");
818 url.set(j,String::T_ESC);
819 url.convertTo(String::T_ASCII);
821 if (!url.contains("http://"))
822 url.prepend("http://");
824 html.setRefreshURL(url.cstr());
828 html.startTagEnd("h3","Please wait...");
835 if (cmpCGIarg(cmd,"cmd=","viewxml"))
840 }else if (cmpCGIarg(cmd,"cmd=","clearlog"))
842 sys->logBuf->clear();
843 sprintf(jumpStr,"/%s/viewlog.html",servMgr->htmlPath);
846 }else if (cmpCGIarg(cmd,"cmd=","save"))
849 peercastInst->saveSettings();
851 sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
853 }else if (cmpCGIarg(cmd,"cmd=","reg"))
856 chanMgr->broadcastID.toStr(idstr);
857 sprintf(jumpStr,"http://www.peercast.org/register/?id=%s",idstr);
859 }else if (cmpCGIarg(cmd,"cmd=","edit_bcid"))
864 while (cp=nextCGIarg(cp,curr,arg))
866 if (strcmp(curr,"id")==0)
868 else if (strcmp(curr,"del")==0)
869 servMgr->removeValidBCID(id);
870 else if (strcmp(curr,"valid")==0)
872 bcid = servMgr->findValidBCID(id);
874 bcid->valid = getCGIargBOOL(arg);
877 peercastInst->saveSettings();
878 sprintf(jumpStr,"/%s/bcid.html",servMgr->htmlPath);
881 }else if (cmpCGIarg(cmd,"cmd=","add_bcid"))
883 BCID *bcid = new BCID();
887 while (cp=nextCGIarg(cp,curr,arg))
889 if (strcmp(curr,"id")==0)
890 bcid->id.fromStr(arg);
891 else if (strcmp(curr,"name")==0)
893 else if (strcmp(curr,"email")==0)
894 bcid->email.set(arg);
895 else if (strcmp(curr,"url")==0)
897 else if (strcmp(curr,"valid")==0)
898 bcid->valid = getCGIargBOOL(arg);
899 else if (strcmp(curr,"result")==0)
904 LOG_DEBUG("Adding BCID : %s",bcid->name.cstr());
905 servMgr->addValidBCID(bcid);
906 peercastInst->saveSettings();
909 http.writeLine(HTTP_SC_OK);
911 http.writeString("OK");
914 sprintf(jumpStr,"/%s/bcid.html",servMgr->htmlPath);
919 }else if (cmpCGIarg(cmd,"cmd=","apply"))
921 //servMgr->numFilters = 0;
922 ServFilter *currFilter=servMgr->filters;
923 bool beginfilt = false;
930 int newPort=servMgr->serverHost.port;
931 int enableGetName = 0;
932 int allowConnectPCST = 0;
933 int disableAutoBumpIfDirect = 0; //JP-MOD
934 int asxDetailedMode = 0; //JP-MOD
937 while (cp=nextCGIarg(cp,curr,arg))
939 LOG_DEBUG("ARG: %s = %s", curr, arg);
942 if (strcmp(curr,"serveractive")==0)
943 servMgr->autoServe = getCGIargBOOL(arg);
944 else if (strcmp(curr,"port")==0)
945 newPort = getCGIargINT(arg);
946 else if (strcmp(curr,"icymeta")==0)
948 int iv = getCGIargINT(arg);
950 else if (iv > 16384) iv = 16384;
952 chanMgr->icyMetaInterval = iv;
954 }else if (strcmp(curr,"passnew")==0)
955 strcpy(servMgr->password,arg);
956 else if (strcmp(curr,"root")==0)
957 servMgr->isRoot = getCGIargBOOL(arg);
958 else if (strcmp(curr,"brroot")==0)
959 brRoot = getCGIargBOOL(arg);
960 else if (strcmp(curr,"getupd")==0)
961 getUpd = getCGIargBOOL(arg);
962 else if (strcmp(curr,"huint")==0)
963 chanMgr->setUpdateInterval(getCGIargINT(arg));
964 else if (strcmp(curr,"forceip")==0)
965 servMgr->forceIP = arg;
966 else if (strcmp(curr,"htmlPath")==0)
968 strcpy(servMgr->htmlPath,"html/");
969 strcat(servMgr->htmlPath,arg);
970 }else if (strcmp(curr,"djmsg")==0)
973 msg.set(arg,String::T_ESC);
974 msg.convertTo(String::T_UNICODE);
975 chanMgr->setBroadcastMsg(msg);
977 else if (strcmp(curr,"pcmsg")==0)
979 servMgr->rootMsg.set(arg,String::T_ESC);
980 servMgr->rootMsg.convertTo(String::T_UNICODE);
981 }else if (strcmp(curr,"minpgnu")==0)
982 servMgr->minGnuIncoming = atoi(arg);
983 else if (strcmp(curr,"maxpgnu")==0)
984 servMgr->maxGnuIncoming = atoi(arg);
989 else if (strcmp(curr,"maxcin")==0)
990 servMgr->maxControl = getCGIargINT(arg);
992 else if (strcmp(curr,"maxup")==0)
993 servMgr->maxBitrateOut = getCGIargINT(arg);
994 else if (strcmp(curr,"maxrelays")==0)
995 servMgr->setMaxRelays(getCGIargINT(arg));
996 else if (strcmp(curr,"maxdirect")==0)
997 servMgr->maxDirect = getCGIargINT(arg);
998 else if (strcmp(curr,"maxrelaypc")==0)
999 chanMgr->maxRelaysPerChannel = getCGIargINT(arg);
1000 else if (strncmp(curr,"filt_",5)==0)
1003 servMgr->numFilters = 0;
1008 if (strncmp(fs,"ip",2)==0) // ip must be first
1010 currFilter = &servMgr->filters[servMgr->numFilters];
1012 currFilter->host.fromStrIP(arg,DEFAULT_PORT);
1013 if ((currFilter->host.ip) && (servMgr->numFilters < (ServMgr::MAX_FILTERS-1)))
1015 servMgr->numFilters++;
1016 servMgr->filters[servMgr->numFilters].init(); // clear new entry
1017 LOG_DEBUG("numFilters = %d", servMgr->numFilters);
1020 }else if (strncmp(fs,"bn",2)==0)
1021 currFilter->flags |= ServFilter::F_BAN;
1022 else if (strncmp(fs,"pr",2)==0)
1023 currFilter->flags |= ServFilter::F_PRIVATE;
1024 else if (strncmp(fs,"nw",2)==0)
1025 currFilter->flags |= ServFilter::F_NETWORK;
1026 else if (strncmp(fs,"di",2)==0)
1027 currFilter->flags |= ServFilter::F_DIRECT;
1032 else if (strcmp(curr,"clientactive")==0)
1033 servMgr->autoConnect = getCGIargBOOL(arg);
1034 else if (strcmp(curr,"yp")==0)
1038 String str(arg,String::T_ESC);
1039 str.convertTo(String::T_ASCII);
1040 servMgr->rootHost = str;
1043 else if (strcmp(curr,"yp2")==0)
1047 String str(arg,String::T_ESC);
1048 str.convertTo(String::T_ASCII);
1049 servMgr->rootHost2 = str;
1052 else if (strcmp(curr,"deadhitage")==0)
1053 chanMgr->deadHitAge = getCGIargINT(arg);
1054 else if (strcmp(curr,"refresh")==0)
1055 servMgr->refreshHTML = getCGIargINT(arg);
1056 else if (strcmp(curr,"auth")==0)
1058 if (strcmp(arg,"cookie")==0)
1059 servMgr->authType = ServMgr::AUTH_COOKIE;
1060 else if (strcmp(arg,"http")==0)
1061 servMgr->authType = ServMgr::AUTH_HTTPBASIC;
1063 }else if (strcmp(curr,"expire")==0)
1065 if (strcmp(arg,"session")==0)
1066 servMgr->cookieList.neverExpire = false;
1067 else if (strcmp(arg,"never")==0)
1068 servMgr->cookieList.neverExpire = true;
1071 else if (strcmp(curr,"logDebug")==0)
1072 showLog |= atoi(arg)?(1<<LogBuffer::T_DEBUG):0;
1073 else if (strcmp(curr,"logErrors")==0)
1074 showLog |= atoi(arg)?(1<<LogBuffer::T_ERROR):0;
1075 else if (strcmp(curr,"logNetwork")==0)
1076 showLog |= atoi(arg)?(1<<LogBuffer::T_NETWORK):0;
1077 else if (strcmp(curr,"logChannel")==0)
1078 showLog |= atoi(arg)?(1<<LogBuffer::T_CHANNEL):0;
1080 else if (strcmp(curr,"allowHTML1")==0)
1081 allowServer1 |= atoi(arg)?(ALLOW_HTML):0;
1082 else if (strcmp(curr,"allowNetwork1")==0)
1083 allowServer1 |= atoi(arg)?(ALLOW_NETWORK):0;
1084 else if (strcmp(curr,"allowBroadcast1")==0)
1085 allowServer1 |= atoi(arg)?(ALLOW_BROADCAST):0;
1086 else if (strcmp(curr,"allowDirect1")==0)
1087 allowServer1 |= atoi(arg)?(ALLOW_DIRECT):0;
1089 else if (strcmp(curr,"allowHTML2")==0)
1090 allowServer2 |= atoi(arg)?(ALLOW_HTML):0;
1091 else if (strcmp(curr,"allowBroadcast2")==0)
1092 allowServer2 |= atoi(arg)?(ALLOW_BROADCAST):0;
1095 else if (strcmp(curr, "autoRelayKeep") ==0)
1096 servMgr->autoRelayKeep = getCGIargINT(arg);
1097 else if (strcmp(curr, "autoMaxRelaySetting") ==0)
1098 servMgr->autoMaxRelaySetting = getCGIargINT(arg);
1099 else if (strcmp(curr, "autoBumpSkipCount") ==0)
1100 servMgr->autoBumpSkipCount = getCGIargINT(arg);
1101 else if (strcmp(curr, "kickPushStartRelays") ==0)
1102 servMgr->kickPushStartRelays = getCGIargINT(arg);
1103 else if (strcmp(curr, "kickPushInterval") ==0)
1104 servMgr->kickPushInterval = getCGIargINT(arg);
1105 else if (strcmp(curr, "allowConnectPCST") ==0)
1106 allowConnectPCST = atoi(arg) ? 1 : 0;
1107 else if (strcmp(curr, "enableGetName") ==0)
1108 enableGetName = atoi(arg)? 1 : 0;
1109 else if (strcmp(curr, "autoPort0Kick") ==0)
1110 servMgr->autoPort0Kick = getCGIargBOOL(arg);
1111 else if (strcmp(curr, "allowOnlyVP") ==0)
1112 servMgr->allowOnlyVP = getCGIargBOOL(arg);
1113 else if (strcmp(curr, "kickKeepTime") ==0)
1114 servMgr->kickKeepTime = getCGIargINT(arg);
1116 else if (strcmp(curr, "maxRelaysIndexTxt") ==0) // for PCRaw (relay)
1117 servMgr->maxRelaysIndexTxt = getCGIargINT(arg);
1118 else if (strcmp(curr, "disableAutoBumpIfDirect") ==0) //JP-MOD
1119 disableAutoBumpIfDirect = atoi(arg) ? 1 : 0;
1120 else if (strcmp(curr, "asxDetailedMode") ==0) //JP-MOD
1121 asxDetailedMode = getCGIargINT(arg);
1126 servMgr->showLog = showLog;
1127 servMgr->allowServer1 = allowServer1;
1128 servMgr->allowServer2 = allowServer2;
1129 servMgr->enableGetName = enableGetName;
1130 servMgr->allowConnectPCST = allowConnectPCST;
1131 servMgr->disableAutoBumpIfDirect = disableAutoBumpIfDirect; //JP-MOD
1132 servMgr->asxDetailedMode = asxDetailedMode; //JP-MOD
1133 if (!(servMgr->allowServer1 & ALLOW_HTML) && !(servMgr->allowServer2 & ALLOW_HTML))
1134 servMgr->allowServer1 |= ALLOW_HTML;
1136 if (servMgr->serverHost.port != newPort)
1138 Host lh(ClientSocket::getIP(NULL),newPort);
1141 sprintf(jumpStr,"http://%s/%s/settings.html",ipstr,servMgr->htmlPath);
1143 servMgr->serverHost.port = newPort;
1144 servMgr->restartServer=true;
1145 //html.setRefresh(3);
1146 //html.setRefreshURL(jumpStr);
1149 // html.startBody();
1150 // html.startTagEnd("h1","Please wait...");
1157 //servMgr->serverHost.toStr(ipstr);
1158 //sprintf(jumpStr,"/%s/settings.html",ipstr,servMgr->htmlPath);
1163 sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
1167 peercastInst->saveSettings();
1169 peercastApp->updateSettings();
1171 if ((servMgr->isRoot) && (brRoot))
1172 servMgr->broadcastRootSettings(getUpd);
1178 }else if (cmpCGIarg(cmd,"cmd=","fetch"))
1185 while (cp=nextCGIarg(cp,curr,arg))
1187 if (strcmp(curr,"url")==0)
1189 curl.set(arg,String::T_ESC);
1190 curl.convertTo(String::T_UNICODE);
1191 }else if (strcmp(curr,"name")==0)
1193 info.name.set(arg,String::T_ESC);
1194 info.name.convertTo(String::T_UNICODE);
1195 }else if (strcmp(curr,"desc")==0)
1197 info.desc.set(arg,String::T_ESC);
1198 info.desc.convertTo(String::T_UNICODE);
1199 }else if (strcmp(curr,"genre")==0)
1201 info.genre.set(arg,String::T_ESC);
1202 info.genre.convertTo(String::T_UNICODE);
1203 }else if (strcmp(curr,"contact")==0)
1205 info.url.set(arg,String::T_ESC);
1206 info.url.convertTo(String::T_UNICODE);
1207 }else if (strcmp(curr,"bitrate")==0)
1209 info.bitrate = atoi(arg);
1210 }else if (strcmp(curr,"type")==0)
1212 info.contentType = ChanInfo::getTypeFromStr(arg);
1213 }else if (strcmp(curr,"bcstClap")==0) //JP-MOD
1215 info.ppFlags |= ServMgr::bcstClap;
1220 info.bcID = chanMgr->broadcastID;
1222 Channel *c = chanMgr->createChannel(info,NULL);
1224 c->startURL(curl.cstr());
1227 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1230 }else if (cmpCGIarg(cmd,"cmd=","stopserv"))
1234 while (cp=nextCGIarg(cp,curr,arg))
1236 if (strcmp(curr,"index")==0)
1238 Servent *s = servMgr->findServentByIndex(atoi(arg));
1243 sprintf(jumpStr,"/%s/connections.html",servMgr->htmlPath);
1247 }else if (cmpCGIarg(cmd,"cmd=","hitlist"))
1250 bool stayConnected=hasCGIarg(cmd,"relay");
1253 ChanHitList *chl = chanMgr->hitlist;
1259 sprintf(tmp,"c%d=",index);
1260 if (cmpCGIarg(cmd,tmp,"1"))
1263 if (!(c=chanMgr->findChannelByID(chl->info.id)))
1265 c = chanMgr->createChannel(chl->info,NULL);
1267 throw StreamException("out of channels");
1268 c->stayConnected = stayConnected;
1277 char *findArg = getCGIarg(cmd,"keywords=");
1279 if (hasCGIarg(cmd,"relay"))
1282 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1285 }else if (cmpCGIarg(cmd,"cmd=","clear"))
1288 while (cp=nextCGIarg(cp,curr,arg))
1290 if (strcmp(curr,"hostcache")==0)
1291 servMgr->clearHostCache(ServHost::T_SERVENT);
1292 else if (strcmp(curr,"hitlists")==0)
1293 chanMgr->clearHitLists();
1294 else if (strcmp(curr,"packets")==0)
1296 stats.clearRange(Stats::PACKETSSTART,Stats::PACKETSEND);
1297 servMgr->numVersions = 0;
1301 sprintf(jumpStr,"/%s/index.html",servMgr->htmlPath);
1304 }else if (cmpCGIarg(cmd,"cmd=","upgrade"))
1306 if (servMgr->downloadURL[0])
1308 sprintf(jumpStr,"/admin?cmd=redirect&url=%s",servMgr->downloadURL);
1315 }else if (cmpCGIarg(cmd,"cmd=","connect"))
1319 Servent *s = servMgr->servents;
1322 sprintf(tmp,"c%d=",s->serventIndex);
1323 if (cmpCGIarg(cmd,tmp,"1"))
1325 if (hasCGIarg(cmd,"stop"))
1326 s->thread.active = false;
1330 sprintf(jumpStr,"/%s/connections.html",servMgr->htmlPath);
1333 }else if (cmpCGIarg(cmd,"cmd=","shutdown"))
1335 servMgr->shutdownTimer = 1;
1337 }else if (cmpCGIarg(cmd,"cmd=","stop"))
1341 while (cp=nextCGIarg(cp,curr,arg))
1343 if (strcmp(curr,"id")==0)
1347 Channel *c = chanMgr->findChannelByID(id);
1349 c->thread.active = false;
1350 c->thread.finish = true;
1354 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1357 }else if (cmpCGIarg(cmd,"cmd=","bump"))
1361 while (cp=nextCGIarg(cp,curr,arg))
1363 if (strcmp(curr,"id")==0)
1367 Channel *c = chanMgr->findChannelByID(id);
1371 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1374 }else if (cmpCGIarg(cmd,"cmd=","keep"))
1378 while (cp=nextCGIarg(cp,curr,arg))
1380 if (strcmp(curr,"id")==0)
1384 Channel *c = chanMgr->findChannelByID(id);
1387 //c->stayConnected = true;
1388 if (!c->stayConnected)
1390 //if (servMgr->getFirewall() == ServMgr::FW_OFF)
1391 c->stayConnected = true;
1394 c->stayConnected = false;
1397 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1400 }else if (cmpCGIarg(cmd,"cmd=","relay"))
1404 while (cp=nextCGIarg(cp,curr,arg))
1406 if (strcmp(curr,"id")==0)
1407 info.id.fromStr(arg);
1411 if (!chanMgr->findChannelByID(info.id))
1414 ChanHitList *chl = chanMgr->findHitList(info);
1416 throw StreamException("channel not found");
1419 Channel *c = chanMgr->createChannel(chl->info,NULL);
1421 throw StreamException("out of channels");
1423 c->stayConnected = true;
1427 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1431 }else if (cmpCGIarg(cmd,"net=","add"))
1436 while (cmd=nextCGIarg(cmd,curr,arg))
1438 if (strcmp(curr,"ip")==0)
1441 h.fromStrIP(arg,DEFAULT_PORT);
1442 if (servMgr->addOutgoing(h,id,true))
1443 LOG_NETWORK("Added connection: %s",arg);
1445 }else if (strcmp(curr,"id")==0)
1452 }else if (cmpCGIarg(cmd,"cmd=","logout"))
1455 servMgr->cookieList.remove(cookie);
1457 }else if (cmpCGIarg(cmd,"cmd=","login"))
1464 cookie.set(idstr,sock->host.ip);
1465 servMgr->cookieList.add(cookie);
1467 http.writeLine(HTTP_SC_FOUND);
1468 if (servMgr->cookieList.neverExpire)
1469 http.writeLineF("%s id=%s; path=/; expires=\"Mon, 01-Jan-3000 00:00:00 GMT\";",HTTP_HS_SETCOOKIE,idstr);
1471 http.writeLineF("%s id=%s; path=/;",HTTP_HS_SETCOOKIE,idstr);
1472 http.writeLineF("Location: /%s/index.html",servMgr->htmlPath);
1475 }else if (cmpCGIarg(cmd,"cmd=","setmeta"))
1478 while (cp=nextCGIarg(cp,curr,arg))
1480 if (strcmp(curr,"name")==0)
1483 chname.set(arg,String::T_ESC);
1484 chname.convertTo(String::T_ASCII);
1485 for(int i=0; i<ChanMgr::MAX_CHANNELS; i++)
1487 Channel *c = &chanMgr->channels[i];
1488 if ((c->isActive()) && (c->status == Channel::S_BROADCASTING) && (strcmp(c->info.name.cstr(),chname.cstr())==0))
1490 ChanInfo newInfo = c->info;
1492 while (cmd=nextCGIarg(cmd,curr,arg))
1495 chmeta.set(arg,String::T_ESC);
1496 chmeta.convertTo(String::T_ASCII);
1497 if (strcmp(curr,"desc")==0)
1498 newInfo.desc = chmeta.cstr();
1499 else if (strcmp(curr,"url")==0)
1500 newInfo.url = chmeta.cstr();
1501 else if (strcmp(curr,"genre")==0)
1502 newInfo.genre = chmeta.cstr();
1503 else if (strcmp(curr,"comment")==0)
1504 newInfo.comment = chmeta.cstr();
1505 else if (strcmp(curr,"t_contact")==0)
1506 newInfo.track.contact = chmeta.cstr();
1507 else if (strcmp(curr,"t_title")==0)
1508 newInfo.track.title = chmeta.cstr();
1509 else if (strcmp(curr,"t_artist")==0)
1510 newInfo.track.artist = chmeta.cstr();
1511 else if (strcmp(curr,"t_album")==0)
1512 newInfo.track.album = chmeta.cstr();
1513 else if (strcmp(curr,"t_genre")==0)
1514 newInfo.track.genre = chmeta.cstr();
1516 c->updateInfo(newInfo);
1518 newInfo.id.toStr(idstr);
1519 sprintf(jumpStr,"/%s/relayinfo.html?id=%s",servMgr->htmlPath,idstr);
1525 chname.set(arg,String::T_ESC);
1526 chname.convertTo(String::T_ASCII);
1528 Channel *c = chanMgr->findChannelByName(chname.cstr());
1529 if (c && (c->isActive()) && (c->status == Channel::S_BROADCASTING)){
1530 ChanInfo newInfo = c->info;
1531 newInfo.ppFlags = ServMgr::bcstNone; //JP-MOD
1532 while (cmd=nextCGIarg(cmd,curr,arg))
1535 chmeta.set(arg,String::T_ESC);
1536 chmeta.convertTo(String::T_ASCII);
1537 if (strcmp(curr,"desc")==0)
1538 newInfo.desc = chmeta.cstr();
1539 else if (strcmp(curr,"url")==0)
1540 newInfo.url = chmeta.cstr();
1541 else if (strcmp(curr,"genre")==0)
1542 newInfo.genre = chmeta.cstr();
1543 else if (strcmp(curr,"comment")==0)
1544 newInfo.comment = chmeta.cstr();
1545 else if (strcmp(curr,"bcstClap")==0) //JP-MOD
1546 newInfo.ppFlags |= ServMgr::bcstClap;
1547 else if (strcmp(curr,"t_contact")==0)
1548 newInfo.track.contact = chmeta.cstr();
1549 else if (strcmp(curr,"t_title")==0)
1550 newInfo.track.title = chmeta.cstr();
1551 else if (strcmp(curr,"t_artist")==0)
1552 newInfo.track.artist = chmeta.cstr();
1553 else if (strcmp(curr,"t_album")==0)
1554 newInfo.track.album = chmeta.cstr();
1555 else if (strcmp(curr,"t_genre")==0)
1556 newInfo.track.genre = chmeta.cstr();
1558 c->updateInfo(newInfo);
1560 newInfo.id.toStr(idstr);
1561 sprintf(jumpStr,"/%s/relayinfo.html?id=%s",servMgr->htmlPath,idstr);
1573 sprintf(jumpStr,"/%s/index.html",servMgr->htmlPath);
1578 }catch(StreamException &e)
1580 html.startTagEnd("h1","ERROR - %s",e.msg);
1581 LOG_ERROR("html: %s",e.msg);
1589 String jmp(jumpArg,String::T_HTML);
1590 jmp.convertTo(String::T_ASCII);
1591 html.locateTo(jmp.cstr());
1597 // -----------------------------------
1598 static XML::Node *createChannelXML(Channel *c)
1600 XML::Node *n = c->info.createChannelXML();
1601 n->add(c->createRelayXML(true));
1602 n->add(c->info.createTrackXML());
1603 // n->add(c->info.createServentXML());
1606 // -----------------------------------
1607 static XML::Node *createChannelXML(ChanHitList *chl)
1609 XML::Node *n = chl->info.createChannelXML();
1610 n->add(chl->createXML());
1611 n->add(chl->info.createTrackXML());
1612 // n->add(chl->info.createServentXML());
1615 // -----------------------------------
1616 void Servent::handshakeXML()
1624 XML::Node *rn = new XML::Node("peercast");
1628 rn->add(new XML::Node("servent uptime=\"%d\"",servMgr->getUptime()));
1630 rn->add(new XML::Node("bandwidth out=\"%d\" in=\"%d\"",
1631 stats.getPerSecond(Stats::BYTESOUT)-stats.getPerSecond(Stats::LOCALBYTESOUT),
1632 stats.getPerSecond(Stats::BYTESIN)-stats.getPerSecond(Stats::LOCALBYTESIN)
1635 rn->add(new XML::Node("connections total=\"%d\" relays=\"%d\" direct=\"%d\"",servMgr->totalConnected(),servMgr->numStreams(Servent::T_RELAY,true),servMgr->numStreams(Servent::T_DIRECT,true)));
1637 XML::Node *an = new XML::Node("channels_relayed total=\"%d\"",chanMgr->numChannels());
1640 Channel *c = chanMgr->channel;
1644 an->add(createChannelXML(c));
1649 // add public channels
1651 XML::Node *fn = new XML::Node("channels_found total=\"%d\"",chanMgr->numHitLists());
1654 ChanHitList *chl = chanMgr->hitlist;
1658 fn->add(createChannelXML(chl));
1665 if (servMgr->isRoot)
1667 // add private channels
1669 XML::Node *pn = new XML::Node("priv_channels");
1672 ChanHitList *chl = chanMgr->hitlist;
1676 if (chl->info.isPrivate())
1677 pn->add(createChannelXML(chl));
1684 XML::Node *hc = new XML::Node("host_cache");
1685 for(i=0; i<ServMgr::MAX_HOSTCACHE; i++)
1687 ServHost *sh = &servMgr->hostCache[i];
1688 if (sh->type != ServHost::T_NONE)
1691 sh->host.toStr(ipstr);
1693 hc->add(new XML::Node("host ip=\"%s\" type=\"%s\" time=\"%d\"",ipstr,ServHost::getTypeStr(sh->type),sh->time));
1700 sock->writeLine(HTTP_SC_OK);
1701 sock->writeLineF("%s %s",HTTP_HS_SERVER,PCX_AGENT);
1702 sock->writeLineF("%s %s",HTTP_HS_CONTENT,MIME_XML);
1703 sock->writeLine("Connection: close");
1705 sock->writeLine("");
1710 // -----------------------------------
1711 void Servent::readICYHeader(HTTP &http, ChanInfo &info, char *pwd, size_t szPwd)
1713 char *arg = http.getArgStr();
1716 if (http.isHeader("x-audiocast-name") || http.isHeader("icy-name") || http.isHeader("ice-name"))
1718 info.name.set(arg,String::T_ASCII);
1719 info.name.convertTo(String::T_UNICODE);
1721 }else if (http.isHeader("x-audiocast-url") || http.isHeader("icy-url") || http.isHeader("ice-url"))
1722 info.url.set(arg,String::T_ASCII);
1723 else if (http.isHeader("x-audiocast-bitrate") || (http.isHeader("icy-br")) || http.isHeader("ice-bitrate") || http.isHeader("icy-bitrate"))
1724 info.bitrate = atoi(arg);
1725 else if (http.isHeader("x-audiocast-genre") || http.isHeader("ice-genre") || http.isHeader("icy-genre"))
1727 info.genre.set(arg,String::T_ASCII);
1728 info.genre.convertTo(String::T_UNICODE);
1730 }else if (http.isHeader("x-audiocast-description") || http.isHeader("ice-description"))
1732 info.desc.set(arg,String::T_ASCII);
1733 info.desc.convertTo(String::T_UNICODE);
1735 }else if (http.isHeader("Authorization"))
1736 http.getAuthUserPass(NULL, pwd, 0, sizeof(pwd));
1737 else if (http.isHeader(PCX_HS_CHANNELID))
1738 info.id.fromStr(arg);
1739 else if (http.isHeader("ice-password"))
1742 if (strlen(arg) < 64)
1744 }else if (http.isHeader("content-type"))
1746 if (stristr(arg,MIME_OGG))
1747 info.contentType = ChanInfo::T_OGG;
1748 else if (stristr(arg,MIME_XOGG))
1749 info.contentType = ChanInfo::T_OGG;
1751 else if (stristr(arg,MIME_MP3))
1752 info.contentType = ChanInfo::T_MP3;
1753 else if (stristr(arg,MIME_XMP3))
1754 info.contentType = ChanInfo::T_MP3;
1756 else if (stristr(arg,MIME_WMA))
1757 info.contentType = ChanInfo::T_WMA;
1758 else if (stristr(arg,MIME_WMV))
1759 info.contentType = ChanInfo::T_WMV;
1760 else if (stristr(arg,MIME_ASX))
1761 info.contentType = ChanInfo::T_ASX;
1763 else if (stristr(arg,MIME_NSV))
1764 info.contentType = ChanInfo::T_NSV;
1765 else if (stristr(arg,MIME_RAW))
1766 info.contentType = ChanInfo::T_RAW;
1768 else if (stristr(arg,MIME_MMS))
1769 info.srcProtocol = ChanInfo::SP_MMS;
1770 else if (stristr(arg,MIME_XPCP))
1771 info.srcProtocol = ChanInfo::SP_PCP;
1772 else if (stristr(arg,MIME_XPEERCAST))
1773 info.srcProtocol = ChanInfo::SP_PEERCAST;
1775 else if (stristr(arg,MIME_XSCPLS))
1776 info.contentType = ChanInfo::T_PLS;
1777 else if (stristr(arg,MIME_PLS))
1778 info.contentType = ChanInfo::T_PLS;
1779 else if (stristr(arg,MIME_XPLS))
1780 info.contentType = ChanInfo::T_PLS;
1781 else if (stristr(arg,MIME_M3U))
1782 info.contentType = ChanInfo::T_PLS;
1783 else if (stristr(arg,MIME_MPEGURL))
1784 info.contentType = ChanInfo::T_PLS;
1785 else if (stristr(arg,MIME_TEXT))
1786 info.contentType = ChanInfo::T_PLS;
1793 // -----------------------------------
1794 void Servent::handshakeICY(Channel::SRC_TYPE type, bool isHTTP)
1800 // default to mp3 for shoutcast DSP (doesn`t send content-type)
1801 if (type == Channel::SRC_SHOUTCAST)
1802 info.contentType = ChanInfo::T_MP3;
1804 while (http.nextHeader())
1806 LOG_DEBUG("ICY %s",http.cmdLine);
1807 readICYHeader(http, info, loginPassword.cstr(), loginPassword.MAX_LEN);
1812 // check password before anything else, if needed
1813 if (!loginPassword.isSame(servMgr->password))
1815 if (!sock->host.isLocalhost() || !loginPassword.isEmpty())
1816 throw HTTPException(HTTP_SC_UNAUTHORIZED,401);
1820 // we need a valid IP address before we start
1821 servMgr->checkFirewall();
1824 // attach channel ID to name, channel ID is also encoded with IP address
1825 // to help prevent channel hijacking.
1828 info.id = chanMgr->broadcastID;
1829 info.id.encode(NULL,info.name.cstr(),loginMount.cstr(),info.bitrate);
1831 LOG_DEBUG("Incoming source: %s : %s",info.name.cstr(),ChanInfo::getTypeStr(info.contentType));
1835 sock->writeStringF("%s\n\n",HTTP_SC_OK);
1837 sock->writeLine("OK");
1839 Channel *c = chanMgr->findChannelByID(info.id);
1842 LOG_CHANNEL("ICY channel already active, closing old one");
1843 c->thread.shutdown();
1847 info.comment = chanMgr->broadcastMsg;
1848 info.bcID = chanMgr->broadcastID;
1850 c = chanMgr->createChannel(info,loginMount.cstr());
1852 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
1854 c->startICY(sock,type);
1858 // -----------------------------------
1859 void Servent::handshakeLocalFile(const char *fn)
1864 if (servMgr->getModulePath) //JP-EX
1866 peercastApp->getDirectory();
1867 fileName = servMgr->modulePath;
1869 fileName = peercastApp->getPath();
1871 fileName.append(fn);
1873 LOG_DEBUG("Writing HTML file: %s",fileName.cstr());
1875 HTML html("",*sock);
1877 char *args = strstr(fileName.cstr(),"?");
1881 if (fileName.contains(".htm"))
1883 html.writeOK(MIME_HTML);
1884 html.writeTemplate(fileName.cstr(),args);
1886 }else if (fileName.contains(".css"))
1888 html.writeOK(MIME_CSS);
1889 html.writeRawFile(fileName.cstr());
1890 }else if (fileName.contains(".jpg"))
1892 html.writeOK(MIME_JPEG);
1893 html.writeRawFile(fileName.cstr());
1894 }else if (fileName.contains(".gif"))
1896 html.writeOK(MIME_GIF);
1897 html.writeRawFile(fileName.cstr());
1898 }else if (fileName.contains(".png"))
1900 html.writeOK(MIME_PNG);
1901 html.writeRawFile(fileName.cstr());
1905 // -----------------------------------
1906 void Servent::handshakeRemoteFile(const char *dirName)
1908 ClientSocket *rsock = sys->createSocket();
1910 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
1913 const char *hostName = "www.peercast.org"; // hardwired for "security"
1916 host.fromStrName(hostName,80);
1924 rhttp.writeLineF("GET /%s HTTP/1.0",dirName);
1925 rhttp.writeLineF("%s %s",HTTP_HS_HOST,hostName);
1926 rhttp.writeLineF("%s %s",HTTP_HS_CONNECTION,"close");
1927 rhttp.writeLineF("%s %s",HTTP_HS_ACCEPT,"*/*");
1928 rhttp.writeLine("");
1931 bool isTemplate = false;
1932 while (rhttp.nextHeader())
1934 char *arg = rhttp.getArgStr();
1937 if (rhttp.isHeader("content-type"))
1942 MemoryStream mem(100*1024);
1943 while (!rsock->eof())
1947 len = rsock->readUpto(buf,sizeof(buf));
1956 int fileLen = mem.getPosition();
1961 if (contentType.contains(MIME_HTML))
1964 sock->writeLine(HTTP_SC_OK);
1965 sock->writeLineF("%s %s",HTTP_HS_SERVER,PCX_AGENT);
1966 sock->writeLineF("%s %s",HTTP_HS_CACHE,"no-cache");
1967 sock->writeLineF("%s %s",HTTP_HS_CONNECTION,"close");
1968 sock->writeLineF("%s %s",HTTP_HS_CONTENT,contentType.cstr());
1970 sock->writeLine("");
1974 HTML html("",*sock);
1975 html.readTemplate(mem,sock,0);
1977 sock->write(mem.buf,fileLen);