mosquitto

package module
v0.0.0-...-03d6208 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2021 License: LGPL-3.0 Imports: 7 Imported by: 0

README

Go-Mosquitto-Plugin

This module implements the mosquitto v2 plugin interface as a Go module.

The new interface offers some interesting new features like message manipulation, and a callback whether a client disconnects. This module is far from complete, but does implement some key concepts like registering for events and forcefully disconnect clients.

  • KickClientByUsername
  • KickClientByClientID
  • Publish
  • CallbackRegister
  • CallbackUnregister

This module also integrates with mosquittos internal logging mechanisms by implementing a logger that simply calls mosquitto_log_printf under the hood.

There is a test plugin in plugins/test which is only supposed to be used for exploring or testing different aspects of the interface and will not make any sense from a security perspective.

The skeleton plugin provides a minimal reference on how to create a plugin.

Plugins have to be built with go build -buildmode=c-shared

Documentation

Index

Constants

View Source
const (
	MosqAclNone        = Access(C.MOSQ_ACL_NONE)
	MosqAclRead        = Access(C.MOSQ_ACL_READ)
	MosqAclWrite       = Access(C.MOSQ_ACL_WRITE)
	MosqAclSubscribe   = Access(C.MOSQ_ACL_SUBSCRIBE)
	MosqAclUnsubscribe = Access(C.MOSQ_ACL_UNSUBSCRIBE)
)
View Source
const (
	MosqErrAuthContinue         = Error(C.MOSQ_ERR_AUTH_CONTINUE)
	MosqErrNoSubscribers        = Error(C.MOSQ_ERR_NO_SUBSCRIBERS)
	MosqErrSubExists            = Error(C.MOSQ_ERR_SUB_EXISTS)
	MosqErrConnPending          = Error(C.MOSQ_ERR_CONN_PENDING)
	MosqErrSuccess              = Error(C.MOSQ_ERR_SUCCESS)
	MosqErrNoMem                = Error(C.MOSQ_ERR_NOMEM)
	MosqErrProtocol             = Error(C.MOSQ_ERR_PROTOCOL)
	MosqErrInval                = Error(C.MOSQ_ERR_INVAL)
	MosqErrNoConn               = Error(C.MOSQ_ERR_NO_CONN)
	MosqErrConnRefused          = Error(C.MOSQ_ERR_CONN_REFUSED)
	MosqErrNotFound             = Error(C.MOSQ_ERR_NOT_FOUND)
	MosqErrConnLost             = Error(C.MOSQ_ERR_CONN_LOST)
	MosqErrTLS                  = Error(C.MOSQ_ERR_TLS)
	MosqErrPayloadSize          = Error(C.MOSQ_ERR_PAYLOAD_SIZE)
	MosqErrNotSupported         = Error(C.MOSQ_ERR_NOT_SUPPORTED)
	MosqErrAuth                 = Error(C.MOSQ_ERR_AUTH)
	MosqErrACLDenied            = Error(C.MOSQ_ERR_ACL_DENIED)
	MosqErrUnknown              = Error(C.MOSQ_ERR_UNKNOWN)
	MosqErrErrno                = Error(C.MOSQ_ERR_ERRNO)
	MosqErrEAI                  = Error(C.MOSQ_ERR_EAI)
	MosqErrProxy                = Error(C.MOSQ_ERR_PROXY)
	MosqErrPluginDefer          = Error(C.MOSQ_ERR_PLUGIN_DEFER)
	MosqErrMalformedUTF8        = Error(C.MOSQ_ERR_MALFORMED_UTF8)
	MosqErrKeepAlive            = Error(C.MOSQ_ERR_KEEPALIVE)
	MosqErrLookup               = Error(C.MOSQ_ERR_LOOKUP)
	MosqErrMalformedPacket      = Error(C.MOSQ_ERR_MALFORMED_PACKET)
	MosqErrDuplicateProperty    = Error(C.MOSQ_ERR_DUPLICATE_PROPERTY)
	MosqErrTLSHandshake         = Error(C.MOSQ_ERR_TLS_HANDSHAKE)
	MosqErrQOSNotSupported      = Error(C.MOSQ_ERR_QOS_NOT_SUPPORTED)
	MosqErrOversizePacket       = Error(C.MOSQ_ERR_OVERSIZE_PACKET)
	MosqErrOCSP                 = Error(C.MOSQ_ERR_OCSP)
	MosqErrTimeout              = Error(C.MOSQ_ERR_TIMEOUT)
	MosqErrRetainNotSupported   = Error(C.MOSQ_ERR_RETAIN_NOT_SUPPORTED)
	MosqErrTopicAliasInvalid    = Error(C.MOSQ_ERR_TOPIC_ALIAS_INVALID)
	MosqErrAdministrativeAction = Error(C.MOSQ_ERR_ADMINISTRATIVE_ACTION)
	MosqErrAlreadyExists        = Error(C.MOSQ_ERR_ALREADY_EXISTS)
)

Errors from mosquitto.h

