forked from fiorix/go-diameter
/
metadata.go
53 lines (43 loc) · 1.47 KB
/
metadata.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
// Copyright 2013-2015 go-diameter authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package smpeer
import (
"golang.org/x/net/context"
"github.com/andrewostroumov/go-diameter/v4/diam/datatype"
"github.com/andrewostroumov/go-diameter/v4/diam/sm/smparser"
)
type key int
const metadataKey key = 0
// Metadata contains information about a diameter peer, acquired
// during the CER/CEA handshake.
type Metadata struct {
OriginHost datatype.DiameterIdentity
OriginRealm datatype.DiameterIdentity
Applications []uint32 // Acct or Auth IDs supported by the peer.
}
// FromCER creates a Metadata object from data in the CER.
func FromCER(cer *smparser.CER) *Metadata {
return &Metadata{
OriginHost: cer.OriginHost,
OriginRealm: cer.OriginRealm,
Applications: cer.Applications(),
}
}
// FromCEA creates a Metadata object from data in the CEA.
func FromCEA(cea *smparser.CEA) *Metadata {
return &Metadata{
OriginHost: cea.OriginHost,
OriginRealm: cea.OriginRealm,
Applications: cea.Applications(),
}
}
// NewContext returns a new Context that carries a Metadata object.
func NewContext(ctx context.Context, metadata *Metadata) context.Context {
return context.WithValue(ctx, metadataKey, metadata)
}
// FromContext extracts a Metadata object from the context.
func FromContext(ctx context.Context) (*Metadata, bool) {
meta, ok := ctx.Value(metadataKey).(*Metadata)
return meta, ok
}