-
Notifications
You must be signed in to change notification settings - Fork 1
/
diameter.scm
202 lines (173 loc) · 8.13 KB
/
diameter.scm
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
;;;
;;; Copyright (c) 2012 Michael Tuexen
;;;
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or
;;; without modification, are permitted provided that the
;;; following conditions are met:
;;; 1. Redistributions of source code must retain the above
;;; copyright notice, this list of conditions and the
;;; following disclaimer.
;;; 2. Redistributions in binary form must reproduce the
;;; above copyright notice, this list of conditions and
;;; the following disclaimer in the documentation and/or
;;; other materials provided with the distribution.
;;; 3. Neither the name of the project nor the names of
;;; its contributors may be used to endorse or promote
;;; products derived from this software without specific
;;; prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS
;;; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
;;; BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;;; DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS
;;; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
;;; IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
;;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
;;; OF SUCH DAMAGE.
;;; $Id: diameter.scm,v 1.2 2012/08/27 20:36:57 tuexen Exp $
(define (number-of-padding-bytes l)
(remainder (- 4 (remainder l 4)) 4))
(define (padding-bytes data)
(zero-bytes (number-of-padding-bytes (length data))))
;;; (append-padding-bytes (list 1 2 3))
(define mandatory-flag #b01000000)
(define (make-avp code flags data)
(append (uint32->bytes code)
(uint8->bytes flags)
(uint24->bytes (+ 8 (length data)))
data
(padding-bytes data)))
(define siemens-ag-pen 4329)
(define 3gpp-pen 10415)
(define vodafone-information-technology-and-technology-management-pen 12645)
(define nokia-networks-pen 28458)
(define diameter-common-message-app-id 0)
(define 3gpp-cx-app-id 16777216)
(define 3gpp-sh-app-id 16777217)
(define 3gpp-wx-app-id 16777219)
(define 3gpp-zh-app-id 16777221)
(define 3gpp-s6a-app-id 16777251)
(define 3gpp-swx-app-id 16777265)
(define nsn-hd-application-app-id 16777317)
(define host-ip-address-avp-code 257)
(define auth-application-id-code 258)
(define acct-application-id-code 259)
(define vendor-specific-application-id-code 260)
(define origin-host-avp-code 264)
(define supported-vendor-id-avp-code 265)
(define vendor-id-avp-code 266)
(define result-avp-code 268)
(define product-name-avp-code 269)
(define origin-realm-avp-code 296)
(define (make-host-ip-address-avp address)
(make-avp host-ip-address-avp-code mandatory-flag (append (list 0 1)
(uint32->bytes (inet-pton AF_INET address)))))
;;; (make-host-ip-address-avp "0.0.0.1")
(define (make-auth-application-id-avp id)
(make-avp auth-application-id-code mandatory-flag (uint32->bytes id)))
;;; (make-auth-application-id-avp 28458)
(define (make-acct-application-id-avp id)
(make-avp acct-application-id-code mandatory-flag (uint32->bytes id)))
;;; (make-acct-application-id-avp 28458)
(define (make-vendor-specific-application-id-avp pen app-id)
(make-avp vendor-specific-application-id-code mandatory-flag (append (make-vendor-id-avp pen)
(make-auth-application-id-avp app-id))))
;;; (make-vendor-specific-application-id-avp nokia-networks-pen nsn-hd-application-app-id)
(define (make-origin-host-avp origin-host)
(make-avp origin-host-avp-code mandatory-flag (string->bytes origin-host)))
;;; (make-origin-host-avp "www1.sctp.de")
(define (make-vendor-id-avp pen)
(make-avp vendor-id-avp-code mandatory-flag (uint32->bytes pen)))
;;; (make-vendor-id-avp nokia-networks-pen)
(define (make-supported-vendor-id-avp pen)
(make-avp supported-vendor-id-avp-code mandatory-flag (uint32->bytes pen)))
;;; (make-supported-vendor-id-avp nokia-networks-pen)
(define diameter-success 2001)
(define (make-result-avp result)
(make-avp result-avp-code mandatory-flag (uint32->bytes result)))
;;; (make-result-avp diameter-success)
(define (make-product-name-avp name)
(make-avp product-name-avp-code mandatory-flag (string->bytes name)))
;;; (make-product-name-avp "STT")
(define (make-origin-realm-avp origin-realm)
(make-avp origin-realm-avp-code mandatory-flag (string->bytes origin-realm)))
;;; (make-origin-realm-avp "www1.sctp.de")
(define CER 257) ;;; Capabilities-Exchange-Request
(define CEA 257) ;;; Capabilities-Exchange-Answer
(define DWR 280) ;;; Device-Watchdog-Request
(define DWA 280) ;;; Device-Watchdog-Answer
(define request-flag #b10000000)
(define no-flag 0)
(define version 1)
(define (make-diameter-message flags code app-id h2h-id e2e-id avps)
(append (uint8->bytes version)
(uint24->bytes (+ 20 (apply + (map length avps))))
(uint8->bytes flags)
(uint24->bytes code)
(uint32->bytes app-id)
(uint32->bytes h2h-id)
(uint32->bytes e2e-id)
(apply append avps)))
;;; (make-diameter-message request-flag DWR diameter-common-message-app-id 2 3 (list))
(define (get-app-id l)
(bytes->uint32 (list-tail l 8)))
(define (get-hop-by-hop-id l)
(bytes->uint32 (list-tail l 12)))
;;; (get-hop-by-hop-id (make-diameter-message request-flag DWR diameter-common-message-app-id 2 3 (list)))
(define (get-end-to-end-id l)
(bytes->uint32 (list-tail l 16)))
;;; (get-end-to-end-id (make-diameter-message request-flag DWR diameter-common-message-app-id 2 3 (list)))
(define (make-capability-exchange-request app-id h2h-id e2e-id origin-host origin-realm host-ip-address vendor-id product-name avps)
(make-diameter-message request-flag CER app-id h2h-id e2e-id
(append (list (make-origin-host-avp origin-host)
(make-origin-realm-avp origin-realm)
(make-host-ip-address-avp host-ip-address)
(make-vendor-id-avp vendor-id)
(make-product-name-avp product-name))
avps)))
;;; (make-capability-exchange-request diameter-common-message-app-id 1 2 "a.b" "c.d" "1.1.1.1" nokia-networks-pen "STT" (list))
(define (make-capability-exchange-answer app-id h2h-id e2e-id result origin-host origin-realm host-ip-address vendor-id product-name)
(make-diameter-message no-flag CEA app-id h2h-id e2e-id
(list (make-result-avp result)
(make-origin-host-avp origin-host)
(make-origin-realm-avp origin-realm)
(make-host-ip-address-avp host-ip-address)
(make-vendor-id-avp vendor-id)
(make-product-name-avp product-name))))
;;; (make-capability-exchange-answer diameter-common-message-app-id 1 2 diameter-success "a.b" "c.d" "1.1.1.1" nokia-networks-pen "STT")
(define (make-capability-exchange-answer-from-request request origin-host origin-realm host-ip-address vendor-id product-name)
(make-capability-exchange-answer (get-app-id request)
(get-hop-by-hop-id request)
(get-end-to-end-id request)
diameter-success
origin-host
origin-realm
host-ip-address
vendor-id
product-name))
(define (make-device-watchdog-request app-id h2h-id e2e-id origin-host origin-realm)
(make-diameter-message request-flag DWR app-id h2h-id e2e-id
(list (make-origin-host-avp origin-host)
(make-origin-realm-avp origin-realm))))
;;; (make-device-watchdog-request diameter-common-message-app-id 1 2 "a.b" "c.d")
(define (make-device-watchdog-answer app-id h2h-id e2e-id result origin-host origin-realm)
(make-diameter-message no-flag DWA app-id h2h-id e2e-id
(list (make-result-avp result)
(make-origin-host-avp origin-host)
(make-origin-realm-avp origin-realm))))
;;; (make-device-watchdog-answer diameter-common-message-app-id 1 2 diameter-success "a.b" "c.d")
(define (make-device-watchdog-answer-from-request request origin-host origin-realm)
(make-device-watchdog-answer (get-app-id request)
(get-hop-by-hop-id request)
(get-end-to-end-id request)
diameter-success
origin-host
origin-realm))