title: How do I control the size of protocol header/message in Scapy?
body: <p>I'm trying to create the ZMTP protocol for Scapy but can't find how to effectively pad a field and the packet up to a certain byte size.</p>
  
  <p>I've tried creating the Greeting message described here, <a href="https://www.codeproject.com/Articles/863889/ZeroMQ-Diving-into-the-Wire" rel="nofollow noreferrer">https://www.codeproject.com/Articles/863889/ZeroMQ-Diving-into-the-Wire</a>, as a new protocol in Scapy:</p>
  
  <pre><code>class ZmtpGreeting(Packet):
      name = "ZMTP Greeting"
      fields_desc = [
          XByteField("signature", "ff00000000000000117f"),
          XByteField("version_major", "03"),
          XByteField("version_minor", "00"),
          PadField(XByteField("security_mechanism", "4e554c4c"), 20, padwith="\x00"),
          XByteField("as_server", "00"),
      ]
  </code></pre>
  
  <p><strong>Problem 1:</strong>
  The above implementation doesn't seem to include the padding up to 20 bytes for the security_mechanism field:</p>
  
  <pre><code>###[ IP ]###
    version= 4
    ihl= None
    tos= 0x0
    len= None
    id= 1
    flags=
    frag= 0
    ttl= 64
    proto= tcp
    chksum= None
    src= 1.1.1.2
    dst= 1.1.1.1
    \options\
  ###[ TCP ]###
       sport= 1111
       dport= 2222
       seq= 1
       ack= 1
       dataofs= None
       reserved= 0
       flags= PA
       window= 8192
       chksum= None
       urgptr= 0
       options= []
  ###[ ZMTP Greeting ]###
          signature= ff00000000000000117f
          version_major= 03
          version_minor= 00
          security_mechanism= 4e554c4c
          as_server= 00
  </code></pre>
  
  <p>This entire packet should also be padded to 64 octets and I'm not sure how to include that in the protocol (or as padding outside of it).</p>
  
  <p><strong>Problem 2:</strong>
  I am unable to use raw(..) or len(..) because they fail with the following error:</p>
  
  <pre><code>fields.py in addfield(self, pkt, s, val)
      138         `pkt`) to the raw string packet `s`, and return the new string packet.
      139         """
  --&gt; 140         return s + struct.pack(self.fmt, self.i2m(pkt, val))
      141
      142     def getfield(self, pkt, s):
  
  error: required argument is not an integer
  </code></pre>
  
  <p><strong>Problem 3:</strong>
  This is in its own python file under layers, but only works when I directly paste it into the Scapy console.</p>
  
  <p>I've been looking through the documentation for Scapy but there haven't found out how to achieve this. Thanks in advance!</p>