github.com/pion/webrtc/v4@v4.0.1/networktype.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package webrtc 5 6 import ( 7 "fmt" 8 9 "github.com/pion/ice/v4" 10 ) 11 12 func supportedNetworkTypes() []NetworkType { 13 return []NetworkType{ 14 NetworkTypeUDP4, 15 NetworkTypeUDP6, 16 // NetworkTypeTCP4, // Not supported yet 17 // NetworkTypeTCP6, // Not supported yet 18 } 19 } 20 21 // NetworkType represents the type of network 22 type NetworkType int 23 24 const ( 25 // NetworkTypeUnknown is the enum's zero-value 26 NetworkTypeUnknown NetworkType = iota 27 28 // NetworkTypeUDP4 indicates UDP over IPv4. 29 NetworkTypeUDP4 30 31 // NetworkTypeUDP6 indicates UDP over IPv6. 32 NetworkTypeUDP6 33 34 // NetworkTypeTCP4 indicates TCP over IPv4. 35 NetworkTypeTCP4 36 37 // NetworkTypeTCP6 indicates TCP over IPv6. 38 NetworkTypeTCP6 39 ) 40 41 // This is done this way because of a linter. 42 const ( 43 networkTypeUDP4Str = "udp4" 44 networkTypeUDP6Str = "udp6" 45 networkTypeTCP4Str = "tcp4" 46 networkTypeTCP6Str = "tcp6" 47 ) 48 49 func (t NetworkType) String() string { 50 switch t { 51 case NetworkTypeUDP4: 52 return networkTypeUDP4Str 53 case NetworkTypeUDP6: 54 return networkTypeUDP6Str 55 case NetworkTypeTCP4: 56 return networkTypeTCP4Str 57 case NetworkTypeTCP6: 58 return networkTypeTCP6Str 59 default: 60 return ErrUnknownType.Error() 61 } 62 } 63 64 // Protocol returns udp or tcp 65 func (t NetworkType) Protocol() string { 66 switch t { 67 case NetworkTypeUDP4: 68 return "udp" 69 case NetworkTypeUDP6: 70 return "udp" 71 case NetworkTypeTCP4: 72 return "tcp" 73 case NetworkTypeTCP6: 74 return "tcp" 75 default: 76 return ErrUnknownType.Error() 77 } 78 } 79 80 // NewNetworkType allows create network type from string 81 // It will be useful for getting custom network types from external config. 82 func NewNetworkType(raw string) (NetworkType, error) { 83 switch raw { 84 case networkTypeUDP4Str: 85 return NetworkTypeUDP4, nil 86 case networkTypeUDP6Str: 87 return NetworkTypeUDP6, nil 88 case networkTypeTCP4Str: 89 return NetworkTypeTCP4, nil 90 case networkTypeTCP6Str: 91 return NetworkTypeTCP6, nil 92 default: 93 return NetworkTypeUnknown, fmt.Errorf("%w: %s", errNetworkTypeUnknown, raw) 94 } 95 } 96 97 func getNetworkType(iceNetworkType ice.NetworkType) (NetworkType, error) { 98 switch iceNetworkType { 99 case ice.NetworkTypeUDP4: 100 return NetworkTypeUDP4, nil 101 case ice.NetworkTypeUDP6: 102 return NetworkTypeUDP6, nil 103 case ice.NetworkTypeTCP4: 104 return NetworkTypeTCP4, nil 105 case ice.NetworkTypeTCP6: 106 return NetworkTypeTCP6, nil 107 default: 108 return NetworkTypeUnknown, fmt.Errorf("%w: %s", errNetworkTypeUnknown, iceNetworkType.String()) 109 } 110 }