Reputation: 101
I'm writing a tacacs+ client,
I'm trying to write a simple packet to send to the server, I checked the RFC page and I saw the protocol needs to get this header in every packet:
+----------------+----------------+----------------+----------------+
|major | minor | | | |
|version| version| type | seq_no | flags |
+----------------+----------------+----------------+----------------+
| |
| session_id |
+----------------+----------------+----------------+----------------+
| |
| length |
+----------------+----------------+----------------+----------------+
now I'm pretty new with this, and I'm not sure how do I build this header, is creating a buffer like this good enough?:
snprintf(buffer, sizeof(buffer), "%x%x%x%x%x",version,version,type,seq,flags,session_id,length);
and then take this buffer and attach it the body in the same way and send:
send(socket,buffer,sizeof(buffer),0);
or is there another way of doing this whole packet building?
Upvotes: 2
Views: 1149
Reputation: 385789
What you have doesn't produce the right output at all!
The following is portable:
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
uint8_t* pack_uint8(uint8_t* dest, uint8_t src) {
*(dest++) = src;
return dest;
}
uint8_t* pack_uint32be(uint8_t* dest, uint32_t src) {
*(dest++) = src >> 24;
*(dest++) = (src >> 16) & 0xFF;
*(dest++) = (src >> 8) & 0xFF;
*(dest++) = src & 0xFF;
return dest;
}
uint8_t* build_packet(
uint8_t maj_version,
uint8_t min_version,
uint8_t type,
uint8_t seq,
uint8_t flags,
uint32_t session_id,
uint32_t body_length,
const uint8_t* body
) {
uint8_t* packet = malloc(12 + body_length);
uint8_t* p = packet;
p = pack_uint8(p, ( maj_version << 4 ) | min_version);
p = pack_uint8(p, type);
p = pack_uint8(p, seq);
p = pack_uint8(p, flags);
p = pack_uint32be(p, session_id);
p = pack_uint32be(p, body_length);
memcpy(p, body, body_length);
return packet;
}
Upvotes: 3