diff --git a/can-bus/Makefile b/can-bus/Makefile index c17235e7..e284330f 100644 --- a/can-bus/Makefile +++ b/can-bus/Makefile @@ -1,6 +1,6 @@ CC=gcc LIBS=-lwolfssl -CFLAGS=-ggdb3 -Iisotp-c -Wno-cpp +CFLAGS=-Iisotp-c -Wno-cpp -Wall -Wextra -Wdeclaration-after-statement COMMON_OBJS=isotp-c/isotp.o common.o CLIENT_OBJS=client.o @@ -8,6 +8,9 @@ SERVER_OBJS=server.o all: client server +isotp-c/isotp.o: isotp-c/isotp.c + @$(CC) -c $< -o $@ + %.o: %.c @$(CC) -c $< -o $@ $(CFLAGS) diff --git a/can-bus/README.md b/can-bus/README.md index 7a819d7b..ada19371 100644 --- a/can-bus/README.md +++ b/can-bus/README.md @@ -35,32 +35,26 @@ server vcan0 On the client you will see (byte numbers will vary): ``` -Sending 164 bytes -Receiving 95 bytes -Receiving 954 bytes -Receiving 338 bytes -Receiving 9 bytes -Sending 75 bytes -Sending 6 bytes -Sending 45 bytes -Receiving 6 bytes -Receiving 45 bytes +Sending 242 bytes +Receiving 128 bytes +Receiving 28 bytes +Receiving 974 bytes +Receiving 286 bytes +Receiving 58 bytes +Sending 58 bytes SSL handshake done! ``` And on the server: ``` -Receiving 164 bytes -Sending 95 bytes -Sending 954 bytes -Sending 338 bytes -Sending 9 bytes -Receiving 75 bytes -Receiving 6 bytes -Receiving 45 bytes -Sending 6 bytes -Sending 45 bytes +Receiving 242 bytes +Sending 128 bytes +Sending 28 bytes +Sending 974 bytes +Sending 286 bytes +Sending 58 bytes +Receiving 58 bytes SSL handshake done! ``` @@ -69,22 +63,18 @@ Once you see the message "SSL handshake done!" on both consoles you can enter te For example, on the client if we type "Hello world, this is a TLS test!": ``` -Hello world, this is a TLS test! +Hello world! This is a CAN bus test! +Sending: Hello world! This is a CAN bus test! -Sending: Hello world, this is a TLS test! - - -Sending 62 bytes +Sending 59 bytes Message sent ``` The server will echo: ``` -Receiving 62 bytes - - -Got message: Hello world, this is a TLS test! +Receiving 59 bytes +Got message: Hello world! This is a CAN bus test! ``` ## Cleaning Up diff --git a/can-bus/client.c b/can-bus/client.c index 497d44d1..0c60224c 100644 --- a/can-bus/client.c +++ b/can-bus/client.c @@ -28,8 +28,6 @@ int main(int argc, char *argv[]) WOLFSSL_CTX *ctx = NULL; WOLFSSL_METHOD* method = NULL; WOLFSSL* ssl = NULL; - uint8_t data[CAN_MSG_LEN]; - int length; int ret; if (argc != 2) { @@ -53,7 +51,7 @@ int main(int argc, char *argv[]) ssize_t line_size = 0; line_size = getline(&line, &len, stdin); if (line_size > 0) { - printf("\nSending: %.*s\n\n", (int)line_size, line); + printf("Sending: %s\n", line); wolfSSL_send(ssl, line, line_size, 0); printf("Message sent\n"); } diff --git a/can-bus/common.c b/can-bus/common.c index 7ba4ced0..9feb614c 100644 --- a/can-bus/common.c +++ b/can-bus/common.c @@ -74,20 +74,21 @@ int isotp_user_send_can(const uint32_t arbitration_id, const uint8_t* data, /* Our CAN bus receive function */ int can_receive(uint8_t data[CAN_MSG_LEN], int *length) { int nbytes; + int ret; struct can_frame frame; struct pollfd p[1]; - p[0].fd = sock; - p[0].events = POLLIN; + p[0].fd = sock; + p[0].events = POLLIN; /* Poll for new data */ - int retval = poll(p, 1, 10); + ret = poll(p, 1, 10); - if (retval < 0) { + if (ret < 0) { perror("Poll error\n"); return 1; } - else if (retval == 0) { + else if (ret == 0) { /* No data */ *length = 0; return EAGAIN; @@ -159,7 +160,8 @@ int send_ssl(WOLFSSL *ssl, char *buf, int sz, void *ctx) uint8_t data[CAN_MSG_LEN]; int length; IsoTpLink *g_link = (struct IsoTpLink*)ctx; - int ret = isotp_send(g_link, buf, sz); + int ret = isotp_send(g_link, (uint8_t*)buf, sz); + (void) ssl; printf("Sending %d bytes\n", sz); if (ret) { @@ -189,8 +191,8 @@ int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx) uint8_t data[CAN_MSG_LEN]; int data_len; uint16_t msg_len = 0; - int ret; IsoTpLink *g_link = (struct IsoTpLink*)ctx; + (void) ssl; if (!copy_buf_len) { while (isotp_receive(g_link, copy_buf, ISOTP_BUFSIZE, &msg_len) @@ -213,7 +215,7 @@ int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx) } } - if (copy_buf_len >= sz) { + if (copy_buf_len >= (size_t)sz) { memcpy(buf, copy_buf_ptr, sz); copy_buf_ptr+= sz; copy_buf_len-= sz; @@ -231,11 +233,11 @@ int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx) void close_ssl(WOLFSSL_CTX *ctx, WOLFSSL *ssl) { if (ssl) { - int ret = SSL_SHUTDOWN_NOT_DONE; - while (ret == SSL_SHUTDOWN_NOT_DONE) { + int ret = WOLFSSL_SHUTDOWN_NOT_DONE; + while (ret == WOLFSSL_SHUTDOWN_NOT_DONE) { ret = wolfSSL_shutdown(ssl); } - if (ret != SSL_SUCCESS) { + if (ret != WOLFSSL_SUCCESS) { char buffer[ERR_MSG_LEN]; int err = wolfSSL_get_error(ssl, ret); fprintf(stderr, "Error shutting down TLS connection: %d, %s", @@ -247,10 +249,12 @@ void close_ssl(WOLFSSL_CTX *ctx, WOLFSSL *ssl) wolfSSL_free(ssl); wolfSSL_CTX_free(ctx); + wolfSSL_Cleanup(); } void sig_handle(int dummy) { + (void) dummy; keep_running = 0; } @@ -283,9 +287,9 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx, WOLFSSL* ssl = NULL; if (type == SERVICE_TYPE_CLIENT) { - method = wolfTLSv1_2_client_method(); + method = wolfTLSv1_3_client_method(); } else { - method = wolfTLSv1_2_server_method(); + method = wolfTLSv1_3_server_method(); } if (!method) { @@ -311,10 +315,10 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx, ret = wolfSSL_CTX_load_verify_locations(ctx, "client.pem", NULL); } else { ret = wolfSSL_CTX_use_certificate_file(ctx, "server.pem", - SSL_FILETYPE_PEM); + WOLFSSL_FILETYPE_PEM); } - if (ret != SSL_SUCCESS) { + if (ret != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: failed to load cert, " "please check the file.\n"); close_ssl(ctx, NULL); @@ -323,7 +327,7 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx, if (type == SERVICE_TYPE_SERVER) { if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, "server.key", - SSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { + WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { fprintf(stderr, "ERROR: failed to load key file, " "please check the file.\n"); close_ssl(ctx, NULL); @@ -350,7 +354,7 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx, wolfSSL_set_using_nonblock(ssl, 1); - if (ret != SSL_SUCCESS) { + if (ret != WOLFSSL_SUCCESS) { char buffer[ERR_MSG_LEN]; int err = wolfSSL_get_error(ssl, ret); fprintf(stderr, "ERROR: failed to connect using wolfSSL: %d, %s\n", diff --git a/can-bus/common.h b/can-bus/common.h index db94e13d..c3824ada 100644 --- a/can-bus/common.h +++ b/can-bus/common.h @@ -38,6 +38,7 @@ #include #include +#include #include #define ISOTP_BUFSIZE 16384 @@ -58,7 +59,7 @@ int can_connect(const char *address, uint16_t filter); void can_close(void); int send_ssl(WOLFSSL *ssl, char *buf, int sz, void *ctx); -int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx); +int recv_ssl(WOLFSSL* ssl, char *buf, int sz, void* ctx); void close_ssl(WOLFSSL_CTX *ctx, WOLFSSL *ssl); int setup_connection(const char *interface, int local_id, int remote_id); int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx, diff --git a/can-bus/server.c b/can-bus/server.c index ea8aa78b..0b750d36 100644 --- a/can-bus/server.c +++ b/can-bus/server.c @@ -21,6 +21,8 @@ #include "common.h" +#define RECV_MSG_LEN 64 + extern volatile int keep_running; int main(int argc, char *argv[]) @@ -46,10 +48,12 @@ int main(int argc, char *argv[]) } while(keep_running) { - char reply[64]; - int input = wolfSSL_read(ssl, reply, sizeof(reply)); + int input; + char reply[RECV_MSG_LEN]; + memset(reply, 0, RECV_MSG_LEN); + input = wolfSSL_read(ssl, reply, RECV_MSG_LEN); if (input > 0) { - printf("\n\nGot message: %.*s\n", input, reply); + printf("Got message: %s\n", reply); } }