Add version string to handshake

This commit is contained in:
Matthias Schiffer 2012-06-04 17:21:32 +02:00
parent a157804e7b
commit f2bb9fd6d4
4 changed files with 27 additions and 5 deletions

View file

@ -715,6 +715,8 @@ int main(int argc, char *argv[]) {
update_time(&ctx); update_time(&ctx);
pr_info(&ctx, "fastd " FASTD_VERSION " starting");
init_sockets(&ctx); init_sockets(&ctx);
init_tuntap(&ctx); init_tuntap(&ctx);

View file

@ -46,6 +46,7 @@ static const char const *RECORD_TYPES[RECORD_MAX] = {
"(protocol specific 5)", "(protocol specific 5)",
"MTU", "MTU",
"method name", "method name",
"version name",
}; };
static const char const *REPLY_TYPES[REPLY_MAX] = { static const char const *REPLY_TYPES[REPLY_MAX] = {
@ -61,11 +62,13 @@ static const char const *REPLY_TYPES[REPLY_MAX] = {
fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) { fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) {
size_t protocol_len = strlen(ctx->conf->protocol->name); size_t protocol_len = strlen(ctx->conf->protocol->name);
size_t method_len = strlen(ctx->conf->method->name); size_t method_len = strlen(ctx->conf->method->name);
size_t version_len = strlen(FASTD_VERSION);
fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0,
2*5 + /* handshake type, mode */ 2*5 + /* handshake type, mode */
6 + /* MTU */ 6 + /* MTU */
4+protocol_len + /* protocol name */ 4+protocol_len + /* protocol name */
4+method_len + /* method name */ 4+method_len + /* method name */
4+version_len + /* version name */
tail_space tail_space
); );
fastd_packet *request = buffer.data; fastd_packet *request = buffer.data;
@ -79,20 +82,23 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, size_t tail_space) {
fastd_handshake_add(ctx, &buffer, RECORD_PROTOCOL_NAME, protocol_len, ctx->conf->protocol->name); fastd_handshake_add(ctx, &buffer, RECORD_PROTOCOL_NAME, protocol_len, ctx->conf->protocol->name);
fastd_handshake_add(ctx, &buffer, RECORD_METHOD_NAME, method_len, ctx->conf->method->name); fastd_handshake_add(ctx, &buffer, RECORD_METHOD_NAME, method_len, ctx->conf->method->name);
fastd_handshake_add(ctx, &buffer, RECORD_VERSION_NAME, version_len, FASTD_VERSION);
return buffer; return buffer;
} }
fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake *handshake, size_t tail_space) { fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake *handshake, size_t tail_space) {
bool first = (AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE]) == 1); bool first = (AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE]) == 1);
size_t mtu_size = 0; size_t version_len = strlen(FASTD_VERSION);
size_t extra_size = 0;
if (first) if (first)
mtu_size = 6; extra_size = 6 + /* MTU */
4+version_len; /* version name */
fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0,
2*5 + /* handshake type, reply code */ 2*5 + /* handshake type, reply code */
mtu_size + extra_size +
tail_space tail_space
); );
fastd_packet *request = buffer.data; fastd_packet *request = buffer.data;
@ -103,8 +109,10 @@ fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, const fastd_handshake
fastd_handshake_add_uint8(ctx, &buffer, RECORD_HANDSHAKE_TYPE, AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE])+1); fastd_handshake_add_uint8(ctx, &buffer, RECORD_HANDSHAKE_TYPE, AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE])+1);
fastd_handshake_add_uint8(ctx, &buffer, RECORD_REPLY_CODE, 0); fastd_handshake_add_uint8(ctx, &buffer, RECORD_REPLY_CODE, 0);
if (first) if (first) {
fastd_handshake_add_uint16(ctx, &buffer, RECORD_MTU, ctx->conf->mtu); fastd_handshake_add_uint16(ctx, &buffer, RECORD_MTU, ctx->conf->mtu);
fastd_handshake_add(ctx, &buffer, RECORD_VERSION_NAME, version_len, FASTD_VERSION);
}
return buffer; return buffer;
} }

View file

@ -44,6 +44,7 @@ typedef enum _fastd_handshake_record_type {
RECORD_PROTOCOL5, RECORD_PROTOCOL5,
RECORD_MTU, RECORD_MTU,
RECORD_METHOD_NAME, RECORD_METHOD_NAME,
RECORD_VERSION_NAME,
RECORD_MAX, RECORD_MAX,
} fastd_handshake_record_type; } fastd_handshake_record_type;

View file

@ -519,6 +519,7 @@ static inline bool has_field(const fastd_handshake *handshake, uint8_t type, siz
static void protocol_handshake_handle(fastd_context *ctx, const fastd_peer_address *address, const fastd_peer_config *peer_conf, const fastd_handshake *handshake) { static void protocol_handshake_handle(fastd_context *ctx, const fastd_peer_address *address, const fastd_peer_config *peer_conf, const fastd_handshake *handshake) {
handshake_key *handshake_key; handshake_key *handshake_key;
char *peer_version_name = NULL;
maintenance(ctx); maintenance(ctx);
@ -568,6 +569,12 @@ static void protocol_handshake_handle(fastd_context *ctx, const fastd_peer_addre
switch(handshake->type) { switch(handshake->type) {
case 1: case 1:
if (handshake->records[RECORD_VERSION_NAME].data)
peer_version_name = strndup(handshake->records[RECORD_VERSION_NAME].data, handshake->records[RECORD_VERSION_NAME].length);
pr_debug(ctx, "received handshake from %P[%I] using fastd %s", peer, address, peer_version_name);
free(peer_version_name);
respond_handshake(ctx, address, peer, &ctx->protocol_state->handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, handshake); respond_handshake(ctx, address, peer, &ctx->protocol_state->handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, handshake);
break; break;
@ -583,7 +590,11 @@ static void protocol_handshake_handle(fastd_context *ctx, const fastd_peer_addre
return; return;
} }
pr_debug(ctx, "received handshake response from %P[%I]", peer, address); if (handshake->records[RECORD_VERSION_NAME].data)
peer_version_name = strndup(handshake->records[RECORD_VERSION_NAME].data, handshake->records[RECORD_VERSION_NAME].length);
pr_debug(ctx, "received handshake response from %P[%I] using fastd %s", peer, address, peer_version_name);
free(peer_version_name);
finish_handshake(ctx, address, peer, handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, handshake); finish_handshake(ctx, address, peer, handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, handshake);
break; break;