@@ -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,39 @@ 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
+ addr := target .String ()
209
+ var sport string
210
+ if strings .Contains (addr , "tcp" ) {
211
+ s , err := target .ValueForProtocol (ma .P_TCP )
212
+ if err != nil {
213
+ return err
214
+ }
215
+ sport = s
216
+ } else if strings .Contains (addr , "udp" ) {
217
+ s , err := target .ValueForProtocol (ma .P_UDP )
218
+ if err != nil {
219
+ return err
220
+ }
221
+ sport = s
222
+ } else {
223
+ return fmt .Errorf ("address does not contain tcp or udp protocol" )
224
+ }
225
+
226
+ port , err := strconv .Atoi (sport )
227
+ if err != nil {
228
+ return err
229
+ }
230
+
231
+ if port == 0 {
232
+ return fmt .Errorf ("port can't be 0" )
233
+ }
234
+
235
+ return nil
236
+ }
237
+
199
238
// forwardRemote forwards libp2p service connections to a manet address
200
239
func forwardRemote (ctx context.Context , p * p2p.P2P , proto protocol.ID , target ma.Multiaddr ) error {
201
240
// TODO: return some info
0 commit comments