diff options
author | Sjoerd Simons <sjoerd@luon.net> | 2007-06-28 13:09:02 +0000 |
---|---|---|
committer | Sjoerd Simons <sjoerd@luon.net> | 2007-06-28 13:09:02 +0000 |
commit | 66a3097310d515ed3840671331d4a851e36a4406 (patch) | |
tree | 2d16fcbf04d2c9bcd3dc69b872b714412db4fee7 /lib/gibber/gibber-r-multicast-packet.c | |
parent | 4db1d315ab7f4d1dd5630f22d096a3dffd84a0a7 (diff) | |
download | telepathy-salut-66a3097310d515ed3840671331d4a851e36a4406.tar.gz |
Add Whois packet parser/builder
20070628130902-93b9a-537e64e55880f548354b20daf7b65d83f88ae601.gz
Diffstat (limited to 'lib/gibber/gibber-r-multicast-packet.c')
-rw-r--r-- | lib/gibber/gibber-r-multicast-packet.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/gibber/gibber-r-multicast-packet.c b/lib/gibber/gibber-r-multicast-packet.c index 61f449bf..d7ba3188 100644 --- a/lib/gibber/gibber-r-multicast-packet.c +++ b/lib/gibber/gibber-r-multicast-packet.c @@ -412,3 +412,59 @@ gibber_r_multicast_packet_diff(guint32 from, guint32 to) { return MIN(to - from, G_MAXINT); } + +/* Generate a new raw whois packet, free with g_free */ +guint8 * +gibber_r_multicast_whois_packet (GibberRMulticastPacketType type, + guint32 id, + gchar *name, + gsize *length) +{ + guint8 *result; + gsize offset = 0; + g_assert (length != NULL); + g_assert (type == PACKET_TYPE_WHOIS_REPLY + || type == PACKET_TYPE_WHOIS_REQUEST); + g_assert ((type == PACKET_TYPE_WHOIS_REPLY && name != NULL) || name == NULL); + + /* 8 bit type, 8 bit version, 32 bit id + optional string */ + *length = 6 + (name == NULL ? 0 : strlen(name) + 1); + result = g_malloc(*length); + + /* Type */ + add_guint8 (result, *length, &offset, type); + /* Version */ + add_guint8 (result, *length, &offset, 0); + /* Identifier */ + add_guint32 (result, *length, &offset, id); + + if (type == PACKET_TYPE_WHOIS_REPLY) { + add_string (result, *length, &offset, name); + } + return result; +} + +GibberRMulticastWhoisPacket * +gibber_r_multicast_whois_new_from_packet (guint8 *data, gsize length) +{ + GibberRMulticastWhoisPacket *result; + gsize offset = 0; + + result = g_slice_new0 (GibberRMulticastWhoisPacket); + result->type = get_guint8 (data, length, &offset); + result->version = get_guint8 (data, length, &offset); + result->id = get_guint32 (data, length, &offset); + + if (result->type == PACKET_TYPE_WHOIS_REPLY) { + result->name = get_string (data, length, &offset); + } + + return result; +} + +void +gibber_r_multicast_whois_free (GibberRMulticastWhoisPacket *packet) +{ + g_free (packet->name); + g_slice_free (GibberRMulticastWhoisPacket, packet); +} |