7
#include <rutil/Data.hxx>
8
#include <rutil/Socket.hxx>
9
#include <resip/stack/Symbols.hxx>
10
#include <rutil/TransportType.hxx>
11
#include <rutil/Logger.hxx>
12
#include <resip/stack/Tuple.hxx>
13
#include <rutil/DnsUtil.hxx>
14
#include <rutil/ParseBuffer.hxx>
15
#include <resip/stack/Transport.hxx>
17
#include "AppSubsystem.hxx"
18
#include "HttpBase.hxx"
19
#include "HttpConnection.hxx"
20
#include "WebAdmin.hxx"
21
#include "rutil/WinLeakCheck.hxx"
24
using namespace resip;
25
using namespace mohparkserver;
28
#define RESIPROCATE_SUBSYSTEM AppSubsystem::MOHPARKSERVER
39
for( int i=0; i<MaxConnections; i++)
43
delete mConnection[i] ; mConnection[i]=0;
49
HttpBase::HttpBase( int port, IpVersion ipVer, const Data& realm ):
52
mTuple(Data::Empty,port,ipVer,TCP,Data::Empty)
56
for ( int i=0 ; i<MaxConnections; i++)
62
mFd = ::socket(ipVer == V4 ? PF_INET : PF_INET6, SOCK_STREAM, 0);
64
mFd = ::socket(PF_INET, SOCK_STREAM, 0);
67
if ( mFd == INVALID_SOCKET )
70
ErrLog (<< "Failed to create socket: " << strerror(e));
75
DebugLog (<< "Creating fd=" << (int)mFd
76
<< (ipVer == V4 ? " V4/" : " V6/") );
80
if ( ::setsockopt ( mFd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) )
82
if ( ::setsockopt ( mFd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) )
86
ErrLog (<< "Couldn't set sockoptions SO_REUSEPORT | SO_REUSEADDR: " << strerror(e));
91
DebugLog (<< "Binding to " << Tuple::inet_ntop(mTuple));
93
if ( ::bind( mFd, &mTuple.getMutableSockaddr(), mTuple.length()) == SOCKET_ERROR )
96
if ( e == EADDRINUSE )
98
ErrLog (<< mTuple << " already in use ");
102
ErrLog (<< "Could not bind to " << mTuple);
108
bool ok = makeSocketNonBlocking(mFd);
111
ErrLog (<< "Could not make HTTP socket non-blocking " << port );
116
// do the listen, seting the maximum queue size for compeletly established
117
// sockets -- on linux, tcp_max_syn_backlog should be used for the incomplete
118
// queue size(see man listen)
119
int e = listen(mFd,5 );
124
InfoLog (<< "Failed listen " << strerror(e));
132
HttpBase::buildFdSet(FdSet& fdset)
134
fdset.setRead( mFd );
136
for( int i=0; i<MaxConnections; i++)
138
if ( mConnection[i] )
140
mConnection[i]->buildFdSet(fdset);
147
HttpBase::process(FdSet& fdset)
149
if (fdset.readyToRead(mFd))
152
struct sockaddr& peer = tuple.getMutableSockaddr();
153
socklen_t peerLen = tuple.length();
154
Socket sock = accept( mFd, &peer, &peerLen);
155
if ( sock == SOCKET_ERROR )
161
// !jf! this can not be ready in some cases
164
ErrLog(<< "Some error reading from socket: " << e);
165
// .bwc. This is almost certainly a bad assert that a nefarious
166
// endpoint could hit.
167
// assert(0); // Transport::error(e);
171
makeSocketNonBlocking(sock);
173
int c = nextConnection;
174
nextConnection = ( nextConnection+1 ) % MaxConnections;
176
if ( mConnection[c] )
178
delete mConnection[c]; mConnection[c] = 0;
181
mConnection[c] = new HttpConnection(*this,sock);
183
DebugLog (<< "Received TCP connection as connection=" << c << " fd=" << (int)sock);
186
for( int i=0; i<MaxConnections; i++)
188
if ( mConnection[i] )
190
bool ok = mConnection[i]->process(fdset);
193
delete mConnection[i]; mConnection[i]=0;
200
void HttpBase::setPage( const Data& page, int pageNumber, int response, const Mime& type )
202
for ( int i=0 ; i<MaxConnections; i++)
204
if ( mConnection[i] )
206
if ( mConnection[i]->mPageNumber == pageNumber )
208
mConnection[i]->setPage( page,response,type );
214
bool HttpBase::isSane()
219
/* ====================================================================
220
* The Vovida Software License, Version 1.0
222
* Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
224
* Redistribution and use in source and binary forms, with or without
225
* modification, are permitted provided that the following conditions
228
* 1. Redistributions of source code must retain the above copyright
229
* notice, this list of conditions and the following disclaimer.
231
* 2. Redistributions in binary form must reproduce the above copyright
232
* notice, this list of conditions and the following disclaimer in
233
* the documentation and/or other materials provided with the
236
* 3. The names "VOCAL", "Vovida Open Communication Application Library",
237
* and "Vovida Open Communication Application Library (VOCAL)" must
238
* not be used to endorse or promote products derived from this
239
* software without prior written permission. For written
240
* permission, please contact vocal@vovida.org.
242
* 4. Products derived from this software may not be called "VOCAL", nor
243
* may "VOCAL" appear in their name, without prior written
244
* permission of Vovida Networks, Inc.
246
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
247
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
249
* NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
250
* NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
251
* IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
252
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
253
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
254
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
255
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
257
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
260
* ====================================================================
262
* This software consists of voluntary contributions made by Vovida
263
* Networks, Inc. and many individuals on behalf of Vovida Networks,
264
* Inc. For more information on Vovida Networks, Inc., please see
265
* <http://www.vovida.org/>.