scieee Science in your language
[en] (orig)

Cognitive network scenario through a SIP proxy server

Abstract

[ANGLÈS] Implementation of two different session establishment scenarios to the solution of adding a new Cognitive Transport Service layer, implemented with SIP and NRDL, to solve the problem of the lack of communication between applications and network.

Read accessible full text

Cognitive network scenario through a SIP proxy server

Author: Somoza de la Osa, Jordi
Publisher: Universitat Politècnica de Catalunya
Year: 2012
Source: https://upcommons.upc.edu/bitstream/2099.1/16156/4/Cognitive_Network_Scenario_through_a_SIP_Proxy_Server_J.Somoza.pdf
Mas e Thesis
Cogni i e Ne wo k Scena io h ough a SIP
P oxy Se e
Au ho
Jo di Somoza de la Osa
Thesis ad iso s
F anco Callega i
Wal e Ce oni
Aldo Campi
Dipa imen o di Ele onica, In o ma ica e Sis emis ica
Uni e si à di Bologna, Facol à di Ingegne ia
Ap il 2012
KEY WORDS
SIP P oxy
Nex Gene a ion Ne wo k
Cogni i e T anspo Se ice laye
GRATITUDE
Abo e all, I'd like o hank my pa en s – Julio and Bea iz – and my
b o he Vic o o always suppo ing me h ough all hese yea s.
Secondly, I'd like o hank he 'Palace os' – E ik, Vic o , C ispins,
Ta i, Gasu, Agu, Fa ga and Bo des - o always unde s anding and
encou aging me.
I'd also like o hank my iends and pa ne s – Vicenç, Vi ginia,
Ch is ian, Al a o and many o he s – o all he laughs we had o-
ge he .
I wan ed also o hank all he people I' e me in Bologna o all he
expe iences I' e had his yea .
Basically I'd like o hank e e yone who has 'su e ed' my lack o
ime h ough all hese yea s.
And las bu no leas , I'd like o hank my supe iso s – F anco Cal-
lega i, Wal e Ce oni and Aldo Campi – o all he dedica ion and
help hey' e p o ided me du ing he p epa a ion o his p ojec .
CONTENTS
1. In oduc ion...........................................................................................................................3
2. The s eps o he new cogni i e ne wo k in as uc u e.........................................................5
2.1 Why SIP and NRDL?..............................................................................................6
2.2 Al e na i es o In e ac ion.......................................................................................6
2.3 The Applica ion-o ien ed Module...........................................................................7
2.4 Resou ce Managemen ............................................................................................8
3. NRDL (Ne wo k Resou ce Desc ip ion Language) and SIP (Session Ini ia ion
P o ocol)...................................................................................................................................9
3.1 NRDL.....................................................................................................................9
3.1.1 Secu i y...................................................................................................12
3.2 SIP.........................................................................................................................12
3.2.1 B ie Desc ip ion abou SIP....................................................................13
3.2.2 Session Es ablishmen and End o Session............................................13
3.2.3 SIP Heade Fields...................................................................................14
3.2.4 Lis o SIP Responses Codes..................................................................15
4. Simula ion Scena io............................................................................................................16
4.1 S anda d Session Es ablishmen ...........................................................................16
4.2 UPDATE Du ing Session Es ablishmen ..............................................................18
5. Implemen a ion...................................................................................................................20
5.1 S eps Followed......................................................................................................20
5.1.1 Web In e ace.........................................................................................20
5.1.2 Sou ce and Reques Po s and IP............................................................21
5.1.3 REGISTER Me hod...............................................................................22
5.1.4 SIP P oxy Implemen a ion.....................................................................23
5.1.5 Sa ing and Modi ying NRDL Code......................................................24
5.1.6 SIP Heade Implemen a ion...................................................................25
5.1.7 Sending he UPDATE............................................................................27
5.1.8 Responding P ope ly..............................................................................27
5.2 P oblems Found....................................................................................................28
5.3 Execu ion o Simula ions......................................................................................29
5.3.1 Synch onous Mode (S anda d Session Es ablishmen )..........................30
5.3.2 Synch onous Mode (UPDATE Du ing Session Es ablishmen )............30
1
6. Resul s.................................................................................................................................32
6.1 S anda d Session Es ablishmen ...........................................................................32
6.1.1 Call.........................................................................................................32
6.1.2 Mig a ion................................................................................................33
6.1.3 End.........................................................................................................33
6.2 UPDATE Du ing Session Es ablishmen ..............................................................34
6.2.1 Call.........................................................................................................34
7. Conclusion and Fu u e........................................................................................................37
8. Re e ences...........................................................................................................................38
Annexes..................................................................................................................................39
A. Synch onous Mode (UPDATE Du ing Session Es ablishmen )............................39
B. phpse app sc ip s..................................................................................................40
index.php.............................................................................................40
C. 10.10.10.66 UA sc ip s...........................................................................................41
C.I es somoza_s anda d.................................................................................41
in i e.php.............................................................................................41
mig .php..............................................................................................42
bye.php................................................................................................45
index.php.............................................................................................46
Socke SIP.class.php.............................................................................56
Clien SIP.class.php..............................................................................59
C.II es somoza_upda e...................................................................................85
Clien SIP.class.php..............................................................................85
D. 10.10.10.67 UA sc ip s.........................................................................................111
D.I es somoza_s anda d...............................................................................111
clien 2.php.........................................................................................111
Clien SIP.class.php............................................................................122
D.II es somoza_upda e.................................................................................148
Clien SIP.class.php............................................................................148
2

1. In oduc ion.
Nowadays, he In e ne is based on IP anspo se ice, p o iding an end- o-end connec ion
be ween applica ions and ocusing on packe ou ing.
Al hough i is ue ha he use o he In e ne P o ocol has been a g ea success o he
in e connec ion o ne wo ks using di e en p o ocols a he anspo laye , such as TCP o UDP
among o he s, and con ibu ed o de elop he ne wo k o ne wo ks we cu en ly ha e, he way he
In e ne is has la ely been discussed. The eason is no ano he ha i has e ol ed so much ha he
ne wo k i sel has o be econside ed.
The in oduc ion in he ne wo k o unc ions and sub-blocks non exis ing a he ime he In e ne
was concei ed, such as NATs o i ewalls, and he e olu ion o applica ions ha e conside ably
con ibu ed o expand his hough . Mo e and mo e, applica ions equi e e ec i e suppo o QoS
and la ge scale p o isioning, which leads hem o he need o being mo e ne wo k-awa e and o he
ne wo k o be mo e applica ion-awa e.
A his momen , hese applica ions communica e end- o-end and he esou ce managemen is
p o ided by cen alized applica ions. Ne e heless, conside able o e head is added leading o a high
ine iciency o he ne wo k due o he lack o da a-speed abo e all. Fo his eason, a new signaling
laye was necessa y. This new signaling laye had o be able o make applica ions capable o
in e ac ing wi h he ne wo k in o de o eques he esou ces needed.
This akes us o a p oblem. How will applica ions and ne wo k communica e? Un il now, as a
consequence o he OSI model, he ne wo k and he applica ions did no communica e wi h each
o he . The applica ion laye in a node, only alked wi h he applica ion laye o ano he node, and
he same wi h he ne wo k laye . I is now necessa y o he ne wo k laye and he applica ion laye
o communica e. The applica ion has o be capable o explaining o he ne wo k which esou ces i
needs and he ne wo k has o be capable o elling he applica ion whe he o no his esou ces a e
a ailable. This is why a new Cogni i e T anspo Se ice laye was p oposed on op o he
adi ional anspo laye .
This CTS laye will pe mi applica ions and ne wo k in e ac and will be di ided in o wo sub-
laye s; he session laye and he p esen a ion laye . The session laye has o be implemen ed wi h a
session-o ien ed p o ocol, capable o p o iding secu i y and e ol ed enough o be us ed, in his
case, he Session O ien ed P o ocol. On he o he side, he p esen a ion laye will be based on he
Ne wo k Resou ce Desc ip ion Language. The NRDL will be used as a se ices desc ip ion
language and as a se ice nego ia ion p o ocol and will be he ac ual link be ween ne wo k and
applica ions.
3
The aim o his hesis is o implemen wo di e en scena ios o es he iabili y o he solu ion
p oposed. To do so, we a e going o es ablish a session be ween wo hos s which a e going o
communica e h ough a SIP P oxy based on an OpenSIPs dis ibu ion. This SIP P oxy is going o
a end he calle esou ces eques and, once he session is es ablished we a e going o mig a e a VM
be ween he hos s. The mig a ion was implemen ed in a p e ious wo k.
These simula ions a e going o be de eloped wi h wo Linux con igu ed machines E he ne
connec ed, and he languages used o implemen he di e en componen s will be php, ja asc ip
and h ml o he use in e ace.
4
2. The s eps o he new cogni i e ne wo k
in as uc u e.
Due o he change expe imen ed by he In e ne h ough he las yea s, and he need o ha ing
applica ions capable o changing, on demand, he se -up o connec i i y se ice wi h speci ic
cha ac e is ics ac oss high capaci y anspo ne wo ks, a new p o ocol laye was needed.
Nowadays, he e is no a widely accep ed way o implemen ha . Howe e , i has been p o ed ha
combining wo echnologies cu en ly a ailable such as he Session Ini ia ion P o ocol (SIP) and he
Ne wo k Resou ce Desc ip ion Language (NRDL) we can each his objec i e. SIP is no able o
p o ide se ices on i s own, bu b ings he possibili y o use i o implemen se ices. On he o he
hand, is NRDL he language which allows applica ions say o he ne wo k wha esou ces do hey
need, and he ne wo k say which applica ions a e a ailable.
This combina ion o echnologies will allow applica ions exchange messages wi h he ne wo k, in
o de o make eques s o he nego ia ion and ese a ion o esou ces on he ne wo k, b inging a
new signaling laye called Cogni i e T anspo Se ice (CTS) laye .
This new anspo laye should be implemen ed by decoupling he signaling p o ocol and is di ided
in o wo sub-laye s:
- The session laye , implemen ed by he session-o ien ed p o ocol SIP.
- The P esen a ion laye , which uses NRDL.
5
Fig. 1 Cogni i e T anspo Se ice (CTS) laye
2.1 Why SIP and NRDL?
The choice o SIP o implemen he new session laye was d i en by he nex s a emen s:
- The necessi y o ha ing a signaling p o ocol, which is esponsible o de ining he syn ax
used o se ice nego ia ion, session-o ien ed. SIP is able o ini ia e, modi y and e mina e
sessions.
- The need o ha ing a secu e session p o ocol, including au hen ica ion, au ho iza ion and
accoun ing (AAA).
- SIP is nowadays a widely e ol ed p o ocol due o i s adop ion by 3GPP o be he main
p o ocol in IP Mul imedia Subsys em a chi ec u e.
The choice o NRDL o be used in he new p esen a ion laye was d i en by he nex s a emen s:
•I is neu al o he session p o ocol used.
•I is able o ep esen ne wo k esou ces as well as a ibu es o a communica ion.
Acco ding o his, he only di e ence be ween he p oposed solu ion and he cu en In e ne would
be ha be o e he exchange o da a, he e would be a anspo se ice eques . This means ha
be o e sending he da a, he sende applica ion will p epa e a NRDL documen , encapsula ed on he
body o a SIP message, which will go h ough he ne wo k by means o a SIP session, and will
allow he sende and ecei e o nego ia e and a ange he ne wo k se ice needed o es ablish he
communica ion. When he NRDL documen a i es o he ecei e , i e i ies he ne wo k se ices
eques ed by he sende and p epa es ano he NRDL documen as an answe , ac ing as an ACK.
This NRDL documen con ains, apa om he ne wo k se ices om he ecei e poin o iew, i s
local po and o he in o ma ion ha he sende would need. O cou se, he exchange on in o ma ion
will be held un il he ne wo k se ices a e inally es ablished o canceled. F om ha momen being,
he exchange o da a will ake place un il he session is closed.
The e is no ye a Call-se up ime ecommenda ion speci ically o his ype o communica ion, bu
he ecommenda ions by he ITU-T o a gene al Call-se up ime is be 7.5 s.
2.2 Al e na i es o In e ac ion.
As men ioned be o e, when a calle wan s o s a a SIP session wi h a callee wi h speci ic ne wo k
se ices, i p epa es a NRDL documen , sends i embedded in a SIP INVITE message o an
in e media e node which p ocesses i , sends i o he nex node, and wai s o he callee
con i ma ion. Howe e , he e a e se e al al e na i es o his no mal beha io which would be:
- S a a ull ese a ion o ne wo k esou ces aking in o accoun ha i he callee does no
accep he call hey would be eleased. This beha io is use ul when he se -up ime is a
6
3.2.1 B ie Desc ip ion abou SIP.
SIP was bo n in 1996 in o de o me ge Session Ini ia ion P o ocol (SIP) and Simple Con e ence
Ini ia ion P o ocol (SCIP).
SIP is a ansac ion o ien ed p o ocol, meaning ha i is composed o eques - esponse sequences. I
wo ks wi h a numbe o eliable and un eliable anspo p o ocols such as TCP, UDP o SCTP and
uses he e ansmission in o de o p o ide eliabili y o e UDP. In his simula ion we a e using
UDP. I pe mi s he econ igu a ion o sessions and enables se e s o main ain a minimal s a e in o.
Fig. 5 SIP session es ablishmen .
3.2.2 Session Es ablishmen and End o Session.
As a i s app oxima ion o he exchange o messages aking pa in his simula ion and as a b ie
explana ion o how a session is es ablished and inished be ween wo Use Agen s, I will explain
how i is done be ween wo use agen s wi h a P oxy be ween he wo o hem. In ano he sec ion, I
will alk abou he ac ual messages low aking pa in his simula ion.
All SIP con e sa ions ini ia e wi h an INVITE message sen by he calle , which needs o be
esponded wi h a 200OK message and con i med, again by he calle wi h an ACK. This is known
as he h ee-way handshake. I is impo an o no ice ha he callee epo s, be o e sending he 200
OK, i s s a us changes.
In o de o end a dialog, he calle sends a BYE message o he callee, esponded wi h a 200 OK.
This would be how a session is es ablished and ended, howe e , o he messages exchange could
ake place.
In ou case, we will use a SIP P oxy in o de o pa se and nego ia e ne wo k se ices wi h he
in o ma ion embedded in o he SIP body wi h he NRDL.
13

3.2.3 SIP H eade Fields.
Reco d- ou e: Reco d- ou e is an op ional ield. This ield is inse ed by he p oxy and con ains i s
di ec ion. Wi h his inse ion, he p oxy in o ms he Use Agen s (UA) ha i wishes o s ay on he
pa h o all u he messages. In some cases, his ield could be ini ialized wi h a p e-exis ing ou e
se .
Via: This ield shows he anspo p o ocol used and he eques ou e. In a gene al case, each
p oxy would add a line o his ield. In ou case, we will only ha e, a he mos , wo lines. I is
impo an o ema k ha , as he esponse ollows he pa h, he co esponding Via line is dele ed.
F om: This ield shows he calle add ess. Each F om ield con ains a F om ag, which iden i ies he
UA.
To: This ield shows he callee add ess. As he F om ield, i con ains he To ag, which iden i ies he
UA and is no added o he heade un il he UA i sel does.
Fig. 6 SIP heade .
Call_ID: Unique iden i ie o each call and con ains he hos add ess. I mus be he same o all he
messages wi hin a ansac ion.
Cseq: This ield begins wi h a andom numbe and iden i ies each messages exchange. Fo example,
he INVITE and i s co esponding esponses will ha e he same iden i ie . And he same o he
BYE o he UPDATE messages and i s esponses.
Con ac : This ield shows he con ac add ess o i s UA and i s po .
Con en ype: Con ains a desc ip ion o he message body.
Max Fo wa ds: This ield is used o limi he numbe o p oxies ha can o wa d he eques un il i
eaches he inal use . This coun is dec emen ed by each se e ha o wa ds he eques . The
ecommended ini ial alue in his case is 70.
Use Agen : This ield shows he clien agen who deals he communica ion.
Con en leng h: Leng h o he body.
14
3.2.4 Lis o SIP Responses Codes.
100-199 In o ma ional esponses.
200-299 Success ul esponses.
300-399 Redi ec ion esponses.
400-499 Clien Failu e esponses.
500-599 Se e Failu e esponses.
600-699 Global Failu e esponses.
15
4. Simula ion Scena io.
In his p ojec , in o de o es and p o e his solu ion o he p oblem exposed, we a e going o se
ou wo possible scena ios whe e we a e going o see he possibili ies ha he use o SIP and NRDL
o e . The wo scena ios will be he synch onous mode wi h a s anda d session es ablishmen and
wi h an UPDATE du ing he es ablishmen o session.
In bo h modes we will ha e wo UA communica ing h ough a P oxy Se e which will cap u e he
messages sen by he UAs and ac in consequence making changes in he ne wo k. We will also
mig a e a VM be ween bo h UA a e he session is es ablished. The main di e ence will be ha ,
while in he i s case he es ablishmen o session will ake place acco ding o he calle ini ial
eques s, in he second case he callee UA will p o ide in o ma ion o he calle o o ce him o send
an UPDATE o make some changes on he eques s made be o ehand wi h he INVITE message.
4.1. S anda d Session Es ablishmen .
16
Fig. 7 Session Es ablishmen
Be o e s a ing any session, as hey communica e h ough a P oxy Se e , he i s hing ha he
UAs mus do is egis e o he P oxy sending a message wi h he REGISTER me hod. In his
message, hey mus speci y a Use Name and a Passwo d in o de o p o ide secu i y o he session
i sel . I he P oxy accep s he e minal eques ing egis a ion, i will send back a 200 OK which
will con i m he UA ha he egis a ion has aken place.
A e he wo UAs a e egis e ed, he calle will send an INVITE message o he p oxy. This
INVITE message will be embedded wi h a NRDL code in i s body, con aining he ne wo k eques s
o ne wo k changes demanded. In o de o p e en he calle o send ano he INVITE message, he
P oxy will send wo 100 esponses. I i did no , while he P oxy was looking o ne wo k esou ces,
he UA could hink he message had been los .
Once he P oxy o wa ds he INVITE message, he callee eads i and modi ies he code adding i s
local po , he ne wo k se ices eques ed om i s poin o iew and o he in o ma ion ha he
calle could need.
A his poin , when he callee has accep ed he session es ablishmen , he P oxy will check whe he
he NRDL code has he p ope s uc u e o no and make he ne wo k esou ces ese a ion o he
changes eques ed. A e i is done, he P oxy will o wa d he 200 OK sen by he callee, o he
calle o le i know ha he ese a ion was made. In esponse, he calle will send a message wi h
he me hod ACK o he callee o le i also know ha he ese a ion was made.
A his poin , he session will be es ablished and bo h he calle and he callee will be able o
exchange messages in a secu e way.
Fig. 8 Session end
Once he calle wan s o end he session, i sends a message wi h he BYE me hod o he callee
h ough he p oxy. When he message ge s o he P oxy, i checks again whe he he NRDL body is
co ec o no and undoes he change made du ing he session es ablishmen .
When he BYE ge o he callee, i esponds wi h a 200 OK. A his poin , he session is ended.
17
4.2 UPDATE Du ing Session Es ablishmen .
18
Fig. 9 Session es ablishmen wi h UPDATE.