View Source
const (
	MosqEvtReload          = Event(C.MOSQ_EVT_RELOAD)
	MosqEvtACLCheck        = Event(C.MOSQ_EVT_ACL_CHECK)
	MosqEvtBasicAuth       = Event(C.MOSQ_EVT_BASIC_AUTH)
	MosqEvtEXTAuthStart    = Event(C.MOSQ_EVT_EXT_AUTH_START)
	MosqEvtEXTAuthContinue = Event(C.MOSQ_EVT_EXT_AUTH_CONTINUE)
	MosqEvtControl         = Event(C.MOSQ_EVT_CONTROL)
	MosqEvtMessage         = Event(C.MOSQ_EVT_MESSAGE)
	MosqEvtPSKKey          = Event(C.MOSQ_EVT_PSK_KEY)
	MosqEvtTick            = Event(C.MOSQ_EVT_TICK)
	MosqEvtDisconnect      = Event(C.MOSQ_EVT_DISCONNECT)
)
View Source
const (
	ProtocolMQTT           = Protocol(C.mp_mqtt)
	ProtocolMQTTSN         = Protocol(C.mp_mqttsn)
	ProtocolMQTTWebsockets = Protocol(C.mp_websockets)
)
View Source
const (
	MQTTProtocolV31  = ProtocolVersion(C.MQTT_PROTOCOL_V31)
	MQTTProtocolV311 = ProtocolVersion(C.MQTT_PROTOCOL_V311)
	MQTTProtocolV5   = ProtocolVersion(C.MQTT_PROTOCOL_V5)
)
View Source
const (
	MosqPluginVersion = C.MOSQ_PLUGIN_VERSION
)

Variables

This section is empty.

Functions

func CallbackRegister

func CallbackRegister(pluginID PluginID, event Event, cb interface{}, eventData interface{}) error

CallbackRegister * Register a callback for an event. * * Parameters: * pluginID - the plugin identifier, as provided by <mosquitto_plugin_init>. * event - the event to register a callback for. Can be one of: * * MosqEvtReload * * MosqEvtACLCheck * * MosqEvtBasicAuth * * MosqEvtEXTAuthStart * * MosqEvtEXTAuthContinue * * MosqEvtControl * * MosqEvtMessage * * MosqEvtPSKKey * * MosqEvtTick * * MosqEvtDisconnect * cb - the callback function * eventData - event specific data * * Returns: * nil - on success * MosqErrInval - if cb_func is NULL * MosqErrAlreadyExists - if cb_func has already been registered for this event * MosqErrNotSupported - if the event is not supported

func CallbackUnregister

func CallbackUnregister(pluginID PluginID, event Event, cb interface{}, eventData interface{}) error

CallbackUnregister * Unregister a previously registered callback function. * * Parameters: * pluginID - the plugin identifier, as provided by <mosquitto_plugin_init>. * event - the event to register a callback for. Can be one of: * * MosqEvtReload * * MosqEvtACLCheck * * MosqEvtBasicAuth * * MosqEvtEXTAuthStart * * MosqEvtEXTAuthContinue * * MosqEvtControl * * MosqEvtMessage * * MosqEvtPSKKey * * MosqEvtTick * * MosqEvtDisconnect * cb - the callback function * eventData - event specific data * * Returns: * nil - on success * MosqErrInval - if cb_func is NULL * MosqErrNotFound - if cb_func was not registered for this event * MosqErrNotSupported - if the event is not supported

func KickClientByClientID

func KickClientByClientID(clientID string, withWill bool) int

KickClientByClientID * Forcefully disconnect a client from the broker. * * If clientid != "", then the client with the matching client id is * disconnected from the broker. * If clientid == "", then all clients are disconnected from the broker. * * If with_will == true, then if the client has a Last Will and Testament * defined then this will be sent. If false, the LWT will not be sent.

func KickClientByUsername

func KickClientByUsername(username string, withWill bool) int

KickClientByUsername * Forcefully disconnect a client from the broker. * * If username != "", then all clients with a matching username are kicked * from the broker. * If username == "", then all clients that do not have a username are * kicked. * * If with_will == true, then if the client has a Last Will and Testament * defined then this will be sent. If false, the LWT will not be sent.

func Publish

func Publish(clientID, topic string, payload []byte, qos int, retain bool) error

Publish * Publish a message from within a plugin. * * This function allows a plugin to publish a message. Messages published in * this way are treated as coming from the broker and so will not be passed to * `mosquitto_auth_acl_check(, MOSQ_ACL_WRITE, , )` for checking. Read access * will be enforced as normal for individual clients when they are due to * receive the message. * * It can be used to send messages to all clients that have a matching * subscription, or to a single client whether or not it has a matching * subscription. * * Parameters: * clientID - optional string. If set to "", the message is delivered to all * clients. If non-empty, the message is delivered only to the * client with the corresponding client id. If the client id * specified is not connected, the message will be dropped. * topic - message topic. * payload - payload bytes. * qos - message QoS to use. * retain - should retain be set on the message. This does not apply if * clientid is non-NULL. * properties - MQTT v5 properties to attach to the message. If the function * returns success, then properties is owned by the broker and * will be freed at a later point. * * Returns: * nil - on success * MosqErrInval - if topic is NULL, if payloadlen < 0, if payloadlen > 0 * and payload is NULL, if qos is not 0, 1, or 2.

