@@ -176,6 +176,12 @@ Example:
176
176
return
177
177
}
178
178
179
+ // port can't be 0
180
+ if err := checkPort (target ); err != nil {
181
+ res .SetError (err , cmdkit .ErrNormal )
182
+ return
183
+ }
184
+
179
185
allowCustom , _ , err := req .Option ("allow-custom-protocol" ).Bool ()
180
186
if err != nil {
181
187
res .SetError (err , cmdkit .ErrNormal )
@@ -196,6 +202,40 @@ Example:
196
202
},
197
203
}
198
204
205
+ // checkPort checks whether target multiaddr contains tcp or udp protocol
206
+ // and whether the port is equal to 0
207
+ func checkPort (target ma.Multiaddr ) error {
208
+ // get tcp or udp port from multiaddr
209
+ getPort := func () (string , error ) {
210
+ sport , _ := target .ValueForProtocol (ma .P_TCP )
211
+ if sport != "" {
212
+ return sport , nil
213
+ }
214
+
215
+ sport , _ = target .ValueForProtocol (ma .P_UDP )
216
+ if sport != "" {
217
+ return sport , nil
218
+ }
219
+ return "" , fmt .Errorf ("address does not contain tcp or udp protocol" )
220
+ }
221
+
222
+ sport , err := getPort ()
223
+ if err != nil {
224
+ return err
225
+ }
226
+
227
+ port , err := strconv .Atoi (sport )
228
+ if err != nil {
229
+ return err
230
+ }
231
+
232
+ if port == 0 {
233
+ return fmt .Errorf ("port can't be 0" )
234
+ }
235
+
236
+ return nil
237
+ }
238
+
199
239
// forwardRemote forwards libp2p service connections to a manet address
200
240
func forwardRemote (ctx context.Context , p * p2p.P2P , proto protocol.ID , target ma.Multiaddr ) error {
201
241
// TODO: return some info
0 commit comments