# What about flexible ports?

**URL:** https://discuss.elastic.co/t/what-about-flexible-ports/54940
**Category:** Beats
**Tags:** packetbeat
**Created:** [July 7, 2016, 11:53am UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940 "2016-07-07T11:53:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lindsayshow](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@lindsayshow](https://discuss.elastic.co/u/lindsayshow)
#### Post date: [July 7, 2016, 11:53am UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940/1 "2016-07-07T11:53:47Z")

</div>

I have developed a new protocol in packetbeat.  
But in the packetbeat.yml file,I found we must set the ports[] item? cause，plugin interface has a function named GetPorts（）.

---

<div class="post-metadata">

### Author: ![steffens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steffens/32/79630_2.png) [@steffens](https://discuss.elastic.co/u/steffens)
#### Post date: [July 7, 2016, 12:27pm UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940/2 "2016-07-07T12:27:36Z")

</div>

every packet requires lookup based on port-number. The lookup table is pre-computed on startup based on GetPorts().

---

<div class="post-metadata">

### Author: ![lindsayshow](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@lindsayshow](https://discuss.elastic.co/u/lindsayshow)
#### Post date: [July 8, 2016, 1:23am UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940/3 "2016-07-08T01:23:43Z")

</div>

so,I can't listen all ports? But，some protocols don't have stabled ports,so I can't set the item in the packetbeat.yml file.

---

<div class="post-metadata">

### Author: ![steffens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steffens/32/79630_2.png) [@steffens](https://discuss.elastic.co/u/steffens)
#### Post date: [July 8, 2016, 11:31am UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940/4 "2016-07-08T11:31:18Z")

</div>

currently GetPorts() requires you to return an array of ports to listen on. The plugin is totally free to return a huge array with some very big port-range.

e.g.

define port-range in your config

```auto
type PortRange [2]uint16

type Config struct {
   ...
   Ports PortRange
}

```

And use `PortRange` to return a complete range via GetPorts:

```auto
func (p *plugin) GetPorts() []uint16 {
    range := p.Config.PortRange
    first := range[0]
    last := range[1]
    ports := make([]uint16, 0, last - first + 1)
    for i := first; i <= last; i++ {
        ports = append(ports, i)
    }
    return ports
}

```

When configuring your plugin, make sure the config does not overlap ports with other protocols, as packetbeat TCP layer can not tell where to route packets to if similar ports are used between multiple protocol plugins.

One can even extend the ports config idea like:

```auto
type PortsConfig struct {
    Range *PortRangeConfig `config:"range"`
    List *[]uint16 `config:"list"`
}

type Config struct {
    Ports PortsConfig `config:"ports"`
}

```

Using this structure to configure your ports one can configure a protocol like:

```auto
packetbeat.protocols.myproto:
    ports.range: [1000, 2000]

packetbeat.protocols.another_proto:
    ports.list: [1,2,3,4,5]

```

---

<div class="post-metadata">

### Author: ![lindsayshow](https://avatars.discourse-cdn.com/v4/letter/l/5daacb/32.png) [@lindsayshow](https://discuss.elastic.co/u/lindsayshow)
#### Post date: [July 11, 2016, 2:01am UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940/5 "2016-07-11T02:01:45Z")

</div>

Thx!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 28, 2016, 11:53am UTC](https://discuss.elastic.co/t/what-about-flexible-ports/54940/6 "2016-07-28T11:53:49Z")

</div>

This topic was automatically closed after 21 days. New replies are no longer allowed.
