Added support for building emulation in Windows.

It has not been tested but it should not break any linux build.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2024-01-01 01:54:49 +01:00
parent adf53b4231
commit a9dc6fd7f8
22 changed files with 681 additions and 269 deletions

View File

@@ -17,31 +17,45 @@
#include "emulation.h"
#include <stdio.h>
#ifndef _MSC_VER
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <poll.h>
#include <netinet/tcp.h>
typedef int socket_t;
#include <fcntl.h>
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#else
#include <ws2tcpip.h>
#define O_NONBLOCK _O_NONBLOCK
#define close closesocket
typedef SOCKET socket_t;
typedef int socklen_t;
#define msleep Sleep
#pragma comment(lib, "Ws2_32.lib")
#endif
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include "pico_keys.h"
#include "apdu.h"
#include "usb.h"
#include "ccid/ccid.h"
#include <netinet/tcp.h>
int ccid_sock = 0;
int hid_server_sock = 0;
int hid_client_sock = -1;
socket_t ccid_sock = 0;
socket_t hid_server_sock = 0;
socket_t hid_client_sock = INVALID_SOCKET;
extern uint8_t thread_type;
extern const uint8_t *cbor_data;
extern size_t cbor_len;
extern uint8_t cmd;
extern int cbor_parse(uint8_t cmd, const uint8_t *data, size_t len);
#ifndef _MSC_VER
int msleep(long msec) {
struct timespec ts;
int res;
@@ -60,11 +74,18 @@ int msleep(long msec) {
return res;
}
#endif
int emul_init(char *host, uint16_t port) {
struct sockaddr_in serv_addr;
fprintf(stderr, "\n Starting emulation envionrment\n");
if ((ccid_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#ifdef _MSC_VER
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("winsock initialization failure\n");
}
#endif
if ((ccid_sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
perror("socket");
return -1;
}
@@ -85,10 +106,17 @@ int emul_init(char *host, uint16_t port) {
close(ccid_sock);
return -1;
}
#ifdef _MSC_VER
unsigned long on = 1;
if (0 != ioctlsocket(ccid_sock, FIONBIO, &on)) {
perror("ioctlsocket FIONBIO");
}
#else
int x = fcntl(ccid_sock, F_GETFL, 0);
fcntl(ccid_sock, F_SETFL, x | O_NONBLOCK);
int flag = 1;
setsockopt(ccid_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
#endif
// HID server
@@ -96,7 +124,7 @@ int emul_init(char *host, uint16_t port) {
uint16_t hid_port = port - 1;
struct sockaddr_in server_sockaddr;
if ((hid_server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
if ((hid_server_sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
perror("socket");
return -1;
}
@@ -107,7 +135,7 @@ int emul_init(char *host, uint16_t port) {
return 1;
}
#if HAVE_DECL_SO_NOSIGPIPE
#if defined(HAVE_DECL_SO_NOSIGPIPE)
if (setsockopt(hid_server_sock, SOL_SOCKET, SO_NOSIGPIPE, (void *) &yes, sizeof yes) != 0) {
perror("setsockopt");
close(hid_server_sock);
@@ -145,7 +173,7 @@ uint8_t *driver_prepare_response_emul(uint8_t itf) {
return apdu.rdata;
}
int get_sock_itf(uint8_t itf) {
socket_t get_sock_itf(uint8_t itf) {
#ifdef USB_ITF_CCID
if (itf == ITF_CCID) {
return ccid_sock;
@@ -156,7 +184,7 @@ int get_sock_itf(uint8_t itf) {
return hid_client_sock;
}
#endif
return -1;
return INVALID_SOCKET;
}
extern void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, uint16_t len);
@@ -164,20 +192,20 @@ const uint8_t *complete_report = NULL;
uint16_t complete_len = 0;
extern bool last_write_result;
extern uint16_t send_buffer_size[ITF_TOTAL];
int driver_write_emul(uint8_t itf, const uint8_t *buffer, size_t buffer_size) {
uint16_t driver_write_emul(uint8_t itf, const uint8_t *buffer, uint16_t buffer_size) {
uint16_t size = htons(buffer_size);
int sock = get_sock_itf(itf);
socket_t sock = get_sock_itf(itf);
// DEBUG_PAYLOAD(buffer,buffer_size);
int ret = 0;
do {
ret = send(sock, &size, sizeof(size), 0);
if (ret < 0) {
ret = send(sock, (const char *)&size, sizeof(size), 0);
if (ret == SOCKET_ERROR) {
msleep(10);
}
} while (ret <= 0);
do {
ret = send(sock, buffer, (uint16_t) buffer_size, 0);
if (ret < 0) {
ret = send(sock, (const char *)buffer, buffer_size, 0);
if (ret == SOCKET_ERROR) {
msleep(10);
}
} while (ret <= 0);
@@ -203,7 +231,7 @@ uint32_t emul_write(uint8_t itf, uint16_t size) {
return emul_write_offset(itf, size, 0);
}
void driver_exec_finished_cont_emul(uint8_t itf, size_t size_next, size_t offset) {
void driver_exec_finished_cont_emul(uint8_t itf, uint16_t size_next, uint16_t offset) {
#ifdef USB_ITF_HID
if (itf == ITF_HID) {
driver_exec_finished_cont_hid(size_next, offset);
@@ -218,7 +246,7 @@ void driver_exec_finished_cont_emul(uint8_t itf, size_t size_next, size_t offset
int driver_process_usb_packet_emul(uint8_t itf, uint16_t len) {
if (len > 0) {
uint8_t *data = usb_get_rx(itf), *rdata = usb_get_tx(itf);
uint8_t *data = usb_get_rx(itf);
#ifdef USB_ITF_CCID
if (itf == ITF_CCID) {
if (len == 1) {
@@ -228,14 +256,14 @@ int driver_process_usb_packet_emul(uint8_t itf, uint16_t len) {
}
}
else {
size_t sent = 0;
uint16_t sent = 0;
DEBUG_PAYLOAD(data, len);
if ((sent = apdu_process(itf, data, len)) > 0) {
process_apdu();
}
apdu_finish();
if (sent > 0) {
size_t ret = apdu_next();
uint16_t ret = apdu_next();
DEBUG_PAYLOAD(rdata, ret);
emul_write(itf, ret);
}
@@ -301,7 +329,7 @@ uint16_t emul_read(uint8_t itf) {
}
}
#endif
int sock = get_sock_itf(itf);
socket_t sock = get_sock_itf(itf);
//printf("get_sockt itf %d - %d\n", itf, sock);
uint16_t len = 0;
fd_set input;
@@ -310,7 +338,7 @@ uint16_t emul_read(uint8_t itf) {
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0 * 1000;
int n = select(sock + 1, &input, NULL, NULL, &timeout);
int n = select((int)(sock + 1), &input, NULL, NULL, &timeout);
if (n == -1) {
//printf("read wrong [itf:%d]\n", itf);
//something wrong
@@ -319,13 +347,13 @@ uint16_t emul_read(uint8_t itf) {
//printf("read timeout [itf:%d]\n", itf);
}
if (FD_ISSET(sock, &input)) {
int valread = recv(sock, &len, sizeof(len), 0);
int valread = recv(sock, (char *)&len, sizeof(len), 0);
len = ntohs(len);
if (len > 0) {
while (true) {
valread = recv(sock, usb_get_rx(itf), len, 0);
valread = recv(sock, (char *)usb_get_rx(itf), len, 0);
if (valread > 0) {
return valread;
return (uint16_t)valread;
}
msleep(10);
}

View File

@@ -47,7 +47,7 @@ uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset) {
#ifndef ENABLE_EMULATION
uint8_t pkt_max = 64;
#endif
int w = 0;
uint16_t w = 0;
if (len > sizeof(tx_buffer[itf])) {
len = sizeof(tx_buffer[itf]);
}
@@ -72,8 +72,9 @@ uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset) {
return w;
}
size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len) {
uint16_t size = MIN(sizeof(rx_buffer[itf]) - w_offset[itf], len);
#ifndef ENABLE_EMULATION
uint16_t usb_rx(uint8_t itf, const uint8_t *buffer, uint16_t len) {
uint16_t size = MIN((uint16_t)sizeof(rx_buffer[itf]) - w_offset[itf], (uint16_t)len);
if (size > 0) {
if (buffer == NULL) {
#ifdef USB_ITF_HID
@@ -83,7 +84,7 @@ size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len) {
#endif
#ifdef USB_ITF_CCID
if (itf == ITF_CCID) {
size = driver_read_ccid(rx_buffer[itf] + w_offset[itf], size);
size = (uint16_t)driver_read_ccid(rx_buffer[itf] + w_offset[itf], size);
}
#endif
}
@@ -94,9 +95,10 @@ size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len) {
}
return size;
}
#endif
uint32_t usb_write_flush(uint8_t itf) {
int w = 0;
uint16_t w = 0;
if (w_len[itf] > 0) {
#ifndef ENABLE_EMULATION
#ifdef USB_ITF_HID
@@ -217,7 +219,7 @@ void card_init_core1() {
#endif
}
size_t finished_data_size = 0;
uint16_t finished_data_size = 0;
void card_start(void (*func)(void)) {
#ifndef ENABLE_EMULATION
@@ -237,6 +239,8 @@ void card_start(void (*func)(void)) {
multicore_launch_core1(func);
}
led_set_blink(BLINK_MOUNTED);
#else
(void)func;
#endif
}

View File

@@ -66,43 +66,43 @@ extern uint8_t card_locked_itf;
#ifdef USB_ITF_HID
extern int driver_process_usb_packet_hid(uint16_t rx_read);
extern void driver_exec_finished_hid(size_t size_next);
extern void driver_exec_finished_cont_hid(size_t size_next, size_t offset);
extern void driver_exec_finished_hid(uint16_t size_next);
extern void driver_exec_finished_cont_hid(uint16_t size_next, uint16_t offset);
extern void driver_exec_timeout_hid();
extern bool driver_mounted_hid();
extern uint8_t *driver_prepare_response_hid();
extern int driver_write_hid(uint8_t, const uint8_t *, size_t);
extern size_t driver_read_hid(uint8_t *, size_t);
extern int driver_write_hid(uint8_t, const uint8_t *, uint16_t);
extern uint16_t driver_read_hid(uint8_t *, uint16_t);
extern int driver_process_usb_nopacket_hid();
#endif
#ifdef USB_ITF_CCID
extern int driver_process_usb_packet_ccid(uint16_t rx_read);
extern void driver_exec_finished_ccid(size_t size_next);
extern void driver_exec_finished_cont_ccid(size_t size_next, size_t offset);
extern void driver_exec_finished_ccid(uint16_t size_next);
extern void driver_exec_finished_cont_ccid(uint16_t size_next, uint16_t offset);
extern void driver_exec_timeout_ccid();
extern bool driver_mounted_ccid();
extern uint8_t *driver_prepare_response_ccid();
extern int driver_write_ccid(const uint8_t *, size_t);
extern size_t driver_read_ccid(uint8_t *, size_t);
extern int driver_write_ccid(const uint8_t *, uint16_t);
extern uint16_t driver_read_ccid(uint8_t *, uint16_t);
extern int driver_process_usb_nopacket_ccid();
#endif
#ifdef ENABLE_EMULATION
extern int driver_process_usb_packet_emul(uint8_t, uint16_t rx_read);
extern void driver_exec_finished_emul(uint8_t, size_t size_next);
extern void driver_exec_finished_cont_emul(uint8_t, size_t size_next, size_t offset);
extern void driver_exec_finished_emul(uint8_t, uint16_t size_next);
extern void driver_exec_finished_cont_emul(uint8_t, uint16_t size_next, uint16_t offset);
extern void driver_exec_timeout_emul(uint8_t);
extern bool driver_mounted_emul(uint8_t);
extern uint8_t *driver_prepare_response_emul(uint8_t);
extern int driver_write_emul(uint8_t, const uint8_t *, size_t);
extern size_t driver_read_emul(uint8_t, uint8_t *, size_t);
extern uint16_t driver_write_emul(uint8_t, const uint8_t *, uint16_t);
extern uint16_t driver_read_emul(uint8_t, uint8_t *, uint16_t);
extern int driver_process_usb_nopacket_emul(uint8_t);
extern uint16_t emul_read(uint8_t);
#else
extern uint16_t usb_rx(uint8_t itf, const uint8_t *buffer, uint16_t len);
#endif
extern size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len);
extern void card_start(void (*func)(void));
extern void card_exit();
extern void usb_init();
@@ -111,7 +111,7 @@ extern uint8_t *usb_get_rx(uint8_t itf);
extern uint8_t *usb_get_tx(uint8_t itf);
extern uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset);
extern void usb_clear_rx(uint8_t itf);
extern size_t finished_data_size;
extern uint16_t finished_data_size;
extern void usb_set_timeout_counter(uint8_t itf, uint32_t v);
extern void card_init_core1();
extern uint32_t usb_write_flush(uint8_t itf);