Again, as in he o he simula ion scena io, as he communica ion akes place be ween wo Use
Agen s wi h a P oxy in he middle, SIP pe mi s o egis e he e minals o add secu i y o he
ansac ions. The mechanism is he same in e e y case.
A e he egis a ion, he calle send an INVITE message o he p oxy wi h i s ne wo k eques s
embedded in o he body o he message wi h he NRDL code. Again, he P oxy sends wo 100
esponses o ale he calle ha he possible delay is no because o he INVITE message loss, ha
i is because o he esea ch o esou ces eques ed. A e i , he P oxy o wa ds he INVITE
message o he callee.
I is a his poin when he callee sends a 183 Session in p og ess esponse o he calle o con ey
in o ma ion abou he p og ess o he call. Fo his eason, he calle UA sends an UPDATE
message o he callee o make some changes o he ne wo k eques s acco ding o he in o ma ion
sen be o ehand wi h he 183 esponse.
When he callee UA ecei es he UPDATE message, i esponds wi h a 200 OK modi ying he
NRDL body embedded in he UPDATE and, a his poin , he p oxy, a e checking whe he he
NRDL code is well s uc u ed and applying he changes eques ed, o wa ds he message o he
calle UA.
I is impo an o say ha he callee esponds wi h wo 200 OK a his poin . The i s one applies o
he UPDATE message sen by he calle , and he second one o he INVITE message. The di e ence
emains, abo e all, a he de ini ion o he me hod and ags p esen on i s heade s. I is impo an o
send he UPDATE 200 OK esponse be o e he INVITEs one. We mus end he UPDATE
ansac ion be o e ending he INVITE one.
A e i , he calle UA sends a message wi h he ACK me hod o indica e he calle ha he session
has been es ablished.
A e he communica ion has aken place, once he calle wan s o end he session, i sends a BYE
message o he p oxy, ha a e checking i s co ec ness undoes he changes made du ing he
session es ablishmen and o wa ds i o he calle , which sends a 200 OK wi h he BYE me hod in
o de o end he session.
19
5. Implemen a ion.
In his sec ion he way he simula ion scena io was implemen ed, he p oblems ound and he way
he simula ions ha e o be execu ed is desc ibed.
5.1 S eps Followed.
5.1.1 Web In e ace.
When we i s go his P ojec , he i s hing we wan ed o do was o change he way he messages
low was displayed in he web b owse in o de o make i mo e use - iendly and a ac i e o he
eye o he one pe o ming he simula ions.
Fi s o all, he e was no di e en ia ion be ween he messages sen by one UA and he o he . We
wan ed o display each o hem in di e en colo s. The i s we did was c ea e a unc ion ha would
look o a chain in o ano he chain.
This way, inding a di e ence be ween he messages sen by one UA and he o he , we would only
ha e o look o his di e ence in e e y message and hen display any o hem in a di e en colo
depending whe he i was ound o no . In ou case, as we wan ed o di e en ia e he messages
acco ding o UA sending he message, we would ha e o see he di e ence be ween he di e en
F om heade s.
Secondly, when displayed, all he in o ma ion was showed a he i s ins ance. We wan ed o show
i s he i s line o he message showing which i was and hen be able o display he whole heade
and he body o any message by clicking on i . To do ha , we c ea ed a unc ion which would show
o hide a ex when execu ed.
20
Fu he mo e, as he messages low was an a ay, we would i s ha e o spli he a ay, sepa a ing
e e y message om he o he s adding p e iously an speci ic sen ence, in ou case “Rispos a
ice u a”, when adding a new message o he message low a ay.
And hen, di ide e e y message be ween he eques o esponse iden i ie and he es o he
message. In ou case, he a iable in is he posi ion o he wo d sepa a ing he heade and he ail o
he message.
We mus ake in o accoun ha no all messages i s heade elemen is he same. Because o his,
we had o a y he in a iable in some cases.
The esul o his was:
5.1.2 Sou ce and Reques Po s and IP.
The nex s ep was o change he way he wo UA communica ed. Un il hen, he wo UA we e
21
Fig. 10 Web in e ace esul .
communica ing di ec ly wi h hei public IP add esses which we e 137.204.107.66 and
137.204.107.67. The UA would now be communica ing h ough an E he ne cable. Thei IP
add esses would be 10.10.10.66 and 10.10.10.67 and hei co esponding po s would be 50601 o
he calle UA and 5060 o he callee UA.
To change his we had o go o he class Socke de ined in Socke SIP.class.php, change he a iable
$s c_ip o each UA wi h he new IP add ess and check i he a iable $s c_po was well de ined.
Now, we wan ed hem o communica e h ough a P oxy Se e which would ha e o deal wi h he
eques s made by he calle and he esponses sen by he callee. This P oxy is placed in
10.10.10.66:5060. To ha e his change made we had o con igu e he a iable $ eques Po o each
UA socke class o be 5060 and se he p oxy IP add ess wi h he unc ion de ined in
Clien SIP.class.php called se P oxy($p oxy) be o e he INVITE is sen . No e ha he a iable
$p oxy has he o ma o an IP add ess, in his case 10.10.10.66.
5.1.3 REGISTER Me hod.
A his poin , he simula ion scena io ha was al eady unning did almos he same he synch onous
mode wi h a s anda d session es ablishmen had o do. The only di e ence, apa om he p esence
o a SIP P oxy be ween hem, was ha he wo UA would ha e o egis e be o e s a ing he session
es ablishmen . To do so we added REGISTER o he lis o allowed me hods and de ined a unc ion
called egis e a he Clien SIP class.
This unc ion has o be execu ed be o e he INVITE is sen . The e is he e a di e ence be ween
when do we execu e he unc ion in he calle UA and he callee UA.
In he case o he calle , we execu e he unc ion when we wan o s a he session and send he
INVITE, igh be o e i is sen . Howe e , in he case o he callee, as we do no know when he
INVITE will be sen , he egis e unc ion is execu ed a he ime we open he socke o lis en o he
incoming messages. Tha is why, la e on when we will see he esul s, we will see ha he
REGISTER message om he callee ge s always o he P oxy be o e he calle REGISTER eques
22
Some imes opensips c ashes due o an e o in he p ocessing o he SIP packe . This could be
because o some hing missing in a heade o a message which is supposed o ha e body is emp y.
To sol e his you ha e o eboo he sys em, execu e he commands included in he ile
/ oo /i con ig. x in o de o ac i a e he aliases o he loopback in e ace and s a he opensips
se ice wi h he command:
% opensipsc l s a
Ano he p oblem we ound was ha , once you ha e execu ed he simula ion, you can no eboo he
i ual machine loca ed a he callee UA no mally. The eason is s ill unknown, bu o do so, you
ha e o kill he p ocess.
The i s s ep would be ind he co esponding p ocess. We would ecommend o execu e he line
“ps –eL | g ep Vi ”. The command “ps –eL ” selec s all he p ocesses being execu ed, p in s
in o ma ion abou all he ligh weigh p ocesses and makes a lis o hem. On he o he hand, he
command “g ep Vi ” il e s all he p ocesses ha con ain he chain “Vi ”.
The nex s ep is killing he p ocess. No e ha he co ec p ocess o kill is he one execu ing he
co esponding i ual machine, in ou case he one named Video. To kill he p ocess you ha e o
execu e he line “kill -9 PID” whe e PID is he one associa ed o he p ocess o kill and -9 indica es
ha he signal you wan o end o he p ocess is SIGKILL, as he command kill i sel does no kill a
p ocess.
Finally, he i ual machine emains in ABORTED s a e. You jus ha e o open and close i
no mally.
Ano he p oblem ha we had was ha he Remo e Desk op Viewe may ge s uck some imes. To ix
i , you only ha e o close he connec ion and connec no mally again. As i is only a emo e desk op,
once you connec again, you will ha e e e y hing as you le i , so i is no a big deal.
The e was also a p oblem o pe missions. As seen be o e, we some imes ha e o execu e he
unc ion sys em() o execu e commands in o a sc ip , which needs oo pe missions. To his end, we
ha e o change he /e c/sudoe s ile o gi e apache his pe missions.
5.3 Execu ion o Simula ions.
We a e now going o explain how o pe o m bo h simula ions. I would seem o be easy, bu he
ac o ha ing ound some p oblems du ing he p ocess encou ages me o explain wi h e e y de ail
how o execu e he simula ions.
Fi s o all, i is necessa y o open a i ual desk wi h he wo se e s used. To do so, open he
Remo e Desk op Viewe applica ion and connec o 137.204.107.66:3 and 137.204.107.67:3. Then,
open a e minal in bo h se e s and ype:
% xhos +
29

This command disables he access con ol allowing any hos o access o he se e . In o he wo ds,
i will allow us o use g aphical in e aces o ou applica ions, in ou case, Vi ualbox.
Then, connec as a oo wi h you passwo d yping:
% sudo su -
Open Vi ualbox wi h he g aphical in e ace. No e, in his case, ha we will only be able o in e ac
wi h ou VM Video i we open Vi ualbox as a oo :
% i ualbox & (137.204.107.66)
%Vi ualbox & (137.204.107.67)
Open he VM Video in he 137.204.107.66 se e and lea e he 137.204.107.67 swi ched o , as we
a e going o mig a e he i s VM o he second one. Now, bo h machines a e p epa ed so nex s ep
is execu ing he simula ions.
5.3.1 Synch onous Mode (S anda d Session Es ablishmen ).
To execu e his simula ion, open you local hos s web b owse and go o:
h p://137.204.107.66/ es somoza_s anda d/ (Execu es 137.204.107.66 sc ip )
and
h p://137.204.107.67/php-sip/ es somoza_s anda d/clien 2.php (Execu es 137.204.107.67
sc ip )
I e e y hing is ine, when execu ing he 137.204.107.67 sc ip , i will keep loading he page. This
means ha he sc ip has been well execu ed and ha he machine is lis ening o an incoming
connec ion. I his does no happen, i means ha he las ime he simula ion was execu ed, i ended
ab up ly. To sol e his p oblem, you ha e o es a he h pd se ice yping:
% /e c/ini .d/h pd es a
No e ha only a use wi h oo pe missions will be able o es a he h pd se ice.
Now we ha e he web in e ace o execu e he simula ion. P ess Call o es ablish a session be ween
bo h UA. The messages low will be displayed. P ess Mig azione o pe o m he mig a ion and Bye
o end he session.
In o de o es i he SIP P oxy has done he changes eques ed, in ou case a change a he
IPTables, ping he co esponding IP a e he es ablishmen o session and a e he end o session.
I e e y hing wo ks ine, you will no be able o ping any 173.194.35.0/24 IP a e he
es ablishmen o session bu you will be able o do so a e he end o session.
5.3.2 Synch onous Mode (UPDATE Du ing Session Es ablishmen ).
To execu e his simula ion he p ocess is he same as wi h he s anda d session es ablishmen . The
30
only di e ence will be, ob iously, ha we will ha e o execu e di e en sc ip s. To do so, open you
web b owse and go o:
h p://137.204.107.66/ es somoza_upda e/ (Execu es 137.204.107.66 sc ip )
and
h p://137.204.107.67/php-sip/ es somoza_upda e/clien 2.php (Execu es 137.204.107.67 sc ip )
In o de o es i he SIP P oxy has done he changes eques ed, in ou case a change a he
IPTables, ping he co esponding IP a e he es ablishmen o session and a e he end o session.
I e e y hing wo ks ine, you will no be able o ping any 173.194.35.0/24 IP excep om
173.194.35.159 a e he es ablishmen o session bu you will be able o do so a e he end o
session.
31
6. Resul s.
In his sec ion we a e going o explain exac ly wha do we ge when execu ing he simula ions.
Once explained in p e ious sec ions wha was he objec i e o he hesis, wha kind o simula ions
we would implemen , how we did ha and how o execu e he simula ions, i will be easy o
unde s and wha happens when we execu e e e y s ep o he simula ion.
6.1 S anda d Session Es ablishmen .
Be o e s a ing he communica ion, bo h UA open a socke . In he case o he calle UA, i
co esponds o 10.10.10.66:50601 and will be used o send all he eques s o he P oxy whose
socke is 10.10.10.66:5060 and is con inuously lis ening o a possible connec ion. In he case o he
callee, when execu ing he php sc ip , i opens a socke wi h i s co esponding IP and po ,
10.10.10.67:5060, and s a s lis ening.
6.1.1 Call.
In his case, bo h UA egis e hemsel es o he SIP P oxy by sending a REGISTER eques wi h a
Use Name and a Passwo d. As he P oxy accep s bo h e minals, i sends back a 200 OK esponse
o each o hem indica ing ha hey ha e been egis e ed.
A e he calle UA ecei es he egis e con i ma ion i s a s he es ablishmen o session. I , hen,
sends an INVITE message o he SIP P oxy wi h he NRDL code embedded o i s body, indica ing
he ne wo k eques s i is making o es ablish he session. When he P oxy ecei es he INVITE
message, i s a s looking o esou ces a he ne wo k in o de o p o ide he calle UA he session
i is demanding. I is no an objec i e o his hesis o implemen he code ha does so. While he
P oxy is looking o esou ces, i sends a 100 T ying and 100 Gi ing a y o he calle UA. This
esponses indica e he calle UA ha he INVITE message has no been los h ough he ne wo k
and ha he delay in he esponse is caused by he heo e ically esea ch o esou ces.
Once he P oxy has checked i i is able o p o ide he ne wo k eques , in his case we suppose i is,
i o wa ds he in i e o he callee UA. I is now ime o he callee o accep o decline he in i a ion
due o i s capaci y o es ablish he session. I i is able o es ablish he session, we assume i is, he
callee UA sa es he NRDL code ecei ed in o de o modi y i in case i was necessa y and
esponses wi h a 200 OK wi h he INVITE me hod o indica e he P oxy o which eques he
esponse co esponds. I is now ime o he P oxy o sa e he esou ces eques ed and con i med by
bo h Use Agen s o make he changes demanded. In ou case, he ne wo k eques is going o be a
change a he IPTables, a new ule o d op all he messages coming om 173.194.35.0/24. In o de
o check his, we ping his hos a e he session is es ablished.
32
A e he esou ces a e ese ed, he SIP P oxy o wa ds he esponse and he calle UA sends an
ACK o he callee in o de o con i m ha he session is es ablished.
6.1.2 Mig a ion.
Once he session is es ablished, i is ime o mig a e he VM Video om he calle o he callee. To
do so, he calle UA opens a socke and sends he . di ile, which con ains all he da a o he Video
VM, and he log ile, which con ains all he in o ma ion o he VM. A his poin , he Video VM has
mig a ed om he Calle UA o he Callee UA.
6.1.3 End.
Once he VM has mig a ed, he calle ends he session. To do so, i sends a BYE eques o he
callee. When he SIP P oxy ge s he message, and be o e o wa ding i o he callee, i eleases he
esou ces ese ed o his session, in ou case, dele es he ule a he IPTables. Finally, he callee
sends a 200 OK esponse o he calle o con i m ha he session has ended. To es i he change a
he IPTables has been made, we jus ping he hos again.
33
Fig. 11 Ping a e he session es ablishmen .
Fig. 12 Video VM mig a ion.
6.2 UPDATE Du ing Session Es ablishmen .
In his simula ion scena io, basically he only hing ha changes is he es ablishmen o session. Fo
his eason, in o de no o be edundan , i is he only hing we a e going o explain.
6.2.1 Call.
34
Fig. 13 Ping a e he end o session.
Fig. 14 Session es ablishmen wi h an UPDATE messages low as showed on he web b owse ..

