aboutsummaryrefslogtreecommitdiff
path: root/src/client.c
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/client.c
parent7f8f79eb9d3cab407410814b2db261d89f11af2e (diff)
downloadmqtt-e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7.tar.gz
mqtt-e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7.zip
Support for username and password
Diffstat (limited to 'src/client.c')
-rw-r--r--src/client.c57
1 files changed, 57 insertions, 0 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);