From 4062727333f620229ad459fc8a12a750ba594e8f Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 19 Mar 2020 12:26:28 +0900 Subject: [PATCH] negotiate() function extracted from vpnkit From: https://github.com/moby/vpnkit commit 2ffc1dd8a84ea7359dd09b1f4b51bb728d4f46a0 c/vpnkit-tap-vsockd/tap-vsockd.c With minimum changes to make it build. --- arch/sim/src/sim/vpnkit/negotiate.c | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 arch/sim/src/sim/vpnkit/negotiate.c diff --git a/arch/sim/src/sim/vpnkit/negotiate.c b/arch/sim/src/sim/vpnkit/negotiate.c new file mode 100644 index 0000000000..0df758ae68 --- /dev/null +++ b/arch/sim/src/sim/vpnkit/negotiate.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "protocol.h" + +#define ERROR(fmt, ...) \ + fprintf(stderr, "up_vpnkit: " fmt "\r\n", ##__VA_ARGS__) +#define INFO(fmt, ...) \ + fprintf(stderr, "up_vpnkit: " fmt "\r\n", ##__VA_ARGS__) + +/* Negotiate a vmnet connection, returns 0 on success and 1 on error. */ +int negotiate(int fd, struct vif_info *vif) +{ + enum command command = ethernet; + struct init_message *me; + struct ethernet_args args; + struct init_message you; + char *txt; + + me = create_init_message(); + if (!me) + goto err; + + if (write_init_message(fd, me) == -1) + goto err; + + if (read_init_message(fd, &you) == -1) + goto err; + + if (me->version != you.version) { + ERROR("Server did not accept our protocol version (client: %d, server: %d)", me->version, you.version); + goto err; + } + + txt = print_init_message(&you); + if (!txt) + goto err; + + INFO("Server reports %s", txt); + free(txt); + + if (write_command(fd, &command) == -1) + goto err; + + /* We don't need a uuid */ + memset(&args.uuid_string[0], 0, sizeof(args.uuid_string)); + if (write_ethernet_args(fd, &args) == -1) + goto err; + + if (read_vif_response(fd, vif) == -1) + goto err; + + return 0; +err: + ERROR("Failed to negotiate vmnet connection"); + return 1; +}