reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
FlowManagerSipXSocket.cxx
Go to the documentation of this file.
1// !slg! At least for builds in Visual Studio on windows this include needs to be above ASIO includes since inlined getErrno has
2// a different linkage signature if included after - haven't investigated the full details as to exactly why this happens
3#include <rutil/Socket.hxx>
4
5#include <asio.hpp>
6#ifdef USE_SSL
7#include <asio/ssl.hpp>
8#endif
9#include <rutil/Data.hxx>
10#include <rutil/DnsUtil.hxx>
11
12// SYSTEM INCLUDES
13#include "rutil/ResipAssert.h"
14#include <stdio.h>
15#ifndef _WIN32
16#include <netinet/in.h>
17#include <sys/socket.h>
18#include <arpa/inet.h>
19#endif
20
21// APPLICATION INCLUDES
22#include "reflow/Flow.hxx"
24
25using namespace std;
26using namespace recon;
27
28// Constructor
30 : OsSocket(),
31 mFlow(flow)
32{
33#ifndef WIN32
34 setsockopt (flow->getSocketDescriptor(), IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int));
35#else
36 // TODO:: Implement QoS request under Windows. The following works under NT 4.0 and Windows 9x only (http://support.microsoft.com/kb/248611)
37 setsockopt (flow->getSocketDescriptor(), IPPROTO_IP, 3 /* IP_TOS */, (char *)&tos, sizeof(int));
38#endif
39}
40
41
42// Destructor
46
48{
49 resip_assert(false);
50 return 0;
51}
52
58
59int FlowManagerSipXSocket::read(char* buffer, int bufferLength)
60{
61 //cout << "read: bufferlen=" << bufferLength << endl;
62 resip_assert(mFlow);
63 unsigned int len = bufferLength;
64 if(mFlow->receive(buffer, len, 0))
65 {
66 //cout << "read: done: len=0" << endl;
67 return 0;
68 }
69
70 //cout << "read: done: len=" << len << endl;
71 return len;
72}
73
74int FlowManagerSipXSocket::read(char* buffer, int bufferLength,
75 UtlString* ipAddress, int* port)
76{
77 asio::ip::address receivedAddress;
78 unsigned short receivedPort=0;
79
80 //cout << "read(get address): bufferlen=" << bufferLength << endl; // **********
81 resip_assert(mFlow);
82
83 unsigned int len = bufferLength;
84 if(mFlow->receive(buffer, len, 0, &receivedAddress, &receivedPort))
85 {
86 //cout << "read(get address): done, len=0" << endl;
87 return 0;
88 }
89
90 if (ipAddress)
91 {
92 *ipAddress = receivedAddress.to_string().c_str();
93 }
94
95 if (port)
96 {
97 *port = (int)receivedPort ;
98 }
99 //cout << "read(get address): done, len=" << len << endl;
100
101 return len;
102}
103
104int FlowManagerSipXSocket::read(char* buffer, int bufferLength,
105 struct in_addr* ipAddress, int* port)
106{
107 int iRC;
108 int iReceivedPort;
109 UtlString receivedIp;
110
111 iRC = read(buffer, bufferLength, &receivedIp, &iReceivedPort) ;
112 if (ipAddress)
113 {
114 if (resip::DnsUtil::inet_pton(AF_INET, receivedIp.data(), ipAddress) <= 0)
115 {
116 // Handle invalid IP string by zeroing the address
117 ipAddress->s_addr = INADDR_NONE;
118 }
119 }
120
121 if (port)
122 *port = iReceivedPort ;
123
124 return iRC ;
125}
126
127int FlowManagerSipXSocket::read(char* buffer, int bufferLength, long waitMilliseconds)
128{
129 //cout << "read: bufferlen=" << bufferLength << ", waitMilliseconds=" << waitMilliseconds << "ms" << endl;
130 resip_assert(mFlow);
131 unsigned int len = bufferLength;
132 if(mFlow->receive(buffer, len, waitMilliseconds))
133 {
134 return 0;
135 }
136 return len;
137}
138
139int FlowManagerSipXSocket::write(const char* buffer, int bufferLength)
140{
141 //cout << "write: bufferlen=" << bufferLength << endl; // *********
142 resip_assert(mFlow);
143 mFlow->send((char *)buffer, bufferLength);
144 return bufferLength;
145}
146
147int FlowManagerSipXSocket::write(const char* buffer,
148 int bufferLength,
149 const char* ipAddress,
150 int port)
151{
152 //cout << "write: bufferlen=" << bufferLength << ", address=" << ipAddress << ", port=" << port << endl;
153 resip_assert(mFlow);
154 mFlow->sendTo(asio::ip::make_address(ipAddress), port, (char*)buffer, bufferLength);
155 return bufferLength;
156}
157
158int FlowManagerSipXSocket::write(const char* buffer, int bufferLength,
159 long waitMilliseconds)
160{
161 //cout << "write: bufferlen=" << bufferLength << ", waitMilliseconds=" << waitMilliseconds << endl;
162 resip_assert(0);
163 mFlow->send((char*)buffer, bufferLength); // !SLG! We don't have a timed out send??? Not used by sipX anyway
164 return bufferLength;
165}
166
167
168/* ====================================================================
169
170 Copyright (c) 2021, Daniel Pocock https://danielpocock.com
171 Copyright (c) 2007-2008, Plantronics, Inc.
172 Copyright (c) 2008-2018, SIP Spectrum, Inc.
173 All rights reserved.
174
175 Redistribution and use in source and binary forms, with or without
176 modification, are permitted provided that the following conditions are
177 met:
178
179 1. Redistributions of source code must retain the above copyright
180 notice, this list of conditions and the following disclaimer.
181
182 2. Redistributions in binary form must reproduce the above copyright
183 notice, this list of conditions and the following disclaimer in the
184 documentation and/or other materials provided with the distribution.
185
186 3. Neither the name of Plantronics nor the names of its contributors
187 may be used to endorse or promote products derived from this
188 software without specific prior written permission.
189
190 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
194 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
195 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
196 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
198 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
200 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
201
202 ==================================================================== */
#define resip_assert(x)
This file needs to work for both C and C++ code.
Handles cross-platform sockets compatibility.
#define INADDR_NONE
Definition adig.c:51
This class represents a Flow that is created by the Flow Manager.
Definition Flow.hxx:45
unsigned int getSocketDescriptor()
Definition Flow.cxx:265
void send(char *buffer, unsigned int size)
Turn Send Methods WARNING - if using Secure media, then there must be room at the end of the passed i...
Definition Flow.cxx:272
void sendTo(const asio::ip::address &address, unsigned short port, char *buffer, unsigned int size)
Definition Flow.cxx:289
asio::error_code receive(char *buffer, unsigned int &size, unsigned int timeout, asio::ip::address *sourceAddress=0, unsigned short *sourcePort=0)
Receive Methods.
Definition Flow.cxx:412
unsigned int getSelectSocketDescriptor()
Returns a socket descriptor that can be used in a select call WARNING - this descriptor should not be...
Definition Flow.cxx:259
virtual int read(char *buffer, int bufferLength)
virtual int write(const char *buffer, int bufferLength)
FlowManagerSipXSocket(Flow *flow, int tos)
static int inet_pton(const Data &printableIp, struct in_addr &dst)
Convert from the presentation format of the IPv4 address to struct in_addr.
Definition DnsUtil.cxx:275