25 #include <arpa/inet.h>
28 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
40 #define HWPORT "4950" // the port of the front end (hardware) target
41 #define MAXBUFLEN 1492
44 void* get_in_addr(
struct sockaddr* sa)
46 if(sa->sa_family == AF_INET)
48 return &(((
struct sockaddr_in*)sa)->sin_addr);
51 return &(((
struct sockaddr_in6*)sa)->sin6_addr);
54 int makeSocket(
char* ip,
int port)
57 struct addrinfo hints, *servinfo, *p;
60 struct sockaddr_storage their_addr;
62 char s[INET6_ADDRSTRLEN];
64 memset(&hints, 0,
sizeof hints);
65 hints.ai_family = AF_UNSPEC;
66 hints.ai_socktype = SOCK_DGRAM;
68 if((rv = getaddrinfo(ip, HWPORT, &hints, &servinfo)) != 0)
70 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
75 for(p = servinfo; p != NULL; p = p->ai_next)
77 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
88 fprintf(stderr,
"sw: failed to create socket\n");
92 freeaddrinfo(servinfo);
97 int main(
int argc,
char* argv[])
104 struct addrinfo hints, *servinfo, *p;
107 struct sockaddr_storage their_addr;
109 char s[INET6_ADDRSTRLEN];
113 fprintf(stderr,
"usage: sw hostname type-of-test\n");
117 memset(&hints, 0,
sizeof hints);
118 hints.ai_family = AF_UNSPEC;
119 hints.ai_socktype = SOCK_DGRAM;
121 if((rv = getaddrinfo(argv[1], HWPORT, &hints, &servinfo)) != 0)
123 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
128 for(p = servinfo; p != NULL; p = p->ai_next)
130 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
132 perror(
"sw: socket");
141 fprintf(stderr,
"sw: failed to create socket\n");
145 freeaddrinfo(servinfo);
151 char buff[MAXBUFLEN];
152 unsigned int packetSz;
153 int type = atoi(argv[2]);
154 cout <<
"sw: Line " << __LINE__ <<
":::"
155 <<
"Type of Test: " << type << endl;
160 string test =
"hehehel";
164 const unsigned int RX_ADDR_OFFSET = 2;
165 const unsigned int RX_DATA_OFFSET = 10;
166 const unsigned int TX_DATA_OFFSET = 2;
178 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
181 for(
int i = 0; i < buff[1]; ++i, ++val)
182 memcpy((
void*)&buff[RX_DATA_OFFSET + i * 8],
185 packetSz = RX_DATA_OFFSET + buff[1] * 8;
187 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
190 perror(
"sw: sendto");
193 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
197 packetSz = RX_DATA_OFFSET;
199 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
202 perror(
"sw: sendto");
205 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
206 printf(
"sent packet contents: ");
208 for(
int i = 0; i < numbytes; ++i)
210 printf(
"%2.2X", (
unsigned char)buff[i]);
217 if((numbytes = recvfrom(sockfd,
221 (
struct sockaddr*)&their_addr,
228 printf(
"sw: got read response from %s\n",
229 inet_ntop(their_addr.ss_family,
230 get_in_addr((
struct sockaddr*)&their_addr),
233 printf(
"sw: packet is %d bytes long\n", numbytes);
234 printf(
"recv packet contents: ");
236 for(
int i = 0; i < numbytes; ++i)
238 printf(
"%2.2X", (
unsigned char)buff[i]);
244 memcpy((
void*)&val, (
void*)&buff[TX_DATA_OFFSET], 8);
245 cout <<
"sw: Line " << __LINE__ <<
":::"
246 <<
"Value read: " << hex << val << endl;
256 addr = 0x0000000100000009;
257 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
258 memset((
void*)&buff[RX_DATA_OFFSET], 0, 7);
259 memset((
void*)&buff[RX_DATA_OFFSET + 7], 1, 1);
260 packetSz = RX_DATA_OFFSET + buff[1] * 8;
262 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
265 perror(
"sw: sendto");
268 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
275 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
278 memcpy((
void*)&buff[RX_DATA_OFFSET], (
void*)&val, 8);
279 packetSz = RX_DATA_OFFSET + buff[1] * 8;
281 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
284 perror(
"sw: sendto");
287 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
293 cout <<
"sw: Line " << __LINE__ <<
":::"
294 <<
"Number of packets expecting: " << sz << endl;
295 for(
int i = 0; i < sz; ++i)
297 cout <<
"sw: Line " << __LINE__ <<
":::"
298 <<
"Waiting for data packet: " << i << endl;
302 if((numbytes = recvfrom(sockfd,
306 (
struct sockaddr*)&their_addr,
313 printf(
"sw: got read response from %s\n",
314 inet_ntop(their_addr.ss_family,
315 get_in_addr((
struct sockaddr*)&their_addr),
318 printf(
"sw: packet is %d bytes long\n", numbytes);
319 printf(
"recv packet contents: ");
321 for(
int i = 0; i < numbytes; ++i)
323 printf(
"%2.2X", (
unsigned char)buff[i]);
329 memcpy((
void*)&val, (
void*)&buff[TX_DATA_OFFSET], 8);
330 cout <<
"sw: Line " << __LINE__ <<
":::"
331 <<
"Value read: " << val << endl;
339 addr = 0x0000000100000009;
340 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
341 memset((
void*)&buff[RX_DATA_OFFSET], 0, 7);
342 memset((
void*)&buff[RX_DATA_OFFSET + 7], 0, 1);
343 packetSz = RX_DATA_OFFSET + buff[1] * 8;
345 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
348 perror(
"sw: sendto");
351 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
354 if((numbytes = recvfrom(sockfd,
358 (
struct sockaddr*)&their_addr,
365 printf(
"sw: got read response from %s\n",
366 inet_ntop(their_addr.ss_family,
367 get_in_addr((
struct sockaddr*)&their_addr),
370 printf(
"sw: packet is %d bytes long\n", numbytes);
371 printf(
"recv packet contents: ");
373 for(
int i = 0; i < numbytes; ++i)
375 printf(
"%2.2X", (
unsigned char)buff[i]);
382 cout <<
"sw: Line " << __LINE__ <<
":::"
383 <<
"INVALID Type of Test: " << type << endl;