25 #include <arpa/inet.h>
28 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
38 #define HWPORT "4950" // the port of the front end (hardware) target
39 #define MAXBUFLEN 1492
42 void* get_in_addr(
struct sockaddr* sa)
44 if(sa->sa_family == AF_INET)
46 return &(((
struct sockaddr_in*)sa)->sin_addr);
49 return &(((
struct sockaddr_in6*)sa)->sin6_addr);
52 int makeSocket(
char* ip,
int )
55 struct addrinfo hints, *servinfo, *p;
62 memset(&hints, 0,
sizeof hints);
63 hints.ai_family = AF_UNSPEC;
64 hints.ai_socktype = SOCK_DGRAM;
66 if((rv = getaddrinfo(ip, HWPORT, &hints, &servinfo)) != 0)
68 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
73 for(p = servinfo; p != NULL; p = p->ai_next)
75 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
86 fprintf(stderr,
"sw: failed to create socket\n");
90 freeaddrinfo(servinfo);
95 int main(
int argc,
char* argv[])
102 struct addrinfo hints, *servinfo, *p;
105 struct sockaddr_storage their_addr;
107 char s[INET6_ADDRSTRLEN];
111 fprintf(stderr,
"usage: sw hostname type-of-test\n");
115 memset(&hints, 0,
sizeof hints);
116 hints.ai_family = AF_UNSPEC;
117 hints.ai_socktype = SOCK_DGRAM;
119 if((rv = getaddrinfo(argv[1], HWPORT, &hints, &servinfo)) != 0)
121 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
126 for(p = servinfo; p != NULL; p = p->ai_next)
128 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
130 perror(
"sw: socket");
139 fprintf(stderr,
"sw: failed to create socket\n");
143 freeaddrinfo(servinfo);
149 char buff[MAXBUFLEN];
150 unsigned int packetSz;
151 int type = atoi(argv[2]);
152 std::cout <<
"sw: Line " << __LINE__ <<
":::"
153 <<
"Type of Test: " << type << std::endl;
158 std::string test =
"hehehel";
160 std::cout << std::endl;
162 const unsigned int RX_ADDR_OFFSET = 2;
163 const unsigned int RX_DATA_OFFSET = 10;
164 const unsigned int TX_DATA_OFFSET = 2;
175 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
178 for(
int i = 0; i < buff[1]; ++i, ++val)
179 memcpy((
void*)&buff[RX_DATA_OFFSET + i * 8],
182 packetSz = RX_DATA_OFFSET + buff[1] * 8;
184 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
187 perror(
"sw: sendto");
190 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
194 packetSz = RX_DATA_OFFSET;
196 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
199 perror(
"sw: sendto");
202 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
203 printf(
"sent packet contents: ");
205 for(
int i = 0; i < numbytes; ++i)
207 printf(
"%2.2X", (
unsigned char)buff[i]);
214 if((numbytes = recvfrom(sockfd,
218 (
struct sockaddr*)&their_addr,
225 printf(
"sw: got read response from %s\n",
226 inet_ntop(their_addr.ss_family,
227 get_in_addr((
struct sockaddr*)&their_addr),
230 printf(
"sw: packet is %d bytes long\n", numbytes);
231 printf(
"recv packet contents: ");
233 for(
int i = 0; i < numbytes; ++i)
235 printf(
"%2.2X", (
unsigned char)buff[i]);
241 memcpy((
void*)&val, (
void*)&buff[TX_DATA_OFFSET], 8);
242 std::cout <<
"sw: Line " << __LINE__ <<
":::"
243 <<
"Value read: " << std::hex << val << std::endl;
252 addr = 0x0000000100000009;
253 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
254 memset((
void*)&buff[RX_DATA_OFFSET], 0, 7);
255 memset((
void*)&buff[RX_DATA_OFFSET + 7], 1, 1);
256 packetSz = RX_DATA_OFFSET + buff[1] * 8;
258 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
261 perror(
"sw: sendto");
264 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
270 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
273 memcpy((
void*)&buff[RX_DATA_OFFSET], (
void*)&val, 8);
274 packetSz = RX_DATA_OFFSET + buff[1] * 8;
276 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
279 perror(
"sw: sendto");
282 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
287 std::cout <<
"sw: Line " << __LINE__ <<
":::"
288 <<
"Number of packets expecting: " << sz << std::endl;
289 for(
int i = 0; i < sz; ++i)
291 std::cout <<
"sw: Line " << __LINE__ <<
":::"
292 <<
"Waiting for data packet: " << i << std::endl;
296 if((numbytes = recvfrom(sockfd,
300 (
struct sockaddr*)&their_addr,
307 printf(
"sw: got read response from %s\n",
308 inet_ntop(their_addr.ss_family,
309 get_in_addr((
struct sockaddr*)&their_addr),
312 printf(
"sw: packet is %d bytes long\n", numbytes);
313 printf(
"recv packet contents: ");
315 for(
int i = 0; i < numbytes; ++i)
317 printf(
"%2.2X", (
unsigned char)buff[i]);
323 memcpy((
void*)&val, (
void*)&buff[TX_DATA_OFFSET], 8);
324 std::cout <<
"sw: Line " << __LINE__ <<
":::"
325 <<
"Value read: " << val << std::endl;
332 addr = 0x0000000100000009;
333 memcpy((
void*)&buff[RX_ADDR_OFFSET], (
void*)&addr, 8);
334 memset((
void*)&buff[RX_DATA_OFFSET], 0, 7);
335 memset((
void*)&buff[RX_DATA_OFFSET + 7], 0, 1);
336 packetSz = RX_DATA_OFFSET + buff[1] * 8;
338 if((numbytes = sendto(sockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
341 perror(
"sw: sendto");
344 printf(
"sw: sent %d bytes to %s\n", numbytes, argv[1]);
347 if((numbytes = recvfrom(sockfd,
351 (
struct sockaddr*)&their_addr,
358 printf(
"sw: got read response from %s\n",
359 inet_ntop(their_addr.ss_family,
360 get_in_addr((
struct sockaddr*)&their_addr),
363 printf(
"sw: packet is %d bytes long\n", numbytes);
364 printf(
"recv packet contents: ");
366 for(
int i = 0; i < numbytes; ++i)
368 printf(
"%2.2X", (
unsigned char)buff[i]);
375 std::cout <<
"sw: Line " << __LINE__ <<
":::"
376 <<
"INVALID Type of Test: " << type << std::endl;