Appease nxstyle complaints in files from vpnkit

This commit is contained in:
YAMAMOTO Takashi
2020-03-19 16:55:16 +09:00
committed by Xiang Xiao
parent cd10bd67c4
commit 124c00b661
3 changed files with 369 additions and 237 deletions
+62 -32
View File
@@ -36,61 +36,91 @@
* limitations under the License.
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "protocol.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define ERROR(fmt, ...) \
fprintf(stderr, "up_vpnkit: " fmt "\r\n", ##__VA_ARGS__)
#define INFO(fmt, ...) \
fprintf(stderr, "up_vpnkit: " fmt "\r\n", ##__VA_ARGS__)
/****************************************************************************
* Public Functions
****************************************************************************/
/* 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;
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;
me = create_init_message();
if (!me)
{
goto err;
}
if (write_init_message(fd, me) == -1)
goto err;
if (write_init_message(fd, me) == -1)
{
goto err;
}
if (read_init_message(fd, &you) == -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;
}
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;
txt = print_init_message(&you);
if (!txt)
{
goto err;
}
INFO("Server reports %s", txt);
free(txt);
INFO("Server reports %s", txt);
free(txt);
if (write_command(fd, &command) == -1)
goto err;
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;
/* We don't need a uuid */
if (read_vif_response(fd, vif) == -1)
goto err;
memset(&args.uuid_string[0], 0, sizeof(args.uuid_string));
if (write_ethernet_args(fd, &args) == -1)
{
goto err;
}
return 0;
if (read_vif_response(fd, vif) == -1)
{
goto err;
}
return 0;
err:
ERROR("Failed to negotiate vmnet connection");
return 1;
ERROR("Failed to negotiate vmnet connection");
return 1;
}
+212 -152
View File
@@ -36,6 +36,10 @@
* limitations under the License.
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
@@ -46,219 +50,275 @@
#include "protocol.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define ERROR(fmt, ...) \
fprintf(stderr, "up_vpnkit: " fmt "\r\n", ##__VA_ARGS__)
/****************************************************************************
* Public Data
****************************************************************************/
/* Version 0 of the protocol used this */
char expected_hello_old[5] = {'V', 'M', 'N', 'E', 'T'};
char expected_hello_old[5] =
{
'V', 'M', 'N', 'E', 'T'
};
/* Version 1 and later of the protocol used this */
char expected_hello[5] = {'V', 'M', 'N', '3', 'T'};
char expected_hello[5] =
{
'V', 'M', 'N', '3', 'T'
};
/****************************************************************************
* Public Functions
****************************************************************************/
int really_read(int fd, uint8_t *buffer, size_t total)
{
size_t remaining = total;
ssize_t n;
size_t remaining = total;
ssize_t n;
while (remaining > 0) {
n = read(fd, buffer, remaining);
if (n == 0) {
ERROR("EOF reading from socket: closing\n");
goto err;
}
if (n < 0) {
ERROR(
"Failure reading from socket: closing: %s",
strerror(errno));
goto err;
}
remaining -= (size_t) n;
buffer = buffer + n;
}
return 0;
while (remaining > 0)
{
n = read(fd, buffer, remaining);
if (n == 0)
{
ERROR("EOF reading from socket: closing\n");
goto err;
}
if (n < 0)
{
ERROR("Failure reading from socket: closing: %s",
strerror(errno));
goto err;
}
remaining -= (size_t)n;
buffer = buffer + n;
}
return 0;
err:
/*
* On error: stop reading from the socket and trigger a clean
* shutdown
*/
shutdown(fd, SHUT_RD);
return -1;
/* On error: stop reading from the socket and trigger a clean
* shutdown
*/
shutdown(fd, SHUT_RD);
return -1;
}
int really_write(int fd, uint8_t *buffer, size_t total)
{
size_t remaining = total;
ssize_t n;
size_t remaining = total;
ssize_t n;
while (remaining > 0) {
n = write(fd, buffer, remaining);
if (n == 0) {
ERROR("EOF writing to socket: closing");
goto err;
}
if (n < 0) {
ERROR(
"Failure writing to socket: closing: %s",
strerror(errno));
goto err;
}
remaining -= (size_t) n;
buffer = buffer + n;
}
return 0;
while (remaining > 0)
{
n = write(fd, buffer, remaining);
if (n == 0)
{
ERROR("EOF writing to socket: closing");
goto err;
}
if (n < 0)
{
ERROR("Failure writing to socket: closing: %s",
strerror(errno));
goto err;
}
remaining -= (size_t) n;
buffer = buffer + n;
}
return 0;
err:
/* On error: stop listening to the socket */
shutdown(fd, SHUT_WR);
return -1;
/* On error: stop listening to the socket */
shutdown(fd, SHUT_WR);
return -1;
}
struct init_message *create_init_message()
{
struct init_message *m;
struct init_message *m;
m = malloc(sizeof(struct init_message));
if (!m)
return NULL;
m = malloc(sizeof(struct init_message));
if (!m)
{
return NULL;
}
bzero(m, sizeof(struct init_message));
memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello));
m->version = CURRENT_VERSION;
memset(&m->commit[0], 0, sizeof(m->commit));
bzero(m, sizeof(struct init_message));
memcpy(&m->hello[0], &expected_hello[0], sizeof(m->hello));
m->version = CURRENT_VERSION;
memset(&m->commit[0], 0, sizeof(m->commit));
return m;
return m;
}
char *print_init_message(struct init_message *m)
{
char tmp[41];
char tmp[41];
memcpy(&tmp[0], &m->commit[0], 40);
tmp[40] = '\000';
char *buffer;
int n;
memcpy(&tmp[0], &m->commit[0], 40);
tmp[40] = '\000';
char *buffer;
int n;
buffer = malloc(80);
if (!buffer)
return NULL;
buffer = malloc(80);
if (!buffer)
{
return NULL;
}
n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp);
if (n < 0) {
perror("Failed to format init_message");
exit(1);
}
return buffer;
n = snprintf(buffer, 80, "version %d, commit %s", m->version, tmp);
if (n < 0)
{
perror("Failed to format init_message");
exit(1);
}
return buffer;
}
int read_init_message(int fd, struct init_message *ci)
{
int res;
int res;
bzero(ci, sizeof(struct init_message));
bzero(ci, sizeof(struct init_message));
res = really_read(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
if (res == -1) {
ERROR("Failed to read hello from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
if (res == -1)
{
ERROR("Failed to read hello from client");
return -1;
}
res = memcmp(&ci->hello[0],
&expected_hello_old[0], sizeof(expected_hello_old));
if (res == 0) {
ci->version = 0;
return 0;
}
res = memcmp(&ci->hello[0],
&expected_hello_old[0], sizeof(expected_hello_old));
if (res == 0)
{
ci->version = 0;
return 0;
}
res = memcmp(&ci->hello[0],
&expected_hello[0], sizeof(expected_hello));
if (res != 0) {
ERROR("Failed to read header magic from client");
return -1;
}
res = memcmp(&ci->hello[0],
&expected_hello[0], sizeof(expected_hello));
if (res != 0)
{
ERROR("Failed to read header magic from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->version, sizeof(ci->version));
if (res == -1) {
ERROR("Failed to read header version from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->version, sizeof(ci->version));
if (res == -1)
{
ERROR("Failed to read header version from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->commit[0], sizeof(ci->commit));
if (res == -1) {
ERROR("Failed to read header hash from client");
return -1;
}
res = really_read(fd, (uint8_t *)&ci->commit[0], sizeof(ci->commit));
if (res == -1)
{
ERROR("Failed to read header hash from client");
return -1;
}
return 0;
return 0;
}
int write_init_message(int fd, struct init_message *ci)
{
int res;
int res;
res = really_write(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
if (res == -1) {
ERROR("Failed to write hello to client");
return -1;
}
if (ci->version > 0) {
res = really_write(fd, (uint8_t *)&ci->version,
sizeof(ci->version));
if (res == -1) {
ERROR("Failed to write version to client");
return -1;
}
res = really_write(fd, (uint8_t *)&ci->commit[0],
sizeof(ci->commit));
if (res == -1) {
ERROR("Failed to write header hash to client");
return -1;
}
}
return 0;
res = really_write(fd, (uint8_t *)&ci->hello[0], sizeof(ci->hello));
if (res == -1)
{
ERROR("Failed to write hello to client");
return -1;
}
if (ci->version > 0)
{
res = really_write(fd, (uint8_t *)&ci->version,
sizeof(ci->version));
if (res == -1)
{
ERROR("Failed to write version to client");
return -1;
}
res = really_write(fd, (uint8_t *)&ci->commit[0],
sizeof(ci->commit));
if (res == -1)
{
ERROR("Failed to write header hash to client");
return -1;
}
}
return 0;
}
int read_vif_response(int fd, struct vif_info *vif)
{
struct msg_response msg;
struct msg_response msg;
if (really_read(fd, (uint8_t*)&msg, sizeof(msg)) == -1) {
ERROR("Client failed to read server response");
return -1;
}
switch (msg.response_type) {
case rt_vif:
memcpy((uint8_t*)vif, (uint8_t*)&msg.vif, sizeof(*vif));
return 0;
case rt_disconnect:
ERROR("Server disconnected: %*s", msg.disconnect.len, msg.disconnect.msg);
return -1;
default:
ERROR("Unknown response type from server");
return -1;
}
if (really_read(fd, (uint8_t *)&msg, sizeof(msg)) == -1)
{
ERROR("Client failed to read server response");
return -1;
}
switch (msg.response_type)
{
case rt_vif:
memcpy((uint8_t *)vif, (uint8_t *)&msg.vif, sizeof(*vif));
return 0;
case rt_disconnect:
ERROR("Server disconnected: %*s", msg.disconnect.len,
msg.disconnect.msg);
return -1;
default:
ERROR("Unknown response type from server");
return -1;
}
}
int write_command(int fd, enum command *c)
{
uint8_t command = *c;
uint8_t command = *c;
if (really_write(fd, (uint8_t *)&command, sizeof(command)) == -1) {
ERROR("Failed to write command to client");
return -1;
}
return 0;
if (really_write(fd, (uint8_t *)&command, sizeof(command)) == -1)
{
ERROR("Failed to write command to client");
return -1;
}
return 0;
}
int write_ethernet_args(int fd, struct ethernet_args *args)
{
uint8_t buffer[40];
memset(&buffer[0], 0, sizeof(buffer));
memcpy(&buffer[0], (uint8_t *)&args->uuid_string[0], 36);
uint8_t buffer[40];
memset(&buffer[0], 0, sizeof(buffer));
memcpy(&buffer[0], (uint8_t *)&args->uuid_string[0], 36);
if (really_write(fd, (uint8_t *)&buffer, sizeof(buffer)) == -1) {
ERROR("Failed to write ethernet args to client");
return -1;
}
return 0;
if (really_write(fd, (uint8_t *)&buffer, sizeof(buffer)) == -1)
{
ERROR("Failed to write ethernet args to client");
return -1;
}
return 0;
}
+95 -53
View File
@@ -36,80 +36,122 @@
* limitations under the License.
****************************************************************************/
#ifndef _VMNET_PROTOCOL_H_
#define _VMNET_PROTOCOL_H_
#ifndef _ARCH_SIM_SRC_SIM_VPNKIT_PROTOCOL_H_
#define _ARCH_SIM_SRC_SIM_VPNKIT_PROTOCOL_H_
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <stdint.h>
/* Client -> Server init_message */
/* Server -> Client init_message */
struct init_message {
char hello[5];
uint8_t _padding[3];
uint32_t version;
char commit[40]; /* git sha of the compiled commit */
};
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/*
* This should be bumped whenever we add something (like a feature or a
/* This should be bumped whenever we add something (like a feature or a
* bugfix) and we wish the UI to be able to detect when to trigger a
* reinstall.
*/
#define CURRENT_VERSION 22
/****************************************************************************
* Public Types
****************************************************************************/
/* Client -> Server init_message */
/* Server -> Client init_message */
struct init_message
{
char hello[5];
uint8_t _padding[3];
uint32_t version;
char commit[40]; /* git sha of the compiled commit */
};
/* Client -> Server command */
enum command
{
ethernet = 1,
};
/* Server -> Client response */
enum response_type
{
rt_vif = 1,
rt_disconnect = 2,
};
/* Client -> Server command arguments */
struct ethernet_args
{
char uuid_string[36];
};
/* Server -> Client: details of a vif */
struct vif_info
{
uint16_t mtu;
uint16_t max_packet_size;
uint8_t mac[6];
}
__attribute__((packed));
/* Server -> Client: disconnect w/reason */
struct disconnect_reason
{
uint8_t len;
char msg[256];
}
__attribute__((packed));
struct msg_response
{
uint8_t response_type;
union
{
struct vif_info vif;
struct disconnect_reason disconnect;
};
}
__attribute__((packed));
/****************************************************************************
* Inline functions
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
extern char expected_hello[5];
extern char expected_hello_old[5];
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
extern struct init_message *create_init_message(void);
extern int read_init_message(int fd, struct init_message *ci);
extern int write_init_message(int fd, struct init_message *ci);
extern char *print_init_message(struct init_message *m);
/* Client -> Server command */
enum command {
ethernet = 1,
};
/* Server -> Client response */
enum response_type {
rt_vif = 1,
rt_disconnect = 2,
};
extern int write_command(int fd, enum command *c);
/* Client -> Server command arguments */
struct ethernet_args {
char uuid_string[36];
};
extern int write_ethernet_args(int fd, struct ethernet_args *args);
/* Server -> Client: details of a vif */
struct vif_info {
uint16_t mtu;
uint16_t max_packet_size;
uint8_t mac[6];
} __attribute__((packed));
/* Server -> Client: disconnect w/reason */
struct disconnect_reason {
uint8_t len;
char msg[256];
} __attribute__((packed));
struct msg_response {
uint8_t response_type;
union {
struct vif_info vif;
struct disconnect_reason disconnect;
};
} __attribute__((packed));
extern int read_vif_response(int fd, struct vif_info *vif);
extern char expected_hello[5];
extern char expected_hello_old[5];
extern int really_read(int fd, uint8_t *buffer, size_t total);
extern int really_write(int fd, uint8_t *buffer, size_t total);
#endif /* _VMNET_PROTOCOL_H_ */
#endif /* _ARCH_SIM_SRC_SIM_VPNKIT_PROTOCOL_H_ */