-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsipdial.ino
574 lines (517 loc) · 14.6 KB
/
sipdial.ino
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* ====================================================================
*
* Copyright (c) 2018 Juerge Liegner All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. Neither the name of the author(s) nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================*/
#include <ESP8266WiFi.h>
#include <WifiUdp.h>
//------------------------------------------------
// configuration with fix ip
//------------------------------------------------
// wlan param
const char* ssid = "ssid";
const char* password = "geheim";
// sip params
const char *sipip = "192.168.8.1";
int sipport = 5060;
const char *sipuser = "fbox-627";
const char *sippasswd = "geheim";
// dial params
const char *sipdialnr = "**622";
const char *sipdialtext = "Tuerklingel";
// network params
const char *ip = "192.168.8.69";
const char *gw = "192.168.8.1";
const char *mask = "255.255.255.0";
const char *dns = "192.168.8.1";
//------------------------------------------------
#define DEBUGLOG
WiFiUDP Udp;
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// hardware and api independent Sip class
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
class Sip
{
char *pbuf;
size_t lbuf;
char caRead[256];
const char *pSipIp;
int iSipPort;
const char *pSipUser;
const char *pSipPassWd;
const char *pMyIp;
int iMyPort;
const char *pDialNr;
const char *pDialDesc;
uint32_t callid;
uint32_t tagid;
uint32_t branchid;
int iAuthCnt;
uint32_t iRingTime;
uint32_t iMaxTime;
int iDialRetries;
int iLastCSeq;
void AddSipLine(const char* constFormat , ... );
bool AddCopySipLine(const char *p, const char *psearch);
bool ParseParameter(char *dest, int destlen, const char *name, const char *line, char cq='\"');
bool ParseReturnParams(const char *p);
int GrepInteger(const char *p, const char *psearch);
void Ack(const char *pIn);
void Cancel(int seqn);
void Bye(int cseq);
void Ok(const char *pIn);
void Invite(const char *pIn=0);
uint32_t Millis();
uint32_t Random();
int SendUdp();
void MakeMd5Digest(char *pOutHex33, char *pIn);
public:
Sip(char *pBuf, size_t lBuf);
void Init(const char *SipIp, int SipPort, const char *MyIp, int MyPort, const char *SipUser, const char *SipPassWd, int MaxDialSec=10);
void HandleUdpPacket(const char *p);
bool Dial(const char *DialNr, const char *DialDesc="");
bool IsBusy() { return iRingTime !=0; }
};
Sip::Sip(char *pBuf, size_t lBuf)
{
pbuf=pBuf;
lbuf=lBuf;
pDialNr="";
pDialDesc="";
}
bool Sip::Dial(const char *DialNr, const char *DialDesc)
{
if (iRingTime)
return false;
iDialRetries=0;
pDialNr=DialNr;
pDialDesc=DialDesc;
Invite();
iDialRetries++;
iRingTime=Millis();
return true;
}
void Sip::Cancel(int cseq)
{
if (caRead[0]==0)
return;
pbuf[0]=0;
AddSipLine("%s sip:%s@%s SIP/2.0", "CANCEL", pDialNr, pSipIp);
AddSipLine("%s", caRead);
AddSipLine("CSeq: %i %s", cseq, "CANCEL");
AddSipLine("Max-Forwards: 70");
AddSipLine("User-Agent: sip-client/0.0.1");
AddSipLine("Content-Length: 0");
AddSipLine("");
SendUdp();
}
void Sip::Bye(int cseq)
{
if (caRead[0]==0)
return;
pbuf[0]=0;
AddSipLine("%s sip:%s@%s SIP/2.0", "BYE", pDialNr, pSipIp);
AddSipLine("%s", caRead);
AddSipLine("CSeq: %i %s", cseq, "BYE");
AddSipLine("Max-Forwards: 70");
AddSipLine("User-Agent: sip-client/0.0.1");
AddSipLine("Content-Length: 0");
AddSipLine("");
SendUdp();
}
void Sip::Ack(const char *p)
{
char ca[32];
bool b=ParseParameter(ca, (int)sizeof(ca), "To: <", p, '>');
if (!b)
return;
pbuf[0]=0;
AddSipLine("ACK %s SIP/2.0", ca);
AddCopySipLine(p, "Call-ID: ");
int cseq=GrepInteger(p, "\nCSeq: ");
AddSipLine("CSeq: %i ACK", cseq);
AddCopySipLine(p, "From: ");
AddCopySipLine(p, "Via: ");
AddCopySipLine(p, "To: ");
AddSipLine("Content-Length: 0");
AddSipLine("");
SendUdp();
}
void Sip::Ok(const char *p)
{
pbuf[0]=0;
AddSipLine("SIP/2.0 200 OK");
AddCopySipLine(p, "Call-ID: ");
AddCopySipLine(p, "CSeq: ");
AddCopySipLine(p, "From: ");
AddCopySipLine(p, "Via: ");
AddCopySipLine(p, "To: ");
AddSipLine("Content-Length: 0");
AddSipLine("");
SendUdp();
}
void Sip::Init(const char *SipIp, int SipPort, const char *MyIp, int MyPort, const char *SipUser, const char *SipPassWd, int MaxDialSec)
{
caRead[0]=0;
pbuf[0]=0;
pSipIp=SipIp;
iSipPort=SipPort;
pSipUser=SipUser;
pSipPassWd=SipPassWd;
pMyIp=MyIp;
iMyPort=MyPort;
iAuthCnt=0;
iRingTime=0;
iMaxTime=MaxDialSec*1000;
}
void Sip::AddSipLine(const char* constFormat , ... )
{
va_list arglist;
va_start( arglist, constFormat);
uint16_t l=(uint16_t)strlen(pbuf);
char *p=pbuf+l;
vsnprintf(p, lbuf-l, constFormat, arglist );
va_end( arglist );
l=(uint16_t)strlen(pbuf);
if (l<(lbuf-2))
{
pbuf[l]='\r';
pbuf[l+1]='\n';
pbuf[l+2]=0;
}
}
// call invite without or with the response from peer
void Sip::Invite(const char *p)
{
// prevent loops
if (p && iAuthCnt>3)
return;
// using caRead for temp. store realm and nonce
char *caRealm=caRead;
char *caNonce=caRead+128;
char *haResp=0;
int cseq=1;
if (!p)
{
iAuthCnt=0;
if (iDialRetries==0)
{
callid=Random();
tagid=Random();
branchid=Random();
}
}
else
{
cseq=2;
if ( ParseParameter(caRealm, 128, " realm=\"", p)
&& ParseParameter(caNonce, 128, " nonce=\"", p))
{
// using output buffer to build the md5 hashes
// store the md5 haResp to end of buffer
char *ha1Hex=pbuf;
char *ha2Hex=pbuf+33;
haResp=pbuf+lbuf-34;
char *pTemp=pbuf+66;
snprintf(pTemp, lbuf-100, "%s:%s:%s", pSipUser, caRealm, pSipPassWd);
MakeMd5Digest(ha1Hex, pTemp);
snprintf(pTemp, lbuf-100, "INVITE:sip:%s@%s", pDialNr, pSipIp);
MakeMd5Digest(ha2Hex, pTemp);
snprintf(pTemp, lbuf-100, "%s:%s:%s", ha1Hex, caNonce, ha2Hex);
MakeMd5Digest(haResp, pTemp);
}
else
{
caRead[0]=0;
return;
}
}
pbuf[0]=0;
AddSipLine("INVITE sip:%s@%s SIP/2.0", pDialNr, pSipIp);
AddSipLine("Call-ID: %010u@%s", callid, pMyIp);
AddSipLine("CSeq: %i INVITE", cseq);
AddSipLine("Max-Forwards: 70");
// not needed for fritzbox
// AddSipLine("User-Agent: sipdial by jl");
AddSipLine("From: \"%s\" <sip:%s@%s>;tag=%010u", pDialDesc, pSipUser, pSipIp, tagid);
AddSipLine("Via: SIP/2.0/UDP %s:%i;branch=%010u;rport=%i", pMyIp, iMyPort, branchid, iMyPort);
AddSipLine("To: <sip:%s@%s>", pDialNr, pSipIp);
AddSipLine("Contact: \"%s\" <sip:%s@%s:%i;transport=udp>", pSipUser, pSipUser, pMyIp, iMyPort);
if (p)
{
// authentication
AddSipLine("Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"sip:%s@%s\", response=\"%s\"", pSipUser, caRealm, caNonce, pDialNr, pSipIp, haResp);
iAuthCnt++;
}
AddSipLine("Content-Type: application/sdp");
// not needed for fritzbox
// AddSipLine("Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO");
AddSipLine("Content-Length: 0");
AddSipLine("");
caRead[0]=0;
SendUdp();
}
// parse parameter value from http formated string
bool Sip::ParseParameter(char *dest, int destlen, const char *name, const char *line, char cq)
{
const char *qp;
const char *r;
if ((r = strstr(line, name)) != NULL)
{
r = r + strlen(name);
qp = strchr(r, cq);
int l = qp - r;
if (l<destlen)
{
strncpy(dest, r, l);
dest[l]=0;
return true;
}
}
return false;
}
// search a line in response date (p) and append on
// pbuf
bool Sip::AddCopySipLine(const char *p, const char *psearch)
{
char *pa=strstr((char*)p, psearch);
if (pa)
{
char *pe=strstr(pa, "\r");
if (pe==0)
pe=strstr(pa, "\n");
if (pe>pa)
{
char c=*pe;
*pe=0;
AddSipLine("%s", pa);
*pe=c;
return true;
}
}
return false;
}
int Sip::GrepInteger(const char *p, const char *psearch)
{
int param=-1;
const char *pc=strstr(p, psearch);
if (pc)
{
param=atoi(pc+strlen(psearch));
}
return param;
}
// copy Call-ID, From, Via and To from response
// to caRead
// using later for BYE or CANCEL the call
bool Sip::ParseReturnParams(const char *p)
{
pbuf[0]=0;
AddCopySipLine(p, "Call-ID: ");
AddCopySipLine(p, "From: ");
AddCopySipLine(p, "Via: ");
AddCopySipLine(p, "To: ");
if (strlen(pbuf)>=2)
{
strcpy(caRead, pbuf);
caRead[strlen(caRead)-2]=0;
}
return true;
}
void Sip::HandleUdpPacket(const char *p)
{
uint32_t iWorkTime=iRingTime ? (Millis()-iRingTime) : 0;
if (iRingTime && iWorkTime>iMaxTime)
{
// Cancel(3);
Bye(3);
iRingTime=0;
}
if (!p)
{
// max 5 dial retry when loos first invite packet
if (iAuthCnt==0 && iDialRetries<5 && iWorkTime>(iDialRetries*200))
{
iDialRetries++;
delay(30);
Invite();
}
return;
}
if (strstr(p, "SIP/2.0 401 Unauthorized")==p)
{
Ack(p);
// call Invite with response data (p) to build auth md5 hashes
Invite(p);
}
else if (strstr(p, "BYE")==p)
{
Ok(p);
iRingTime=0;
}
else if (strstr(p, "SIP/2.0 200")==p) // OK
{
ParseReturnParams(p);
Ack(p);
}
else if ( strstr(p, "SIP/2.0 183 ")==p // Session Progress
|| strstr(p, "SIP/2.0 180 ")==p ) // Ringing
{
ParseReturnParams(p);
}
else if (strstr(p, "SIP/2.0 100 ")==p) // Trying
{
ParseReturnParams(p);
Ack(p);
}
else if ( strstr(p, "SIP/2.0 486 ")==p // Busy Here
|| strstr(p, "SIP/2.0 603 ")==p // Decline
|| strstr(p, "SIP/2.0 487 ")==p) // Request Terminatet
{
Ack(p);
iRingTime=0;
}
else if (strstr(p, "INFO")==p)
{
iLastCSeq=GrepInteger(p, "\nCSeq: ");
Ok(p);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// hardware dependent interface functions
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
int Sip::SendUdp()
{
Udp.beginPacket(pSipIp, iSipPort);
Udp.write(pbuf, strlen(pbuf));
Udp.endPacket();
#ifdef DEBUGLOG
Serial.printf("\r\n----- send %i bytes -----------------------\r\n%s", strlen(pbuf), pbuf);
Serial.printf("------------------------------------------------\r\n");
#endif
return 0;
}
// generate a 30 bit random number
uint32_t Sip::Random()
{
// return ((((uint32_t)rand())&0x7fff)<<15) + ((((uint32_t)rand())&0x7fff));
return secureRandom(0x3fffffff);
}
uint32_t Sip::Millis()
{
return (uint32_t)millis()+1;
}
void Sip::MakeMd5Digest(char *pOutHex33, char *pIn)
{
MD5Builder aMd5;
aMd5.begin();
aMd5.add(pIn);
aMd5.calculate();
aMd5.getChars(pOutHex33);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Arduino setup() and loop()
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
char caSipIn[2048];
char caSipOut[2048];
Sip aSip(caSipOut, sizeof(caSipOut));
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(false);
delay(10);
Serial.print("\r\n\r\n");
Serial.printf("Connecting to %s\r\n", ssid);
IPAddress Ip;
IPAddress Gw;
IPAddress Mask;
IPAddress Dns;
Ip.fromString(ip);
Gw.fromString(gw);
Mask.fromString(mask);
Dns.fromString(dns);
WiFi.config(Ip,Gw,Mask,Dns);
if (String(ssid) != WiFi.SSID())
{
Serial.print("Wifi begin...\r\n");
WiFi.begin(ssid, password);
}
int i=0;
for (i=0; i<100; i++)
{
if (WiFi.status() == WL_CONNECTED)
break;
delay(100);
Serial.print(".");
}
if (i>=100)
{
// without connection go to sleep
delay(1000);
ESP.deepSleep(0);
}
WiFi.persistent(true);
Serial.printf("\r\nWiFi connected to: %s\r\n", WiFi.localIP().toString().c_str());
Udp.begin(sipport);
aSip.Init(sipip, sipport, ip, sipport, sipuser, sippasswd, 15);
aSip.Dial(sipdialnr, sipdialtext);
}
int deepSleepDelay=0;
void loop(void)
{
int packetSize = Udp.parsePacket();
if (packetSize>0)
{
caSipIn[0]=0;
packetSize=Udp.read(caSipIn, sizeof(caSipIn));
if (packetSize>0)
{
caSipIn[packetSize]=0;
#ifdef DEBUGLOG
IPAddress remoteIp = Udp.remoteIP();
Serial.printf("\r\n----- read %i bytes from: %s:%i ----\r\n", (int)packetSize, remoteIp.toString().c_str(), Udp.remotePort());
Serial.print(caSipIn);
Serial.printf("----------------------------------------------------\r\n");
#endif
}
}
aSip.HandleUdpPacket((packetSize>0) ? caSipIn : 0 );
if (!aSip.IsBusy() && deepSleepDelay==0)
deepSleepDelay=millis();
if (deepSleepDelay && (millis()-deepSleepDelay)>2000)
ESP.deepSleep(0);
}