A e he call is execu ed, bo h UA egis e o he SIP P oxy by sending a REGISTER eques wi h
hei Use Name and Passwo d. This is an op ion SIP o e s in o de o p o ide secu i y o he
session. Once a UA is egis e ed, i can eely in e ac wi h he SIP P oxy. A e he egis a ion is
accep ed, he P oxy sends a 200 OK esponse o he Use Agen s o con i m he egis a ion.
Then, he calle UA sends he INVITE eques o he callee wi h all i s ne wo k eques s, embedded
o he body o he message as an NRDL code, o es ablish a new session. A his ime, he SIP P oxy
sends he 100 T ying and 100 Gi ing a y o he calle be o e o wa ding he message in o de o
p e en i o esend he INVITE message while he P oxy is looking heo e ically o esou ces.
A e he esea ch, he INVITE message is o wa ded. Now, a e heo e ically analyzing he session
esou ces demand, he callee UA sends a 183 Session in p og ess esponse o he calle wi h
in o ma ion abou i sel in o de o change he se -up o he session. We a e going o suppose in his
case, ha in he INVITE message he calle wan ed o d op all he messages coming om
173.194.35.0/24 bu he callee asks he calle o accep 173.194.35.159 among all he IP amily.
I is hen ime o he calle UA o send an UPDATE message wi h he new in o ma ion o he
P oxy. The callee now sends wo 200 OK esponses. The i s one ega ding o he UPDATE
eques . Be o e o wa ding i o he calle , he P oxy adds a ule o he IPTables. The second 200
OK esponse co esponds o he INVITE me hod, and he P oxy adds he co esponding ule o he
IPTables be o e o wa ding he esponse. Finally, he calle UA sends an ACK message in o de o
con i m ha he session is es ablished.
In o de o es i he ules we e added, we ping 173.194.35.159 and 173.194.35.160.
A e he session is es ablished, he mig a ion akes place he same way i did wi h he s anda d
session es ablishmen .
Again, he ules a e dele ed when he BYE message goes h ough he SIP P oxy.
35
Fig. 15 Ping a e he session es ablishmen .
36
Fig. 16 Ping a e he end o session.
7. Conclusion and Fu u e.
The aim o his wo k was o es whe he he solu ion o implemen he new Cogni i e T anspo
Se ice laye wi h SIP and NRDL wo ked wi h wo di e en es ablishmen o session scena ios
be ween wo hos s h ough a SIP P oxy based on an OpenSIPs dis ibu ion.
I has been widely p o ed ha he solu ion p oposed wo ks h ough a SIP P oxy, which is capable o
in e cep ing he messages going h ough i , iden i y hem and ac in consequence wi h e e y single
eques o esponse, making any change on he ne wo k.
Fu he mo e, i has been p o ed and explained how we ha e o change he code in o de o es ablish
di e en scena ios, pe o m di e en ne wo k changes and change he NRDL code a hos ge s om
he eques s and esponses. The mig a ion implemen ed in ano he p ojec , is also pe o med
pe ec ly no ma e how he session was es ablished, which changes we e made o whe he he e is a
SIP P oxy be ween hos s o no .
Ne e heless, in he u u e will be he applica ions he ones which will ha e o be capable o
changing he se -up o connec i i y se ices wi h speci ic cha ac e is ics o e high-capaci y
anspo ne wo ks. The implemen a ion o hese new kind o applica ions could be mo e o less
easy, bu anyway he esul o he combina ion o he new ne wo k, now applica ion-awa e, and he
new amily o ne wo k-awa e applica ions could lead o a mo e e icien way o using he ne wo k
and esou ces a ailable in i .
In any case, his is no he end o he es o his solu ion. In his p ojec we implemen ed wo
synch onous scena ios, bu he e a e many o he s. A synch onous scena io is he one in which in he
session es ablishmen , he SIP P oxy is able o p o ide he esou ces eques ed by he calle UA,
howe e , he session es ablishmen could be asynch onous.
An asynch onous session es ablishmen is he one in which when he calle UA asks o he
esou ces needed o es ablish he session, his esou ces a e no a ailable. In his case, when
ecei ing he INVITE, he SIP P oxy will look o esou ces and ejec he eques when ealizing i
can p o ide hem. A e a ce ain ime, he calle UA will send an UPDATE wi h he eques and
will no s op sending UPDATES un il he SIP P oxy accep s he eques and o wa ds i o he callee
UA.
One hing is clea , he In e ne has o e ol e and his solu ion is p o ing i sel o be a good
possibili y o change he e iciency o he ne wo k and he pe o mance o applica ions in he u u e.
37
8. Re e ences.
[1] “Applica ion Scena ios o Au oma ed T anspo Se ice in he Fu u e In e ne ”, Wal e Ce oni,
Aldo Campi, F anco Callega i, DEIS Uni e si y o Bologna, Cesena, I aly.
[2] “Ne wo k Resou ce Desc ip ion Language”, A. Campi, F. Callega i, DEIS Uni e si y o
Bologna, Via Venezia 54, 47023 Cesena (FC) ITALY.
[3] “Con igu a ion o Ne wo k Resou ces o Fu u e In e ne applica ion Se ices”, F. Callega i, W.
Ce oni, B. Ma ini, M. Gha baoui, A. Campi, P. Cas oldi, DEIS Uni e si y o Bologna, CNIT,
Scuola Supe io e San 'Anna.
[4] “Au oma ed T anspo Se ice Managemen in he Fu u e. In e ne : Concep s and Ope a ions”,
F anco Callega i, Aldo Campi, Wal e Ce oni.
[5] “PHP Manual”, A ailable: h p://php.ne /manual/en/index.php.
[6] “Mig azione di Macchine Vi uali in uno Scena io di Re e O ien a a ai Se izi”, Tesi di Lau ea
di: Robe o Mo ini, Alma Ma e S udio um Uni e si à di Bologna.
[7] “P ác icas de labo a o io de Telemá ica II, Fil ado de paque es y NAT”, Mónica Aguila , Juanjo
Alins, Osca Espa za, Jose L. Muñoz, Ma cos Pos igo, An oni X. Val e de, ENTEL, Uni e si a
Poli ècnica de Ca alunya.
[8] “SIP Tu o ial”, Jö g O , Helsinki Uni e si y o Technology.
[9] “Pa se NRDL su A chi e u a clien -se e in PHP”, Robe o Mo ini, Uni e si à di Bologna.
38
else{
echo da e('h:i:s')." Il ile è aggio na o, in io dell'OK. n";
socke _w i e($sock,"OK");
}
}
$ c =socke _ ead($sock,1024);
echo da e('h:i:s')." Rice u a la po a su cui esegui e la mig azione: ".$ c ." n";
echo da e('h:i:s')." Esecuzione del comando di elepo ing sulla po a ".$ c ." n";
sleep(5);
sys em("sudo VBoxManage con ol m ".$_GET['nome']." elepo --hos ".$sck-
>ge Reques Hos ()." --po ".$ c )." n";
echo da e('h:i:s')." Mig azione esegui a n";
$ c =socke _ ead($sock,1024);
echo da e('h:i:s')." Rice u o: ".$ c ." n";
socke _close($sock);
?>
bye.php
<?php
equi e_once("Clien SIP.class.php");
equi e_once("Socke SIP.class.php");
session_s a ();
i (isse ($_SESSION['clien ']) && isse ($_SESSION['socke '])){
i (isse ($_GET['bye'])){
/*$b n = $_GET['bu on'];
$aq = s pos($b n, "[");
$cq = s pos($b n, "]");
$end = $cq - $aq;
$index = subs ($b n, $aq, $end);*/
$ esCode = "";
$cln = $_SESSION['clien '];
$sck = $_SESSION['socke '];
$sockNew = new Socke ($sck->ge S cPo ()); //C eo la nuo a socke di comunicazione
$sockNew->se Reques Hos ('10.10.10.66'); //Recupe o l'ip del des ina a io
$sockNew->se Reques Po ($sck->ge Reques Po ()); // ecupe o la po a del des ina a io
$cln ->se NewSocke ($sockNew); //Passo all'is anza del clien la nuo a socke
$cln ->se Me hod('BYE');
y{
$ esCode = $cln ->send();
}ca ch(Excep ion $e){
echo "Bye e o : ".$e;
}
$sockNew->closeSocke ();
$ esCode .= " Chiama a e mina a.--".$cln ->ge MessageFlow();
$_SESSION = a ay();
session_des oy();
//echo $ esCode." Chiama a e mina a.";
echo $ esCode;
}
}else{
45

echo "Nessuna is anza memo izza a";
}
?>
index.php
<?php
session_s a ();
?>
<h ml>
<head>
< i le> Clien - SIP </ i le>
<sc ip ype=" ex /ja asc ip ">
unc ion sendIn i e(){ /*Pe me e di e e ua e l'handshake a chiaman e e chiama o.
Ri o na il isul a o dell'handshake insieme ad in o mazioni aggiun i e ( lusso messaggi)*/
a o mDa i = documen . o ms[0];
a sBody = ge Reques Body( o mDa i);
//i (window.XMLH pReques ){
a oXMLH p = new XMLH pReques (); //C eazione ogge o XMLH pReques pe
e e ua e chiama e asinc one
//}else{
// ale ("no xmlh p eques ");
//}
oXMLH p.open("pos ", o mDa i.ac ion, ue);
oXMLH p.se Reques Heade ("Con en -Type", "applica ion/x-www- o m-u lencoded");
oXMLH p.on eadys a echange = unc ion(){/*Ges isce lo s a o della ichies a HTTP*/
i (oXMLH p. eadyS a e == 4){
i (oXMLH p.s a us == 200){
a s a usG = documen .ge Elemen ById("lblS a usG");
a b nBye = documen .ge Elemen ById("b nBye");
a b nMig = documen .ge Elemen ById("b nMig");
b nBye.disabled = alse;
b nMig.disabled = alse;
s a usG.inne HTML = "HTTP s a us-> " + oXMLH p.s a us; //S a o della
ichies a HTTP
//showResul (oXMLH p. esponseTex );
showResul Send(oXMLH p. esponseTex ); //S ampa nell'apposi o <di > la
esponse HTTP
//Abili a i se izi disponibili
a lis = documen .ge Elemen ById("lis Id");
lis .disabled = alse;
}else{
ale ("E o e: " + oXMLH p.s a usTex );
}
}
};
oXMLH p.send(sBody); //In ia la ichies a HTTP (INVITE SIP)
}
unc ion sendBye(){/*Ges isce la chiusu a del dialogo a chiaman e e chiama o*/
46
a bu on = documen .ge Elemen ById("b nBye");
//i (window.XMLH pReques ){
a oXMLH p = new XMLH pReques ();
//}else{
// ale ("no xmlh p eques ");
//}
oXMLH p.open("ge ","bye.php?bye=" + bu on. alue, ue);
oXMLH p.on eadys a echange = unc ion(){
i (oXMLH p. eadyS a e == 4){
i (oXMLH p.s a us == 200){
a s a usG = documen .ge Elemen ById("lblS a usG");
s a usG.inne HTML = "HTTP s a us-> " + oXMLH p.s a us;
a esponse = oXMLH p. esponseTex ;
showResul Bye( esponse); //Mos a il isul a o della chiusu a del dialogo
/* a spli _a ay = esponse.spli ("--");
a s a eDial = spli _a ay[0];
a messageFlow = spli _a ay[1];*/
//showResul (s a eDial);
// a di Ris = documen .ge Elemen ById(" is");
//di Ris.inne HTML = messageFlow;
//showResul (oXMLH p. esponseTex );
a b nBye = documen .ge Elemen ById("b nBye");
b nBye.disabled = ue;
//Disabili o i se izi
a lis = documen .ge Elemen ById("lis Id");
lis .disabled = ue;
}else{
ale ("E o e: " + oXMLH p.s a usTex );
}
}
};
oXMLH p.send(null);
}
unc ion sendMig(){/*Ges isce la mig azione a chiaman e e chiama o*/
a bu on = documen .ge Elemen ById("b nMig");
a selec = documen .ge Elemen ById("lis Id");
//i (window.XMLH pReques ){
a oXMLH p = new XMLH pReques ();
//}else{
// ale ("no xmlh p eques ");
//}
oXMLH p.open("ge ","mig .php?nome=" + selec .op ions[selec .selec edIndex]. alue,
ue);
oXMLH p.on eadys a echange = unc ion(){
i (oXMLH p. eadyS a e == 4){
i (oXMLH p.s a us == 200){
a s a usG = documen .ge Elemen ById("lblS a usG");
s a usG.inne HTML = "HTTP s a us-> " + oXMLH p.s a us;
a esponse = oXMLH p. esponseTex ;
showResul Mig( esponse); //Mos a il isul a o della chiusu a del dialogo
/* a spli _a ay = esponse.spli ("--");
a s a eDial = spli _a ay[0];
a messageFlow = spli _a ay[1];*/
47
//showResul (s a eDial);
// a di Ris = documen .ge Elemen ById(" is");
//di Ris.inne HTML = messageFlow;
//showResul (oXMLH p. esponseTex );
a b nMig = documen .ge Elemen ById("b nMig");
b nMig.disabled = ue;
//Disabili o i se izi
a lis = documen .ge Elemen ById("lis Id");
lis .disabled = ue;
}else{
ale ("E o e: " + oXMLH p.s a usTex );
}
}
};
oXMLH p.send(null);
}
unc ion ge Reques Body( o mDa i){
a aPa am = new A ay();
o ( a i=0; i< o mDa i.elemen s.leng h; i++){
a sPa am = encodeURIComponen ( o mDa i.elemen s[i].name);
sPa am += "=";
sPa am += encodeURIComponen ( o mDa i.elemen s[i]. alue);
aPa am.push(sPa am);
}
e u n aPa am.join("&");
}
unc ion showResul ( is){
a di Id = documen .ge Elemen ById("lblS a usD");
//di Id.s yle. loa = " igh ";
di Id.inne HTML += is + " ";
/* a spli _a ay = is.spli ("Rispos a ice u a");
di Id.inne HTML += "<p>" + spli _a ay[0] + " </p>";
di Id.inne HTML += "<p>" + spli _a ay[1] + " </p>";*/
}
unc ion showResul Send( is){/*Mos a il isul a o della sendIn i e()*/
// a di Id = documen .ge Elemen ById(" isSend");
// a A = documen .ge Elemen ById("a");
a send1 = documen .ge Elemen ById(" isSend1");
a send2 = documen .ge Elemen ById(" isSend2");
a send3 = documen .ge Elemen ById(" isSend3");
a send4 = documen .ge Elemen ById(" isSend4");
a send5 = documen .ge Elemen ById(" isSend5");
a send6 = documen .ge Elemen ById(" isSend6");
a send7 = documen .ge Elemen ById(" isSend7");
a send8 = documen .ge Elemen ById(" isSend8");
a send9 = documen .ge Elemen ById(" isSend9");
a send10 = documen .ge Elemen ById(" isSend10");
a send11 = documen .ge Elemen ById(" isSend11");
//send11.s yle.display='none';
48
a send22 = documen .ge Elemen ById(" isSend22");
//send22.s yle.display='none';
a send33 = documen .ge Elemen ById(" isSend33");
//send33.s yle.display='none';
a send44 = documen .ge Elemen ById(" isSend44");
//send44.s yle.display='none';
a send55 = documen .ge Elemen ById(" isSend55");
//send55.s yle.display='none';
a send66 = documen .ge Elemen ById(" isSend66");
a send77 = documen .ge Elemen ById(" isSend77");
a send88 = documen .ge Elemen ById(" isSend88");
a send99 = documen .ge Elemen ById(" isSend99");
a send1010 = documen .ge Elemen ById(" isSend1010");
send=1;
is= is. eplace("In io ACK.","Rispos a ice u a");
is= is. eplace("In io upda e","Rispos a ice u a");
is= is. eplace("In io ihies a","Rispos a ice u a");
a spli _a ay = is.spli ("Rispos a ice u a");
o ( a i=0; i<spli _a ay.leng h; i++){
spli _a ay[i] = spli _a ay[i]. eplace(/ </g, "<");
a in = spli _a ay[i].indexO ('Via');
a heade =spli _a ay[i].subs ing(0, in);
a ail=spli _a ay[i].subs ing( in);
i ( indChain( ail,"F om: <sip:[email p o ec ed]") == 1){
//di Id.inne HTML += "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
i (send==1){
send1.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send11.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=2;
}
else i (send==2){
send2.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send22.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=3;
}
else i (send==3){
send3.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send33.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=4;
}
else i (send==4){
send4.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send44.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=5;
}
49
else i (send==5){
send5.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send55.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=6;
}
else i (send==6){
send6.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send66.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=7;
}
else i (send==7){
send7.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send77.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=8;
}
else i (send==8){
send8.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send88.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=9;
}
else i (send==9){
send9.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send99.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=10;
}
else{
send10.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
send1010.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + "
</p e></p>";
send=1;
}
}
else{
//di Id.inne HTML += ;//"<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
i (send==1){
send1.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send11.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=2;
}
else i (send==2){
send2.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
50

</p e></p>";
send22.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=3;
}
else i (send==3){
send3.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send33.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=4;
}
else i (send==4){
send4.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send44.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=5;
}
else i (send==5){
send5.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send55.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=6;
}
else i (send==6){
send6.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send66.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=7;
}
else i (send==7){
send7.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send77.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=8;
}
else i (send==8){
send8.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send88.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=9;
}
else i (send==9){
send9.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send99.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
send=10;
}
else{
send10.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
send1010.inne HTML = "<p><p e>" + ail. on colo ("#000000") + "
</p e></p>";
send=1;
}
}
51
}
}
unc ion showResul Bye( is){/*Mos a il isul a o della sendBye()*/
// a di Id = documen .ge Elemen ById(" isBye");
a bye1 = documen .ge Elemen ById(" isBye1");
a bye2 = documen .ge Elemen ById(" isBye2");
// a bye3 = documen .ge Elemen ById(" isBye3");
// a bye4 = documen .ge Elemen ById(" isBye4");
a bye11 = documen .ge Elemen ById(" isBye11");
a bye22 = documen .ge Elemen ById(" isBye22");
// a bye33 = documen .ge Elemen ById(" isBye33");
// a bye44 = documen .ge Elemen ById(" isBye44");
a sen =1;
// is= is. eplace("In io ihies aBYE","Rispos a ice u aBYE");
a spli _a ay0 = is.spli ("In io BYE");
a spli _a ay = spli _a ay0[1].spli ("Rispos a ice u a");
o ( a i=0; i<spli _a ay.leng h; i++){
spli _a ay[i] = spli _a ay[i]. eplace(/ </g, "<");
//di Id.inne HTML += "<p><p e>" + spli _a ay[i] + " </p e></p>";
a in = spli _a ay[i].indexO ('Via');
a heade =spli _a ay[i].subs ing(0, in);
a ail=spli _a ay[i].subs ing( in);
i ( indChain( ail,"F om: <sip:[email p o ec ed]") == 1){
i (sen ==1){
bye1.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
bye11.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + " </p e></p>";
sen =2;
}
else i (sen ==2){
bye2.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
bye22.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + " </p e></p>";
sen =3;
}
/*else i (sen ==3){
bye3.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
bye33.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + " </p e></p>";
sen =4;
}
else {
bye4.inne HTML = "<p><p e>" + heade . on colo ("#FF9900") + "
</p e></p>";
bye44.inne HTML = "<p><p e>" + ail. on colo ("#FF9900") + " </p e></p>";
sen =1;
}*/
}
else{
i (sen ==1){
bye1.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
bye11.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
52
sen =2;
}
else i (sen ==2){
bye2.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
bye22.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
sen =3;
}
/*else i (sen ==3){
bye3.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
bye33.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
sen =4;
}
else {
bye4.inne HTML = "<p><p e>" + heade . on colo ("#000000") + "
</p e></p>";
bye44.inne HTML = "<p><p e>" + ail. on colo ("#000000") + " </p e></p>";
sen =1;
}*/
}
}
}
unc ion showResul Mig( is){/*Mos a il isul a o della sendBye()*/
a di Id = documen .ge Elemen ById(" isMig");
a spli _a ay = is.spli ("Rispos a ice u a");
o ( a i=0; i<spli _a ay.leng h; i++){
spli _a ay[i] = spli _a ay[i]. eplace(/ </g, "<");
di Id.inne HTML += "<p><p e>" + spli _a ay[i] + " </p e></p>";
}
}
unc ion indChain(Chain,lChain){
a esul =0;
o ( a i=0 ; i<Chain.leng h ; i++){
i (Chain[i] == lChain[0]){
o ( a j=0 ; j<lChain.leng h ; j++){
i (Chain[i+j] != lChain[j])b eak;
else i (j==lChain.leng h-1) esul =1;
}
}
}
e u n esul ;
}
unc ion displayElem(id){
a x=documen .ge Elemen ById(id);
i (x.s yle.display=='inline') x.s yle.display='none';
i (x.s yle.display=='none') x.s yle.display='block';
else i (x.s yle.display='block') x.s yle.display='none';
}
</sc ip >
53
</head>
<body id="bodyId" s yle="backg ound-colo :#494949; on - amily:a ial, hel e ica, sans-se i ;">
<di s yle="wid h:930px; ma gin:0 au o;backg ound-colo :#FFFFFF; padding:15px;">
< able cellspacing="0" cellpadding="0" wid h="930" >
< >
< d wid h="190" align="middle">
<img s c="images/logo.gi ">
</ d>
< d s yle="backg ound-colo :#18964A;" aling="le ">
<img s c="images/ es a a.gi ">
</ d>
</ >
</ able>
<p><label>Use Agen : <s ong>use 1</s ong> </label><di id="lblS a usG"></di ></p>
<p>Ques o use agen decide di a e mig a e un suo se izio in un'al o Use Agen , scel o
secondo de e mina i c i e i.</p>
<p><label>S a o dialogo:</label><di id="lblS a usD"></di ></p>
< o m ac ion="in i e.php" me hod="pos " onsubmi ="sendIn i e(); e u n alse">
<p><!-- <label>Chiaman e:</label> --><inpu ype="hidden" name="calle " size="20"
alue="sip:[email p o ec ed]" /></p>
<p><label>Use Agen scel o come a ge della mig azione: </label><inpu
ype=" ex " name="callee" size="20" alue="sip:[email p o ec ed]" /></p>
<p><inpu id="b ncll" ype="submi " name="call" alue="Call" /></p>
</ o m>
<di id="migId">
<?php
$s ing = shell_exec('sudo VBoxManage lis unning ms');
$nome=explode('"',$s ing);
?>
< o m ac ion="mig .php" me hod="ge " onsubmi ="sendMig(); e u n alse">
<p><inpu id="b nMig" ype="submi " name="mig" alue="Mig azione"
disabled="disabled"/>
<p>Se izio che e à a o mig a e:
<selec id ="lis Id" name="lis " >
<?php o ($i=1; $i<coun ($nome); $i+=2){ ?>
<op ion alue="<?php echo $nome[$i] ?>"> <?php echo $nome[$i] ?></op ion>
<?php } ?>
</selec >
</p>
</ o m>
</di >
<di id="byeId">
< o m ac ion="bye.php" me hod="ge " onsubmi ="sendBye(); e u n alse">
<inpu id="b nBye" ype="submi " name="bye" alue="Bye" disabled="disabled"/>
</ o m>
</di >
<di id=" is"></di >
<di id=" isSend1" s yle="colo :#000000"
onclick="ja asc ip :displayElem('main1')"></di >
<ul id="main1" class="ni el2">
<di id=" isSend11"></di >
54
p i a e $passwo d;
/**
* To
*/
p i a e $ o;
/**
* To ag
*/
p i a e $ o_ ag;
/**
* F om
*/
p i a e $ om;
/**
* F om Use
*/
p i a e $ om_use ;
/**
* F om ag
*/
p i a e $ om_ ag;
/**
* INVITE F om ag
*/
p i a e $in _ om_ ag;
/**
* Via ag
*/
p i a e $ ia;
/**
* INVITE Via ag
*/
p i a e $in _ ia;
/**
* Con en ype
*/
p i a e $con en _ ype;
/**
* Body
*/
p i a e $body;
/**
* Recei ed Response
61

*/
p i a e $ esponse; // whole esponse body
p i a e $ es_code;
p i a e $ es_con ac ;
p i a e $ es_cseq_me hod;
p i a e $ es_cseq_numbe ;
/**
* Recei ed Reques
*/
p i a e $ eq_me hod;
p i a e $ eq_cseq_me hod;
p i a e $ eq_cseq_numbe ;
p i a e $ eq_con ac ;
/**
* Au hen ica ion
*/
p i a e $au h;
/**
* Rou es
*/
p i a e $ ou es = a ay();
/**
* Reques ias
*/
p i a e $ eques _ ia = a ay();
/**
* Addi ional heade s
*/
p i a e $ex a_heade s = a ay();
/**
* Ges ione callID dinamico
*/
p i a e $use Chiaman e;
p i a e $hashChiaman e;
p i a e $use Chiama o;
p i a e $hashChiama o;
/**
* Cons uc o
*
* @pa am $socke _is
*/
public unc ion __cons uc ($socke _is )
{
$ his->socke = $socke _is ;
}
/**
62
* Des uc o
*/
public unc ion __des uc ()
{
//$ his->socke ->closeSocke ();
}
/**
* Se s debuggin ON/OFF
*
* @pa am bool $s a us
*/
public unc ion se Debug($s a us = alse)
{
$ his->debug = $s a us;
}
/**
* Adds adi ional heade
*
* @pa am s ing $heade
*/
public unc ion addHeade ($heade )
{
$ his->ex a_heade s[] = $heade ;
}
/**
* Se s F om heade
*
* @pa am s ing $ om
*/
public unc ion se F om($ om)
{
/*Ges ione callID dinamico*/
$ his->use Chiaman e = explode(":", $ om);
$ his->hashChiaman e = subs (md5($ his->use Chiaman e[1]), 0, 6);
$ d = open("/ mp/hashChiaman e. x ", "w");
w i e($ d, $ his->hashChiaman e);
close($ d);
/**/
i (p eg_ma ch('/<.*>$/',$ om))
{
$ his-> om = $ om;
}
else
{
$ his-> om = '<'.$ om.'>';
}
$m = a ay();
i (!p eg_ma ch('/sip:(.*)@/i',$ his-> om,$m))
{
63
h ow new PhpSIPExcep ion('Failed o pa se F om use name.');
}
$ his-> om_use = $m[1];
}
/**
* Se s me hod
*
* @pa am s ing $me hod
*/
public unc ion se Me hod($me hod)
{
i (!in_a ay($me hod,$ his->allowed_me hods))
{
h ow new PhpSIPExcep ion('In alid me hod.');
}
$ his->me hod = $me hod;
i ($me hod == 'INVITE')
{
/*Ape u a ile NRDL_INVITE. d */
$pa h = "./NRDL_INVITE. d ";
$ d = open($pa h," ");
$n dl = ead($ d, ilesize($pa h));
/*$body = " =0 n";
$body.= "o=click2dial 0 0 IN IP4 ".$ his->socke ->ge Ip()." n";
$body.= "s=click2dial call n";
$body.= "c=IN IP4 ".$ his->socke ->ge Ip()." n";
$body.= " =0 0 n";
$body.= "m=audio 8000 RTP/AVP 0 8 18 3 4 97 98 n";
$body.= "a= pmap:0 PCMU/8000 n";
$body.= "a= pmap:18 G729/8000 n";
$body.= "a= pmap:97 ilbc/8000 n";
$body.= "a= pmap:98 speex/8000 n";*/
/*Ges ione callID dinamico*/
$ce ca = "diag12345";
$sos i uisci = $ his->hashChiaman e."".$ his->hashChiama o;
$n dl = s _ eplace($ce ca, $sos i uisci, $n dl);
/**/
/*Aggiun a del con enu o n dl al body*/
$body = $n dl." n";
$ his->body = $body;
$ his->se Con en Type(null);
}
i ($me hod == 'REFER')
64
{
$ his->se Body('');
}
i ($me hod == 'CANCEL')
{
$ his->se Body('');
$ his->se Con en Type(null);
}
i ($me hod == 'MESSAGE')
{
$ his->se Con en Type(null);
}
i ($me hod == 'UPDATE'){
$ his->se Body('');
}
i ($me hod == 'REGISTER'){
$ his->se Body('');
}
i ($me hod == 'BYE'){
//Copy NRDL_ ecei ed. d in BYE.
$pa h = "./NRDL_ ecei ed. d ";
$ d = open($pa h," ");
$n dl = ead($ d, ilesize($pa h));
$body = $n dl." n";
$ his->body = $body;
}
}
/**
* Se s SIP P oxy
*
* @pa am $p oxy
*/
public unc ion se P oxy($p oxy)
{
$ his->p oxy = $p oxy;
}
/**
* Se s eques URI
*
* @pa am s ing $u i
*/
public unc ion se U i($u i)
{
/*Ges ione callID dinamico*/
$ his->use Chiama o = explode(":", $u i);
$ his->hashChiama o = subs (md5($ his->use Chiama o[1]), 0, 6);
$ d = open("/ mp/hashChiama o. x ", "w");
w i e($ d, $ his->hashChiama o);
close($ d);
65
/**/
i (s pos($u i,'sip:') === alse)
{
h ow new PhpSIPExcep ion("Only sip: URI suppo ed.");
}
$ his->u i = $u i;
$ his-> o = '<'.$u i.'>';
i ($ his->p oxy)
{
i (s pos($ his->p oxy,':'))
{
$ emp = explode(":",$ his->p oxy);
$ his->socke ->se Reques Hos ($ emp[0]);
$ his->socke ->se Reques Po ($ emp[1]);
}
else
{
$ his->socke ->se Reques Hos ($ his->p oxy);
}
}
else
{
$u l = s _ eplace("sip:","sip://",$u i);
i (!$u l = @pa se_u l($u l))
{
h ow new PhpSIPExcep ion("Failed o pa se URI.");
}
$ his->socke ->se Reques Hos ($u l['hos ']);
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
}
}
/**
* Se s use name
*
* @pa am s ing $use name
*/
public unc ion se Use name($use name)
{
$ his->use name = $use name;
}
/**
* Se s Use Agen
66

*
* @pa am s ing $use _agen
*/
public unc ion se Use Agen ($use _agen )
{
$ his->use _agen = $use _agen ;
}
/**
* Se s passwo d
*
* @pa am s ing $passwo d
*/
public unc ion se Passwo d($passwo d)
{
$ his->passwo d = $passwo d;
}
/**
* Sends SIP eques
*
* @ e u n s ing Reply
*/
public unc ion send()
{
i (!$ his-> om)
{
h ow new PhpSIPExcep ion('Missing F om.');
}
i (!$ his->me hod)
{
h ow new PhpSIPExcep ion('Missing Me hod.');
}
i (!$ his->u i)
{
h ow new PhpSIPExcep ion('Missing URI.');
}
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
i ($ his->me hod == 'BYE') $ his->messageFlow .= "In io BYE".$da a." n";
else $ his->messageFlow .= "In io ihies a".$da a." n";
$ his-> eadResponse();
/*To na o dalla le u a della p ima ispos a, de e gua da e qual è il codice o na o: $ his-
> es_code*/
//Con ollo la p ima ci a
$p imaCi a = subs ($ his-> es_code,0,1);
i ($p imaCi a == '2' && $ his-> es_cseq_me hod == 'BYE'){/*Richies a di BYE anda a a buon
ine*/
e u n $ his-> es_code;
67
}
swi ch($p imaCi a){
case '2': //ack in ia o p eceden emen e: il dialogo è s abili o->La socke de e imane e a i a
pe ges i e la chiusu a del dialogo
b eak;
case '1': // ispos a p o iso ia: de e o na e a legge e pe s a e in a esa di una ispos a
de ini i a
$i = 0;
while (subs ($ his-> es_code,0,1) == '1' && $i < 4){
//p in ("<BR>".$i."<BR>");
//@ lush();
$ his-> eadResponse();
$i++;
}
/*i ($i == 4){
$ his->messageE o = "Nessuna ispos a de ini i a pe enu a dopo ".$i."
en a i i";
}else{
//Rice u a una ispos a non p o iso ia p ima dei 4 en a i i
//L'ack a in ia o al des ina a io
e u n $ his-> es_code;
}*/
b eak;
case '4': //4xx clien e o
i ($ his-> es_code == '407'){
$ his->cseq++;
$ his->au h();
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his-> eadResponse();
}
i ($ his-> es_code == '401'){
$ his->cseq++;
$ his->au hWWW();
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his-> eadResponse();
}
b eak;
de aul : $ his->messageFlow = "Nessun da o da legge e n";
}//Chiusu a swi ch
$ his->ex a_heade s = a ay();
$ his->cseq++;
e u n $ his-> es_code;
}
/**
* Lis en o eques
*
* @ odo This needs o be imp o ed
*/
68
public unc ion lis en()
{
$ his-> eadResponse();
swi ch($ his-> eq_me hod){
case 'BYE': //Richies a di chiusu a
$ his-> eply("200","OK");
//$ his-> eq_me hod .= " Response in ia a dopo la icezione di ichies a di
chiusu a n";
//$ his->socke ->closeSocke ();
b eak;
case 'INVITE':
/*Impos o il des ina a io al p imo eques _ ia o a o*/
o ($i = 0; $i < 1; $i++){
//$ his->socke ->se Reques Hos ($ his-> eques _ ia[$i]); //P imo ia che incon a
/*Es aggo l'ip e l'e en uale po a dal p imo ia*/
$des = $ his-> eques _ ia[$i];
$semicolon = s pos($des , ";");
i ($semicolon !== alse){
$des = subs ($des , 0, $semicolon);
}
$ emp = explode(" ", $des ); //Con iene l'ip e l'e en uale po a
$ emp2 = explode(":", $ emp[1]);
$ his->socke ->se Reques Hos ($ emp2[0]);//Se o l'ip
i (isse ($ emp2[1])){//Se c'è anche la po a
$ his->se Reques Po ($ emp2[1]);
}
}
$ his-> eplyWi hBody("100","T ying");
$ his-> eplyWi hBody("200","OK");
b eak;
case 'ACK':
/*Impos o come nuo o des ina a io il Con ac */
$u l = s _ eplace("sip:","sip://",$ his-> eq_con ac );
i (!$u l = @pa se_u l($u l))
{
h ow new PhpSIPExcep ion("Failed o pa se URI-Con ac .");
}
$ his->socke ->se Reques Hos ($u l['hos ']);
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
b eak;
// de aul : echo 'No eques ecei ed';
}
e u n $ his-> eq_me hod;
}
/**
69
* Reads esponse
*/
p i a e unc ion eadResponse()
{
$ his-> esponse = $ his->socke -> ec Da a();
$ his->messageFlow .= "Rispos a ice u a n".$ his-> esponse." n";
// Response
$ esul = a ay();
i (p eg_ma ch('/^SIP /2 .0 ([0-9]{3})/',$ his-> esponse,$ esul ))
{
$ his-> es_code = im($ esul [1]);
$ es_class = subs ($ his-> es_code,0,1);
i ($ es_class == '1' || $ es_class == '2')
{
$ his->dialog = ue;
}
$ his->pa seResponse();
}
// Reques
else
{
$ his->pa seReques ();
}
}
/**
* Pa se Response
*/
p i a e unc ion pa seResponse()
{
// il p oxy è impos a o, $ his-> eques Hos = p oxy
// il p oxy non è impos a o e non c'è una RR, allo a $ his-> eques Hos = Con ac del messaggio
di ispos a
// il p oxy non è impos a o e c'è una RR, allo a $ his-> eques Hos = p ima eco d- ou e
impos a a.
// To ag
$ esul = a ay();
i (p eg_ma ch('/^To: .*; ag=(.*)$/im',$ his-> esponse,$ esul ))
{
$ his-> o_ ag = im($ esul [1]);
}
// Rou e
$ esul = a ay();
i (p eg_ma ch_all('/^Reco d-Rou e: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ou e)
{
i (!in_a ay( im($ ou e),$ his-> ou es))
{
70
$ his->socke ->sendDa a($ );
$ his->messageFlow .= "In io esponse con body. n".$ ." n";
}
public unc ion egis e ()
{
$ his->se Use name('cana io');
$ his->se Passwo d('c ema');
$ his->se Me hod('REGISTER');
$ his->se U i('sip:[email p o ec ed]');
$dades = $ his-> o ma Reques ();
$ his->socke ->sendDa a($dades);
$ his->messageFlow = ' Call comple ed'." n n n".$dades." n";
$ his-> eadResponse();
}
/**
* ACK
*/
p i a e unc ion ack()
{
/*Pe co so ile NRDL*/
$pa h = "/ a /www/h ml/sip/ es somoza/NRDL_ACK. d ";
$n dl = "";
i ($ his-> es_cseq_me hod == 'INVITE' && $ his-> es_code == '200')
{
$a = 'ACK '.$ his-> es_con ac .' SIP/2.0'." n";
/*Ape u a ile NRDL_ACK*/
$ d = open($pa h," ");
$n dl = ead($ d, ilesize($pa h));
/*Ges ione callID dinamico*/
$ce ca = "diag12345";
$sos i uisci = $ his->hashChiaman e."".$ his->hashChiama o;
$n dl = s _ eplace($ce ca, $sos i uisci, $n dl);
/**/
}
else
{
$a = 'ACK '.$ his->u i.' SIP/2.0'." n";
}
// Via
$a.= 'Via: '.$ his->in _ ia." n";
// Rou e
i ($ his-> ou es)
{
o each ($ his-> ou es as $ ou e)
{
$a.= 'Rou e: '.$ ou e." n";
}
}
// F om
i (!$ his-> om_ ag) $ his->se F omTag();
$a.= 'F om: '.$ his-> om.'; ag='.$ his->in _ om_ ag." n";
77

// To
i ($ his-> o_ ag)
$a.= 'To: '.$ his-> o.'; ag='.$ his-> o_ ag." n";
else
$a.= 'To: '.$ his-> o." n";
// Call-ID
i (!$ his->call_id) $ his->se CallId();
$a.= 'Call-ID: '.$ his->in _call_id." n";
//CSeq
$a.= 'CSeq: '.$ his->cseq.' ACK'." n";
// Au hen ica ion
i ($ his-> es_code == '200' && $ his->au h)
{
$a.= 'P oxy-Au ho iza ion: '.$ his->au h." n";
}
// Max-Fo wa ds
$a.= 'Max-Fo wa ds: 70'." n";
// Use -Agen
$a.= 'Use -Agen : '.$ his->use _agen ." n";
// Con en -Leng h
$a.= 'Con en -Leng h: '.s len($n dl)." n";
$a.= " n";
//Aggiun a del body con enen e l'NRDL_ACK
$a.= $n dl." n";
// $dx_ip = $ his->socke ->ge F omip();
// $dx_po = $ his->socke ->ge F ompo ();
//$ his->socke ->sendDa a($a, $dx_ip, $dx_po );
$ his->socke ->sendDa a($a);
$ his->messageFlow .= "In io ACK. n".$a." n";
}
/**
* Fo ma s SIP eques
*
* @ e u n s ing
*/
p i a e unc ion o ma Reques ()
{
i (in_a ay($ his->me hod,a ay('BYE','REFER','SUBSCRIBE')))
{
$ = $ his->me hod.' '.$ his-> es_con ac .' SIP/2.0'." n";
}
else
{
$ = $ his->me hod.' '.$ his->u i.' SIP/2.0'." n";
}
// Via
i ($ his->me hod != 'CANCEL')
{
$ his->se Via();
i ($ his->me hod == 'INVITE')$ his->in _ ia = $ his-> ia;
}
78
$ .= 'Via: '.$ his-> ia." n";
// Rou e
i ($ his->me hod != 'CANCEL' && $ his-> ou es)
{
o each ($ his-> ou es as $ ou e)
{
$ .= 'Rou e: '.$ ou e." n";
}
}
// F om
i ($ his-> om_ ag && $ his->me hod == 'INVITE'){
$ his->se F omTag();
$ his->in _ om_ ag = $ his-> om_ ag;
$ .= 'F om: '.$ his-> om.'; ag='.$ his->in _ om_ ag." n";
}
else i (!$ his-> om_ ag && $ his->me hod == 'REGISTER'){
$ his->se F omTag();
$ .= 'F om: '.$ his-> om.'; ag='.$ his-> om_ ag." n";
}
else i ($ his-> om_ ag && $ his->me hod == 'UPDATE'){
$ his->se F omTag();
$ .= 'F om: '.$ his-> om.'; ag='.$ his-> om_ ag." n";
}
else i ($ his-> om_ ag && $ his->me hod == 'BYE'){
$ his->se F omTag();
$ .= 'F om: '.$ his-> om.'; ag='.$ his-> om_ ag." n";
}
else{
$ .= 'F om: '.$ his-> om.'; ag='.$ his->in _ om_ ag." n";
}
// To
i (!in_a ay($ his->me hod,a ay("INVITE","CANCEL","NOTIFY","UPDATE","BYE")) &&
$ his-> o_ ag)
$ .= 'To: '.$ his-> o.'; ag='.$ his-> o_ ag." n";
else
$ .= 'To: '.$ his-> o." n";
// Au hen ica ion
i ($ his->au h)
{
$ .= $ his->au h." n";
$ his->au h = null;
}
// Call-ID
i ($ his->me hod == 'INVITE'){
$ his->se CallId();
$ his->in _call_id = $ his->call_id;
$ .= 'Call-ID: '.$ his->in _call_id." n";
}
else i ($ his->call_id && $ his->me hod == 'UPDATE'){
$ his->se CallId();
$ .= 'Call-ID: '.$ his->call_id." n";
}
else i (!$ his->call_id && $ his->me hod == 'REGISTER'){
$ his->se CallId();
79
$ .= 'Call-ID: '.$ his->call_id." n";
}
else{
$ .= 'Call-ID: '.$ his->in _call_id." n";
}
//CSeq
i ($ his->me hod == 'CANCEL')
{
$ his->cseq--;
}
$ .= 'CSeq: '.$ his->cseq.' '.$ his->me hod." n";
// Con ac
i ($ his->me hod != 'MESSAGE')
{
$ .= 'Con ac : <sip:'.$ his-> om_use .'@'.$ his->socke ->ge Ip().':'.$ his->socke -
>ge S cPo ().'>'." n";
}
// Con en -Type
i ($ his->con en _ ype)
{
$ .= 'Con en -Type: '.$ his->con en _ ype." n";
}
// Max-Fo wa ds
$ .= 'Max-Fo wa ds: 70'." n";
// Use -Agen
$ .= 'Use -Agen : '.$ his->use _agen ." n";
// Addi ional heade
o each ($ his->ex a_heade s as $heade )
{
$ .= $heade ." n";
}
// Con en -Leng h
$ .= 'Con en -Leng h: '.s len($ his->body)." n";
$ .= " n";
$ .= $ his->body;
e u n $ ;
}
/**
* Se s body
*/
public unc ion se Body($body)
{
$ his->body = $body;
}
/**
* Se s Con en Type
*/
public unc ion se Con en Type($con en _ ype = null)
{
i ($con en _ ype !== null)
{
80
$ his->con en _ ype = $con en _ ype;
}
else
{
swi ch ($ his->me hod)
{
case 'INVITE':
$ his->con en _ ype = 'applica ion/ d +xml'; //$ his->con en _ ype = 'applica ion/sdp';
b eak;
case 'MESSAGE':
$ his->con en _ ype = ' ex /h ml; cha se =u -8';
b eak;
de aul :
$ his->con en _ ype = null;
}
}
}
/**
* Se s Via heade
*/
p i a e unc ion se Via()
{
$ and = and(100000,999999);
$ his-> ia = 'SIP/2.0/UDP '.$ his->socke ->ge IP().':'.$ his->socke -
>ge S cPo ().'; po ;b anch=z9hG4bK'.$ and;
}
/**
* Se s om ag
*/
p i a e unc ion se F omTag()
{
$ his-> om_ ag = and(10000,99999);
}
/**
* Se s call id
*/
p i a e unc ion se CallId()
{
$ his->call_id = md5(uniqid()).'@'.$ his->socke ->ge Ip();
}
/**
* Ge s alue o he heade om he p e ious eques
*
* @pa am s ing $name Heade name
*
* @ e u n s ing o alse
*/
public unc ion ge Heade ($name)
{
i (p eg_ma ch('/^'.$name.': (.*)$/m',$ his-> esponse,$ esul ))
81
{
e u n im($ esul [1]);
}
else
{
e u n alse;
}
}
/**
* Calcula es Diges au hen ica ion esponse
*
*/
public unc ion au h()
{
i (!$ his->use name)
{
h ow new PhpSIPExcep ion("Missing use name");
}
i (!$ his->passwo d)
{
h ow new PhpSIPExcep ion("Missing passwo d");
}
// ealm
$ esul = a ay();
i (!p eg_ma ch('/^P oxy-Au hen ica e: .* ealm="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind ealm in p oxy-au h");
}
$ ealm = $ esul [1];
// nonce
$ esul = a ay();
i (!p eg_ma ch('/^P oxy-Au hen ica e: .* nonce="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind nonce in p oxy-au h");
}
$nonce = $ esul [1];
$ha1 = md5($ his->use name.':'.$ ealm.':'.$ his->passwo d);
$ha2 = md5($ his->me hod.':'.$ his->u i);
$ es = md5($ha1.':'.$nonce.':'.$ha2);
$ his->au h = 'P oxy-Au ho iza ion: Diges use name="'.$ his->use name.'", ealm="'.$ ealm.'",
nonce="'.$nonce.'", u i="'.$ his->u i.'",
esponse="'.$ es.'", algo i hm=MD5';
}
/**
82

* Calcula es WWW au ho iza ion esponse
*
*/
p i a e unc ion au hWWW()
{
i (!$ his->use name)
{
h ow new PhpSIPExcep ion("Missing au h use name");
}
i (!$ his->passwo d)
{
h ow new PhpSIPExcep ion("Missing au h passwo d");
}
$qop_p esen = alse;
i (s pos($ his-> esponse,'qop=') !== alse)
{
$qop_p esen = ue;
// we can only do qop="au h"
i (s pos($ his-> esponse,'qop="au h"') === alse)
{
h ow new PhpSIPExcep ion('Only qop="au h" diges au hen ica ion suppo ed.');
}
}
// ealm
$ esul = a ay();
i (!p eg_ma ch('/^WWW-Au hen ica e: .* ealm="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind ealm in www-au h");
}
$ ealm = $ esul [1];
// nonce
$ esul = a ay();
i (!p eg_ma ch('/^WWW-Au hen ica e: .* nonce="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind nonce in www-au h");
}
$nonce = $ esul [1];
$ha1 = md5($ his->use name.':'.$ ealm.':'.$ his->passwo d);
$ha2 = md5($ his->me hod.':'.$ his->u i);
i ($qop_p esen )
{
$cnonce = md5( ime());
$ es = md5($ha1.':'.$nonce.':00000001:'.$cnonce.':au h:'.$ha2);
}
83
else
{
$ es = md5($ha1.':'.$nonce.':'.$ha2);
}
$ his->au h = 'Au ho iza ion: Diges use name="'.$ his->use name.'", ealm="'.$ ealm.'",
nonce="'.$nonce.'", u i="'.$ his->u i.'", esponse="'.$ es.'", algo i hm=MD5';
i ($qop_p esen )
{
$ his->au h.= ', qop="au h", nc="00000001", cnonce="'.$cnonce.'"';
}
}
/**
* Rese s callid, o/ om ags e c.
*
*/
public unc ion newCall()
{
$ his->cseq = 20;
$ his->call_id = null;
$ his-> o_ ag = null;;
$ his-> om_ ag = null;;
/**
* Body
*/
$ his->body = null;
/**
* Recei ed Response
*/
$ his-> esponse = null;
$ his-> es_code = null;
$ his-> es_con ac = null;
$ his-> es_cseq_me hod = null;
$ his-> es_cseq_numbe = null;
/**
* Recei ed Reques
*/
$ his-> eq_me hod = null;
$ his-> eq_cseq_me hod = null;
$ his-> eq_cseq_numbe = null;
$ his-> eq_con ac = null;
$ his-> ou es = a ay();
$ his-> eques _ ia = a ay();
}
/*Tiene accia di u i i messaggi ice u i e in ia i*/
public unc ion ge MessageFlow(){
e u n $ his->messageFlow;
84
}
public unc ion se NewSocke ($newSock){
$ his->socke = $newSock;
}
}
?>
C.II es somoza_upda e
Clien SIP.class.php
<?php
/**
* PHP SIP UAC class
*
* @ing oup API
* @au ho
*
* @ e sion SVN: $Id: PhpSIP.class.php 24 2009-11-25 11:39:57Z le el7sys ems $
*/
equi e_once ('PhpSIP.Excep ion.php');
equi e_once('Socke SIP.class.php');
class PhpSIP
{
/*
* S inga con enen e i messaggi scambia i
*/
p i a e $messageFlow;
p i a e $debug = alse;
/**
* Final Response ime (in seconds)
*/
p i a e $ _ ime = 7;
/**
* Allowed me hods a ay
*/
p i a e $allowed_me hods = a ay(
"CANCEL","NOTIFY",
"INVITE","BYE","REFER","OPTIONS","SUBSCRIBE","MESSAGE","UPDATE","REGISTER"
);
/**
* Dialog es ablished
*/
p i a e $dialog = alse;
/**
* The opened socke we lis en o incoming SIP messages
85
*/
p i a e $socke ;
/**
* Sou ce IP add ess
*/
p i a e $use _agen = 'PHP SIP';
/**
* CSeq
*/
p i a e $cseq = 20;
/**
* Call ID
*/
p i a e $call_id;
/**
* INVITE Call ID
*/
p i a e $in _call_id;
/**
* Con ac
*/
p i a e $con ac ;
/**
* Reques URI
*/
p i a e $u i;
/**
* Reques hos
*/
p i a e $hos ;
/**
* Reques po
*/
p i a e $po = 5060;
/**
* Ou boud SIP p oxy
*/
public $p oxy;
/**
* Me hod
*/
p i a e $me hod;
/**
* Au h use name
86
}
$ his->socke ->se Reques Hos ($u l['hos ']);
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
}
}
/**
* Se s use name
*
* @pa am s ing $use name
*/
public unc ion se Use name($use name)
{
$ his->use name = $use name;
}
/**
* Se s Use Agen
*
* @pa am s ing $use _agen
*/
public unc ion se Use Agen ($use _agen )
{
$ his->use _agen = $use _agen ;
}
/**
* Se s passwo d
*
* @pa am s ing $passwo d
*/
public unc ion se Passwo d($passwo d)
{
$ his->passwo d = $passwo d;
}
/**
* Sends SIP eques
*
* @ e u n s ing Reply
*/
public unc ion send()
{
i (!$ his-> om)
{
h ow new PhpSIPExcep ion('Missing F om.');
}
i (!$ his->me hod)
93

{
h ow new PhpSIPExcep ion('Missing Me hod.');
}
i (!$ his->u i)
{
h ow new PhpSIPExcep ion('Missing URI.');
}
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
i ($ his->me hod == 'BYE') $ his->messageFlow .= "In io BYE".$da a." n";
else $ his->messageFlow .= "In io ihies a".$da a." n";
$ his-> eadResponse();
/*To na o dalla le u a della p ima ispos a, de e gua da e qual è il codice o na o: $ his-
> es_code*/
//Con ollo la p ima ci a
$p imaCi a = subs ($ his-> es_code,0,1);
i ($p imaCi a == '2' && $ his-> es_cseq_me hod == 'BYE'){/*Richies a di BYE anda a a buon
ine*/
e u n $ his-> es_code;
}
swi ch($p imaCi a){
case '2': //ack in ia o p eceden emen e: il dialogo è s abili o->La socke de e imane e a i a
pe ges i e la chiusu a del dialogo
b eak;
case '1': // ispos a p o iso ia: de e o na e a legge e pe s a e in a esa di una ispos a
de ini i a
$i = 0;
while (subs ($ his-> es_code,0,1) == '1' && $i < 4){
//p in ("<BR>".$i."<BR>");
//@ lush();
$ his-> eadResponse();
$i++;
}
/*i ($i == 4){
$ his->messageE o = "Nessuna ispos a de ini i a pe enu a dopo ".$i."
en a i i";
}else{
//Rice u a una ispos a non p o iso ia p ima dei 4 en a i i
//L'ack a in ia o al des ina a io
e u n $ his-> es_code;
}*/
b eak;
case '4': //4xx clien e o
i ($ his-> es_code == '407'){
$ his->cseq++;
$ his->au h();
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his-> eadResponse();
94
}
i ($ his-> es_code == '401'){
$ his->cseq++;
$ his->au hWWW();
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his-> eadResponse();
}
b eak;
de aul : $ his->messageFlow = "Nessun da o da legge e n";
}//Chiusu a swi ch
$ his->ex a_heade s = a ay();
$ his->cseq++;
e u n $ his-> es_code;
}
/**
* Lis en o eques
*
* @ odo This needs o be imp o ed
*/
public unc ion lis en()
{
$ his-> eadResponse();
swi ch($ his-> eq_me hod){
case 'BYE': //Richies a di chiusu a
$ his-> eply("200","OK");
//$ his-> eq_me hod .= " Response in ia a dopo la icezione di ichies a di
chiusu a n";
//$ his->socke ->closeSocke ();
b eak;
case 'INVITE':
/*Impos o il des ina a io al p imo eques _ ia o a o*/
o ($i = 0; $i < 1; $i++){
//$ his->socke ->se Reques Hos ($ his-> eques _ ia[$i]); //P imo ia che incon a
/*Es aggo l'ip e l'e en uale po a dal p imo ia*/
$des = $ his-> eques _ ia[$i];
$semicolon = s pos($des , ";");
i ($semicolon !== alse){
$des = subs ($des , 0, $semicolon);
}
$ emp = explode(" ", $des ); //Con iene l'ip e l'e en uale po a
$ emp2 = explode(":", $ emp[1]);
$ his->socke ->se Reques Hos ($ emp2[0]);//Se o l'ip
i (isse ($ emp2[1])){//Se c'è anche la po a
$ his->se Reques Po ($ emp2[1]);
}
}
$ his-> eplyWi hBody("100","T ying");
$ his-> eplyWi hBody("200","OK");
b eak;
95
case 'ACK':
/*Impos o come nuo o des ina a io il Con ac */
$u l = s _ eplace("sip:","sip://",$ his-> eq_con ac );
i (!$u l = @pa se_u l($u l))
{
h ow new PhpSIPExcep ion("Failed o pa se URI-Con ac .");
}
$ his->socke ->se Reques Hos ($u l['hos ']);
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
b eak;
// de aul : echo 'No eques ecei ed';
}
e u n $ his-> eq_me hod;
}
/**
* Reads esponse
*/
p i a e unc ion eadResponse()
{
$ his-> esponse = $ his->socke -> ec Da a();
$ his->messageFlow .= "Rispos a ice u a n".$ his-> esponse." n";
// Response
$ esul = a ay();
i (p eg_ma ch('/^SIP /2 .0 ([0-9]{3})/',$ his-> esponse,$ esul ))
{
$ his-> es_code = im($ esul [1]);
$ es_class = subs ($ his-> es_code,0,1);
i ($ es_class == '1' || $ es_class == '2')
{
$ his->dialog = ue;
}
$ his->pa seResponse();
}
// Reques
else
{
$ his->pa seReques ();
}
}
/**
* Pa se Response
*/
p i a e unc ion pa seResponse()
96
{
// il p oxy è impos a o, $ his-> eques Hos = p oxy
// il p oxy non è impos a o e non c'è una RR, allo a $ his-> eques Hos = Con ac del messaggio
di ispos a
// il p oxy non è impos a o e c'è una RR, allo a $ his-> eques Hos = p ima eco d- ou e
impos a a.
// To ag
$ esul = a ay();
i (p eg_ma ch('/^To: .*; ag=(.*)$/im',$ his-> esponse,$ esul ))
{
$ his-> o_ ag = im($ esul [1]);
}
// Rou e
$ esul = a ay();
i (p eg_ma ch_all('/^Reco d-Rou e: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ou e)
{
i (!in_a ay( im($ ou e),$ his-> ou es))
{
$ his-> ou es[] = im($ ou e);
}
}
}
// Reques ia
$ esul = a ay();
$ his-> eques _ ia = a ay();
i (p eg_ma ch_all('/^Via: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ia)
{
$ his-> eques _ ia[] = im($ ia);
}
}
// Response con ac
$ esul = a ay();
i (p eg_ma ch('/^Con ac :.*<(.*)>/im',$ his-> esponse,$ esul ))
{
$ his-> es_con ac = im($ esul [1]);
$semicolon = s pos($ his-> es_con ac ,";");
i ($semicolon !== alse)
{
$ his-> es_con ac = subs ($ his-> es_con ac ,0,$semicolon);
}
}
// Response CSeq me hod
97
$ esul = a ay();
i (p eg_ma ch('/^CSeq: [0-9]+ (.*)$/im',$ his-> esponse,$ esul ))
{
$ his-> es_cseq_me hod = im($ esul [1]);
}
i ($ his-> es_cseq_me hod == 'INVITE' && in_a ay(subs ($ his-
> es_code,0,1),a ay('2','3','4','5','6')))
{
//Copy body in NRDL_ ecei ed_I. d .
$ om='<?xml';
$ o='RDF>';
$pos=$ his-> indChain($ his-> esponse,$ om);
$pos2=$ his-> indChain($ his-> esponse,$ o);
$pa h='./NRDL_ ecei ed_I. d ';
$ d= open($pa h,'w');
o ($i=$pos;$i<$pos2+s len($ o);$i++) w i e($ d,$ his-> esponse[$i]);
close($ d);
}
/*Con olli pe decide e do e in ia e i messaggi*/
i ($ his->p oxy){//Se è s a o speci ica o un p oxy, i messaggi anno in ia i lì
$ his->socke ->se Reques Hos ($ his->p oxy);
}elsei (!$ his->p oxy && coun ($ his-> ou es) == 0){//Se non c'è un p oxy e neanche una RR
//I da i anno in ia i all'indi izzo speci ica o nel Con ac
$p imaCi a = subs ($ his-> es_code,0,1);
i ($p imaCi a == '2'){
//Alla icezione di una ispos a de ini i a posi i a l'ack iene in ia o all'end-poin , negli al i
casi al se e
//In ques o modo le comunicazioni d'o a in poi sa anno a i 2 in e locu o i
$u l = s _ eplace("sip:","sip://",$ his-> es_con ac );
i (!$u l = @pa se_u l($u l))
{
h ow new PhpSIPExcep ion("Failed o pa se URI.");
}
i (isse ($u l['hos '])){
$ his->socke ->se Reques Hos ($u l['hos ']);
}else{
echo "No ip n";
}
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
}
}elsei (!$ his->p oxy && coun ($ his-> ou es) != 0 ){//Se non c'è un p oxy, ma c'è almeno una
RR
98

o ($i = 0; $i < 1; $i ++){
$index1 = s pos($ his-> ou es[$i],":");
$index1 += 1;
$index2 = s pos($ his-> ou es[$i],";");
$len = $index2 - $index1;
$ip = subs ($ his-> ou es[$i], $index1, $len);
}
$ his->socke ->se Reques Hos ($ip);
}
// ACK 2XX-6XX - only in i es - RFC3261 17.1.2.1
i ($ his-> es_cseq_me hod == 'UPDATE' && subs ($ his-> es_code,0,1) == '2')
{
$ his-> eadResponse();
e u n;
}
i ($ his-> es_cseq_me hod == 'INVITE' && in_a ay(subs ($ his-
> es_code,0,1),a ay('2','3','4','5','6')))
{
$ his->socke ->se Reques Hos ('10.10.10.67');
$ his->ack();
}
i ($ his-> es_cseq_me hod == 'INVITE' && subs ($ his-> es_code,0,3)=='183')
{
$ his->se Me hod('UPDATE');
$da a2 = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a2);
$ his->messageFlow .= "In io upda e n".$da a2." n";
$ his-> eadResponse();
e u n;
}
i ($ his-> es_cseq_me hod == 'BYE' && in_a ay(subs ($ his-
> es_code,0,1),a ay('2','3','4','5','6'))){/*Ricezione di una esponse ela i a ad una ichies a di BYE
in ia a dal Clien */
$ his->messageFlow .= "Rice u o esponse a segui o dell'in io del BYE n";
}
e u n $ his-> es_code;
}
/******* indChain unc ion*******/
//Re u ns posi ion lChain in Chain
public unc ion indChain($Chain,$lChain){
$ esul =-1;
o ($i=0 ; $i<s len($Chain); $i++){
i ($ esul !=0 && $ esul !=-1)b eak;
i ($Chain[$i] == $lChain[0]){
$ esul =$i;
o ($j=0 ; $j<s len($lChain) ; $j++){
99
i ($Chain[$i+$j] != $lChain[$j]){
$ esul =0;
b eak;
}
else i ($j==s len($lChain)-1)b eak;
}
}
}
e u n $ esul ;
}
/*******************************/
/**
* Pa se Reques
*/
p i a e unc ion pa seReques ()
{
$ emp = explode(" n",$ his-> esponse);
$ emp = explode(" ",$ emp[0]);
$ his-> eq_me hod = im($ emp[0]);
// Rou e
$ esul = a ay();
i (p eg_ma ch_all('/^Reco d-Rou e: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ou e)
{
i (!in_a ay( im($ ou e),$ his-> ou es))
{
$ his-> ou es[] = im($ ou e);
}
}
}
// Reques ia
$ esul = a ay();
$ his-> eques _ ia = a ay();
i (p eg_ma ch_all('/^Via: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ia)
{
$ his-> eques _ ia[] = im($ ia);
}
}
// Me hod con ac
$ esul = a ay();
i (p eg_ma ch('/^Con ac : <(.*)>/im',$ his-> esponse,$ esul ))
{
$ his-> eq_con ac = im($ esul [1]);
100
$semicolon = s pos($ his-> es_con ac ,";");
i ($semicolon !== alse)
{
$ his-> es_con ac = subs ($ his-> es_con ac ,0,$semicolon);
}
}
// Response CSeq me hod
i (p eg_ma ch('/^CSeq: [0-9]+ (.*)$/im',$ his-> esponse,$ esul ))
{
$ his-> eq_cseq_me hod = im($ esul [1]);
}
// Response CSeq numbe
i (p eg_ma ch('/^CSeq: ([0-9]+) .*$/im',$ his-> esponse,$ esul ))
{
$ his-> eq_cseq_numbe = im($ esul [1]);
}
/*Rice ca del To, F om, Call-ID*/
i (p eg_ma ch('/^To: <(.*)>/im',$ his-> esponse,$ esul ))
{
$ his-> o ='<'. im($ esul [1]).'>';
}
/*Es aggo lo use name del des ina a io*/
$m = a ay();
i (!p eg_ma ch('/sip:(.*)@/i',$ his-> o,$m))
{
h ow new PhpSIPExcep ion('Failed o pa se F om use name.');
}
$ his-> om_use
= $m[1];
i (p eg_ma ch('/^F om: <(.*)>/im',$ his-> esponse,$ esul ))
{
$ his-> om ='<'. im($ esul [1].'>');
}
/*Es aggo il ag del mi en e*/
i (p eg_ma ch('/^F om: .*; ag=(.*)$/im', $ his-> esponse, $ esul )){
$ his-> o_ ag = im($ esul [1]);
}
i (p eg_ma ch('/^Call-ID: (.*)$/im', $ his-> esponse, $ esul )){
$ his->call_id = im($ esul [1]);
}
}
/**
* Send Response
*
* @pa am in $code Response code
* @pa am s ing $ ex Response ex
*/
101
public unc ion eply($code,$ ex )
{
$ = 'SIP/2.0 '.$code.' '.$ ex ." n";
// Via
o each ($ his-> eques _ ia as $ ia)
{
$ .= 'Via: '.$ ia." n";
}
// F om
$ .= 'F om: '.$ his-> om.'; ag='.$ his-> o_ ag." n";
// To
$ .= 'To: '.$ his-> o.'; ag='.$ his-> om_ ag." n";
// Call-ID
$ .= 'Call-ID: '.$ his->call_id." n";
//CSeq
$ .= 'CSeq: '.$ his-> eq_cseq_numbe .' '.$ his-> eq_cseq_me hod." n";
// Max-Fo wa ds
$ .= 'Max-Fo wa ds: 70'." n";
// Use -Agen
$ .= 'Use -Agen : '.$ his->use _agen ." n";
// Con en -Leng h
$ .= 'Con en -Leng h: 0'." n";
$ .= " n";
$ his->socke ->sendDa a($ );
$ his->messageFlow .= "In io esponse. n".$ ." n";
}
public unc ion eplyWi hBody($code,$ ex )
{
/*Ape u a del NRDL da me e e nella esponse*/
$pa h = "/ a /www/h ml/php-sip/ es /NRDL_200OK. d ";
$ d = open($pa h," ");
$n dl = ead($ d, ilesize($pa h));
/*Ges ione callID dinamico*/
$ce ca = "diag12345";
$sos i uisci = $ his->hashChiaman e."".$ his->hashChiama o;
$n dl = s _ eplace($ce ca, $sos i uisci, $n dl);
/**/
$ = 'SIP/2.0 '.$code.' '.$ ex ." n";
// Via
o each ($ his-> eques _ ia as $ ia)
{
$ .= 'Via: '.$ ia." n";
}
// F om
$ .= 'F om: '.$ his-> om.'; ag='.$ his-> o_ ag." n";
// To
i ($code == '200'){
i (!$ his-> om_ ag){
$ his->se F omTag();
$ his-> om_ ag = "1".$ his-> om_ ag;
}
102
// ealm
$ esul = a ay();
i (!p eg_ma ch('/^P oxy-Au hen ica e: .* ealm="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind ealm in p oxy-au h");
}
$ ealm = $ esul [1];
// nonce
$ esul = a ay();
i (!p eg_ma ch('/^P oxy-Au hen ica e: .* nonce="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind nonce in p oxy-au h");
}
$nonce = $ esul [1];
$ha1 = md5($ his->use name.':'.$ ealm.':'.$ his->passwo d);
$ha2 = md5($ his->me hod.':'.$ his->u i);
$ es = md5($ha1.':'.$nonce.':'.$ha2);
$ his->au h = 'P oxy-Au ho iza ion: Diges use name="'.$ his->use name.'", ealm="'.$ ealm.'",
nonce="'.$nonce.'", u i="'.$ his->u i.'", esponse="'.$ es.'", algo i hm=MD5';
}
/**
* Calcula es WWW au ho iza ion esponse
*
*/
p i a e unc ion au hWWW()
{
i (!$ his->use name)
{
h ow new PhpSIPExcep ion("Missing au h use name");
}
i (!$ his->passwo d)
{
h ow new PhpSIPExcep ion("Missing au h passwo d");
}
$qop_p esen = alse;
i (s pos($ his-> esponse,'qop=') !== alse)
{
$qop_p esen = ue;
// we can only do qop="au h"
i (s pos($ his-> esponse,'qop="au h"') === alse)
{
h ow new PhpSIPExcep ion('Only qop="au h" diges au hen ica ion suppo ed.');
}
109

}
// ealm
$ esul = a ay();
i (!p eg_ma ch('/^WWW-Au hen ica e: .* ealm="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind ealm in www-au h");
}
$ ealm = $ esul [1];
// nonce
$ esul = a ay();
i (!p eg_ma ch('/^WWW-Au hen ica e: .* nonce="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind nonce in www-au h");
}
$nonce = $ esul [1];
$ha1 = md5($ his->use name.':'.$ ealm.':'.$ his->passwo d);
$ha2 = md5($ his->me hod.':'.$ his->u i);
i ($qop_p esen )
{
$cnonce = md5( ime());
$ es = md5($ha1.':'.$nonce.':00000001:'.$cnonce.':au h:'.$ha2);
}
else
{
$ es = md5($ha1.':'.$nonce.':'.$ha2);
}
$ his->au h = 'Au ho iza ion: Diges use name="'.$ his->use name.'", ealm="'.$ ealm.'",
nonce="'.$nonce.'", u i="'.$ his->u i.'", esponse="'.$ es.'", algo i hm=MD5';
i ($qop_p esen )
{
$ his->au h.= ', qop="au h", nc="00000001", cnonce="'.$cnonce.'"';
}
}
/**
* Rese s callid, o/ om ags e c.
*
*/
public unc ion newCall()
{
$ his->cseq = 20;
$ his->call_id = null;
$ his-> o_ ag = null;;
$ his-> om_ ag = null;;
110
/**
* Body
*/
$ his->body = null;
/**
* Recei ed Response
*/
$ his-> esponse = null;
$ his-> es_code = null;
$ his-> es_con ac = null;
$ his-> es_cseq_me hod = null;
$ his-> es_cseq_numbe = null;
/**
* Recei ed Reques
*/
$ his-> eq_me hod = null;
$ his-> eq_cseq_me hod = null;
$ his-> eq_cseq_numbe = null;
$ his-> eq_con ac = null;
$ his-> ou es = a ay();
$ his-> eques _ ia = a ay();
}
/*Tiene accia di u i i messaggi ice u i e in ia i*/
public unc ion ge MessageFlow(){
e u n $ his->messageFlow;
}
public unc ion se NewSocke ($newSock){
$ his->socke = $newSock;
}
}
?>
D. 10.10.10.67 UA sc ip s.
D.I es somoza_s anda d.
clien 2.php
<?php
/*Si me e in ascol o di e en uali ichies e*/
equi e_once("Clien SIP.class.php");
equi e_once("Socke SIP.class.php");
da e_de aul _ imezone_se ("Eu ope/Mad id");
y{
$sockLis en = new Socke (5060);// 50602
111
$cln Lis en = new PhpSIP($sockLis en);
}ca ch(Excep ion $e) {
echo "Excep ion: n";
echo $e;
}
$cln Lis en-> egis e ();
o (;;){
y{
$ esul = $cln Lis en->lis en();
}ca ch(Excep ion $e) {
echo "Excep ion in Clien 2: n";
echo $e;
}
i ($ esul == 'ACK'){
$ esul = 'none';
$handshake = $cln Lis en->ge MessageFlow();
//echo "Rice u o ACK. n";
//$handshake = explode("Rispos a ice u a", $cln Lis en->ge MessageFlow());
/*$pid= pcn l_ o k();
i ($pid==-1){p in "E o e nella o k n";}
i ($pid){//p ocesso pad e
}
else{*/
//echo $sockLis en->ge Reques Hos ()." ".$sockLis en->ge Reques Po ();
//qui cui and à pcn l e la po a sa à conco da a dall'NRDL
//$se e = a ay( 'add ess' => '137.204.107.67', 'po ' => 7001 );//$sockLis en-
>ge Reques Hos (), 'po ' => 7001 );
$se e = a ay( 'add ess' => '10.10.10.67', 'po ' => 7001 );//$sockLis en->ge Reques Hos (),
'po ' => 7001 );
$sock = socke _c ea e(AF_INET, SOCK_STREAM, SOL_TCP);
socke _se _op ion($sock, SOL_SOCKET, SO_REUSEADDR,1);
socke _bind($sock,$se e ['add ess'],$se e ['po ']);
socke _lis en($sock);
$clien =socke _accep ($sock);
socke _close($sock);
$ m=socke _ ead($clien ,1024);
$mm="Mig azione:<b />";
$mm.=da e('h:i:s')." - Rice u o se izio da as e i e: ".$ m."<b />";
$s =shell_exec("sudo VBoxManage lis ms");
$ ms=explode('"', $s );
$ o =coun ($ ms);
o ($i=0;$i<$ o ;$i+=2){
unse ($ ms[$i]);
}
se _ ime_limi (200);
i (in_a ay($ m,$ ms)){
$mm.=da e('h:i:s')." - Se izio p esen e, in io del ile di log: ";
//echo "/ oo /Vi ualBox VMs/".$ m."/".$ m.".log";
112
$da a= ile_ge _con en s("/ oo /Vi ualBox VMs/".$ m."/".$ m.".log");
$mm.=$da a."<b />";
socke _w i e($clien ,$da a);
$ c =socke _ ead($clien ,10);
$mm.=da e('h:i:s')." - Rispos a: ".$ c ."<b />";
i ($ c =="UPDATE"){
i ( !( $ ile = @ open( "/ oo /Vi ualBox VMs/".$ m."/".$ m.". di", "w" ) ) ) {
echo "File open e o . n";
exi ;
}
i ( !( $ c = @socke _ ead( $clien , 1024, PHP_BINARY_READ) ) ) {
echo "E o e nella icezione";
exi ;
}
$i=1;
do {
i ( !( @ w i e( $ ile, $ c , 1024 ) ) ) {
echo $i." E o e nella sc i u a";
exi ;
}
i ( !( $ c = @socke _ ead( $clien , 1024, PHP_BINARY_READ) ) ) {
echo "E o e nella icezione";
exi ;
}
i (s pos($ c ,"ENDFILEVDI")!== alse){
$w i=subs ($ c ,0,s pos($ c ,"ENDFILEVDI"));
i ( !( @ w i e( $ ile, $w i, s len($w i)) ) ) {
//echo $i." Rice u o ENDFILE ".s len($w i);
b eak;
}
b eak;
}
$i++;
//echo " ec:".$ c ."<b />";
} while( /*$ c != "ENDFILE"*/ ue );
close($ ile);
$mm.=da e('h:i:s')." - Rice u o VDI<b />";
//echo da e('h:i:s')." - Rice u o VM<b />";
$log=subs ($ c ,s pos($ c ,"ENDFILEVDI"));
$log=s _ eplace("ENDFILEVDI","",$log);
i (s pos($log,"ENDLOG")=== alse){
$ c = socke _ ead($clien ,1024);
$log.=$ c ;
}
$log=s _ eplace("ENDLOG","",$log);
i ( !( $ ile = @ open( "/ oo /Vi ualBox VMs/".$ m."/".$ m.".log", "w" ) ) ) {
echo "File open e o . n";
exi ;
}
i ( !( @ w i e( $ ile, $log, s len($log) ) ) ) {
113
echo "E o e nella sc i u a";
exi ;
}
close($ ile);
$mm.=da e('h:i:s')." - Rice u o LOG<b />";
}
$mm.=da e('h:i:s')." - Esecuzione del comando di p edisposizione della macchina
i uale pe acce a e il as e imen o sulla po a 1234.<b />";
}else{
$mm.=da e('h:i:s')." - Se izio non p esen e<b />";
socke _w i e($clien ,"NOTFOUND");
$ca a =socke _ ead($clien ,4096);
$mm.=da e('h:i:s')." - Rice u e ca a e is iche macchina i uale<b />";
$ ighe=explode(" n",$ca a );
o ($i=0;$i<coun ($ ighe);$i++){
i ($ ighe[$i]!=""){ $mm.="     ".
$ ighe[$i]."<b />";}
}
shell_exec('sudo VBoxManage c ea e m --name "'.$ m.'" --os ype Ubun u
-- egis e ');
sys em('sudo VBoxManage modi y m "'.$ m.'" --memo y 512 -- am 12 --usb on');
sys em('sudo chmod 777 -R "/ oo /Vi ualBox VMs/'.$ m.'"');
i ( !($ ile = @ open("/ oo /Vi ualBox VMs/".$ m."/".$ m.". di", "w" ) ) ) {
echo "File open e o . n";
exi ;
}
i ( !( $ c = @socke _ ead( $clien , 1024, PHP_BINARY_READ) ) ) {
echo "E o e nella icezione";
exi ;
}
$i=1;
do {
i ( !( @ w i e( $ ile, $ c , 1024 ) ) ) {
echo $i." E o e nella sc i u a";
exi ;
}
i ( !( $ c = @socke _ ead( $clien , 1024, PHP_BINARY_READ) ) ) {
echo "E o e nella icezione";
exi ;
}
i (s pos($ c ,"ENDFILEVDI")!== alse){
/*echo $i." Rice u o ENDFILE ".$ c ;
exi ();*/
$w i=subs ($ c ,0,s pos($ c ,"ENDFILEVDI"));
i ( !( @ w i e( $ ile, $w i, s len($w i)) ) ) {
b eak;
}
b eak;
}
$i++;
//echo " ec:".$ c ."<b />";
114

} while( /*$ c != "ENDFILE"*/ ue );
close($ ile);
$mm.=da e('h:i:s')." - Rice u o VDI<b />";
$log=subs ($ c ,s pos($ c ,"ENDFILEVDI"));
$log=s _ eplace("ENDFILEVDI","",$log);
i (s pos($log,"ENDLOG")=== alse){
$ c = socke _ ead($clien ,1024);
$log.=$ c ;
}
$log=s _ eplace("ENDLOG","",$log);
i ( !( $ ile = @ open( "/ oo /Vi ualBox VMs/".$ m."/".$ m.".log", "w" ) ) ) {
echo "File open e o . n";
exi ;
}
i ( !( @ w i e( $ ile, $log, s len($log) ) ) ) {
echo "E o e nella sc i u a";
exi ;
}
close($ ile);
$mm.=da e('h:i:s')." - Rice u o LOG<b />";
sys em('sudo VBoxManage s o agec l '.$ m.' --name "Con olle SATA"
--sa apo coun 1 --add sa a');
sys em('sudo VBoxManage s o agea ach '.$ m.' --s o agec l "Con olle SATA"
--po 0 --de ice 0 -- ype hdd --medium "/ oo /Vi ualBox VMs/'.$ m.'/'.$ m.'. di"');
}
sys em("sudo VBoxManage modi y m ".$ m." -- elepo e on -- elepo e po 1234");
$mm.=da e('h:i:s')." - In io della po a su cui esegui e la mig azione<b />";
socke _w i e($clien ,"1234");
$mm.=da e('h:i:s')." - Esecuzione della mig azione<b />";
//sys em("sudo VBoxManage s a m ".$ m." - ype headless");
shell_exec("sudo VBoxManage s a m ".$ m." - ype headless");
socke _w i e($clien ,"END");
$mm.=da e('h:i:s')." - Mig azione e e ua a, END in ia o<b />";
socke _close($clien );
//} pa en esi del o k
}elsei ($ esul == 'BYE'){
//$disconnessione = explode("Rispos a ice u a", $cln Lis en->ge MessageFlow());
$disconnessione = $cln Lis en->ge MessageFlow();
//$disconnessione = s _ eplace("In io esponse.", "Rispos a ice u aIn io esponse.",
$disconnessione);
//$disconnessione = explode("Rispos a ice u a", $disconnessione);
//echo "Rice u a ichies a di chiusu a. Flusso messaggi: n <p e>".$cln Lis en-
>ge MessageFlow()."</p e>";
b eak;
}
}
$sockLis en->closeSocke ();
?>
<h ml>
<head>
115
< i le>Clien 2 - SIP</ i le>
<sc ip languaje="ja asc ip ">
unc ion displayElem(id){
a x=documen .ge Elemen ById(id);
i (x.s yle.display=='inline') x.s yle.display='none';
i (x.s yle.display=='none') x.s yle.display='block';
else i (x.s yle.display='block') x.s yle.display='none';
}
unc ion indChain(Chain,lChain){
a esul =0;
o ( a i=0 ; i<Chain.leng h ; i++){
i (Chain[i] == lChain[0]){
o ( a j=0 ; j<lChain.leng h ; j++){
i (Chain[i+j] != lChain[j])b eak;
else i (j==lChain.leng h-1) esul =1;
}
}
}
e u n esul ;
}
</sc ip >
</head>
<body s yle="backg ound-colo :#494949; on - amily:a ial, hel e ica, sans-se i ;">
<di s yle="wid h:930px; padding:15px; backg ound-colo :#FFFFFF; ma gin:0 au o;">
< able cellspacing="0" cellpadding="0" wid h="930">
< >
< d wid h="190" align="middle">
<img s c="images/logo.gi " />
</ d>
< d s yle="backg ound-colo :#18964A" align="le ">
<img s c="images/ es a a.gi " />
</ d>
</ >
</ able>
<p>Use agen : <s ong>use 2</s ong><p>
<p>Ques o use agen è in ascol o pe ice e e se izi</p>
<di id="hs" s yle="colo :#F10909">
<?php
$handshake = s _ eplace("In io esponse.", "Rispos a ice u a", $handshake);
$e_handshake = explode("Rispos a ice u a", $handshake);
$con =1;
o ($i=0; $i<coun ($e_handshake); $i++){
$e_handshake[$i] = s _ eplace("<", "<", $e_handshake[$i]);
$ in=s pos($e_handshake[$i], "Via");
$ in1=s pos($e_handshake[$i], "Reco d-Rou e");
i ($ in1 != FALSE)$ in=$ in1;
$head=subs ($e_handshake[$i],0,$ in);
$ ail=subs ($e_handshake[$i],$ in);
116
i ((s pos($ ail, "F om: <sip:[email p o ec ed]")!= alse)&&(s pos($ ail,
"OpenSIPS")=== alse)){
i ($con ==1){
?>
<di id="head11" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main11')"><?php
echo "<p e>".$head."</p e>"; ?></di >
<ul id="main11" class="ni el2">
<di id=" ail11" s yle="colo :#FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =2;
}
else i ($con ==2){
?>
<di id="head22" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main22')"><?php echo "<p e>".$head."</p e>"; ?></di >
<ul id="main22" class="ni el2">
<di id=" ail22" s yle="colo :#FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =3;
}
else i ($con ==3){
?>
<di id="head33" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main33')"><?php echo "<p e>".$head."</p e>"; ?></di >
<ul id="main33" class="ni el2">
<di id=" ail33" s yle="colo :#FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =4;
}
else i ($con ==4){
?>
<di id="head44" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main44')"><?php echo "<p e>".$head."</p e>"; ?></di >
<ul id="main44" class="ni el2">
<di id=" ail44" s yle="colo :#FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =5;
}
else i ($con ==5){
?>
<di id="head55" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main55')"><?php echo "<p e>".$head."</p e>";?></di >
<ul id="main55" class="ni el2">
<di id=" ail55" s yle="colo :#FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
117
</ul>
<?php
$con =6;
}
else i ($con ==6){
?>
<di id="head66" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main66')"><?php echo "<p e>".$head."</p e>";?></di >
<ul id="main66" class="ni el2">
<di id=" ail66" s yle="colo :#FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =7;
}
else i ($con ==7){
?>
<di id="head77" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main77')"><?php echo "<p e>".$head."</p e>";?></di >
<ul id="main77" class="ni el2">
<di id=" ail77" s yle="colo :FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =8;
}
else i ($con ==8){
?>
<di id="head88" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main88')"><?php echo "<p e>".$head."</p e>";?></di >
<ul id="main88" class="ni el2">
<di id=" ail88" s yle="colo :FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =9;
}
else i ($con ==9){
?>
<di id="head99" s yle="colo :#FF9000"
onclick="ja asc ip :displayElem('main99')"><?php echo "<p e>".$head."</p e>";?></di >
<ul id="main99" class="ni el2">
<di id=" ail99" s yle="colo :FF9000"><?php echo "<p e>".
$ ail."</p e>"; ?></di >
</ul>
<?php
$con =10;
}
}
else{
i ($con ==1){
?>
118
/**
* Con en ype
*/
p i a e $con en _ ype;
/**
* Body
*/
p i a e $body;
/**
* Recei ed Response
*/
p i a e $ esponse; // whole esponse body
p i a e $ es_code;
p i a e $ es_con ac ;
p i a e $ es_cseq_me hod;
p i a e $ es_cseq_numbe ;
/**
* Recei ed Reques
*/
p i a e $ eq_me hod;
p i a e $ eq_cseq_me hod;
p i a e $ eq_cseq_numbe ;
p i a e $ eq_con ac ;
/**
* Au hen ica ion
*/
p i a e $au h;
/**
* Rou es
*/
p i a e $ ou es = a ay();
/**
* Reques ias
*/
p i a e $ eques _ ia = a ay();
/**
* Addi ional heade s
*/
p i a e $ex a_heade s = a ay();
/**
* Ges ione callID dinamico
*/
p i a e $use Chiaman e;
p i a e $hashChiaman e;
p i a e $use Chiama o;
125

p i a e $hashChiama o;
/**
* Cons uc o
*
* @pa am $socke _is
*/
public unc ion __cons uc ($socke _is )
{
$ his->socke = $socke _is ;
}
/**
* Des uc o
*/
public unc ion __des uc ()
{
//$ his->socke ->closeSocke ();
}
/**
* Se s debuggin ON/OFF
*
* @pa am bool $s a us
*/
public unc ion se Debug($s a us = alse)
{
$ his->debug = $s a us;
}
/**
* Adds adi ional heade
*
* @pa am s ing $heade
*/
public unc ion addHeade ($heade )
{
$ his->ex a_heade s[] = $heade ;
}
/**
* Se s F om heade
*
* @pa am s ing $ om
*/
public unc ion se F om($ om)
{
/*Ges ione callID dinamico*/
$ his->use Chiaman e = explode(":", $ om);
$ his->hashChiaman e = subs (md5($ his->use Chiaman e[1]), 0, 6);
$ d = open("/ mp/hashChiaman e. x ", "w");
w i e($ d, $ his->hashChiaman e);
close($ d);
/**/
126
i (p eg_ma ch('/<.*>$/',$ om))
{
$ his-> om = $ om;
}
else
{
$ his-> om = '<'.$ om.'>';
}
$m = a ay();
i (!p eg_ma ch('/sip:(.*)@/i',$ his-> om,$m))
{
h ow new PhpSIPExcep ion('Failed o pa se F om use name.');
}
$ his-> om_use = $m[1];
}
/**
* Se s me hod
*
* @pa am s ing $me hod
*/
public unc ion se Me hod($me hod)
{
i (!in_a ay($me hod,$ his->allowed_me hods))
{
h ow new PhpSIPExcep ion('In alid me hod.');
}
$ his->me hod = $me hod;
i ($me hod == 'INVITE')
{
/*Open ile NRDL_INVITE. d */
$pa h = "/ a /www/h ml/php-sip/ es /NRDL_INVITE. d ";
$ d = open($pa h," ");
$n dl = ead($ d, ilesize($pa h));
/*$body = " =0 n";
$body.= "o=click2dial 0 0 IN IP4 ".$ his->socke ->ge Ip()." n";
$body.= "s=click2dial call n";
$body.= "c=IN IP4 ".$ his->socke ->ge Ip()." n";
$body.= " =0 0 n";
$body.= "m=audio 8000 RTP/AVP 0 8 18 3 4 97 98 n";
$body.= "a= pmap:0 PCMU/8000 n";
$body.= "a= pmap:18 G729/8000 n";
$body.= "a= pmap:97 ilbc/8000 n";
$body.= "a= pmap:98 speex/8000 n";*/
/*Ges ione callID dinamico*/
$ce ca = "diag12345";
127
$sos i uisci = $ his->hashChiaman e."".$ his->hashChiama o;
$n dl = s _ eplace($ce ca, $sos i uisci, $n dl);
/**/
/*Aggiun a del con enu o n dl al body*/
$body = $n dl." n";
$ his->body = $body;
$ his->se Con en Type(null);
}
i ($me hod == 'REFER')
{
$ his->se Body('');
}
i ($me hod == 'CANCEL')
{
$ his->se Body('');
$ his->se Con en Type(null);
}
i ($me hod == 'MESSAGE')
{
$ his->se Con en Type(null);
}
i ($me hod == 'REGISTER')
{
$ his->se Body('');
}
}
/**
* Se s SIP P oxy
*
* @pa am $p oxy
*/
public unc ion se P oxy($p oxy)
{
$ his->p oxy = $p oxy;
}
/**
* Se s eques URI
*
* @pa am s ing $u i
*/
public unc ion se U i($u i)
{
/*Ges ione callID dinamico*/
$ his->use Chiama o = explode(":", $u i);
$ his->hashChiama o = subs (md5($ his->use Chiama o[1]), 0, 6);
128
$ d = open("/ mp/hashChiama o. x ", "w");
w i e($ d, $ his->hashChiama o);
close($ d);
/**/
i (s pos($u i,'sip:') === alse)
{
h ow new PhpSIPExcep ion("Only sip: URI suppo ed.");
}
$ his->u i = $u i;
$ his-> o = '<'.$u i.'>';
i ($ his->p oxy)
{
i (s pos($ his->p oxy,':'))
{
$ emp = explode(":",$ his->p oxy);
$ his->socke ->se Reques Hos ($ emp[0]);
$ his->socke ->se Reques Po ($ emp[1]);
}
else
{
$ his->socke ->se Reques Hos ($ his->p oxy);
}
}
else
{
$u l = s _ eplace("sip:","sip://",$u i);
i (!$u l = @pa se_u l($u l))
{
h ow new PhpSIPExcep ion("Failed o pa se URI.");
}
$ his->socke ->se Reques Hos ($u l['hos ']);
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
}
}
/**
* Se s use name
*
* @pa am s ing $use name
*/
public unc ion se Use name($use name)
{
$ his->use name = $use name;
}
129
/**
* Se s Use Agen
*
* @pa am s ing $use _agen
*/
public unc ion se Use Agen ($use _agen )
{
$ his->use _agen = $use _agen ;
}
/**
* Se s passwo d
*
* @pa am s ing $passwo d
*/
public unc ion se Passwo d($passwo d)
{
$ his->passwo d = $passwo d;
}
/**
* Sends SIP eques
*
* @ e u n s ing Reply
*/
public unc ion send()
{
i (!$ his-> om)
{
h ow new PhpSIPExcep ion('Missing F om.');
}
i (!$ his->me hod)
{
h ow new PhpSIPExcep ion('Missing Me hod.');
}
i (!$ his->u i)
{
h ow new PhpSIPExcep ion('Missing URI.');
}
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his->messageFlow .= "In io ihies a n".$da a." n";
$ his-> eadResponse();
/*To na o dalla le u a della p ima ispos a, de e gua da e qual è il codice o na o: $ his-
> es_code*/
//Con ollo la p ima ci a
$p imaCi a = subs ($ his-> es_code,0,1);
130

i ($p imaCi a == '2' && $ his-> es_cseq_me hod == 'BYE'){/*Richies a di BYE anda a a buon
ine*/
e u n $ his-> es_code;
}
swi ch($p imaCi a){
case '2': //ack in ia o p eceden emen e: il dialogo è s abili o->La socke de e imane e a i a
pe ges i e la chiusu a del dialogo
b eak;
case '1': // ispos a p o iso ia: de e o na e a legge e pe s a e in a esa di una ispos a
de ini i a
$i = 0;
while (subs ($ his-> es_code,0,1) == '1' && $i < 4){
//p in ("<BR>".$i."<BR>");
//@ lush();
$ his-> eadResponse();
$i++;
}
/*i ($i == 4){
$ his->messageE o = "Nessuna ispos a de ini i a pe enu a dopo ".$i."
en a i i";
}else{
//Rice u a una ispos a non p o iso ia p ima dei 4 en a i i
//L'ack a in ia o al des ina a io
e u n $ his-> es_code;
}*/
b eak;
case '4': //4xx clien e o
i ($ his-> es_code == '407'){
$ his->cseq++;
$ his->au h();
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his-> eadResponse();
}
i ($ his-> es_code == '401'){
$ his->cseq++;
$ his->au hWWW();
$da a = $ his-> o ma Reques ();
$ his->socke ->sendDa a($da a);
$ his-> eadResponse();
}
b eak;
de aul : $ his->messageFlow = "Nessun da o da legge e n";
}//Chiusu a swi ch
$ his->ex a_heade s = a ay();
$ his->cseq++;
e u n $ his-> es_code;
}
/**
* Lis en o eques
131
*
* @ odo This needs o be imp o ed
*/
public unc ion lis en()
{
$ his-> eadResponse();
swi ch($ his-> eq_me hod){
case 'UPDATE':
$ his-> eplyWi hBody("200","OK","UPDATE");// 200OK Associa ed o he
UPDATE ansac ion.
$ his-> eplyWi hBody("200","OK","INVITE");// 200OK Associa ed o he INVITE
ansac ion.
b eak;
case 'BYE': //Richies a
di chiusu a
$ his->socke ->se Reques Po (5060);
$ his-> eplyWi hBody("200","OK","BYE");
//$ his-> eq_me hod .= " Response in ia a dopo la icezione di ichies a di
chiusu a n";
//$ his->socke ->closeSocke ();
b eak;
case 'INVITE':
/*Impos o il des ina a io al p imo eques _ ia o a o*/
o ($i = 0; $i < 1; $i++){
//$ his->socke ->se Reques Hos ($ his-> eques _ ia[$i]); //P imo ia che incon a
/*Es aggo l'ip e l'e en uale po a dal p imo ia*/
$des = $ his-> eques _ ia[$i];
$semicolon = s pos($des , ";");
i ($semicolon !== alse){
$des = subs ($des , 0, $semicolon);
}
$ emp = explode(" ", $des ); //Con iene l'ip e l'e en uale po a
$ emp2 = explode(":", $ emp[1]);
$ his->socke ->se Reques Hos ($ emp2[0]);//Se o l'ip
i (isse ($ emp2[1])){//Se c'è anche la po a
$ his->se Reques Po ($ emp2[1]);
}
}
//$ his-> eplyWi hBody("100","T ying");
$ his-> eplyWi hBody("200","OK","INVITE");// 200OK Associa ed o he INVITE
ansac ion.
//$ his-> eplyWi hBody("183","Session in p og ess","INVITE");
//$ his-> eplyWi hBody("200","OK");
b eak;
case 'ACK':
/*Impos o come nuo o des ina a io il Con ac */
$u l = s _ eplace("sip:","sip://",$ his-> eq_con ac );
i (!$u l = @pa se_u l($u l))
{
h ow new PhpSIPExcep ion("Failed o pa se URI-Con ac .");
}
$ his->socke ->se Reques Hos ($u l['hos ']);
132
i (isse ($u l['po ']))
{
$ his->socke ->se Reques Po ($u l['po ']);
}
b eak;
// de aul : echo 'No eques ecei ed';
}
e u n $ his-> eq_me hod;
}
/**
* Reads esponse
*/
p i a e unc ion eadResponse()
{
$ his-> esponse = $ his->socke -> ec Da a();
$ his->messageFlow .= "Rispos a ice u a n".$ his-> esponse." n";
// Response
$ esul = a ay();
i (p eg_ma ch('/^SIP /2 .0 ([0-9]{3})/',$ his-> esponse,$ esul ))
{
$ his-> es_code = im($ esul [1]);
$ es_class = subs ($ his-> es_code,0,1);
i ($ es_class == '1' || $ es_class == '2')
{
$ his->dialog = ue;
}
$ his->pa seResponse();
}
// Reques
else
{
$ his->pa seReques ();
}
}
/**
* Pa se Response
*/
p i a e unc ion pa seResponse()
{
// il p oxy è impos a o, $ his-> eques Hos = p oxy
// il p oxy non è impos a o e non c'è una RR, allo a $ his-> eques Hos = Con ac del messaggio
di ispos a
// il p oxy non è impos a o e c'è una RR, allo a $ his-> eques Hos = p ima eco d- ou e
impos a a.
// To ag
$ esul = a ay();
133
i (p eg_ma ch('/^To: .*; ag=(.*)$/im',$ his-> esponse,$ esul ))
{
$ his-> o_ ag = im($ esul [1]);
}
// Rou e
$ esul = a ay();
i (p eg_ma ch_all('/^Reco d-Rou e: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ou e)
{
i (!in_a ay( im($ ou e),$ his-> ou es))
{
$ his-> ou es[] = im($ ou e);
}
}
}
// Reques ia
$ esul = a ay();
$ his-> eques _ ia = a ay();
i (p eg_ma ch_all('/^Via: (.*)$/im',$ his-> esponse,$ esul ))
{
o each ($ esul [1] as $ ia)
{
$ his-> eques _ ia[] = im($ ia);
}
}
// Response con ac
$ esul = a ay();
i (p eg_ma ch('/^Con ac :.*<(.*)>/im',$ his-> esponse,$ esul ))
{
$ his-> es_con ac = im($ esul [1]);
$semicolon = s pos($ his-> es_con ac ,";");
i ($semicolon !== alse)
{
$ his-> es_con ac = subs ($ his-> es_con ac ,0,$semicolon);
}
}
// Response CSeq me hod
$ esul = a ay();
i (p eg_ma ch('/^CSeq: [0-9]+ (.*)$/im',$ his-> esponse,$ esul ))
{
$ his-> es_cseq_me hod = im($ esul [1]);
}
/*Con olli pe decide e do e in ia e i messaggi*/
i ($ his->p oxy){//Se è s a o speci ica o un p oxy, i messaggi anno in ia i lì
$ his->socke ->se Reques Hos ($ his->p oxy);
134
/*Ape u a ile NRDL_ACK*/
$ d = open($pa h," ");
$n dl = ead($ d, ilesize($pa h));
/*Ges ione callID dinamico*/
$ce ca = "diag12345";
$sos i uisci = $ his->hashChiaman e."".$ his->hashChiama o;
$n dl = s _ eplace($ce ca, $sos i uisci, $n dl);
/**/
}
else
{
$a = 'ACK '.$ his->u i.' SIP/2.0'." n";
}
// Via
$a.= 'Via: '.$ his-> ia." n";
// Rou e
i ($ his-> ou es)
{
o each ($ his-> ou es as $ ou e)
{
$a.= 'Rou e: '.$ ou e." n";
}
}
// F om
i (!$ his-> om_ ag) $ his->se F omTag();
$a.= 'F om: '.$ his-> om.'; ag='.$ his-> om_ ag." n";
// To
i ($ his-> o_ ag)
$a.= 'To: '.$ his-> o.'; ag='.$ his-> o_ ag." n";
else
$a.= 'To: '.$ his-> o." n";
// Call-ID
i (!$ his->call_id) $ his->se CallId();
$a.= 'Call-ID: '.$ his->call_id." n";
//CSeq
$a.= 'CSeq: '.$ his->cseq.' ACK'." n";
// Au hen ica ion
i ($ his-> es_code == '200' && $ his->au h)
{
$a.= 'P oxy-Au ho iza ion: '.$ his->au h." n";
}
// Max-Fo wa ds
$a.= 'Max-Fo wa ds: 70'." n";
// Use -Agen
$a.= 'Use -Agen : '.$ his->use _agen ." n";
// Con en -Leng h
$a.= 'Con en -Leng h: '.s len($n dl)." n";
$a.= " n";
//Aggiun a del body con enen e l'NRDL_ACK
$a.= $n dl." n";
// $dx_ip = $ his->socke ->ge F omip();
141

// $dx_po = $ his->socke ->ge F ompo ();
//$ his->socke ->sendDa a($a, $dx_ip, $dx_po );
$ his->socke ->sendDa a($a);
$ his->messageFlow .= "In io ACK. n".$a." n";
}
/**
* Fo ma s SIP eques
*
* @ e u n s ing
*/
p i a e unc ion o ma Reques ()
{
i (in_a ay($ his->me hod,a ay('BYE','REFER','SUBSCRIBE')))
{
$ = $ his->me hod.' '.$ his-> es_con ac .' SIP/2.0'." n";
}
else
{
$ = $ his->me hod.' '.$ his->u i.' SIP/2.0'." n";
}
// Via
i ($ his->me hod != 'CANCEL')
{
$ his->se Via();
}
$ .= 'Via: '.$ his-> ia." n";
// Rou e
i ($ his->me hod != 'CANCEL' && $ his-> ou es)
{
o each ($ his-> ou es as $ ou e)
{
$ .= 'Rou e: '.$ ou e." n";
}
}
// F om
i (!$ his-> om_ ag) $ his->se F omTag();
$ .= 'F om: '.$ his-> om.'; ag='.$ his-> om_ ag." n";
// To
i (!in_a ay($ his->me hod,a ay("INVITE","CANCEL","NOTIFY")) && $ his-> o_ ag)
$ .= 'To: '.$ his-> o.'; ag='.$ his-> o_ ag." n";
else
$ .= 'To: '.$ his-> o." n";
// Au hen ica ion
i ($ his->au h)
{
$ .= $ his->au h." n";
$ his->au h = null;
}
// Call-ID
i (!$ his->call_id) $ his->se CallId();
$ .= 'Call-ID: '.$ his->call_id." n";
//CSeq
i ($ his->me hod == 'CANCEL')
142
{
$ his->cseq--;
}
$ .= 'CSeq: '.$ his->cseq.' '.$ his->me hod." n";
// Con ac
i ($ his->me hod != 'MESSAGE')
{
$ .= 'Con ac : <sip:'.$ his-> om_use .'@'.$ his->socke ->ge Ip().':'.$ his->socke -
>ge S cPo ().'>'." n";
}
// Con en -Type
i ($ his->con en _ ype)
{
$ .= 'Con en -Type: '.$ his->con en _ ype." n";
}
// Max-Fo wa ds
$ .= 'Max-Fo wa ds: 70'." n";
// Use -Agen
$ .= 'Use -Agen : '.$ his->use _agen ." n";
// Addi ional heade
o each ($ his->ex a_heade s as $heade )
{
$ .= $heade ." n";
}
// Con en -Leng h
$ .= 'Con en -Leng h: '.s len($ his->body)." n";
$ .= " n";
$ .= $ his->body;
e u n $ ;
}
/**
* Se s body
*/
public unc ion se Body($body)
{
$ his->body = $body;
}
/**
* Se s Con en Type
*/
public unc ion se Con en Type($con en _ ype = null)
{
i ($con en _ ype !== null)
{
$ his->con en _ ype = $con en _ ype;
}
else
{
swi ch ($ his->me hod)
{
case 'INVITE':
143
$ his->con en _ ype = 'applica ion/ d +xml'; //$ his->con en _ ype = 'applica ion/sdp';
b eak;
case 'MESSAGE':
$ his->con en _ ype = ' ex /h ml; cha se =u -8';
b eak;
de aul :
$ his->con en _ ype = null;
}
}
}
/**
* Se s Via heade
*/
p i a e unc ion se Via()
{
$ and = and(100000,999999);
$ his-> ia = 'SIP/2.0/UDP '.$ his->socke ->ge IP().':'.$ his->socke -
>ge S cPo ().'; po ;b anch=z9hG4bK'.$ and;
}
/**
* Se s om ag
*/
p i a e unc ion se F omTag()
{
$ his-> om_ ag = and(10000,99999);
}
/**
* Se s call id
*/
p i a e unc ion se CallId()
{
$ his->call_id = md5(uniqid()).'@'.$ his->socke ->ge Ip();
}
/**
* Ge s alue o he heade om he p e ious eques
*
* @pa am s ing $name Heade name
*
* @ e u n s ing o alse
*/
public unc ion ge Heade ($name)
{
i (p eg_ma ch('/^'.$name.': (.*)$/m',$ his-> esponse,$ esul ))
{
e u n im($ esul [1]);
}
else
{
e u n alse;
}
144
}
/**
* Calcula es Diges au hen ica ion esponse
*
*/
p i a e unc ion au h()
{
i (!$ his->use name)
{
h ow new PhpSIPExcep ion("Missing use name");
}
i (!$ his->passwo d)
{
h ow new PhpSIPExcep ion("Missing passwo d");
}
// ealm
$ esul = a ay();
i (!p eg_ma ch('/^P oxy-Au hen ica e: .* ealm="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind ealm in p oxy-au h");
}
$ ealm = $ esul [1];
// nonce
$ esul = a ay();
i (!p eg_ma ch('/^P oxy-Au hen ica e: .* nonce="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind nonce in p oxy-au h");
}
$nonce = $ esul [1];
$ha1 = md5($ his->use name.':'.$ ealm.':'.$ his->passwo d);
$ha2 = md5($ his->me hod.':'.$ his->u i);
$ es = md5($ha1.':'.$nonce.':'.$ha2);
$ his->au h = 'P oxy-Au ho iza ion: Diges use name="'.$ his->use name.'", ealm="'.$ ealm.'",
nonce="'.$nonce.'", u i="'.$ his->u i.'", esponse="'.$ es.'", algo i hm=MD5';
}
/**
* Calcula es WWW au ho iza ion esponse
*
*/
p i a e unc ion au hWWW()
{
i (!$ his->use name)
{
h ow new PhpSIPExcep ion("Missing au h use name");
145
}
i (!$ his->passwo d)
{
h ow new PhpSIPExcep ion("Missing au h passwo d");
}
$qop_p esen = alse;
i
(s pos($ his-> esponse,'qop=') !== alse)
{
$qop_p esen = ue;
// we can only do qop="au h"
i (s pos($ his-> esponse,'qop="au h"') === alse)
{
h ow new PhpSIPExcep ion('Only qop="au h" diges au hen ica ion suppo ed.');
}
}
// ealm
$ esul = a ay();
i (!p eg_ma ch('/^WWW-Au hen ica e: .* ealm="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind ealm in www-au h");
}
$ ealm = $ esul [1];
// nonce
$ esul = a ay();
i (!p eg_ma ch('/^WWW-Au hen ica e: .* nonce="(.*)"/imU',$ his-> esponse, $ esul ))
{
h ow new PhpSIPExcep ion("Can' ind nonce in www-au h");
}
$nonce = $ esul [1];
$ha1 = md5($ his->use name.':'.$ ealm.':'.$ his->passwo d);
$ha2 = md5($ his->me hod.':'.$ his->u i);
i ($qop_p esen )
{
$cnonce = md5( ime());
$ es = md5($ha1.':'.$nonce.':00000001:'.$cnonce.':au h:'.$ha2);
}
else
{
$ es = md5($ha1.':'.$nonce.':'.$ha2);
}
$ his->au h = 'Au ho iza ion: Diges use name="'.$ his->use name.'", ealm="'.$ ealm.'",
nonce="'.$nonce.'", u i="'.$ his->u i.'", esponse="'.$ es.'", algo i hm=MD5';
146

i ($qop_p esen )
{
$ his->au h.= ', qop="au h", nc="00000001", cnonce="'.$cnonce.'"';
}
}
/**
* Rese s callid, o/ om ags e c.
*
*/
public unc ion newCall()
{
$ his->cseq = 20;
$ his->call_id = null;
$ his-> o_ ag = null;;
$ his-> om_ ag = null;;
/**
* Body
*/
$ his->body = null;
/**
* Recei ed Response
*/
$ his-> esponse = null;
$ his-> es_code = null;
$ his-> es_con ac = null;
$ his-> es_cseq_me hod = null;
$ his-> es_cseq_numbe = null;
/**
* Recei ed Reques
*/
$ his-> eq_me hod = null;
$ his-> eq_cseq_me hod = null;
$ his-> eq_cseq_numbe = null;
$ his-> eq_con ac = null;
$ his-> ou es = a ay();
$ his-> eques _ ia = a ay();
}
/*Tiene accia di u i i messaggi ice u i e in ia i*/
public unc ion ge MessageFlow(){
e u n $ his->messageFlow;
}
public unc ion se NewSocke ($newSock){
$ his->socke = $newSock;
}
}
?>
147
D.II es somoza_upda e
Clien SIP.class.php
<?php
/**
* PHP SIP UAC class
*
* @ing oup API
* @au ho
*
* @ e sion SVN: $Id: PhpSIP.class.php 24 2009-11-25 11:39:57Z le el7sys ems $
*/
equi e_once 'PhpSIP.Excep ion.php';
equi e_once('Socke SIP.class.php');
class PhpSIP
{
/*
* S inga con enen e i messaggi scambia i
*/
p i a e $messageFlow;
p i a e $debug = alse;
/**
* Final Response ime (in seconds)
*/
p i a e $ _ ime = 7;
/**
* Allowed me hods a ay
*/
p i a e $allowed_me hods = a ay(
"CANCEL","NOTIFY",
"INVITE","BYE","REFER","OPTIONS","SUBSCRIBE","MESSAGE","REGISTER"
);
/**
* Dialog es ablished
*/
p i a e $dialog = alse;
/**
* The opened socke we lis en o incoming SIP messages
*/
p i a e $socke ;
/**
* Sou ce IP add ess
*/
p i a e $use _agen = 'PHP SIP';
148
/**
* CSeq
*/
p i a e $cseq = 20;
/**
* INVITE Via
*/
p i a e $in _ ia1;
p i a e $in _ ia2;
/**
* Call ID
*/
p i a e $call_id;
/**
* INVITE Call ID
*/
p i a e $in i e_call_id;
/**
* Con ac
*/
p i a e $con ac ;
/**
* Reques URI
*/
p i a e $u i;
/**
* Reques hos
*/
p i a e $hos ;
/**
* Reques po
*/
p i a e $po = 5060;
/**
* Ou boud SIP p oxy
*/
p i a e $p oxy;
/**
* Me hod
*/
p i a e $me hod;
/**
* Au h use name
*/
149
p i a e $use name;
/**
* Au h passwo d
*/
p i a e $passwo d;
/**
* To
*/
p i a e $ o;
/**
* To ag
*/
p i a e $ o_ ag;
/**
* INVITE To ag
*/
p i a e $in _ ag;
/**
* INVITE F om ag
*/
p i a e $in _ ag;
/**
* F om
*/
p i a e $ om;
/**
* F om Use
*/
p i a e $ om_use ;
/**
* F om ag
*/
p i a e $ om_ ag;
/**
* Via ag
*/
p i a e $ ia;
/**
* Con en ype
*/
p i a e $con en _ ype;
/**
* Body
*/
p i a e $body;
150