Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
lib_util.c File Reference
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include "yubihsm.h"
#include "internal.h"
#include "debug_lib.h"
Include dependency graph for lib_util.c:

Go to the source code of this file.

Macros

#define STATUS_STR   "status="
 
#define VERSION_STR   "version="
 
#define PID_STR   "pid="
 
#define ADDRESS_STR   "address="
 
#define PORT_STR   "port="
 

Functions

void dump_hex (FILE *file, const uint8_t *ptr, uint16_t len)
 
void dump_msg (FILE *file, const Msg *msg)
 
void dump_response (FILE *file, const Msg *msg)
 
void parse_status_data (char *data, yh_connector *connector)
 
bool parse_usb_url (const char *url, unsigned long *serial)
 

Macro Definition Documentation

◆ ADDRESS_STR

#define ADDRESS_STR   "address="

Definition at line 30 of file lib_util.c.

◆ PID_STR

#define PID_STR   "pid="

Definition at line 29 of file lib_util.c.

◆ PORT_STR

#define PORT_STR   "port="

Definition at line 31 of file lib_util.c.

◆ STATUS_STR

#define STATUS_STR   "status="

Definition at line 27 of file lib_util.c.

◆ VERSION_STR

#define VERSION_STR   "version="

Definition at line 28 of file lib_util.c.

Function Documentation

◆ dump_hex()

void dump_hex ( FILE * file,
const uint8_t * ptr,
uint16_t len )

Definition at line 33 of file lib_util.c.

33 {
34
35 uint32_t i;
36
37 for (i = 0; i < len; i++) {
38 if (i && !(i % 8)) {
39 fprintf(file, " ");
40 }
41 fprintf(file, "%02x", ptr[i]);
42 }
43}
unsigned int uint32_t
Definition stdint.h:126
size_t len
Here is the caller graph for this function:

◆ dump_msg()

void dump_msg ( FILE * file,
const Msg * msg )

Definition at line 45 of file lib_util.c.

45 {
46
47 fprintf(file, "SEND >> (03 + %04d) %02x %04x ", msg->st.len, msg->st.cmd,
48 msg->st.len);
49
50 dump_hex(file, msg->st.data, msg->st.len);
51
52 fprintf(file, "\n");
53}
void dump_hex(FILE *file, const uint8_t *ptr, uint16_t len)
Definition lib_util.c:33
struct _Msg::@103 st
Here is the call graph for this function:

◆ dump_response()

void dump_response ( FILE * file,
const Msg * msg )

Definition at line 55 of file lib_util.c.

55 {
56
57 fprintf(file, "RECV << (03 + %04d) %02x %04x ", msg->st.len, msg->st.cmd,
58 msg->st.len);
59
60 dump_hex(file, msg->st.data, msg->st.len);
61
62 fprintf(file, " \n");
63}
Here is the call graph for this function:

◆ parse_status_data()

void parse_status_data ( char * data,
yh_connector * connector )

Definition at line 65 of file lib_util.c.

