Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[platform/upstream/iotivity.git] / resource / csdk / ocsocket / test / sendrecv.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
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>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <net/if.h>
33
34 #include <ifaddrs.h>
35 #include <linux/if_link.h>
36
37 #include <signal.h>
38
39 //static uint8_t MULTICAST_IP_ADDR[] = {224, 0, 1, 187};
40 //static uint32_t TEST_PORT_NUM = 8888;
41
42
43 #define MAX_BUF (1024)
44 uint8_t g_bfr[] = {
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, 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 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
58     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA,
59     0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0x1, 0x2, 0x3, 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,
65     };
66 uint32_t g_bfrlen = sizeof(g_bfr);
67
68 int multicast_test(int argc, char* argv[])
69 {
70     int32_t sfd;
71     char loopch=0;
72     int set_option_on = 1;
73     struct sockaddr_in mcastsock, peer;
74     uint8_t recvbuf[MAX_BUF];
75     uint32_t len, bufLen, fromlen;
76
77     printf("Running multicast tests\n");
78
79     bufLen = atoi(argv[4]);
80     if (bufLen > g_bfrlen) {
81         printf("Warning: Input buffer provided too big. Trimming to supported size\n");
82         bufLen = g_bfrlen;
83     }
84
85     //Create a datagram socket on which to send.
86     sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
87     if (sfd < 0) {
88         printf("Error in opening datagram socket");
89         return -1;
90     }
91
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]));
97
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:");
102         close(sfd);
103         return -1;
104     }
105
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:");
111         close(sfd);
112         return -1;
113     }
114
115     if (bind(sfd, (sockaddr*)&mcastsock, sizeof(mcastsock)) < 0) {
116         printf("bind returns error");
117         close(sfd);
118         return -1;
119     }
120
121     struct ip_mreq mreq;
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");
127         close(sfd);
128         return -1;
129     }
130
131     do {
132         len = sendto(sfd, g_bfr,  bufLen, 0,
133                 (struct sockaddr*)&mcastsock, sizeof(mcastsock));
134         if (len > 0) {
135             printf ("Send %d bytes to %s\n", len, inet_ntoa(mcastsock.sin_addr));
136         }
137
138         fromlen = sizeof(peer);
139         len = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&peer, &fromlen);
140         if (len > 0) {
141             printf("Rcvd %d bytes from %s\n", len, inet_ntoa(peer.sin_addr));
142         }
143
144         sleep(1);
145     } while (true);
146
147     close(sfd);
148     return 0;
149 }
150
151
152
153
154 int unicast_test(int argc, char* argv[])
155 {
156     int32_t sfd;
157     struct sockaddr_in sa, peer;
158     uint8_t recvbuf[MAX_BUF];
159     uint32_t len, bufLen, fromlen;
160
161     printf("Running unicast tests\n");
162
163     bufLen = atoi(argv[4]);
164     if (bufLen > g_bfrlen) {
165         printf("Warning: Input buffer provided too big. Trimming to supported size\n");
166         bufLen = g_bfrlen;
167     }
168
169     sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
170     if (sfd < 0) {
171         printf("socket API ret val %d\n", sfd);
172         return -1;
173     }
174
175     sa.sin_family = AF_INET;
176     sa.sin_addr.s_addr = INADDR_ANY;
177     sa.sin_port = htons(atoi(argv[3]));
178
179     if (bind(sfd, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
180         printf("bin API ret val %d\n", sfd);
181         close(sfd);
182         return -1;
183     }
184
185     peer.sin_family = AF_INET;
186     inet_aton(argv[2], &peer.sin_addr);
187     peer.sin_port = htons(atoi(argv[3]));
188
189     do {
190         len = sendto(sfd, g_bfr,  bufLen, 0,
191                 (struct sockaddr*)&peer, sizeof(peer));
192
193         if (len > 0) {
194             printf ("Send %d bytes to %s\n", len, inet_ntoa(peer.sin_addr));
195         }
196
197         fromlen = sizeof(peer);
198         len = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&peer, &fromlen);
199         if (len > 0) {
200             printf("Rcvd %d bytes from %s\n", len, inet_ntoa(peer.sin_addr));
201         }
202
203         sleep(1);
204     } while(true);
205
206     close(sfd);
207     return 0;
208 }
209
210
211
212
213 int main(int argc, char * argv[])
214 {
215
216     if (argc < 5) {
217         printf("Usage: sendrecv <u|m> <ipAddr 192.168.1.107 | 224.0.1.187> <portnum 8888> <pkt_size 200>\n");
218         return -1;
219     }
220
221     if (argv[1][0] == 'u') {
222         unicast_test(argc, argv);
223     } else {
224         multicast_test(argc, argv);
225     }
226
227     return 0;
228 }