github.com/pion/webrtc/v3@v3.2.24/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/v2" 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 // NetworkTypeUDP4 indicates UDP over IPv4. 26 NetworkTypeUDP4 NetworkType = iota + 1 27 28 // NetworkTypeUDP6 indicates UDP over IPv6. 29 NetworkTypeUDP6 30 31 // NetworkTypeTCP4 indicates TCP over IPv4. 32 NetworkTypeTCP4 33 34 // NetworkTypeTCP6 indicates TCP over IPv6. 35 NetworkTypeTCP6 36 ) 37 38 // This is done this way because of a linter. 39 const ( 40 networkTypeUDP4Str = "udp4" 41 networkTypeUDP6Str = "udp6" 42 networkTypeTCP4Str = "tcp4" 43 networkTypeTCP6Str = "tcp6" 44 ) 45 46 func (t NetworkType) String() string { 47 switch t { 48 case NetworkTypeUDP4: 49 return networkTypeUDP4Str 50 case NetworkTypeUDP6: 51 return networkTypeUDP6Str 52 case NetworkTypeTCP4: 53 return networkTypeTCP4Str 54 case NetworkTypeTCP6: 55 return networkTypeTCP6Str 56 default: 57 return ErrUnknownType.Error() 58 } 59 } 60 61 // Protocol returns udp or tcp 62 func (t NetworkType) Protocol() string { 63 switch t { 64 case NetworkTypeUDP4: 65 return "udp" 66 case NetworkTypeUDP6: 67 return "udp" 68 case NetworkTypeTCP4: 69 return "tcp" 70 case NetworkTypeTCP6: 71 return "tcp" 72 default: 73 return ErrUnknownType.Error() 74 } 75 } 76 77 // NewNetworkType allows create network type from string 78 // It will be useful for getting custom network types from external config. 79 func NewNetworkType(raw string) (NetworkType, error) { 80 switch raw { 81 case networkTypeUDP4Str: 82 return NetworkTypeUDP4, nil 83 case networkTypeUDP6Str: 84 return NetworkTypeUDP6, nil 85 case networkTypeTCP4Str: 86 return NetworkTypeTCP4, nil 87 case networkTypeTCP6Str: 88 return NetworkTypeTCP6, nil 89 default: 90 return NetworkType(Unknown), fmt.Errorf("%w: %s", errNetworkTypeUnknown, raw) 91 } 92 } 93 94 func getNetworkType(iceNetworkType ice.NetworkType) (NetworkType, error) { 95 switch iceNetworkType { 96 case ice.NetworkTypeUDP4: 97 return NetworkTypeUDP4, nil 98 case ice.NetworkTypeUDP6: 99 return NetworkTypeUDP6, nil 100 case ice.NetworkTypeTCP4: 101 return NetworkTypeTCP4, nil 102 case ice.NetworkTypeTCP6: 103 return NetworkTypeTCP6, nil 104 default: 105 return NetworkType(Unknown), fmt.Errorf("%w: %s", errNetworkTypeUnknown, iceNetworkType.String()) 106 } 107 }