65 {
66
67 char *saveptr = NULL;
68 char *str = NULL;
69
70 while ((str = strtok_r(str ? NULL : data, "\n", &saveptr))) {
71 if (strncmp(str, STATUS_STR, strlen(STATUS_STR)) == 0) {
72 if (strcmp(str + strlen(STATUS_STR), "OK") == 0) {
73 connector->has_device = true;
74 } else {
75 connector->has_device = false;
76 }
77 } else if (strncmp(str, VERSION_STR, strlen(VERSION_STR)) == 0) {
78 unsigned long v_maj = 0;
79 unsigned long v_min = 0;
80 unsigned long v_pat = 0;
81
82 str = str + strlen(VERSION_STR);
83 if (sscanf(str, "%lu.%lu.%lu", &v_maj, &v_min, &v_pat) == 0) {
84 DBG_ERR("Unable to parse version string");
85 continue;
86 }
87
88 connector->version_major = v_maj;
89 connector->version_minor = v_min;
90 connector->version_patch = v_pat;
91 } else if (strncmp(str, PID_STR, strlen(PID_STR)) == 0) {
92 char *endptr;
93 unsigned long pid;
94
95 str = str + strlen(PID_STR);
96 errno = 0;
97 pid = strtoul(str, &endptr, 0);
98 if ((errno == ERANGE && pid == ULONG_MAX) || (errno != 0 && pid == 0)) {
99 continue;
100 }
101
102 if (endptr == str || pid == 0) {
103 continue;
104 }
105
106 connector->pid = pid;
107 } else if (strncmp(str, ADDRESS_STR, strlen(ADDRESS_STR)) == 0) {
108 strncpy((char *) connector->address, str + strlen(ADDRESS_STR),
109 sizeof(connector->address) - 1);
110 } else if (strncmp(str, PORT_STR, strlen(PORT_STR)) == 0) {
111 char *endptr;
112 unsigned long port;
113
114 str = str + strlen(PORT_STR);
115 errno = 0;
116 port = strtoul(str, &endptr, 0);
117 if ((errno == ERANGE && port == ULONG_MAX) || (errno != 0 && port == 0)) {
118 continue;
119 }
120
121 if (endptr == str || port == 0) {
122 continue;
123 }
124
125 connector->port = port;
126 }
127 }
128
129 DBG_INFO("response from connector");
130 DBG_INFO("has device: %s", connector->has_device == true ? "yes" : "no");
131 DBG_INFO("version: %d.%d.%d", connector->version_major,
132 connector->version_minor, connector->version_patch);
133 DBG_INFO("pid: %u", connector->pid);
134 DBG_INFO("address: %s", connector->address);
135 DBG_INFO("port: %u", connector->port);
136
137 return;
138}
#define DBG_ERR(...)
Definition debug_lib.h:76
#define DBG_INFO(...)
Definition debug_lib.h:63
#define PORT_STR
Definition lib_util.c:31
#define PID_STR
Definition lib_util.c:29
#define STATUS_STR
Definition lib_util.c:27
#define VERSION_STR
Definition lib_util.c:28
#define ADDRESS_STR
Definition lib_util.c:30
return str
Definition CLI11.hpp:1359
bool has_device
Definition internal.h:43
uint8_t address[32]
Definition internal.h:47
uint32_t port
Definition internal.h:48
uint8_t version_patch
Definition internal.h:46
uint32_t pid
Definition internal.h:49
uint8_t version_minor
Definition internal.h:45
uint8_t version_major
Definition internal.h:44

◆ parse_usb_url()

bool parse_usb_url ( const char * url,
unsigned long * serial )

Definition at line 140 of file lib_util.c.

140 {
141 if (strncmp(url, YH_USB_URL_SCHEME, strlen(YH_USB_URL_SCHEME)) == 0) {
142 url += strlen(YH_USB_URL_SCHEME);
143 char *copy = strdup(url);
144 char *str = NULL;
145 char *saveptr = NULL;
146
147 // if we don't find a serial we still want to return serial 0
148 *serial = 0;
149
150 while ((str = strtok_r(str ? NULL : copy, "&", &saveptr))) {
151 if (strncmp(str, "serial=", strlen("serial=")) == 0) {
152 char *endptr;
153 str += strlen("serial=");
154
155 errno = 0;
156 *serial = strtoul(str, &endptr, 0);
157 if ((errno == ERANGE && *serial == ULONG_MAX) || endptr == str ||
158 (errno != 0 && *serial == 0)) {
159 *serial = 0;
160 DBG_INFO("Failed to parse serial argument: '%s'.", str);
161 }
162 } else {
163 DBG_INFO("Unknown USB option '%s'.", str);
164 }
165 }
166 DBG_INFO("USB url parsed with serial %lu.", *serial);
167 free(copy);
168 return true;
169 }
170 return false;
171}
void copy(const path &from, const path &to)
string url
Definition main.cpp:166
#define YH_USB_URL_SCHEME
URL scheme used for direct USB access.
Definition yubihsm.h:129
uint32_t serial