SourceForge has been redesigned. Learn more.
Close

[50ef25]: / utils / sipquery.cpp  Maximize  Restore  History

Download this file

315 lines (260 with data), 8.9 kB

  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
// Copyright (C) 2008-2010 David Sugar, Tycho Softworks.
//
// This program 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 3 of the License, or
// (at your option) any later version.
//
// This program 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, see <http://www.gnu.org/licenses/>.
#include <sipwitch-config.h>
#include <eXosip2/eXosip.h>
#include <ucommon/ucommon.h>
static int verbose = 0;
static int port = 0;
static int family = AF_INET;
static int protocol = IPPROTO_UDP;
static const char *server = NULL;
static const char *forwarded = NULL;
static const char *proxy = NULL;
static const char *binding = NULL;
static unsigned timeout = 1000;
static int tls = 0;
#if defined(_MSWINDOWS_) && defined(__GNUC__)
// binds addrinfo for mingw32 linkage since otherwise mingw32 cannot
// cannot link proper getaddrinfo/freeaddrinfo calls that eXosip uses.
static Socket::address localhost("127.0.0.1");
#endif
using namespace UCOMMON_NAMESPACE;
PROGRAM_MAIN(argc, argv)
{
int error = 2;
eXosip_event_t *sevent;
const char *cp, *user;
char buffer[256];
char pbuffer[256];
char tbuffer[256];
osip_message_t *msg = NULL;
int rid;
osip_contact_t *contact;
int pos = 0;
int stop = 0;
cp = getenv("SIP_PROXY");
if(cp)
proxy = cp;
cp = getenv("SIP_SERVER");
if(cp)
server = cp;
while(NULL != *(++argv)) {
if(!strcmp(*argv, "--")) {
++argv;
break;
}
if(!strncmp(*argv, "--", 2))
++*argv;
if(!strcmp(*argv, "-q") || !strcmp(*argv, "-quiet")) {
verbose = 0;
continue;
}
if(!strcmp(*argv, "-version")) {
printf("sipquery 0.1\n"
"Copyright (C) 2008 David Sugar, Tycho Softworks\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n");
exit(0);
}
if(!strcmp(*argv, "-v") || !strcmp(*argv, "-verbose")) {
verbose = 1;
continue;
}
if(!strcmp(*argv, "-t") || !strcmp(*argv, "-timeout")) {
if(NULL == *(++argv))
shell::errexit(3, "*** sipuser: timeout option missing timeout\n");
timeout = atoi(*argv) * 1000;
if(!timeout)
shell::errexit(3, "*** sipuser: timeout option invalid\n");
continue;
}
if(!strncmp(*argv, "-timeout=", 9)) {
timeout = atoi(*argv + 9) * 1000;
if(!timeout)
shell::errexit(3, "*** sipuser: timeout option invalid\n");
continue;
}
if(!strcmp(*argv, "-p") || !strcmp(*argv, "-port")) {
if(NULL == *(++argv))
shell::errexit(3, "*** sipuser: port option missing port\n");
port = atoi(*argv);
if(!port)
shell::errexit(3, "*** sipuser: port option invalid number\n");
continue;
}
if(!strncmp(*argv, "-port=", 6)) {
port = atoi(*argv + 6);
if(!port)
shell::errexit(3, "*** sipuser: port option invalid number\n");
continue;
}
if(!strcmp(*argv, "-proxy")) {
proxy = *(++argv);
if(!proxy)
shell::errexit(3, "*** sipuser: proxy option missing proxy\n");
continue;
}
if(!strncmp(*argv, "-proxy=", 7)) {
proxy = *argv + 7;
continue;
}
if(!strcmp(*argv, "-server")) {
server = *(++argv);
if(!server)
shell::errexit(3, "*** sipuser: server option missing proxy\n");
continue;
}
if(!strncmp(*argv, "-server=", 8)) {
server = *argv + 8;
continue;
}
if(!strcmp(*argv, "-forward")) {
forwarded = *(++argv);
if(!forwarded)
shell::errexit(3, "*** sipuser: forwarding option missing interface\n");
continue;
}
if(!strncmp(*argv, "-forward=", 9)) {
forwarded = *argv + 8;
continue;
}
if(!strcmp(*argv, "-?") || !strcmp(*argv, "-h") || !strcmp(*argv, "-help")) {
fprintf(stderr, "usage: sipuser [options] userid\n"
"[options]\n"
" -proxy sip:proxyhost[:port]\n"
" -server sip:server[:port]\n"
" -forward ip-address\n"
" -port port-numer\n"
" -timeout seconds\n"
" -verbsose\n"
"Report bugs to sipwitch-devel@gnu.org\n");
exit(3);
}
if(**argv == '-')
shell::errexit(3, "*** sipuser: %s: unknown option\n", *argv);
break;
}
if(!*argv) {
usage:
shell::errexit(3, "use: sipuser [options] userid\n");
}
user = *(argv++);
if(*argv)
goto usage;
#if defined(WIN32) || defined(_WIn32)
if(!port)
port = 5060;
#else
if(!port)
port = 5060 + getuid();
#endif
if(eXosip_init())
shell::errexit(3, "*** sipuser: failed exosip init\n");
#ifdef AF_INET6
if(family == AF_INET6) {
eXosip_enable_ipv6(1);
if(binding == NULL)
binding = "::0";
}
#endif
if(eXosip_listen_addr(protocol, binding, port, family, tls)) {
#ifdef AF_INET6
if(!binding && family == AF_INET6)
binding = "::0";
#endif
if(!binding)
binding = "*";
shell::errexit(3, "*** sipuser: failed to listen %s:%d\n", binding, port);
}
if(forwarded)
eXosip_masquerade_contact(forwarded, port);
eXosip_set_user_agent("SIPW/sipquery");
if(!strncmp(user, "sip:", 4)) {
tls = 0;
user += 4;
}
else if(!strncmp(user, "sips:", 5)) {
tls = 1;
user += 5;
}
if(!server && strchr(user, '@')) {
server = strchr(user, '@');
++server;
}
if(server && !strncmp(server, "sip:", 4))
server += 4;
else if(server && !strncmp(server, "sips:", 5))
server += 5;
#ifdef AF_INET6
if(family == AF_INET6 && server == NULL)
server = "::1";
#endif
if(server == NULL)
server = "127.0.0.1";
if(!proxy)
proxy = server;
if(strncmp(proxy, "sip:", 4) && strncmp(proxy, "sips:", 5)) {
if(tls)
snprintf(pbuffer, sizeof(pbuffer), "sips:%s", proxy);
else
snprintf(pbuffer, sizeof(pbuffer), "sip:%s", proxy);
proxy = pbuffer;
}
if(tls && !strchr(user, '@'))
snprintf(buffer, sizeof(buffer), "sips:%s@%s", user, server);
else if(!strchr(user, '@'))
snprintf(buffer, sizeof(buffer), "sip:%s@%s", user, server);
else if(tls)
snprintf(buffer, sizeof(buffer), "sips:%s", user);
else
snprintf(buffer, sizeof(buffer), "sip:%s", user);
eXosip_lock();
rid = eXosip_register_build_initial_register(buffer, proxy, NULL, 60, &msg);
if(!msg) {
eXosip_unlock();
shell::errexit(3, "*** sipuser: cannot create query for %s\n", user);
}
snprintf(tbuffer, sizeof(tbuffer), "<%s>", buffer);
osip_message_set_to(msg, tbuffer);
osip_list_ofchar_free(&msg->contacts);
eXosip_register_send_register(rid, msg);
eXosip_unlock();
while(!stop) {
sevent = eXosip_event_wait(0, timeout);
if(!sevent)
shell::errexit(2, "*** sipuser: timed out\n");
if(sevent->type == EXOSIP_REGISTRATION_FAILURE) {
error = 1;
++stop;
}
if(sevent->type == EXOSIP_REGISTRATION_SUCCESS) {
pos = 0;
while(verbose && sevent->response && !osip_list_eol(OSIP2_LIST_PTR sevent->response->contacts, pos)) {
contact = (osip_contact_t *)osip_list_get(OSIP2_LIST_PTR sevent->response->contacts, pos++);
if(contact && contact->url)
printf("%s:%s@%s:%s\n",
contact->url->scheme, contact->url->username,
contact->url->host, contact->url->port);
}
error = 0;
++stop;
}
eXosip_event_free(sevent);
}
eXosip_register_remove(rid);
eXosip_quit();
PROGRAM_EXIT(error);
}