-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathntpm.c
154 lines (130 loc) · 3.91 KB
/
ntpm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <errno.h>
#include <ctype.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "ftdi_spi_tpm.h"
#include "tpm2lib_wrapper.h"
#define SOCKET_ERROR -1
static struct tpm_driver {
/* Parameter(s) may be irrelevant for some drivers. */
int (*drv_init)(uint32_t freq, int debug);
size_t (*drv_process)(uint8_t *message, size_t message_size);
void (*drv_stop)(void); /* Could be NULL. */
} drivers[] = {
{FtdiSpiInit, FtdiSendCommandAndWait, FtdiStop},
{Tpm2LibInit, Tpm2LibProcess}
};
const static char *help_msg =
" Command line options:\n"
" -d[d] - enable debug tracing (more d's - more debug)\n"
" -f NUM - ftdi clock frequency\n"
" -p NUM - port number\n"
" -s - use simulator instead of the USB interface\n";
int main( int argc, char *argv[] )
{
// create and open network socket
struct sockaddr_in serv_addr;
int sockfd;
int opt;
uint16_t port = 9883; /* default port */
uint32_t freq = 1000 * 1000; /* Default frequency 1 MHz */
int driver_index = 0;
int debug_level = 0;
int c;
debug_level = 0;
while ((c = getopt(argc, argv, "df:p:s")) != -1) {
switch (c) {
case 'd':
debug_level++;
break;
case 'f':
freq = atoi(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 's':
driver_index = 1;
break;
case '?':
if ((optopt == 'p') || (optopt == 'f')) {
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
return -1;
}
if (!isprint (optopt))
fprintf (stderr, "Unknown option character \\x%x'.\n", optopt);
default:
fprintf(stderr, "%s", help_msg);
return -2;
}
}
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == SOCKET_ERROR) {
fprintf(stderr, "failed to create socket, error %s\n", strerror(errno));
return -1;
}
memset( (char *) &serv_addr, 0, sizeof( serv_addr ) );
printf("Opening socket on port %d\n", port);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
fprintf(stderr, "failed to bind, error %s\n", strerror(errno));
return -1;
}
if (listen(sockfd, 1) == SOCKET_ERROR) {
fprintf(stderr, "Error %s on listen()\n", strerror(errno));
return -1;
}
if (!drivers[driver_index].drv_init(freq, debug_level)) {
fprintf(stderr, "Failed to initialize FTDI SPI\n");
return -1;
}
while (true) {
// main loop
uint8_t buffer[4096];
int len;
int newsockfd;
printf("\nWaiting for new connection...");
fflush(stdout);
newsockfd = accept(sockfd, 0, 0);
if (newsockfd == SOCKET_ERROR) {
fprintf(stderr, "ERROR on accept (%s)\n", strerror(errno));
shutdown(sockfd, SHUT_RDWR);
return -1;
}
printf("connected.\n");
do {
int written = 0;
len = recv(newsockfd, (char*) buffer, sizeof(buffer), 0);
if ( len == SOCKET_ERROR ) {
fprintf(stderr, "ERROR reading from socket %s\n", strerror(errno));
break;
}
if (!len) {
/* Socket reset on the client side. */
continue;
}
// write command to TPM and read result
len = drivers[driver_index].drv_process(buffer, len);
// write result to network
while (written != len) {
int count = send(newsockfd, buffer + written, len - written, 0);
if (count == SOCKET_ERROR )
fprintf(stderr, "ERROR writing to socket (%s)\n", strerror(errno));
written += count;
}
} while ( len > 0 );
shutdown(newsockfd, SHUT_RDWR);
}
shutdown(sockfd, SHUT_RDWR);
if (drivers[driver_index].drv_stop)
drivers[driver_index].drv_stop();
return 0;
}