Skip to content

Commit 789bacb

Browse files
committed
Refactored some parts, fixed a nasty bug in unpack_publish
1 parent 55e9fdd commit 789bacb

File tree

14 files changed

+222
-102
lines changed

14 files changed

+222
-102
lines changed

src/config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
// Default parameters
3434

35-
#define VERSION "0.1.9"
35+
#define VERSION "0.3.5"
3636
#define DEFAULT_SOCKET_FAMILY INET
3737
#define DEFAULT_LOG_LEVEL DEBUG
3838
#define DEFAULT_LOG_PATH "/tmp/sol.log"
@@ -74,6 +74,7 @@ struct config {
7474
size_t stats_pub_interval;
7575
};
7676

77+
7778
extern struct config *conf;
7879

7980

src/solcore.c renamed to src/core.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <string.h>
2929
#include "util.h"
30-
#include "solcore.h"
30+
#include "core.h"
3131

3232

3333
static int compare_cid(void *c1, void *c2) {
@@ -51,16 +51,27 @@ void topic_init(struct topic *t, const char *name) {
5151

5252
void topic_add_subscriber(struct topic *t,
5353
struct sol_client *client,
54-
unsigned qos) {
54+
unsigned qos,
55+
bool cleansession) {
5556
struct subscriber *sub = sol_malloc(sizeof(*sub));
5657
sub->client = client;
5758
sub->qos = qos;
5859
t->subscribers = list_push(t->subscribers, sub);
60+
61+
// It must be added to the session if cleansession is false
62+
if (!cleansession)
63+
client->session.subscriptions =
64+
list_push(client->session.subscriptions, t);
65+
5966
}
6067

6168

62-
void topic_del_subscriber(struct topic *t, struct sol_client *client) {
69+
void topic_del_subscriber(struct topic *t,
70+
struct sol_client *client,
71+
bool cleansession) {
6372
list_remove_node(t->subscribers, client, compare_cid);
73+
74+
// TODO remomve in case of cleansession == false
6475
}
6576

6677

src/solcore.h renamed to src/core.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
*/
2727

28-
#ifndef SOLCORE_H
29-
#define SOLCORE_H
28+
#ifndef CORE_H
29+
#define CORE_H
3030

3131
#include "trie.h"
3232
#include "list.h"
@@ -49,9 +49,19 @@ struct sol {
4949
};
5050

5151

52+
struct session {
53+
List *subscriptions;
54+
// TODO add pending confirmed messages
55+
};
56+
57+
/*
58+
* Wrapper structure around a connected client, each client can be a publisher
59+
* or a subscriber, it can be used to track sessions too.
60+
*/
5261
struct sol_client {
5362
char *client_id;
5463
int fd;
64+
struct session session;
5565
};
5666

5767

@@ -65,9 +75,9 @@ struct topic *topic_create(const char *);
6575

6676
void topic_init(struct topic *, const char *);
6777

68-
void topic_add_subscriber(struct topic *, struct sol_client *, unsigned);
78+
void topic_add_subscriber(struct topic *, struct sol_client *, unsigned, bool);
6979

70-
void topic_del_subscriber(struct topic *, struct sol_client *);
80+
void topic_del_subscriber(struct topic *, struct sol_client *, bool);
7181

7282
void sol_topic_put(struct sol *, struct topic *);
7383

src/hashtable.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@
3434
#include "util.h"
3535
#include "hashtable.h"
3636

37+
/* Hashtable definition */
38+
struct hashtable {
39+
size_t table_size;
40+
size_t size;
41+
int (*destructor)(struct hashtable_entry *);
42+
struct hashtable_entry *entries;
43+
};
44+
3745

3846
const int INITIAL_SIZE = 4;
3947

4048
const int MAX_CHAIN_LENGTH = 8;
4149

4250
const unsigned long KNUTH_PRIME = 2654435761;
4351

52+
static unsigned long crc32(const uint8_t *, unsigned int);
53+
4454
/*
4555
* Hashing function for a string
4656
*/
@@ -497,7 +507,7 @@ static unsigned long crc32_tab[] = {
497507
};
498508

499509
/* Return a 32-bit CRC of the contents of the buffer. */
500-
unsigned long crc32(const uint8_t *s, unsigned int len) {
510+
static unsigned long crc32(const uint8_t *s, unsigned int len) {
501511
unsigned int i;
502512
uint64_t crc32val;
503513

src/hashtable.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,11 @@ struct hashtable_entry {
4848
bool taken;
4949
};
5050

51-
5251
/*
5352
* An HashTable has some maximum size and current size, as well as the data to
5453
* hold.
5554
*/
56-
typedef struct {
57-
size_t table_size;
58-
size_t size;
59-
int (*destructor)(struct hashtable_entry *);
60-
struct hashtable_entry *entries;
61-
} HashTable;
62-
55+
typedef struct hashtable HashTable;
6356

6457
/*
6558
* HashTable API to create a new `HashTable`, it require a function pointer to
@@ -69,8 +62,8 @@ typedef struct {
6962
HashTable *hashtable_create(int (*destructor)(struct hashtable_entry *));
7063

7164
/* Destroy the hashtable by calling functor `destructor` on every
72-
* `struct hashtable_entry`, thus it needs to have a defined destructor function for
73-
* each different data-type inserted. In case of a NULL destructor, it' ll call
65+
* `struct hashtable_entry`, thus it needs to have a defined destructor function
66+
* for each different data-type inserted. In case of a NULL destructor, it' ll call
7467
* normal free.
7568
*/
7669
void hashtable_release(HashTable *);
@@ -107,7 +100,4 @@ int hashtable_map2(HashTable *,
107100
int (*func)(struct hashtable_entry *, void *), void *);
108101

109102

110-
uint64_t crc32(const uint8_t *, const uint32_t);
111-
112-
113103
#endif

src/mqtt.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* POSSIBILITY OF SUCH DAMAGE.
2626
*/
2727

28+
#include <string.h>
2829
#include <arpa/inet.h>
2930
#include "util.h"
3031
#include "mqtt.h"
@@ -496,7 +497,7 @@ static unsigned char *pack_mqtt_publish(const union mqtt_packet *pkt) {
496497
// Total len of the packet excluding fixed header len
497498
size_t len = 0L;
498499

499-
if (pkt->header.bits.qos > AT_MOST_ONCE)
500+
if (pkt->publish.header.bits.qos > AT_MOST_ONCE)
500501
pktlen += sizeof(uint16_t);
501502

502503
unsigned char *packed = sol_malloc(pktlen);
@@ -519,7 +520,7 @@ static unsigned char *pack_mqtt_publish(const union mqtt_packet *pkt) {
519520
pack_bytes(&ptr, pkt->publish.topic);
520521

521522
// Packet id
522-
if (pkt->header.bits.qos > AT_MOST_ONCE)
523+
if (pkt->publish.header.bits.qos > AT_MOST_ONCE)
523524
pack_u16(&ptr, pkt->publish.pkt_id);
524525

525526
// Finally the payload, same way of topic, payload len -> payload
@@ -586,7 +587,8 @@ struct mqtt_suback *mqtt_packet_suback(unsigned char byte,
586587
suback->header.byte = byte;
587588
suback->pkt_id = pkt_id;
588589
suback->rcslen = rcslen;
589-
suback->rcs = (unsigned char *) sol_strdup((const char *) rcs);
590+
suback->rcs = sol_malloc(rcslen);
591+
memcpy(suback->rcs, rcs, rcslen);
590592

591593
return suback;
592594
}

src/mqtt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,12 @@ typedef union mqtt_header mqtt_disconnect;
214214

215215
union mqtt_packet {
216216

217+
// This will cover PUBACK, PUBREC, PUBREL, PUBCOMP and UNSUBACK
217218
struct mqtt_ack ack;
219+
220+
// This will cover PINGREQ, PINGRESP and DISCONNECT
218221
union mqtt_header header;
222+
219223
struct mqtt_connect connect;
220224
struct mqtt_connack connack;
221225
struct mqtt_suback suback;

src/network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ ssize_t send_bytes(int fd, const unsigned char *buf, size_t len) {
233233

234234
err:
235235

236-
fprintf(stderr, "send(2) - error sending data: %s", strerror(errno));
236+
fprintf(stderr, "send(2) - error sending data: %s\n", strerror(errno));
237237
return -1;
238238
}
239239

src/pack.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ uint8_t unpack_u8(const uint8_t **buf) {
9191

9292

9393
uint16_t unpack_u16(const uint8_t **buf) {
94-
uint16_t val = ntohs(*((uint16_t *) (*buf)));
94+
uint16_t val;
95+
memcpy(&val, *buf, sizeof(uint16_t));
9596
(*buf) += sizeof(uint16_t);
96-
return val;
97+
return ntohs(val);
9798
}
9899

99100

100101
uint32_t unpack_u32(const uint8_t **buf) {
101-
uint32_t val = ntohl(*((uint32_t *) (*buf)));
102+
uint32_t val;
103+
memcpy(&val, *buf, sizeof(uint32_t));
102104
(*buf) += sizeof(uint32_t);
103-
return val;
105+
return ntohl(val);
104106
}
105107

106108

@@ -128,13 +130,15 @@ void pack_u8(uint8_t **buf, uint8_t val) {
128130

129131

130132
void pack_u16(uint8_t **buf, uint16_t val) {
131-
*((uint16_t *) (*buf)) = htons(val);
133+
uint16_t htonsval = htons(val);
134+
memcpy(*buf, &htonsval, sizeof(uint16_t));
132135
(*buf) += sizeof(uint16_t);
133136
}
134137

135138

136139
void pack_u32(uint8_t **buf, uint32_t val) {
137-
*((uint32_t *) (*buf)) = htonl(val);
140+
uint32_t htonlval = htonl(val);
141+
memcpy(*buf, &htonlval, sizeof(uint32_t));
138142
(*buf) += sizeof(uint32_t);
139143
}
140144

0 commit comments

Comments
 (0)