1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/select.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
35 #include <linux/if_link.h>
39 //static uint8_t MULTICAST_IP_ADDR[] = {224, 0, 1, 187};
40 //static uint32_t TEST_PORT_NUM = 8888;
43 #define MAX_BUF (1024)
45 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
46 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
47 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
48 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
49 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 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 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
60 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
61 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
62 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
63 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
64 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
66 uint32_t g_bfrlen = sizeof(g_bfr);
68 int multicast_test(int argc, char* argv[])
72 int set_option_on = 1;
73 struct sockaddr_in mcastsock, peer;
74 uint8_t recvbuf[MAX_BUF];
75 uint32_t len, bufLen, fromlen;
77 printf("Running multicast tests\n");
79 bufLen = atoi(argv[4]);
80 if (bufLen > g_bfrlen) {
81 printf("Warning: Input buffer provided too big. Trimming to supported size\n");
85 //Create a datagram socket on which to send.
86 sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
88 printf("Error in opening datagram socket");
92 //Initialize the group sockaddr structure with a
93 memset((char *) &mcastsock, 0, sizeof(mcastsock));
94 mcastsock.sin_family = AF_INET;
95 mcastsock.sin_addr.s_addr = inet_addr(argv[2]);
96 mcastsock.sin_port = htons(atoi(argv[3]));
98 //Disable loopback so you do not receive your own datagrams.
99 if (setsockopt(sfd, IPPROTO_IP, IP_MULTICAST_LOOP,
100 (char *)&loopch, sizeof(loopch)) < 0) {
101 //printf("setting IP_MULTICAST_LOOP:");
106 //Play nice with other processes who may be listening at this
107 //port/IP address combination
108 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &set_option_on,
109 sizeof(set_option_on)) < 0) {
110 //printf("setting SO_REUSEADDR:");
115 if (bind(sfd, (sockaddr*)&mcastsock, sizeof(mcastsock)) < 0) {
116 printf("bind returns error");
122 memset(&mreq, 0, sizeof(struct ip_mreq));
123 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
124 mreq.imr_multiaddr.s_addr = mcastsock.sin_addr.s_addr;
125 if ((setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq))) < 0) {
126 printf("multicast -- adding membership failed");
132 len = sendto(sfd, g_bfr, bufLen, 0,
133 (struct sockaddr*)&mcastsock, sizeof(mcastsock));
135 printf ("Send %d bytes to %s\n", len, inet_ntoa(mcastsock.sin_addr));
138 fromlen = sizeof(peer);
139 len = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&peer, &fromlen);
141 printf("Rcvd %d bytes from %s\n", len, inet_ntoa(peer.sin_addr));
154 int unicast_test(int argc, char* argv[])
157 struct sockaddr_in sa, peer;
158 uint8_t recvbuf[MAX_BUF];
159 uint32_t len, bufLen, fromlen;
161 printf("Running unicast tests\n");
163 bufLen = atoi(argv[4]);
164 if (bufLen > g_bfrlen) {
165 printf("Warning: Input buffer provided too big. Trimming to supported size\n");
169 sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
171 printf("socket API ret val %d\n", sfd);
175 sa.sin_family = AF_INET;
176 sa.sin_addr.s_addr = INADDR_ANY;
177 sa.sin_port = htons(atoi(argv[3]));
179 if (bind(sfd, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
180 printf("bin API ret val %d\n", sfd);
185 peer.sin_family = AF_INET;
186 inet_aton(argv[2], &peer.sin_addr);
187 peer.sin_port = htons(atoi(argv[3]));
190 len = sendto(sfd, g_bfr, bufLen, 0,
191 (struct sockaddr*)&peer, sizeof(peer));
194 printf ("Send %d bytes to %s\n", len, inet_ntoa(peer.sin_addr));
197 fromlen = sizeof(peer);
198 len = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&peer, &fromlen);
200 printf("Rcvd %d bytes from %s\n", len, inet_ntoa(peer.sin_addr));
213 int main(int argc, char * argv[])
217 printf("Usage: sendrecv <u|m> <ipAddr 192.168.1.107 | 224.0.1.187> <portnum 8888> <pkt_size 200>\n");
221 if (argv[1][0] == 'u') {
222 unicast_test(argc, argv);
224 multicast_test(argc, argv);