aboutsummaryrefslogtreecommitdiffstats
path: root/OsmoVTY.st
blob: 2b6e58183be749c1fd7456d33f4030d8c3fd1834 (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
"Copyright placeholder"

CStruct subclass: Vty_app_info [
    <category: 'libosmovty'>
    <comment: 'I represent the vty_app_info... some structs are wrong'>
    <declaration: #(
        #(#name #string)
        #(#version #string)
        #(#copyright #string)
        #(#tall_ctx #(#ptr #int))
        #(#go_parent_cb #(#ptr #long))
        #(#is_config_node #(#ptr #long))) >
]

CStruct subclass: Vty_vector [
    <category: 'libosmovty'>
    <comment: 'I am the struct _vector class'>
    "the index is wrong but we do not need to access it"
    <declaration: #(
        #(#active #uInt)
        #(#alloced #uInt)
        #(#index #(#ptr #int))) >
]

CStruct subclass: Vty_cmd_element [
    <category: 'libosmovty'>
    <comment: 'I represent the lowlevel command'>
    <declaration: #(
        #(#name #string)
        #(#func #(#ptr #int))
        #(#doc #string)
        #(#daemon #int)
        #(#strvec #(#ptr #{Vty_vector}))
        #(#cmdsize #uInt)
        #(#config #string)
        #(#subconfig #(#ptr #{Vty_vector}))
        #(#attr #uChar))>
]

CStruct subclass: Vty_cmd_node [
    <comment: 'I provide access to a cmd_node'>
    <category: 'libosmovty'>

    <declaration: #(
        #(#node #int)
        #(#prompt #string)
        #(#vtysh #int)
        #(#func #(#ptr #int))
        #(#cmd_vector #(#ptr #{Vty_vector}))) >
]

Object subclass: NodeType [
    <category: 'libosmovty'>

    NodeType class >> AUTH_NODE [
        ^ 0
    ]

    NodeType class >> VIEW_NODE [
        ^ 1
    ]

    NodeType class >> AUTH_ENABLE_NODE [
        ^ 2
    ]

    NodeType class >> ENABLE_NODE [
        ^ 3
    ]

    NodeType class >> CONFIG_NODE [
        ^ 4
    ]

    NodeType class >> SERVICE_NODE [
        ^ 5
    ]

    NodeType class >> DEBUG_NODE [
        ^ 6
    ]

    NodeType class >> VTY_NODE [
        ^ 7
    ]
]

Object subclass: OSMOVTY [
    <comment: 'I provide access to the VTY code'>
    <category: 'libosmovty'>

    OSMOVTY class >> initialize [
        DLD addLibrary: 'libosmovty.so.0'
    ]

    OSMOVTY class >> vty_init: app_info [
        <cCall: 'vty_init' returning: #void args: #(#cObject) >
    ]

    OSMOVTY class >> vty_read_config_file: file_name priv: priv [
        <cCall: 'vty_read_config_file' returning: #int args: #(#string (#ptr #long)) >
    ]

    OSMOVTY class >> telnet_init: tall_context priv: priv port: aPort [
        <cCall: 'telnet_init' returning: #int args: #(#cObject #cObject #int) >
    ]

    OSMOVTY class >> install_node: aNode func: aFunc [
        <cCall: 'install_node' returning: #void args: #(#cObject #cObject) >
    ]

    OSMOVTY class >> install_default: aNode [
        <cCall: 'install_default' returning: #void args: #(#int) >
    ]

    OSMOVTY class >> install_element: aNode cmd: aCmd [
        <cCall: 'install_element' returning: #void args: #(#int #{Vty_cmd_element}) >
    ]

    OSMOVTY class >> install_element_ve: aCmd [
        <cCall: 'install_element_ve' returning: #void args: #(#{Vty_cmd_element}) >
    ]

    OSMOVTY class >> vty_out: aVty msg: aMsg [
            <cCall: 'vty_out' returning: #int args: #(#cObject #string) >
    ]

    OSMOVTY class >> vty_out_newline: aVty [
        <cCall: 'vty_out_newline' returning: #int args: #(#cObject) >
    ]

    OSMOVTY class >> initWith: aName version: aVersion license: aLicense port: aPort [
        | app_info |

        app_info := Vty_app_info new.
        app_info name value: aName.
        app_info version value: aVersion.
        app_info copyright value: aLicense.
        app_info tall_ctx value: 0.
        app_info go_parent_cb value: 0.
        app_info is_config_node value: 0.

        OSMOVTY vty_init: app_info.
        OSMOVTY telnet_init: nil priv: nil port: 4444.
    ]

    OSMOVTY class >> commands [
        ^ Smalltalk at: #VTY_Commands ifAbsent: [
            | array |
            array := OrderedCollection new.
            Smalltalk at: #VTY_Commands put: array.
            array.
        ]
    ]
]

Object subclass: VTYCommand [
    <comment: 'I represent a vty command'>
    <category: 'libosmovty-st'>

    | command callback handler |

    VTYCommand class >> initWith: aCommand help: aHelp handler: aBlock[
        <category: 'creation'>
        "Create a new command. The memory is allocated permamently"

        | command |
        command := Vty_cmd_element gcNew.
        command name value: aCommand.
        command doc value: aHelp.

        ^ (self new)
                command: command;
                handler: aBlock;
                yourself.
    ]

    handleCommand: aCmd vty: aVty argc: aArgc argv: aArgv [
        | str strings |

        str := aArgv castTo: CStringType.
        strings := OrderedCollection new.

        0 to: (aArgc - 1) do: [ :each |
            strings add: (str at: each).
        ].

        ^ handler value: aVty value: strings.
    ]

    handler: aBlock [
        <category: 'private'>
        handler := aBlock.
    ]

    command: aCommand [
        <category: 'private'>

        "We need to know when we are removed as we will segfault soon"
        self addToBeFinalized.

        command := aCommand.
        callback := CCallbackDescriptor
                        for: [ :cmd :vty :argc :argv |
                            self handleCommand: cmd vty: vty argc: argc argv: argv.
                        ]
                        returning: #int
                        withArgs: #(#cObject #cObject #int #cObject).
        command func value: callback.
    ]

    command [
        <category: 'command'>
        ^ command
    ]

    finalize [
        self error: 'The command can not be removed. A crash will happen soon.'
    ]

    install_ve [
        <category: 'install'>
        OSMOVTY commands add: self.
        OSMOVTY install_element_ve: command.
    ]

    install: aNode [
        <category: 'install'>
        OSMOVTY commands add: self.
        OSMOVTY install_element: aNode cmd: command.
    ]
]

Eval [
    OSMOVTY initialize.
]