~ubuntu-branches/ubuntu/utopic/resiprocate/utopic

1 by Daniel Pocock
Import upstream version 1.8.2
1
#include <asio.hpp>
1.1.26 by Daniel Pocock
Import upstream version 1.9.7
2
#ifdef USE_SSL
3
#include <asio/ssl.hpp>
4
#endif
1 by Daniel Pocock
Import upstream version 1.8.2
5
#include <boost/function.hpp>
6
#include <rutil/Data.hxx>
7
8
// SYSTEM INCLUDES
9
#include <assert.h>
10
#include <stdio.h>
11
#ifndef _WIN32
12
#include <netinet/in.h>
13
#include <sys/socket.h>
14
#include <arpa/inet.h>
15
#endif
16
17
// APPLICATION INCLUDES
1.1.2 by Daniel Pocock
Import upstream version 1.8.8
18
#include "reflow/Flow.hxx"
1 by Daniel Pocock
Import upstream version 1.8.2
19
#include "FlowManagerSipXSocket.hxx"
20
21
using namespace std;
22
using namespace recon;
23
24
// Constructor
25
FlowManagerSipXSocket::FlowManagerSipXSocket(Flow* flow, int tos) 
26
        : OsSocket(),
27
        mFlow(flow)
28
{    
29
#ifndef WIN32
30
   setsockopt (flow->getSocketDescriptor(), IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int));
31
#else
32
   // TODO:: Implement QoS  request under Windows.  The following works under NT 4.0 and Windows 9x only (http://support.microsoft.com/kb/248611)
33
   setsockopt (flow->getSocketDescriptor(), IPPROTO_IP, 3 /* IP_TOS */, (char *)&tos, sizeof(int));
34
#endif
35
}
36
37
38
// Destructor
39
FlowManagerSipXSocket::~FlowManagerSipXSocket()
40
{
41
}
42
43
OsSocket* FlowManagerSipXSocket::getSocket()
44
{
45
    assert(false);
46
    return 0;
47
}
48
49
int FlowManagerSipXSocket::getSocketDescriptor() const
50
{ 
51
   assert(mFlow);
52
   return mFlow->getSelectSocketDescriptor();
53
}
54
55
int FlowManagerSipXSocket::read(char* buffer, int bufferLength)
56
{
57
   //cout << "read: bufferlen=" << bufferLength << endl;
58
   assert(mFlow);
59
   unsigned int len = bufferLength;
60
   if(mFlow->receive(buffer, len, 0))
61
   {
62
      //cout << "read: done: len=0" << endl;
63
      return 0;
64
   }
65
66
   //cout << "read: done: len=" << len << endl;
67
   return len;
68
}
69
70
int FlowManagerSipXSocket::read(char* buffer, int bufferLength,
71
       UtlString* ipAddress, int* port)
72
{
73
   asio::ip::address receivedAddress;
74
   unsigned short receivedPort=0;
75
76
   //cout << "read(get address): bufferlen=" << bufferLength << endl;  // **********
77
   assert(mFlow);
78
79
   unsigned int len = bufferLength;
80
   if(mFlow->receive(buffer, len, 0, &receivedAddress, &receivedPort))
81
   {
82
      //cout << "read(get address): done, len=0" << endl;
83
      return 0;
84
   }
85
86
   if (ipAddress)
87
   {
88
      *ipAddress = receivedAddress.to_string().c_str();
89
   }
90
91
   if (port)
92
   {
93
       *port = (int)receivedPort ;
94
   }
95
   //cout << "read(get address): done, len=" << len << endl;
96
97
   return len;
98
}
99
100
int FlowManagerSipXSocket::read(char* buffer, int bufferLength,
101
       struct in_addr* ipAddress, int* port)
102
{
103
    int iRC ;
104
    int iReceivedPort ;
105
    UtlString receivedIp ;
106
107
    iRC = read(buffer, bufferLength, &receivedIp, &iReceivedPort) ;
108
    if (ipAddress)
109
        ipAddress->s_addr = inet_addr(receivedIp) ;
110
111
    if (port)
112
        *port = iReceivedPort ;
113
114
    return iRC ;
115
}
116
117
118
int FlowManagerSipXSocket::read(char* buffer, int bufferLength, long waitMilliseconds)
119
{        
120
   //cout << "read: bufferlen=" << bufferLength << ", waitMilliseconds=" << waitMilliseconds << "ms" << endl;
121
   assert(mFlow);
122
   unsigned int len = bufferLength;
123
   if(!mFlow->receive(buffer, len, waitMilliseconds))
124
   {
125
      return len;
126
   }
127
   else
128
   {
129
      return 0;
130
   }
131
}
132
133
int FlowManagerSipXSocket::write(const char* buffer, int bufferLength)
134
{
135
    //cout << "write: bufferlen=" << bufferLength << endl;  // *********
136
    assert(mFlow);
137
    mFlow->send((char *)buffer, bufferLength);
138
    return 0;
139
}
140
141
int FlowManagerSipXSocket::write(const char* buffer, 
142
                               int bufferLength,
143
                               const char* ipAddress, 
144
                               int port)
145
{
146
   //cout << "write: bufferlen=" << bufferLength << ", address=" << ipAddress << ", port=" << port << endl;
147
   assert(mFlow);
148
   mFlow->sendTo(asio::ip::address::from_string(ipAddress), port, (char*)buffer, bufferLength);
149
   return 0;
150
}
151
152
int FlowManagerSipXSocket::write(const char* buffer, int bufferLength, 
153
                               long waitMilliseconds)
154
{
155
    //cout << "write: bufferlen=" << bufferLength << ", waitMilliseconds=" << waitMilliseconds << endl;
156
    assert(0);
157
    mFlow->send((char*)buffer, bufferLength);  // !SLG! We don't have a timed out send???  Not used by sipX anyway
158
    return 0;
159
}
160
161
162
/* ====================================================================
163
164
 Copyright (c) 2007-2008, Plantronics, Inc.
165
 All rights reserved.
166
167
 Redistribution and use in source and binary forms, with or without
168
 modification, are permitted provided that the following conditions are 
169
 met:
170
171
 1. Redistributions of source code must retain the above copyright 
172
    notice, this list of conditions and the following disclaimer. 
173
174
 2. Redistributions in binary form must reproduce the above copyright
175
    notice, this list of conditions and the following disclaimer in the
176
    documentation and/or other materials provided with the distribution. 
177
178
 3. Neither the name of Plantronics nor the names of its contributors 
179
    may be used to endorse or promote products derived from this 
180
    software without specific prior written permission. 
181
182
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
183
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
184
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
185
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
186
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
187
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
188
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
189
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
190
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
191
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
192
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
193
194
 ==================================================================== */