Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / ocsocket / test / sendrecv.cpp
1 //******************************************************************
2 //
3 // copyright 2014 intel corporation all rights reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <sys/select.h>
9 #include <arpa/inet.h>
10 #include <netinet/in.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <net/if.h>
18
19 #include <ifaddrs.h>
20 #include <linux/if_link.h>
21
22 #include <signal.h>
23
24 //static uint8_t MULTICAST_IP_ADDR[] = {224, 0, 1, 187};
25 //static uint32_t TEST_PORT_NUM = 8888;
26
27
28 #define MAX_BUF (1024)
29 uint8_t g_bfr[] = {
30     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
31     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
32     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
33     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
34     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
35     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,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, 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, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
50     };
51 uint32_t g_bfrlen = sizeof(g_bfr);
52
53 int multicast_test(int argc, char* argv[])
54 {
55     int32_t sfd;
56     char loopch=0;
57     int set_option_on = 1;
58     struct sockaddr_in mcastsock, peer;
59     uint8_t recvbuf[MAX_BUF];
60     uint32_t len, bufLen, fromlen;
61
62     printf("Running multicast tests\n");
63
64     bufLen = atoi(argv[4]);
65     if (bufLen > g_bfrlen) {
66         printf("Warning: Input buffer provided too big. Trimming to supported size\n");
67         bufLen = g_bfrlen;
68     }
69
70     //Create a datagram socket on which to send.
71     sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
72     if (sfd < 0) {
73         printf("Error in opening datagram socket");
74         return -1;
75     }
76
77     //Initialize the group sockaddr structure with a
78     memset((char *) &mcastsock, 0, sizeof(mcastsock));
79     mcastsock.sin_family = AF_INET;
80     mcastsock.sin_addr.s_addr = inet_addr(argv[2]);
81     mcastsock.sin_port = htons(atoi(argv[3]));
82
83     //Disable loopback so you do not receive your own datagrams.
84     if (setsockopt(sfd, IPPROTO_IP, IP_MULTICAST_LOOP,
85                 (char *)&loopch, sizeof(loopch)) < 0) {
86         //printf("setting IP_MULTICAST_LOOP:");
87         close(sfd);
88         return -1;
89     }
90
91     //Play nice with other processes who may be listening at this
92     //port/IP address combination
93     if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &set_option_on,
94                 sizeof(set_option_on)) < 0) {
95         //printf("setting SO_REUSEADDR:");
96         close(sfd);
97         return -1;
98     }
99
100     if (bind(sfd, (sockaddr*)&mcastsock, sizeof(mcastsock)) < 0) {
101         printf("bind returns error");
102         close(sfd);
103         return -1;
104     }
105
106     struct ip_mreq mreq;
107     memset(&mreq, 0, sizeof(struct ip_mreq));
108     mreq.imr_interface.s_addr = htonl(INADDR_ANY);
109     mreq.imr_multiaddr.s_addr = mcastsock.sin_addr.s_addr;
110     if ((setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq))) < 0) {
111         printf("multicast -- adding membership failed");
112         close(sfd);
113         return -1;
114     }
115
116     do {
117         len = sendto(sfd, g_bfr,  bufLen, 0,
118                 (struct sockaddr*)&mcastsock, sizeof(mcastsock));
119         if (len > 0) {
120             printf ("Send %d bytes to %s\n", len, inet_ntoa(mcastsock.sin_addr));
121         }
122
123         fromlen = sizeof(peer);
124         len = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&peer, &fromlen);
125         if (len > 0) {
126             printf("Rcvd %d bytes from %s\n", len, inet_ntoa(peer.sin_addr));
127         }
128
129         sleep(1);
130     } while (true);
131
132     close(sfd);
133     return 0;
134 }
135
136
137
138
139 int unicast_test(int argc, char* argv[])
140 {
141     int32_t sfd;
142     struct sockaddr_in sa, peer;
143     uint8_t recvbuf[MAX_BUF];
144     uint32_t len, bufLen, fromlen;
145
146     printf("Running unicast tests\n");
147
148     bufLen = atoi(argv[4]);
149     if (bufLen > g_bfrlen) {
150         printf("Warning: Input buffer provided too big. Trimming to supported size\n");
151         bufLen = g_bfrlen;
152     }
153
154     sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
155     if (sfd < 0) {
156         printf("socket API ret val %d\n", sfd);
157         return -1;
158     }
159
160     sa.sin_family = AF_INET;
161     sa.sin_addr.s_addr = INADDR_ANY;
162     sa.sin_port = htons(atoi(argv[3]));
163
164     if (bind(sfd, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
165         printf("bin API ret val %d\n", sfd);
166         close(sfd);
167         return -1;
168     }
169
170     peer.sin_family = AF_INET;
171     inet_aton(argv[2], &peer.sin_addr);
172     peer.sin_port = htons(atoi(argv[3]));
173
174     do {
175         len = sendto(sfd, g_bfr,  bufLen, 0,
176                 (struct sockaddr*)&peer, sizeof(peer));
177
178         if (len > 0) {
179             printf ("Send %d bytes to %s\n", len, inet_ntoa(peer.sin_addr));
180         }
181
182         fromlen = sizeof(peer);
183         len = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&peer, &fromlen);
184         if (len > 0) {
185             printf("Rcvd %d bytes from %s\n", len, inet_ntoa(peer.sin_addr));
186         }
187
188         sleep(1);
189     } while(true);
190
191     close(sfd);
192     return 0;
193 }
194
195
196
197
198 int main(int argc, char * argv[])
199 {
200
201     if (argc < 5) {
202         printf("Usage: sendrecv <u|m> <ipAddr 192.168.1.107 | 224.0.1.187> <portnum 8888> <pkt_size 200>\n");
203         return -1;
204     }
205
206     if (argv[1][0] == 'u') {
207         unicast_test(argc, argv);
208     } else {
209         multicast_test(argc, argv);
210     }
211
212     return 0;
213 }