aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2017-02-20 20:16:06 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2017-02-20 20:16:06 +0200
commite9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7 (patch)
tree6cc3e6a1cddf93064cf411a3ffe811876e83a643 /src
parent7f8f79eb9d3cab407410814b2db261d89f11af2e (diff)
downloadmqtt-e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7.tar.gz
mqtt-e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7.zip
Support for username and password
Diffstat (limited to 'src')
-rw-r--r--src/client.c57
-rw-r--r--src/mqtt.h3
-rw-r--r--src/serialize.c10
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);
diff --git a/src/mqtt.h b/src/mqtt.h
index ad84aaf..2b84962 100644
--- a/src/mqtt.h
+++ b/src/mqtt.h
@@ -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;