Skip to main content

A package for creating, parsing, and serializing STUN (RFC8489) packets

Project description

Python Stun Protocol

A python module for creating and parsing STUN packets.

Message

The Message class, defined in message.py, is the main interface to the module. To parse a packet from a buffer, simply do:

from stun_protocol.message import Message

# get a buffer from somewhere (perhaps received over a network)
buffer = ...

# create a message
message = Message.create(buffer)

Stun messages have 4 main components.

  1. The message type, composed of the class and method
    1. The message type class (message.message_class)
    2. The message type method (message.message_method)
  2. The message length (message.length)
  3. The message transaction id (message.transaction_id)
  4. The message attributes (message.attributes)

When creating a message from a buffer, all fields are automatically populated as the buffer is parsed. When constructing a message object directly, the caller must provide the message class and method. The length is dynamically calculated by the message object as needed. The transaction id is optional, a random transaction id will be created if one is not provided. The attributes are also optional and may be added later using the message.add_attribute() function. For example:

from stun_protocol.message import Message
from stun_protocol.attribute import UsernameAttribute

# create a message object with a random transaction id and no attributes
m = Message(MessageClass.REQUEST, MessageMethod.BINDING)

# add a username attribute, note that attributes are defined fully below
m.add_attribute(UsernameAttribute(b'username'))

# some attributes are best added via the explicit helper functions as they have somewhat complex rules
m.add_fingerprint_attribute()

To turn a STUN message into a buffer suitable for sending over a network, use the pack() method:

from stun_protocol.message import Message

m = Message(MessageClass.REQUEST, MessageMethod.BINDING)
buffer = m.pack()

Attributes

Attributes are defined in attribute.py. Different attributes have different rules for the valid values that may be contained in those attributes. All such rules are taken from https://datatracker.ietf.org/doc/html/rfc8489#section-14. When creating attributes, be sure to follow the restrictions for that attribute. For example, the UserhashAttribute requires a bytes value with length 32. Any other length will trigger an exception.

from stun_protocol.attribute import UserhashAttribute

# Below will cause the following exception
# ValueError: UserhashAttribute requires a value of length >= 32 and <= 32 bytes, found 3
attr = UserhashAttribute(b'foo')

Attributes, like messages can be created from buffers. If you know the expected attribute type then you can call create() directly on that type.

from stun_protocol.attribute import UserhashAttribute

# get a buffer from somewhere (perhaps received over a network)
buffer = ...

attr = UserhashAttribute.create(buffer)

More commonly you will not know the type of the attribute, in which case you can use the module level create() function which will return the correct attribute object based on the buffer.

from stun_protocol.attribute import create

# get a buffer from somewhere (perhaps received over a network)
buffer = ...

attr = create(buffer)

There are various base classes for attributes starting with Attribute which is an abstract base class (ABC). The module defines types for all attributes in the RFC, but the base classes allow for creating custom attributes as needed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stun-protocol-0.0.4.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

stun_protocol-0.0.4-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file stun-protocol-0.0.4.tar.gz.

File metadata

  • Download URL: stun-protocol-0.0.4.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for stun-protocol-0.0.4.tar.gz
Algorithm Hash digest
SHA256 23cdad4cdcc9d982bb9ec56493273bf8234a1fa19f9f368cc1a14527b5604c4a
MD5 d56ba3159178cabf6e47e42b0022582c
BLAKE2b-256 306b254e4d3571937e088d1aae869a2d49439f15f4c55b988b0d1c4fdfddd487

See more details on using hashes here.

File details

Details for the file stun_protocol-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: stun_protocol-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for stun_protocol-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bde3dfa326bf4a1c63337d74b9e890a9411666db58dbf4fd68d25acaa660a18d
MD5 4d7ec098e955279484a459bd0f664b2d
BLAKE2b-256 d2a4ae361b9b25c069bdeee4526c75f1e386c0fc463fd5ece104c54a263a5b2e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page