410 lines
9.6 KiB
Smalltalk
410 lines
9.6 KiB
Smalltalk
"
|
|
(C) 2010-2011 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/>.
|
|
"
|
|
|
|
PackageLoader fileInPackage: 'OsmoNetwork'.
|
|
|
|
Object subclass: SCCPConnectionBase [
|
|
| src dst conManager confirmSem proc state |
|
|
|
|
SCCPConnectionBase class >> stateInitial [ ^ 0 ]
|
|
SCCPConnectionBase class >> stateConnected [ ^ 1 ]
|
|
SCCPConnectionBase class >> stateReleased [ ^ 2 ]
|
|
SCCPConnectionBase class >> stateReleaseComplete [ ^ 3 ]
|
|
SCCPConnectionBase class >> stateTimeout [ ^ 3 ]
|
|
|
|
SCCPConnectionBase class >> new [
|
|
^ self shouldNotImplement
|
|
]
|
|
|
|
SCCPConnectionBase class >> on: aHandler [
|
|
^ super new
|
|
initialize;
|
|
conManager: aHandler;
|
|
yourself
|
|
]
|
|
|
|
connectionRequest: aData [
|
|
| res |
|
|
"Send the confirmation now"
|
|
res := Osmo.SCCPConnectionRequest
|
|
initWith: (self srcRef) dest: (Osmo.SCCPAddress createWith: 254) data: aData.
|
|
self nextPut: res toMessage.
|
|
]
|
|
|
|
initialize [
|
|
state := self class stateInitial.
|
|
confirmSem := Semaphore new.
|
|
]
|
|
|
|
conManager [
|
|
<category: 'accessing'>
|
|
^ conManager
|
|
]
|
|
|
|
conManager: aHandler [
|
|
<category: 'private'>
|
|
|
|
"Check if it is not there otherwise bad things happen"
|
|
conManager ifNotNil: [
|
|
^ self error: 'Can only be set once.'.
|
|
].
|
|
|
|
conManager := aHandler.
|
|
conManager addConnection: self
|
|
]
|
|
|
|
srcRef [
|
|
<category: 'access'>
|
|
^ src
|
|
]
|
|
srcRef: aRef [
|
|
<category: 'access'>
|
|
src := aRef
|
|
]
|
|
|
|
dstRef: aRef [
|
|
<category: 'access'>
|
|
dst := aRef
|
|
]
|
|
|
|
dstRef [
|
|
<category: 'access'>
|
|
^ dst
|
|
]
|
|
|
|
nextPutData: aMsg [
|
|
| dt1 |
|
|
dt1 := Osmo.SCCPConnectionData initWith: self dstRef data: aMsg.
|
|
self nextPut: dt1 toMessage.
|
|
]
|
|
|
|
nextPut: aMsg [
|
|
conManager sendMsg: aMsg.
|
|
]
|
|
|
|
confirm: aCC [
|
|
<category: 'connection-handling'>
|
|
self dstRef: aCC src.
|
|
state := self class stateConnected.
|
|
confirmSem signal.
|
|
]
|
|
|
|
release [
|
|
| rlsd |
|
|
|
|
state := self class stateReleased.
|
|
rlsd := Osmo.SCCPConnectionReleased initWithDst: self dstRef src: self srcRef cause: 0.
|
|
self nextPut: rlsd toMessage.
|
|
]
|
|
|
|
releaseComplete: aMSG [
|
|
"TODO: verify that we are in the right state"
|
|
state := self class stateReleaseComplete.
|
|
self terminate.
|
|
]
|
|
|
|
released: aRLSD [
|
|
| rlc |
|
|
"Give up local resources here. We are done."
|
|
|
|
state := self class stateReleaseComplete.
|
|
rlc := Osmo.SCCPConnectionReleaseComplete
|
|
initWithDst: aRLSD src src: aRLSD dst.
|
|
self nextPut: rlc toMessage.
|
|
self terminate.
|
|
]
|
|
|
|
state [ <category: 'accessing'> ^ state ]
|
|
]
|
|
|
|
SCCPConnectionBase subclass: SCCPConnection [
|
|
data: aDT [
|
|
"nothing implemented"
|
|
]
|
|
|
|
terminate [
|
|
"noting implemented"
|
|
]
|
|
]
|
|
|
|
Object subclass: MSGParser [
|
|
<comment: 'I take a SCCP message and recursively parse all the data'>
|
|
|
|
MSGParser class >> parse: aByteArray [
|
|
| sccp |
|
|
"Return a completely decoded subtree"
|
|
|
|
sccp := Osmo.SCCPMessage decode: aByteArray.
|
|
(sccp respondsTo: #data)
|
|
ifTrue: [
|
|
sccp data: (self decodeBSSAP: sccp data).
|
|
].
|
|
|
|
^ sccp
|
|
]
|
|
|
|
MSGParser class >> decodeBSSAP: aData [
|
|
| bssap |
|
|
bssap := BSSAPMessage decode: aData.
|
|
bssap class msgType = BSSAPDTAP msgType
|
|
ifTrue: [
|
|
bssap data: (GSM48MSG decode: bssap data)
|
|
]
|
|
ifFalse: [
|
|
bssap data: (self decodeBSSMAP: bssap data).
|
|
].
|
|
|
|
^ bssap
|
|
]
|
|
|
|
MSGParser class >> decodeBSSMAP: aData [
|
|
| bssmap |
|
|
bssmap := IEMessage decode: aData with: GSM0808IE.
|
|
bssmap findIE: (GSMLayer3Info elementId) ifPresent: [:each |
|
|
each data: (GSM48MSG decode: each data).
|
|
].
|
|
^ bssmap
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
Osmo.SCCPMessage extend [
|
|
sccpInitialDispatch: aHandler [
|
|
^ aHandler dispatchMessage: self.
|
|
]
|
|
|
|
sccpHandlerDispatchOn: aCon [
|
|
"Message is not handled here"
|
|
self logError: 'Unhandled SCCP packet %1' % {self class} area: #sccp.
|
|
^ false
|
|
]
|
|
]
|
|
|
|
Osmo.SCCPUDT extend [
|
|
sccpInitialDispatch: aHandler [
|
|
aHandler handleUDT: self.
|
|
^ true
|
|
]
|
|
]
|
|
|
|
Osmo.SCCPConnectionRequest extend [
|
|
sccpInitialDispatch: aHandler [
|
|
self logNotice: 'New incoming connection' area: #sccp.
|
|
aHandler confirmConnection: self.
|
|
^ true
|
|
]
|
|
]
|
|
|
|
Osmo.SCCPConnectionConfirm extend [
|
|
sccpHandlerDispatchOn: aCon [
|
|
aCon confirm: self.
|
|
^ true
|
|
]
|
|
]
|
|
|
|
Osmo.SCCPConnectionData extend [
|
|
sccpHandlerDispatchOn: aCon [
|
|
aCon data: self.
|
|
^ true
|
|
]
|
|
]
|
|
|
|
Osmo.SCCPConnectionReleased extend [
|
|
sccpHandlerDispatchOn: aCon [
|
|
aCon released: self.
|
|
aCon conManager removeConnection: aCon.
|
|
^ true
|
|
]
|
|
]
|
|
|
|
Osmo.SCCPConnectionReleaseComplete extend [
|
|
sccpHandlerDispatchOn: aCon [
|
|
aCon releaseComplete: self.
|
|
aCon conManager removeConnection: aCon.
|
|
^ true
|
|
]
|
|
]
|
|
|
|
Object subclass: SCCPHandler [
|
|
| connections last_ref connection sem |
|
|
<comment: 'I handle SCCP messages'>
|
|
|
|
SCCPHandler class >> new [
|
|
^ super new initialize; yourself
|
|
]
|
|
|
|
initialize [
|
|
sem := Semaphore forMutualExclusion.
|
|
]
|
|
|
|
addConnection: aConnection [
|
|
sem critical: [
|
|
self connections add: aConnection.
|
|
aConnection srcRef: self assignSrcRef.
|
|
].
|
|
]
|
|
|
|
removeConnection: aConnection [
|
|
self connections remove: aConnection.
|
|
|
|
]
|
|
|
|
registerOn: aDispatcher [
|
|
aDispatcher addHandler: Osmo.IPAConstants protocolSCCP
|
|
on: self with: #handleMsg:.
|
|
]
|
|
|
|
connectionTimeout: aConnection [
|
|
self logError: 'SCCP Connection %1 timedout' % {aConnection srcRef} area: #sccp.
|
|
sem critical: [
|
|
self removeConnection: aConnection.
|
|
]
|
|
]
|
|
|
|
forwardMessage: aMessage with: aConnection [
|
|
^ aMessage sccpHandlerDispatchOn: aConnection.
|
|
|
|
]
|
|
|
|
dispatchMessage: aMessage [
|
|
sem critical: [
|
|
self connections do: [:each |
|
|
each srcRef = aMessage dst
|
|
ifTrue: [
|
|
^ self forwardMessage: aMessage with: each.
|
|
].
|
|
]
|
|
].
|
|
|
|
self logError: 'No one handled connection %1' % {aMessage dst} area: #sccp.
|
|
]
|
|
|
|
dissectMSG: aMsg [
|
|
^ MSGParser parse: (aMsg asByteArray).
|
|
]
|
|
|
|
newConnection: aCon [
|
|
"Interesting for subclasses"
|
|
]
|
|
|
|
connectionSpecies [
|
|
^ SCCPConnection
|
|
]
|
|
|
|
confirmConnection: aMsg [
|
|
| con res |
|
|
|
|
con := self connectionSpecies on: self.
|
|
|
|
"Confirm the message now and send any data"
|
|
con confirm: aMsg.
|
|
aMsg data ifNotNil: [
|
|
con data: aMsg.
|
|
].
|
|
|
|
"Confirm it without sending any new data bad"
|
|
res := Osmo.SCCPConnectionConfirm initWithSrc: (con srcRef) dst: (con dstRef).
|
|
self sendMsg: res toMessage.
|
|
|
|
self newConnection: con.
|
|
^ con.
|
|
]
|
|
|
|
handleMsg: aMsg [
|
|
| sccp |
|
|
|
|
[
|
|
sccp := self dissectMSG: aMsg asByteArray.
|
|
] on: Exception do: [
|
|
self logError: 'Failed to parse message' area: #sccp.
|
|
aMsg toMessageOrByteArray printNl.
|
|
^ false
|
|
].
|
|
|
|
sccp sccpInitialDispatch: self.
|
|
]
|
|
|
|
handleUDT: aSCCP [
|
|
self logNotice: 'Incomind UDT message' area: #sccp.
|
|
]
|
|
|
|
|
|
connection: aConnection [
|
|
connection := aConnection.
|
|
]
|
|
|
|
sendMsg: aMsg [
|
|
"Send a SCCP message."
|
|
connection send: aMsg with: Osmo.IPAConstants protocolSCCP.
|
|
]
|
|
|
|
referenceIsFree: aRef [
|
|
<category: 'private'>
|
|
self connections do: [:each |
|
|
each srcRef = aRef
|
|
ifTrue: [
|
|
^ false
|
|
].
|
|
].
|
|
|
|
^ true
|
|
]
|
|
|
|
assignSrcRef [
|
|
"Find a free SCCP reference"
|
|
1 to: 16rFFFFFE do: [:dummy |
|
|
| ref |
|
|
ref := Random between: 1 and: 16rFFFFFE.
|
|
(self referenceIsFree: ref)
|
|
ifTrue: [
|
|
^ ref.
|
|
].
|
|
].
|
|
|
|
self error: 'No free SCCP Connection. Close some'.
|
|
]
|
|
|
|
connections [
|
|
^ connections ifNil: [ connections := OrderedCollection new. ]
|
|
]
|
|
|
|
doTerminate: aCon [
|
|
<category: 'termination'>
|
|
"I kill the SCCP Connection."
|
|
|
|
[
|
|
aCon terminate
|
|
] on: Error do: [:each |
|
|
each logException: 'Failed to terminate %1' % {aCon srcRef} area: #sccp.
|
|
]
|
|
]
|
|
|
|
linkSetFailed [
|
|
"The underlying has failed, invalidate all connections"
|
|
<category: 'failure'>
|
|
sem critical: [
|
|
self connections do: [:each |
|
|
self doTerminate: each].
|
|
connections := nil.
|
|
]
|
|
]
|
|
]
|
|
|