| 1 |
package sasl |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"strconv" |
| 6 |
|
| 7 |
"golang.org/x/net/context" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
type _key int |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
const ( |
| 17 |
statusKey _key = iota |
| 18 |
bindingAddressKey |
| 19 |
bindingPortKey |
| 20 |
) |
| 21 |
|
| 22 |
func withStatus(ctx context.Context, s statusType) context.Context { |
| 23 |
return context.WithValue(ctx, statusKey, s) |
| 24 |
} |
| 25 |
|
| 26 |
func statusFrom(ctx context.Context) statusType { |
| 27 |
s, ok := ctx.Value(statusKey).(statusType) |
| 28 |
if !ok { |
| 29 |
panic("missing status in context") |
| 30 |
} |
| 31 |
return s |
| 32 |
} |
| 33 |
|
| 34 |
func WithBindingAddress(ctx context.Context, address net.IP) context.Context { |
| 35 |
return context.WithValue(ctx, bindingAddressKey, address) |
| 36 |
} |
| 37 |
|
| 38 |
func BindingAddressFrom(ctx context.Context) net.IP { |
| 39 |
obj := ctx.Value(bindingAddressKey) |
| 40 |
if addr, ok := obj.(net.IP); ok { |
| 41 |
return addr |
| 42 |
} else { |
| 43 |
return nil |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func WithBindingPort(ctx context.Context, port uint16) context.Context { |
| 48 |
return context.WithValue(ctx, bindingPortKey, port) |
| 49 |
} |
| 50 |
|
| 51 |
func BindingPortFrom(ctx context.Context) string { |
| 52 |
if port, ok := ctx.Value(bindingPortKey).(uint16); ok { |
| 53 |
return strconv.Itoa(int(port)) |
| 54 |
} |
| 55 |
return "0" |
| 56 |
} |