-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpreg.c
273 lines (225 loc) · 5.92 KB
/
udpreg.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include "rbcp.h"
static char *default_host = "192.168.10.16";
static int default_port = 4660;
static struct sockaddr_in destsockaddr;
static int sequence = 0;
static int seq1 = 0;
int udpreg_open(char *hostname, int port)
{
//struct sockaddr_in destsockaddr;
struct addrinfo hints, *ai;
int sock;
int status;
struct timeval timeout={3, 0};
memset((char *)&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
status = getaddrinfo(hostname, NULL, &hints, &ai);
if (status != 0) {
fprintf(stderr, "Unknown host %s (%s)\n",
hostname, gai_strerror(status));
perror("udpreg_open");
return -1;
}
memset((char *)&destsockaddr, 0, sizeof(destsockaddr));
memcpy(&destsockaddr, ai->ai_addr, ai->ai_addrlen);
//destsockaddr.sin_addr.s_addr = ai.ai_addr.sin_addr.s_addr;
//destsockaddr.sin_addr.s_addr = inet_addr(dest);
destsockaddr.sin_port = htons(port);
destsockaddr.sin_family = AF_INET;
freeaddrinfo(ai);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("udpreg_open socket");
return -1;
}
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
(char *)&timeout, sizeof(struct timeval));
status = connect(sock,
(struct sockaddr *)&destsockaddr, sizeof(destsockaddr));
if (status != 0) {
perror("udpreg_open connect");
close(sock);
return -1;
}
return sock;
}
int udpreg_send(int sock, char *buf, int len)
{
int status;
status = sendto(sock, buf, len, 0,
(struct sockaddr *)&destsockaddr, sizeof(destsockaddr));
if (status != len) {
perror("udpreg_send sendto");
return -1;
}
return 0;
}
int udpreg_receive(int sock, char *buf)
{
int status;
//socklen_t fromlen;
//status = *len = recvfrom(sock, buf, 1024, 0, NULL, &fromlen);
status = recvfrom(sock, buf, 2048, 0, NULL, NULL);
if (status < 0) {
perror("udpreg_receive recvfrom (Timeout ?)");
return -1;
}
return status;
}
int udpreg_close(int sock)
{
return close(sock);
}
int updreg_wait_ans(int sock)
{
return 0;
}
int udpreg_read(int sock, char *buf, unsigned int addr, int len)
{
struct bcp_header bcp;
struct rbcp_header *rbcp;
char *data;
int status;
int rlen = 0;
int i;
bcp.type = RBCP_VER;
bcp.command = RBCP_CMD_RD;
bcp.address = htonl(addr);
bcp.length = len;
bcp.id = sequence++;
status = udpreg_send(sock, (char *)&bcp, sizeof(bcp));
if (status < 0) printf("udpreg_read udp_send: error\n");
//fprintf(stderr, "#D send\n");
rlen = udpreg_receive(sock, buf);
//fprintf(stderr, "#D receive %d\n", rlen);
rbcp = (struct rbcp_header *)buf;
data = buf + sizeof(struct rbcp_header);
if (rlen > 0) {
printf("type: 0x%x, command: 0x%x, id: 0x%x, length: %d, address: %d",
rbcp->type & 0xff, rbcp->command& 0xff,
rbcp->id & 0xff, rbcp->length & 0xff,
ntohl(rbcp->address & 0xffffffff));
for (i = 0 ; i < (int)(rlen - sizeof(struct rbcp_header)) ; i++) {
if ((i % 8) == 0) printf("\n%04x: ", addr + i);
printf("%02x ", data[i] & 0xff);
}
printf("\n");
}
return 0;
}
int udpreg_write(int sock, char *data, unsigned int addr, int len)
{
struct bcp_header *bcp;
struct rbcp_header *rbcp;
char *bcp_body;
static char sbuf[2048];
static char rbuf[2048];
int status;
int rlen = 0;
int i;
bcp = (struct bcp_header *)sbuf;
bcp_body = sbuf + sizeof(struct bcp_header);
bcp->type = RBCP_VER;
bcp->command = RBCP_CMD_WR;
bcp->address = htonl(addr);
//bcp->length = len + sizeof(struct bcp_header);
bcp->length = len;
bcp->id = sequence++;
if (len < 2048 - sizeof(struct bcp_header)) {
memcpy(bcp_body, data, len);
} else {
fprintf(stderr, "udpreg_write: Buffer overflow %d\n", len);
return -1;
}
status = udpreg_send(sock, (char *)&sbuf, sizeof(struct bcp_header) + len);
if (status < 0) printf("udpreg_write udp_send: error\n");
//fprintf(stderr, "#D send\n");
rlen = udpreg_receive(sock, rbuf);
//fprintf(stderr, "#D receive %d\n", rlen);
rbcp = (struct rbcp_header *)rbuf;
data = rbuf + sizeof(struct rbcp_header);
if (rlen > 0) {
printf("type: 0x%x, command: 0x%x, id: 0x%x, length: %d, address: %d",
rbcp->type & 0xff, rbcp->command& 0xff,
rbcp->id & 0xff, rbcp->length & 0xff,
ntohl(rbcp->address & 0xffffffff));
for (i = 0 ; i < (int)(rlen - sizeof(struct rbcp_header)) ; i++) {
if ((i % 8) == 0) printf("\n%04x: ", addr + i);
printf("%02x ", data[i] & 0xff);
}
printf("\n");
}
return rlen;
}
void helpline(char *cname)
{
printf("%s <Commads>\n", cname);
printf("Commands \n");
printf("--host=<hotname>\n");
printf("--port=<port nmber>\n");
printf("--read=<address>:<number of reading>\n");
printf("--write=<addres>:<data>\n");
return;
}
int main(int argc, char* argv[])
{
int sock;
//struct bcp_header bcp;
static char buf[1024];
//int rlen;
char hostname[256];
int port;
unsigned int raddress, rlen;
unsigned int waddress;
int wdata;
int status;
int i;
strcpy(hostname, default_host);
port = default_port;
wdata = -1;
rlen = 0;
for (i = 1 ; i < argc ; i++) {
if (sscanf(argv[i], "--host=%s", hostname) == 1) ;
if (sscanf(argv[i], "--port=%d", &port) == 1) ;
if (sscanf(argv[i], "--read=%x:%x", &raddress, &rlen) == 1);
if (sscanf(argv[i], "--write=%x:%x", &waddress, &wdata) == 1);
if (strncmp(argv[i], "--seq1", 6) == 0) seq1 = 1;
}
printf("host: %s\n", hostname);
sock = udpreg_open(hostname, port);
if (rlen > 0) {
printf("read: 0x%x : %d\n", raddress, rlen);
udpreg_read(sock, buf, raddress, rlen);
}
if (wdata >= 0) {
printf("write: 0x%x : %x\n", waddress, wdata);
buf[0] = wdata & 0xff;
buf[1] = 0;
status = udpreg_write(sock, buf, waddress, 1);
if (status <= 0) printf("Write Error %d\n", status);
}
if (seq1 == 1) {
int j;
for (j = 0 ; j < 128 ; j++) {
for (i = 0 ; i < 128 ; i++) buf[i] = i;
buf[0] = j;
udpreg_write(sock, buf, 0 + 128*j, 128);
}
}
udpreg_close(sock);
return 0;
}