/
mqiPCF.go
325 lines (282 loc) · 8.73 KB
/
mqiPCF.go
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
package ibmmq
/*
Copyright (c) IBM Corporation 2016,2019
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
Mark Taylor - Initial Contribution
*/
/*
#include <stdlib.h>
#include <cmqc.h>
#include <cmqcfc.h>
*/
import "C"
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"runtime/debug"
"strings"
)
/*
MQCFH is a structure containing the MQ PCF Header fields
*/
type MQCFH struct {
Type int32
StrucLength int32
Version int32
Command int32
MsgSeqNumber int32
Control int32
CompCode int32
Reason int32
ParameterCount int32
}
/*
PCFParameter is a structure containing the data associated with
various types of PCF element. Use the Type field to decide which
of the data fields is relevant.
*/
type PCFParameter struct {
Type int32
Parameter int32
Int64Value []int64 // Always store as 64; cast to 32 when needed
String []string
CodedCharSetId int32
ParameterCount int32
GroupList []*PCFParameter
strucLength int32 // Do not need to expose these
stringLength int32 // lengths
}
/*
NewMQCFH returns a PCF Command Header structure with correct initialisation
*/
func NewMQCFH() *MQCFH {
cfh := new(MQCFH)
cfh.Type = C.MQCFT_COMMAND
cfh.StrucLength = C.MQCFH_STRUC_LENGTH
cfh.Version = C.MQCFH_VERSION_1
cfh.Command = C.MQCMD_NONE
cfh.MsgSeqNumber = 1
cfh.Control = C.MQCFC_LAST
cfh.CompCode = C.MQCC_OK
cfh.Reason = C.MQRC_NONE
cfh.ParameterCount = 0
return cfh
}
/*
Bytes serialises an MQCFH structure as if it were the corresponding C structure
*/
func (cfh *MQCFH) Bytes() []byte {
buf := make([]byte, cfh.StrucLength)
offset := 0
endian.PutUint32(buf[offset:], uint32(cfh.Type))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.StrucLength))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.Version))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.Command))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.MsgSeqNumber))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.Control))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.CompCode))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.Reason))
offset += 4
endian.PutUint32(buf[offset:], uint32(cfh.ParameterCount))
offset += 4
return buf
}
/*
Bytes serialises a PCFParameter into the C structure
corresponding to its type.
TODO: Only a subset of the PCF
parameter types are handled here - those needed for
command queries. Other types could be added if
necessary later.
*/
func (p *PCFParameter) Bytes() []byte {
var buf []byte
switch p.Type {
case C.MQCFT_INTEGER:
buf = make([]byte, C.MQCFIN_STRUC_LENGTH)
offset := 0
endian.PutUint32(buf[offset:], uint32(p.Type))
offset += 4
endian.PutUint32(buf[offset:], uint32(len(buf)))
offset += 4
endian.PutUint32(buf[offset:], uint32(p.Parameter))
offset += 4
endian.PutUint32(buf[offset:], uint32(p.Int64Value[0]))
offset += 4
case C.MQCFT_INTEGER_LIST:
l := len(p.Int64Value)
buf = make([]byte, C.MQCFIL_STRUC_LENGTH_FIXED+4*l)
offset := 0
endian.PutUint32(buf[offset:], uint32(p.Type))
offset += 4
endian.PutUint32(buf[offset:], uint32(len(buf)))
offset += 4
endian.PutUint32(buf[offset:], uint32(p.Parameter))
offset += 4
endian.PutUint32(buf[offset:], uint32(l))
offset += 4
for i := 0; i < l; i++ {
endian.PutUint32(buf[offset:], uint32(p.Int64Value[i]))
offset += 4
}
case C.MQCFT_STRING:
buf = make([]byte, C.MQCFST_STRUC_LENGTH_FIXED+roundTo4(int32(len(p.String[0]))))
offset := 0
endian.PutUint32(buf[offset:], uint32(p.Type))
offset += 4
endian.PutUint32(buf[offset:], uint32(len(buf)))
offset += 4
endian.PutUint32(buf[offset:], uint32(p.Parameter))
offset += 4
endian.PutUint32(buf[offset:], uint32(C.MQCCSI_DEFAULT))
offset += 4
endian.PutUint32(buf[offset:], uint32(len(p.String[0])))
offset += 4
copy(buf[offset:], []byte(p.String[0]))
default:
fmt.Printf("mqiPCF.go: Trying to serialise PCF parameter. Unknown PCF type %d\n", p.Type)
}
return buf
}
/*
ReadPCFHeader extracts the MQCFH from an MQ message
*/
func ReadPCFHeader(buf []byte) (*MQCFH, int) {
fullLen := len(buf)
if fullLen < C.MQCFH_STRUC_LENGTH {
return nil, 0
}
cfh := new(MQCFH)
p := bytes.NewBuffer(buf)
binary.Read(p, endian, &cfh.Type)
binary.Read(p, endian, &cfh.StrucLength)
binary.Read(p, endian, &cfh.Version)
binary.Read(p, endian, &cfh.Command)
binary.Read(p, endian, &cfh.MsgSeqNumber)
binary.Read(p, endian, &cfh.Control)
binary.Read(p, endian, &cfh.CompCode)
binary.Read(p, endian, &cfh.Reason)
binary.Read(p, endian, &cfh.ParameterCount)
bytesRead := fullLen - p.Len()
return cfh, bytesRead
}
/*
ReadPCFParameter extracts the next PCF parameter element from an
MQ message.
*/
func ReadPCFParameter(buf []byte) (*PCFParameter, int) {
var i32 int32
var i64 int64
var mqlong int32
var count int32
pcfParm := new(PCFParameter)
fullLen := len(buf)
p := bytes.NewBuffer(buf)
binary.Read(p, endian, &pcfParm.Type)
binary.Read(p, endian, &pcfParm.strucLength)
switch pcfParm.Type {
// There are more PCF element types but the monitoring packages only
// needed a subset. We can add the others later if necessary.
case C.MQCFT_INTEGER:
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &i32)
pcfParm.Int64Value = append(pcfParm.Int64Value, int64(i32))
case C.MQCFT_INTEGER_LIST:
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &count)
for i := 0; i < int(count); i++ {
binary.Read(p, endian, &i32)
pcfParm.Int64Value = append(pcfParm.Int64Value, int64(i32))
}
case C.MQCFT_INTEGER64:
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &mqlong) // Used for alignment
binary.Read(p, endian, &i64)
pcfParm.Int64Value = append(pcfParm.Int64Value, i64)
case C.MQCFT_INTEGER64_LIST:
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &count)
for i := 0; i < int(count); i++ {
binary.Read(p, endian, &i64)
pcfParm.Int64Value = append(pcfParm.Int64Value, i64)
}
case C.MQCFT_STRING:
offset := int32(C.MQCFST_STRUC_LENGTH_FIXED)
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &pcfParm.CodedCharSetId)
binary.Read(p, endian, &pcfParm.stringLength)
s := string(buf[offset : pcfParm.stringLength+offset])
s = trimToNull(s)
pcfParm.String = append(pcfParm.String, s)
p.Next(int(pcfParm.strucLength - offset))
case C.MQCFT_STRING_LIST:
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &pcfParm.CodedCharSetId)
binary.Read(p, endian, &count)
binary.Read(p, endian, &pcfParm.stringLength)
for i := 0; i < int(count); i++ {
offset := C.MQCFSL_STRUC_LENGTH_FIXED + i*int(pcfParm.stringLength)
s := string(buf[offset : int(pcfParm.stringLength)+offset])
s = trimToNull(s)
pcfParm.String = append(pcfParm.String, s)
}
p.Next(int(pcfParm.strucLength - C.MQCFSL_STRUC_LENGTH_FIXED))
case C.MQCFT_GROUP:
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &pcfParm.ParameterCount)
case C.MQCFT_BYTE_STRING:
// The byte string is converted to a hex string as that's how
// we expect to use it in reporting
offset := int32(C.MQCFBS_STRUC_LENGTH_FIXED)
binary.Read(p, endian, &pcfParm.Parameter)
binary.Read(p, endian, &pcfParm.stringLength)
s := hex.EncodeToString(buf[offset : pcfParm.stringLength+offset])
pcfParm.String = append(pcfParm.String, s)
p.Next(int(pcfParm.strucLength - offset))
default:
// This should not happen, but if it does then dump various pieces of
// debug information that might help solve the problem.
// TODO: Put this in something like an environment variable control option
localerr := fmt.Errorf("mqiPCF.go: Unknown PCF type %d", pcfParm.Type)
fmt.Println("Error: ", localerr)
fmt.Println("Buffer: ", buf)
debug.PrintStack()
// After dumping the stack, we will try to carry on regardless.
// Skip the remains of this structure, assuming it really is
// PCF and we just don't know how to process the element type
p.Next(int(pcfParm.strucLength - 8))
}
bytesRead := fullLen - p.Len()
return pcfParm, bytesRead
}
func roundTo4(u int32) int32 {
return ((u) + ((4 - ((u) % 4)) % 4))
}
func trimToNull(s string) string {
var rc string
i := strings.IndexByte(s, 0)
if i == -1 {
rc = s
} else {
rc = s[0:i]
}
return strings.TrimSpace(rc)
}