|
| 1 | +// Copyright 2018 The go-hep Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package client // import "go-hep.org/x/hep/xrootd/client" |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + |
| 10 | + xrdproto "go-hep.org/x/hep/xrootd/protocol" |
| 11 | + "go-hep.org/x/hep/xrootd/protocol/protocol" |
| 12 | +) |
| 13 | + |
| 14 | +// ProtocolInfo is a response for the `Protocol` request. See details in the xrootd protocol specification. |
| 15 | +type ProtocolInfo struct { |
| 16 | + BinaryProtocolVersion int32 |
| 17 | + ServerType xrdproto.ServerType |
| 18 | + IsManager bool |
| 19 | + IsServer bool |
| 20 | + IsMeta bool |
| 21 | + IsProxy bool |
| 22 | + IsSupervisor bool |
| 23 | + SecurityVersion byte |
| 24 | + ForceSecurity bool |
| 25 | + SecurityLevel protocol.SecurityLevel |
| 26 | + SecurityOverrides []protocol.SecurityOverride |
| 27 | +} |
| 28 | + |
| 29 | +// Protocol obtains the protocol version number, type of the server and security information, such as: |
| 30 | +// the security version, the security options, the security level, and the list of alterations |
| 31 | +// needed to the specified predefined security level. |
| 32 | +func (client *Client) Protocol(ctx context.Context) (ProtocolInfo, error) { |
| 33 | + resp, err := client.call(ctx, protocol.RequestID, protocol.NewRequest(client.protocolVersion, true)) |
| 34 | + if err != nil { |
| 35 | + return ProtocolInfo{}, err |
| 36 | + } |
| 37 | + |
| 38 | + var generalResp protocol.GeneralResponse |
| 39 | + |
| 40 | + if err = xrdproto.Unmarshal(resp, &generalResp); err != nil { |
| 41 | + return ProtocolInfo{}, err |
| 42 | + } |
| 43 | + |
| 44 | + var securityInfo *protocol.SecurityInfo |
| 45 | + if len(resp) > protocol.GeneralResponseLength { |
| 46 | + securityInfo = &protocol.SecurityInfo{} |
| 47 | + err = xrdproto.Unmarshal(resp[protocol.GeneralResponseLength:], securityInfo) |
| 48 | + if err != nil { |
| 49 | + return ProtocolInfo{}, err |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + var info = ProtocolInfo{ |
| 54 | + BinaryProtocolVersion: generalResp.BinaryProtocolVersion, |
| 55 | + ServerType: extractServerType(generalResp.Flags), |
| 56 | + |
| 57 | + // TODO: implement bit-encoded fields support in Unmarshal. |
| 58 | + IsManager: generalResp.Flags&protocol.IsManager != 0, |
| 59 | + IsServer: generalResp.Flags&protocol.IsServer != 0, |
| 60 | + IsMeta: generalResp.Flags&protocol.IsMeta != 0, |
| 61 | + IsProxy: generalResp.Flags&protocol.IsProxy != 0, |
| 62 | + IsSupervisor: generalResp.Flags&protocol.IsSupervisor != 0, |
| 63 | + } |
| 64 | + |
| 65 | + if securityInfo != nil { |
| 66 | + info.SecurityVersion = securityInfo.SecurityVersion |
| 67 | + info.ForceSecurity = securityInfo.SecurityOptions&protocol.ForceSecurity != 0 |
| 68 | + info.SecurityLevel = securityInfo.SecurityLevel |
| 69 | + |
| 70 | + if securityInfo.SecurityOverridesSize > 0 { |
| 71 | + info.SecurityOverrides = make([]protocol.SecurityOverride, securityInfo.SecurityOverridesSize) |
| 72 | + |
| 73 | + const offset = protocol.GeneralResponseLength + protocol.SecurityInfoLength |
| 74 | + const elementSize = protocol.SecurityOverrideLength |
| 75 | + |
| 76 | + for i := byte(0); i < securityInfo.SecurityOverridesSize; i++ { |
| 77 | + err = xrdproto.Unmarshal(resp[offset+elementSize*int(i):], &info.SecurityOverrides[i]) |
| 78 | + if err != nil { |
| 79 | + return ProtocolInfo{}, err |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return info, nil |
| 86 | +} |
| 87 | + |
| 88 | +func extractServerType(flags protocol.Flags) xrdproto.ServerType { |
| 89 | + if int32(flags)&int32(xrdproto.DataServer) != 0 { |
| 90 | + return xrdproto.DataServer |
| 91 | + } |
| 92 | + return xrdproto.LoadBalancingServer |
| 93 | +} |
0 commit comments