1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*
* MP3/MPlayer plugin to VDR (C++)
*
* (C) 2001-2009 Stefan Huelswitt <s.huelswitt@gmx.de>
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <vdr/tools.h>
#include "common.h"
#include "setup-mp3.h"
#include "network.h"
#include "data.h"
#define CON_TIMEOUT 30*1000 // default timeout (ms) for connect operation
#define RW_TIMEOUT 30*1000 // default timeout (ms) for read/write operations
#define BUFFERSIZE 128*1024 // default ringbuffer size (bytes) for async read
#define NETDOWN_TIMEOUT 30 // timeout (s) for shutting down network
const char *netscript=0;
// -----------------------------------------------------------------------------
int RunCommand(const char *cmd, const char *State, const char *Name=0)
{
int res=-1;
if(cmd) {
char *tmp;
if(Name) tmp=aprintf("%s %s \"%s\"",cmd,State,*strescape(Name,"\"$"));
else tmp=aprintf("%s %s",cmd,State);
d(printf("run: executing '%s'\n",tmp))
res=SystemExec(tmp);
free(tmp);
}
return res;
}
// -- cNetScript ---------------------------------------------------------------
class cNetScript : public cThread {
private:
int count;
bool pending;
protected:
virtual void Action(void);
public:
cNetScript(void);
~cNetScript();
void Up(void);
void Down(void);
};
cNetScript ns;
cNetScript::cNetScript(void)
{
count=0; pending=false;
}
cNetScript::~cNetScript()
{
if(pending) Cancel(0);
}
void cNetScript::Up(void)
{
Lock();
if(netscript) {
if(pending) { Cancel(0); pending=false; }
RunCommand(netscript,"up");
count++;
}
Unlock();
}
void cNetScript::Down(void)
{
Lock();
if(netscript) {
if(--count==0) { Start(); pending=true; }
}
Unlock();
}
void cNetScript::Action(void)
{
d(printf("net: netscript down delay\n"))
sleep(NETDOWN_TIMEOUT);
Lock();
RunCommand(netscript,"down");
Unlock();
}
// -- cNetConnect --------------------------------------------------------------
class cNetConnect : public cThread {
private:
int fd;
const char *hostname;
int port;
cMutex conMutex;
cCondVar conCond;
int result;
protected:
virtual void Action(void);
void Done(int res);
public:
cNetConnect(int Fd, const char *Hostname, int Port);
~cNetConnect();
int Wait(int timeoutMs);
};
cNetConnect::cNetConnect(int Fd, const char *Hostname, int Port)
{
fd=Fd;
hostname=Hostname;
port=Port;
result=0;
Start();
}
cNetConnect::~cNetConnect()
{
Cancel(1);
}
int cNetConnect::Wait(int timeoutMs)
{
conMutex.Lock();
if(!result) conCond.TimedWait(conMutex,timeoutMs);
conMutex.Unlock();
return result;
}
void cNetConnect::Done(int res)
{
conMutex.Lock();
result=res;
conCond.Broadcast();
conMutex.Unlock();
}
void cNetConnect::Action(void)
{
d(printf("net: name lookup %s\n",hostname))
struct hostent *hp=gethostbyname(hostname);
if(hp) {
struct sockaddr_in sin;
sin.sin_port=htons(port);
sin.sin_family=AF_INET;
memcpy((char *)&sin.sin_addr,hp->h_addr,hp->h_length);
d(printf("net: connecting to %s:%d\n",hostname,port))
if(connect(fd,(struct sockaddr *)&sin,sizeof(sin))==0) {
d(printf("net: connected\n"))
Done(1);
}
else { esyslog("connect() failed: %s",strerror(errno)); Done(-1); }
}
else { esyslog("Unknown host '%s'",hostname); Done(-1); }
}
// -- cNet ---------------------------------------------------------------------
cNet::cNet(int size, int ConTimeoutMs, int RwTimeoutMs)
:cRingBufferLinear(size>0?size:BUFFERSIZE,1,false)
{
fd=-1; deferedErrno=0; count=0;
connected=netup=false;
rwTimeout =RwTimeoutMs ? RwTimeoutMs :RW_TIMEOUT;
conTimeout=ConTimeoutMs ? ConTimeoutMs:CON_TIMEOUT;
SetTimeouts(50,50);
}
cNet::~cNet()
{
Disconnect();
}
void cNet::Close(void)
{
if(connected) {
connected=false;
Cancel(2);
deferedErrno=0;
}
if(fd>=0) { close(fd); fd=-1; }
Clear(); count=0;
}
void cNet::Disconnect(void)
{
Close();
if(netup) { ns.Down(); netup=false; }
}
bool cNet::Connect(const char *hostname, const int port)
{
Close();
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd>=0) {
ns.Up(); netup=true;
cNetConnect *con=new cNetConnect(fd,hostname,port);
int res=con->Wait(conTimeout);
delete con;
if(res>0) {
if(fcntl(fd,F_SETFL,O_NONBLOCK)>=0) {
deferedErrno=0; connected=true;
Start();
return(true);
}
else esyslog("fnctl() failed: %s",strerror(errno));
}
else if(res==0) esyslog("Connection timed out");
}
else esyslog("socket() failed: %s",strerror(errno));
Disconnect();
return false;
}
void cNet::CopyFromBuff(unsigned char *dest, int n)
{
memcpy(dest,lineBuff,n);
count-=n;
if(count>0) memmove(lineBuff,lineBuff+n,count);
}
int cNet::Gets(char *dest, int len)
{
len--; // let room for trailing zero
int c=0;
while(c<len) {
if(count<=0) {
int r=RingRead(lineBuff,sizeof(lineBuff));
if(r<0) {
if(c==0) return -1;
break;
}
count=r;
}
int n=0;
while(n<count && n+c<len) {
if(lineBuff[n]=='\n') len=0;
n++;
}
CopyFromBuff((unsigned char *)dest,n);
dest+=n; c+=n;
}
*dest=0;
return c;
}
int cNet::Read(unsigned char *dest, int len)
{
int c=0;
if(count>0) {
c=count; if(c>len) c=len;
CopyFromBuff(dest,c);
}
else {
c=RingRead(dest,len);
}
return c;
}
int cNet::Write(unsigned char *dest, int len)
{
int t=0, r;
cPoller poll(fd,true);
do {
if(poll.Poll(rwTimeout)) {
r=write(fd,dest,len);
if(r<0 && errno!=EAGAIN) {
esyslog("write() failed: %s",strerror(errno));
break;
}
dest+=r; len-=r; t+=r;
}
else { esyslog("Write timed out"); break; }
} while(len>0);
return t;
}
int cNet::Puts(char *dest)
{
return Write((unsigned char *)dest,strlen(dest));
}
int cNet::RingRead(unsigned char *dest, int len)
{
int r=0;
const uchar *rd;
for(;;) {
if(!Available() && deferedErrno) {
d(printf("net: ringbuffer empty, async read bailed out\n"))
return -1;
}
rd=Get(r);
if(rd && r>0) {
if(r>len) r=len;
memcpy(dest,rd,r);
Del(r);
return r;
}
}
}
void cNet::Action(void)
{
d(printf("net: async read started\n"))
cPoller poll(fd,false);
while(connected) {
if(poll.Poll(rwTimeout)) {
unsigned char buff[8192];
int r=read(fd,buff,sizeof(buff));
if(r>0) {
int d=0;
do { d+=Put(buff+d,r-d); } while(d<r && connected);
}
else if(r<0 && errno!=EAGAIN) {
deferedErrno=errno;
esyslog("read() failed: %s",strerror(errno));
break;
}
else if(r==0) {
deferedErrno=-1;
d(printf("EOF from read()\n"))
break;
}
}
else {
deferedErrno=-1;
esyslog("Read timed out");
break;
}
}
EnableGet();
connected=false;
}
|