summaryrefslogtreecommitdiffstats
path: root/callagent/transactions/SIPTransaction.st
blob: ad07d837a86f2df1ce8f5c35c41ce054876c6f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"
 (C) 2011,2014 by Holger Hans Peter Freyther
 All Rights Reserved

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
"

Object subclass: SIPTransaction [
    | sem useragent initial_dialog state timeout success failure notification
      cseq branch retransmit_time fail_time removal
      authorization last_was_auth proxy_authorization last_was_proxy_auth |

    <category: 'OsmoSIP-Callagent'>

    SIPTransaction class >> stateInitial    [ <category: 'states'> ^ #initial ]
    SIPTransaction class >> stateTrying     [ <category: 'states'> ^ #trying ]
    SIPTransaction class >> stateProceeding [ <category: 'states'> ^ #proceeding ]
    SIPTransaction class >> stateCompleted  [ <category: 'states'> ^ #completed ]
    SIPTransaction class >> stateTerminated [ <category: 'states'> ^ #terminated ]
    SIPTransaction class >> stateCanceled   [ <category: 'states'> ^ #canceled ]

    Grammar := nil.

    SIPTransaction class >> createWith: aDialog on: aUA  cseq: aCseq [
        <category: 'creation'>
        ^ self new
            initialize;
            userAgent: aUA;
            initialDialog: aDialog;
            setupTransaction: aCseq;
            yourself.
    ]

    SIPTransaction class >> grammar [
        <category: 'private'>
        ^ Grammar ifNil: [Grammar := SIPGrammar new]
    ]

    SIPTransaction class >> verifyGrammar: aData [
        <category: 'private'>
        self grammar parse: aData onError: [:e |
            e logException: 'Outgoing data has parsing error' area: #sip.
            e signal
        ]
    ]


    initialize [
        <category: 'creation'>
        sem := RecursionLock new.
        last_was_auth := false.
        last_was_proxy_auth := false.
    ]

    initialDialog: aDialog [
        <category: 'creation'>
        initial_dialog := aDialog
    ]

    userAgent: aUA [
        <category: 'creation'>
        useragent := aUA
    ]

    state [
        <category: 'state'>
        ^ state ifNil: [^ self class stateInitial]
    ]

    changeState: aState [
        <category: 'state'>

        "I could verify the state change here"
        sem critical: [
            state := aState.
        ].
    ]

    onTimeout: aTimeout [
        <category: 'setup'>
        timeout := aTimeout
    ]

    onSuccess: aSuc [
        <category: 'setup'>
        success := aSuc
    ]

    onFailure: aFail [
        <category: 'setup'>
        failure := aFail
    ]

    onNotification: aNot [
        <category: 'setup'>
        notification := aNot
    ]

    setupTransaction: aCseq [
        <category: 'private'>
        "I setup the transaction"
        useragent addTransaction: self.
        cseq := aCseq.
        branch := useragent class generateBranch.
    ]

    queueData: aData dialog: aDialog [
        <category: 'private'>

        "validate the output"
        self class verifyGrammar: aData.
        useragent queueData: aData dialog: aDialog.
    ]

    branch [
        <category: 'accessing'>
        ^ branch
    ]

    respNotification: aResp dialog: aDialog [
        <category: 'private-dispatch'>

        self changeState: self class stateProceeding.

        notification ifNotNil: [
            notification value: aResp value: aDialog]
    ]

    respSuccess: aResp dialog: aDialog [
        <category: 'private-dispatch'>

        self changeState: self class stateCompleted.
        self removeTransaction.
        ^ success isNil
                ifTrue: [true]
                ifFalse: [success value: aResp value: aDialog].
    ]

    respFailure: aResp dialog: aDialog [
        <category: 'private-dispatch'>

        self removeTransaction.
        failure ifNotNil: [
            failure value: aResp value: aDialog]
    ]

    wrongAuth: aResp dialog: aDialog [
        <category: 'private-dispatch'>
        self logError: ('<1p>(<2p>) response lacks WWW-Authenticate'
                        expandMacrosWith: self class with: self branch) area: #sip.
        self removeTransaction.
        failure ifNotNil: [
            failure value: aResp value: aDialog]
    ]

    respAuthRequired: aResp dialog: aDialog [
        | auth |
        <category: 'private-dispatch'>

        "We are running in circles so better cancel it"
        last_was_auth ifTrue: [
            ^self respFailure: aResp dialog: aDialog].

        last_was_auth := true.
        auth := aResp parameter: 'WWW-Authenticate' ifAbsent: [nil].
        auth ifNil: [^self wrongAuth: aResp dialog: aDialog].

        ((auth at: 'algorithm' ifAbsent: ['MD5']) = 'MD5')
            ifFalse: [^self wrongAuth: aResp dialog: aDialog].

        authorization := SIPAuthorization new
            username: initial_dialog identity username;
            realm: (auth at: 'realm');
            nonce: (auth at: 'nonce');
            uri: initial_dialog destinationAddress;
            yourself.
        authorization
            calculateResponse: initial_dialog identity password
            operation: self class operationName.

        "Increase CSeq and generate a new branch"
        cseq := cseq + 1.
        branch := useragent class generateBranch.

        "Now start again with the auth part"
        self retransmit.
    ]

    respProxyAuthRequired: aResp dialog: aDialog [
        | auth |
        <category: 'private-dispatch'>

        "TODO... fix the duplication"

        "We are running in circles so better cancel it. Not quite correct
        though. There could be multiple proxies. So we should compare the
        realm, nonce, etc. We would actually need to have a list of proxy
        auths for full spec compliance."
        last_was_proxy_auth ifTrue: [
            ^self respFailure: aResp dialog: aDialog].

        last_was_proxy_auth := true.
        auth := aResp parameter: 'Proxy-Authenticate' ifAbsent: [nil].
        auth ifNil: [^self wrongAuth: aResp dialog: aDialog].

        ((auth at: 'algorithm') = 'MD5')
            ifFalse: [^self wrongAuth: aResp dialog: aDialog].

        ((auth at: 'qop') = 'auth')
            ifFalse: [^self wrongAuth: aResp dialog: aDialog].

        proxy_authorization := SIPProxyAuthorization new
            username: initial_dialog identity proxyUsername;
            realm: (auth at: 'realm');
            nonce: (auth at: 'nonce');
            qop: (auth at: 'qop');
            cnonce: SIPURandom newClientNonce;
            uri: initial_dialog destinationAddress;
            yourself.

        "Increase CSeq and generate a new branch"
        cseq := cseq + 1.
        branch := useragent class generateBranch.

        "Now start again with the auth part"
        self retransmit.
    ]

    checkSequenceNumber: aReq [
        | new_cseq |
        <category: 'private-dispatch'>
        "I have to verify the sequence numbers..."
        new_cseq := (aReq parameter: 'CSeq' ifAbsent: [
            self logError: ('<1p>(<2p>) response lacks CSeq.'
                        expandMacrosWith: self class with: self branch) area: #sip.
            ^ false
        ]) number.

        cseq = new_cseq ifFalse: [
            self logError: ('<1p>(<2p>) wrong cseq:. <1p> <2p>.' expandMacrosWithArguments:
                        {self class. self branch. cseq. new_cseq}) area: #sip.
            ^ false
        ].

        ^ true
    ]


    stopRetransmitTimer [
        <category: 'private'>
        "I stop the retransmit timers, e.g. because there was a request,
         or there was a timeout, or someone canceled things."
        sem critical: [
            retransmit_time cancel.
            fail_time cancel.
        ]
    ]

    newData: aResp [
        | dialog |
        <category: 'private-dispatch'>

        (self checkSequenceNumber: aResp) ifFalse: [^ false].

        "Store all dialogs.... and match them..."
        dialog := initial_dialog newFromRequest: aResp.

        "Forget the authorization now"
        authorization := nil.

        self stopRetransmitTimer.
        self dispatchDialog: dialog response: aResp.
    ]

    dispatchDialog: dialog response: aResp [
        | code |
        <category: 'private-dispatch'>

        code := aResp code asInteger.
        code < 200 ifTrue: [
            "Ignore 100 TRYING"
            ^ self respNotification: aResp dialog: dialog
        ].

        code = 200 ifTrue: [
            last_was_auth := false.
            last_was_proxy_auth := false.
            ^ self respSuccess: aResp dialog: dialog
        ].

        code = 401 ifTrue: [
            last_was_proxy_auth := false.
            ^self respAuthRequired: aResp dialog: dialog.
        ].

        code = 407 ifTrue: [
            last_was_auth := false.
            ^self respProxyAuthRequired: aResp dialog: dialog.
        ].

        code > 200 ifTrue: [
            last_was_auth := false.
            last_was_proxy_auth := false.
            ^ self respFailure: aResp dialog: dialog
        ]
    ]

    start [
        sem critical: [
            self state = self class stateInitial ifFalse: [
                ^ self error: 'Can not restart.'
            ].

            "Enter the state, remember the timeout"
            self changeState: self class stateTrying.

            fail_time := Osmo.TimerScheduler instance
                            scheduleInSeconds: 60 block:
                                [sem critical: [self timedout]].
            self retransmit.
        ]
    ]

    retransmit [
        <category: 'timeout'>
        retransmit_time := Osmo.TimerScheduler instance
                            scheduleInSeconds: 10 block:
                                [sem critical: [self retransmit]].
        self transmit.
    ]

    timedout [
        <category: 'timeout'>
        useragent removeTransaction: self.
        self stopRetransmitTimer.
        timeout value.
    ]

    removeTransaction [
        <category: 'timeout'>
        "We want to remove things but we might get a late reply due a packet loss."
        removal ifNil: [
            removal := Osmo.TimerScheduler instance
                        scheduleInSeconds: 50 block:
                            [sem critical: [useragent removeTransaction: self]]].
    ]

    addAuthorizationTo: aRequest [
        <category: 'authentication'>
        authorization ifNotNil: [
            aRequest addParameter: 'Authorization' value: authorization].
        proxy_authorization ifNotNil: [
            proxy_authorization incrementClientNonce.
            proxy_authorization
                calculateResponse: initial_dialog identity proxyPassword
                operation: self class operationName.
            aRequest addParameter: 'Proxy-Authorization' value: proxy_authorization].
    ]

    createInvite: sdp [
        | invite |
        <category: 'invite'>

        invite := (SIPInviteRequest from: initial_dialog)
                        sdp: sdp;
                        addParameter: 'Via' value: (useragent generateVia: branch);
                        addParameter: 'CSeq' value: ('<1p> <2s>'
                                            expandMacrosWith: cseq with: 'INVITE');
                        addParameter: 'Allow' value: 'ACK,BYE';
                        addParameter: 'Call-ID' value: initial_dialog callId;
                        yourself.
        self addAuthorizationTo: invite.
        useragent injectDefaults: invite.
        ^ invite
    ]

    createAck: aBranch dialog: aDialog [
        | ack |
        <category: 'ack'>

        ack := (SIPACKRequest from: aDialog)
            addParameter: 'Via' value: (useragent generateVia: aBranch);
            addParameter: 'CSeq' value: ('<1p> <2s>'
                                            expandMacrosWith: cseq with: 'ACK');
            addParameter: 'Call-ID' value: aDialog callId;
            yourself.
        self addAuthorizationTo: ack.
        useragent injectDefaults: ack.
        ^ ack
    ]

    createBye: aDialog [
        | bye |
        <category: 'invite'>

        bye := (SIPByeRequest from: aDialog)
                        addParameter: 'Via' value: (useragent generateVia: branch);
                        addParameter: 'CSeq' value: ('<1p> <2s>'
                                            expandMacrosWith: cseq with: 'BYE');
                        addParameter: 'Call-ID' value: aDialog callId;
                        yourself.
        self addAuthorizationTo: bye.
        useragent injectDefaults: bye.
        ^ bye
    ]

    createCancel [
        | cancel |
        <category: 'cancel'>

        cancel := (SIPCancelRequest from: initial_dialog)
                        addParameter: 'Via' value: (useragent generateVia: branch);
                        addParameter: 'CSeq' value: ('<1p> <2s>'
                                                expandMacrosWith: cseq with: 'CANCEL');
                        addParameter: 'Call-ID' value: initial_dialog callId;
                        yourself.
        self addAuthorizationTo: cancel.
        useragent injectDefaults: cancel.
        ^ cancel.
    ]
]