diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-20 20:16:06 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-20 20:16:06 +0200 |
| commit | e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7 (patch) | |
| tree | 6cc3e6a1cddf93064cf411a3ffe811876e83a643 | |
| parent | 7f8f79eb9d3cab407410814b2db261d89f11af2e (diff) | |
| download | mqtt-e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7.tar.gz mqtt-e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7.zip | |
Support for username and password
| -rw-r--r-- | src/client.c | 57 | ||||
| -rw-r--r-- | src/mqtt.h | 3 | ||||
| -rw-r--r-- | src/serialize.c | 10 |
3 files changed, 65 insertions, 5 deletions
diff --git a/src/client.c b/src/client.c index 4c4ed7d..a6b0998 100644 --- a/src/client.c +++ b/src/client.c @@ -78,6 +78,8 @@ struct MqttClient int willRetain; /* 1 if client should ignore incoming PUBLISH messages, 0 handle them */ int paused; + bstring userName; + bstring password; }; enum MessageState @@ -276,6 +278,18 @@ int MqttClientConnect(MqttClient *client, const char *host, short port, packet->connectFlags |= (client->willRetain & 1) << 5; } + if (client->userName) + { + packet->connectFlags |= 0x80; + packet->userName = bstrcpy(client->userName); + + if (client->password) + { + packet->connectFlags |= 0x40; + packet->password = bstrcpy(client->password); + } + } + MqttClientQueuePacket(client, &packet->base); return 0; @@ -594,6 +608,49 @@ int MqttClientSetWill(MqttClient *client, const char *topic, const void *msg, return 0; } +int MqttClientSetAuth(MqttClient *client, const char *userName, + const char *password) +{ + assert(client != NULL); + + if (MqttClientIsConnected(client)) + { + LOG_ERROR("MqttClientSetAuth must be called before MqttClientConnect"); + return -1; + } + + if (userName) + { + if (client->userName) + bassigncstr(client->userName, userName); + else + client->userName = bfromcstr(userName); + + if (password) + { + if (client->password) + bassigncstr(client->password, password); + else + client->password = bfromcstr(password); + } + else + { + bdestroy(client->password); + client->password = NULL; + } + } + else + { + bdestroy(client->userName); + client->userName = NULL; + + bdestroy(client->password); + client->password = NULL; + } + + return 0; +} + static void MqttClientQueuePacket(MqttClient *client, MqttPacket *packet) { assert(client != NULL); @@ -103,6 +103,9 @@ void MqttClientSetMaxQueuedMessages(MqttClient *client, int max); int MqttClientSetWill(MqttClient *client, const char *topic, const void *msg, size_t size, int qos, int retain); +int MqttClientSetAuth(MqttClient *client, const char *username, + const char *password); + #if defined(__cplusplus) } #endif diff --git a/src/serialize.c b/src/serialize.c index 3378b80..c1c8eb4 100644 --- a/src/serialize.c +++ b/src/serialize.c @@ -193,12 +193,12 @@ static int MqttPacketConnectSerialize(const MqttPacketConnect *packet, Stream *s { if (StreamWriteMqttString(packet->userName, stream) == -1) return -1; - } - if (packet->connectFlags & 0x40) - { - if (StreamWriteMqttString(packet->password, stream) == -1) - return -1; + if (packet->connectFlags & 0x40) + { + if (StreamWriteMqttString(packet->password, stream) == -1) + return -1; + } } return 0; |
