reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
reprocmd.cpp
Go to the documentation of this file.
1#include <rutil/DnsUtil.hxx>
3#include <rutil/XMLCursor.hxx>
6
7using namespace resip;
8using namespace std;
9
10int
11main(int argc, char** argv)
12{
13#ifdef WIN32
15#endif
16 int sd, rc;
17 struct sockaddr_in localAddr, servAddr;
18 const char* host;
19 short port;
20 int cmdIndex;
21
22 // Look for forward slash in args 1 or 3
23 if (argc >= 2 && argv[1][0] == '/')
24 {
25 host = "127.0.0.1";
26 port = 5081;
27 cmdIndex = 1;
28 }
29 else if (argc >= 4 && argv[3][0] == '/')
30 {
31 host = argv[1];
32 port = (short)atoi(argv[2]);
33 cmdIndex = 3;
34 }
35 else
36 {
37 cerr << "usage: " << argv[0] << " [<server> <port>] /<command> [<parm>=<value>]" << endl;
38 cerr << " Valid Commands are:" << endl;
39 cerr << " /GetStackInfo - retrieves low level information about the stack state" << endl;
40 cerr << " /GetStackStats - retrieves a dump of the stack statistics" << endl;
41 cerr << " /ResetStackStats - resets all cumulative stack statistics to zero" << endl;
42 cerr << " /LogDnsCache - causes the DNS cache contents to be written to the resip logs" << endl;
43 cerr << " /ClearDnsCache - empties the stacks DNS cache" << endl;
44 cerr << " /GetDnsCache - retrieves the DNS cache contents" << endl;
45 cerr << " /GetCongestionStats - retrieves the stacks congestion manager stats and state" << endl;
46 cerr << " /SetCongestionTolerance metric=<SIZE|WAIT_TIME|TIME_DEPTH> maxTolerance=<value>" << endl;
47 cerr << " [fifoDescription=<desc>] - sets congestion tolerances" << endl;
48 cerr << " /Shutdown - signal the proxy to shut down." << endl;
49 cerr << " /Restart - signal the proxy to restart - leaving active registrations in place." << endl;
50 cerr << " /GetProxyConfig - retrieves the all of configuration file settings currently" << endl;
51 cerr << " being used by the proxy" << endl;
52 cerr << " /AddTransport type=<UDP|TCP|etc.> port=<value> [ipVersion=<V4|V6>]" << endl;
53 cerr << " [interface=<ipaddress>] [rruri=<AUTO|sip:host:port>]" << endl;
54 cerr << " [udprcvbuflen=<value>] [stun=<YES|NO>] [flags=<uint>]" << endl;
55 cerr << " [domain=<tlsdomainname>] [ssltype=<DynamicTLS|TLSv1>]" << endl;
56 cerr << " [certfile=<filename>] [keyfile=<filename>]" << endl;
57 cerr << " [tlscvm=<NONE|OPT|MAN>] [tlsuseemail=<YES|NO>]" << endl;
58 cerr << " - adds a new transport to the stack." << endl;
59 cerr << " /RemoveTransport key=<transportKey> - removes the requested transport" << endl;
60 exit(1);
61 }
62
63 // getaddrinfo replaces deprecated gethostbyname
64 struct addrinfo hints = {};
65 struct addrinfo* res = nullptr;
66 hints.ai_family = AF_INET;
67 hints.ai_socktype = SOCK_STREAM;
68
69 if (getaddrinfo(host, nullptr, &hints, &res) != 0 || res == nullptr)
70 {
71 cerr << "unknown host " << host << endl;
72 exit(1);
73 }
74
75 memcpy(&servAddr, res->ai_addr, res->ai_addrlen);
76 servAddr.sin_port = htons(port);
77 freeaddrinfo(res);
78
79 // Create TCP Socket
80 sd = (int)socket(AF_INET, SOCK_STREAM, 0);
81 if (sd < 0)
82 {
83 cerr << "cannot open socket" << endl;
84 exit(1);
85 }
86
87 // bind to any local interface/port
88 localAddr = {};
89 localAddr.sin_family = AF_INET;
90 localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
91 localAddr.sin_port = 0;
92 rc = ::bind(sd, (struct sockaddr*)&localAddr, sizeof(localAddr));
93 if (rc < 0)
94 {
95 cerr << "error binding locally" << endl;
96 exit(1);
97 }
98
99 // Connect to server
100 rc = ::connect(sd, (struct sockaddr*)&servAddr, sizeof(servAddr));
101 if (rc < 0)
102 {
103 cerr << "error connecting" << endl;
104 exit(1);
105 }
106
107 Data command(&argv[cmdIndex][1]); // [1] skips leading slash
108 Data request(1024, Data::Preallocate);
109 bool dumpRawResult = false;
110 if (isEqualNoCase(command, "initialsync"))
111 {
112 request += "<InitialSync>\r\n";
113 request += " <Request>\r\n";
114 request += " <Version>" + Data(REGSYNC_VERSION) + "</Version>\r\n";
115 request += " </Request>\r\n";
116 request += "</InitialSync>\r\n";
117
118 dumpRawResult = true;
119 }
120 else
121 {
122 request += "<";
123 request += command;
124 request += ">\r\n <Request>\r\n";
125 for (int i = cmdIndex + 1; i < argc; i++)
126 {
127 Data parm;
128 Data value;
129 Data arg(argv[i]);
130 ParseBuffer pb(arg);
131 const char *anchor = pb.position();
132 pb.skipToChar('=');
133 if (!pb.eof())
134 {
135 pb.data(parm, anchor);
136 pb.skipChar();
137 anchor = pb.position();
138 pb.skipToEnd();
139 pb.data(value, anchor);
140 }
141 request += " <";
142 request += parm;
143 request += ">";
144 request += value;
145 request += "</";
146 request += parm;
147 request += ">\r\n";
148 }
149 request += " </Request>\r\n";
150 request += "</";
151 request += command;
152 request += ">\r\n";
153 }
154
155 //cout << "Sending:\r\n" << request << endl;
156
157 rc = send(sd, request.c_str(), (int)request.size(), 0);
158 if(rc < 0)
159 {
160 cerr << "error sending request on socket." << endl;
161 closeSocket(sd);
162 exit(1);
163 }
164
165 char readBuffer[8000];
166 while(rc > 0)
167 {
168 rc = recv(sd, (char*)&readBuffer, sizeof(readBuffer), 0);
169 if(rc < 0)
170 {
171 cerr << "error receiving response from socket." << endl;
172 closeSocket(sd);
173 exit(1);
174 }
175
176 if(rc > 0)
177 {
178 Data response(Data::Borrow, (const char*)&readBuffer, rc);
179
180 if (dumpRawResult)
181 {
182 //cout << "Received response: \r\n" << response.xmlCharDataDecode() << endl;
183 cout << "Received response: \r\n" << response << endl;
184 }
185 else
186 {
187 ParseBuffer pb(response);
188 XMLCursor xml(pb);
189 bool responseOK = false;
190 if (xml.firstChild() && xml.nextSibling() && xml.firstChild()) // Move to Response node
191 {
192 while (true)
193 {
194 if (isEqualNoCase(xml.getTag(), "Result"))
195 {
196 unsigned int code = 0;
197 Data text;
199 if (it != xml.getAttributes().end())
200 {
201 code = it->second.convertUnsignedLong();
202 }
203 if (xml.firstChild())
204 {
205 text = xml.getValue().xmlCharDataDecode();
206 xml.parent();
207 }
208 if (code >= 200 && code < 300)
209 {
210 // Success
211 cout << text << endl;
212 }
213 else
214 {
215 cout << "Error " << code << " processing request: " << text << endl;
216 }
217 responseOK = true;
218 }
219 else if (isEqualNoCase(xml.getTag(), "Data"))
220 {
221 if (xml.firstChild())
222 {
223 cout << xml.getValue().xmlCharDataDecode() << endl;
224 xml.parent();
225 }
226 }
227 if (!xml.nextSibling())
228 {
229 // break on no more sibilings
230 break;
231 }
232 }
233 }
234 if (!responseOK)
235 {
236 cout << "Unable to parse response:" << endl << response << endl;
237 }
238 closeSocket(sd);
239 break;
240 }
241 }
242 }
243}
244
245
246/* ====================================================================
247 * The Vovida Software License, Version 1.0
248 *
249 * Copyright (c) 2026, SIP Spectrum, Inc. http://sipspectrum.com
250 * Copyright (c) 2000-2012
251 *
252 * Redistribution and use in source and binary forms, with or without
253 * modification, are permitted provided that the following conditions
254 * are met:
255 *
256 * 1. Redistributions of source code must retain the above copyright
257 * notice, this list of conditions and the following disclaimer.
258 *
259 * 2. Redistributions in binary form must reproduce the above copyright
260 * notice, this list of conditions and the following disclaimer in
261 * the documentation and/or other materials provided with the
262 * distribution.
263 *
264 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
265 * and "Vovida Open Communication Application Library (VOCAL)" must
266 * not be used to endorse or promote products derived from this
267 * software without prior written permission. For written
268 * permission, please contact vocal@vovida.org.
269 *
270 * 4. Products derived from this software may not be called "VOCAL", nor
271 * may "VOCAL" appear in their name, without prior written
272 * permission of Vovida Networks, Inc.
273 *
274 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
275 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
276 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
277 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
278 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
279 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
280 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
281 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
282 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
283 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
284 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
285 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
286 * DAMAGE.
287 *
288 * ====================================================================
289 *
290 * This software consists of voluntary contributions made by Vovida
291 * Networks, Inc. and many individuals on behalf of Vovida Networks,
292 * Inc. For more information on Vovida Networks, Inc., please see
293 * <http://www.vovida.org/>.
294 *
295 */
296/*
297 * vi: set shiftwidth=3 expandtab:
298 */
299
#define REGSYNC_VERSION
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
Data xmlCharDataDecode() const
Performs in-place XML Character Data un-escaping of a Data.
Definition Data.cxx:1464
const char * c_str() const
Returns a null-terminated string representing.
Definition Data.cxx:1097
@ Borrow
The Data instance is borrowing the memory from the passed in buffer.
Definition Data.hxx:260
static const PreallocateType Preallocate
used only to disambiguate constructors
Definition Data.hxx:116
size_type size() const noexcept
Returns the number of bytes in this Data.
Definition Data.hxx:599
iterator find(const Key &key)
Provides string-parsing functionality with protection from buffer overflow.
CurrentPosition skipToEnd()
CurrentPosition skipToChar(char c)
CurrentPosition position() const
CurrentPosition skipChar()
void data(Data &data, const char *start) const
make the passed in data share memory with the buffer (uses Data::Share)
const Data & getValue() const
const Data & getTag() const
const AttributeMap & getAttributes() const
dcm! – add UnusedChecking(_enum) below;
bool isEqualNoCase(const Data &left, const Data &right)
Definition Data.hxx:1116
void initNetwork()
set up network - does nothing in unix but needed for windows
Definition Socket.cxx:83
int closeSocket(Socket fd)
Definition Socket.cxx:134
int main(int argc, char **argv)
Definition reprocmd.cpp:11