func RegisterPlugin

func RegisterPlugin(p Plugin)

RegisterPlugin must be called in an init function in the plugin.

func TopicMatchesSub

func TopicMatchesSub(topic, subscription string) bool

TopicMatchesSub * Check whether a topic matches a subscription. * * For example: * * foo/bar would match the subscription foo/# or +/bar * non/matching would not match the subscription non/+/+ * * Parameters: * topic - topic to check. * subscription - subscription string to check topic against. * * Returns: * true - if topic matches subscription * false - if topic doesn't match subscription, or if invalid parameters

Types

type Access

type Access int

func (Access) String

func (a Access) String() string

type Client

type Client uintptr

func (Client) Address

func (c Client) Address() string

func (Client) CleanSession

func (c Client) CleanSession() bool

func (Client) ClientID

func (c Client) ClientID() string

func (Client) KeepAlive

func (c Client) KeepAlive() int

func (Client) Protocol

func (c Client) Protocol() Protocol

func (Client) ProtocolVersion

func (c Client) ProtocolVersion() ProtocolVersion

func (Client) SetUsername

func (c Client) SetUsername(name string) error

func (Client) SubscriptionCount

func (c Client) SubscriptionCount() int

func (Client) Username

func (c Client) Username() string

type Error

type Error int

func (Error) Error

func (e Error) Error() string

func (Error) String

func (e Error) String() string

type Event

type Event int

func (Event) String

func (e Event) String() string

type EvtAclCheck

type EvtAclCheck uintptr

func (EvtAclCheck) Access

func (e EvtAclCheck) Access() Access

func (EvtAclCheck) Client

func (e EvtAclCheck) Client() Client

ACL Check event

func (EvtAclCheck) Payload

func (e EvtAclCheck) Payload() []byte

func (EvtAclCheck) QoS

func (e EvtAclCheck) QoS() int

func (EvtAclCheck) Retained

func (e EvtAclCheck) Retained() bool

func (EvtAclCheck) Topic

func (e EvtAclCheck) Topic() string

type EvtBasicAuth

type EvtBasicAuth uintptr

func (EvtBasicAuth) Client

func (e EvtBasicAuth) Client() Client

func (EvtBasicAuth) Password

func (e EvtBasicAuth) Password() string

func (EvtBasicAuth) Username

func (e EvtBasicAuth) Username() string

Basic auth event

type EvtControl

type EvtControl uintptr

func (EvtControl) Client

func (e EvtControl) Client() Client

Control event

func (EvtControl) Payload

func (e EvtControl) Payload() []byte

func (EvtControl) QoS

func (e EvtControl) QoS() int

func (EvtControl) Retained

func (e EvtControl) Retained() bool

func (EvtControl) Topic

func (e EvtControl) Topic() string

type EvtDisconnect

type EvtDisconnect uintptr

func (EvtDisconnect) Client

func (e EvtDisconnect) Client() Client

Disconnect event

type EvtExtendedAuth

type EvtExtendedAuth uintptr

type EvtMessage

type EvtMessage uintptr

func (EvtMessage) Client

func (e EvtMessage) Client() Client

Message event

func (EvtMessage) Payload

func (e EvtMessage) Payload() []byte

func (EvtMessage) QoS

func (e EvtMessage) QoS() int

func (EvtMessage) Retained

func (e EvtMessage) Retained() bool

func (EvtMessage) SetPayload

func (e EvtMessage) SetPayload(data []byte)

func (EvtMessage) SetQoS

func (e EvtMessage) SetQoS(qos int)

func (EvtMessage) SetRetained

func (e EvtMessage) SetRetained(retain bool)

func (EvtMessage) SetTopic

func (e EvtMessage) SetTopic(topic string)

func (EvtMessage) Topic

func (e EvtMessage) Topic() string

type EvtPskKey

type EvtPskKey uintptr

type EvtReload

type EvtReload uintptr

func (EvtReload) Options

func (e EvtReload) Options() Options

Reload event

type EvtTick

type EvtTick uintptr

func (EvtTick) Next

func (e EvtTick) Next() time.Time

func (EvtTick) Now

func (e EvtTick) Now() time.Time

Tick event

type Options

type Options map[string]string

type Plugin

type Plugin interface {
	Version(versions []int) int
	Init(id PluginID, options Options) error
	Cleanup(options Options) error
}

type PluginID

type PluginID uintptr

type Protocol

type Protocol int

func (Protocol) String

func (p Protocol) String() string

type ProtocolVersion

type ProtocolVersion int

func (ProtocolVersion) String

func (p ProtocolVersion) String() string

Directories

Path Synopsis
plugins

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL