/* * Used as a bridge for USBHID protocol for FIDO 2.0 and U2F to ease firmware development and testing. * * Client FIDO 2.0, U2F software should bind to UDP port 7112 to send/recv USBHID messages from. * * */ #include #include #include #include #include #include #include #include #include int udp_server() { int fd; if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { perror( "socket failed" ); return 1; } struct timeval read_timeout; read_timeout.tv_sec = 0; read_timeout.tv_usec = 10; if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof(struct timeval)) != 0) { perror( "setsockopt" ); exit(1); } /*fcntl(fd, F_SETFL, fcntl(fd,F_GETFL, 0)|O_NONBLOCK);*/ struct sockaddr_in serveraddr; memset( &serveraddr, 0, sizeof(serveraddr) ); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons( 8111 ); serveraddr.sin_addr.s_addr = htonl( INADDR_ANY ); if ( bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) { perror( "bind failed" ); exit(1); } return fd; } int udp_recv(int fd, uint8_t * buf, int size) { fd_set input; FD_ZERO(&input); FD_SET(fd, &input); struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 100000; int n = select(fd + 1, &input, NULL, NULL, &timeout); if (n == -1) { perror("select\n"); exit(1); } else if (n == 0) return 0; if (!FD_ISSET(fd, &input)) { } int length = recvfrom( fd, buf, size, 0, NULL, 0 ); if ( length < 0 ) { perror( "recvfrom failed" ); exit(1); } return length; } void udp_send(int fd, uint8_t * buf, int size) { struct sockaddr_in serveraddr; memset( &serveraddr, 0, sizeof(serveraddr) ); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons( 7112 ); serveraddr.sin_addr.s_addr = htonl( 0x7f000001 ); // (127.0.0.1) if (sendto( fd, buf, size, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0 ) { perror( "sendto failed" ); exit(1); } } void udp_close(int fd) { close(fd); }