-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
277 lines (237 loc) · 8.61 KB
/
net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* MIT License
*
* Copyright (c) 2020 vzvca
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "vpl.h"
#define M_PORT 5004
#define M_GROUP "239.192.77.10"
#define M_NIC "eth0.10"
#define FBDEV "/dev/fb0"
char *g_mgroup = M_GROUP;
char *g_nic = M_NIC;
int g_mport = M_PORT;
char *g_fbdev = FBDEV;
int g_width = -1;
int g_height = -1;
char *g_out = NULL;
int g_x = 0;
int g_y = 0;
/* packets processors */
extern void pktprocess( unsigned char *pkt, int pktlen );
extern void imgprocess( unsigned char *pkt, int pktlen );
extern int initout( char *file, image_t *img );
extern int initfb( char *fbdev, image_t *img );
extern int test();
/* this is where th video will be stored */
extern image_t *pim;
/*
* --------------------------------------------------------------------------
* Get IP address of network interface
* --------------------------------------------------------------------------
*/
int get_nic_addr(const char * nic, struct sockaddr_in *addr)
{
struct ifreq ifr;
int fd, r;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, nic, IFNAMSIZ);
r = ioctl(fd, SIOCGIFADDR, &ifr);
if (r < 0) {
fprintf( stderr, "get_nic_addr : ioctl could not request device %s\n", nic);
}
close(fd);
*addr = *(struct sockaddr_in *)&ifr.ifr_addr;
return r;
}
/*
* --------------------------------------------------------------------------
* Tune socket timeout
* --------------------------------------------------------------------------
*/
static int set_recv_timeout( int socket, unsigned int timeout_us)
{
struct timeval l_timeout;
l_timeout.tv_sec = 0;
l_timeout.tv_usec = timeout_us;
if( setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &l_timeout, sizeof(l_timeout)) == -1) {
fprintf( stderr, "setRecvTimeout : RCVTIMEO not set\n");
return -1;
}
return 0;
}
/*
* --------------------------------------------------------------------------
* Connect to multicast group
* --------------------------------------------------------------------------
*/
static int connect_to_multicast_group( int socket, char *mgroup, char *nic, int mport)
{
int reuse = 1; /* value to bind several sock on a specific port */
struct ip_mreq lmreq = {0, 0};
struct sockaddr_in nic_addr = {0, 0, 0, 0};
struct sockaddr_in laddr = {0, 0, 0, 0};
/* struct that allows to specify to which IP to subscribe */
lmreq.imr_multiaddr.s_addr = inet_addr(mgroup);
if (!get_nic_addr( nic, &nic_addr )) {
lmreq.imr_interface.s_addr = nic_addr.sin_addr.s_addr;
printf( "%s interface -> ip : %s\n", nic, inet_ntoa(lmreq.imr_interface));
}
else {
fprintf( stderr, "could not get interface address %s\n", nic);
return -1;
}
/* to be able to bind several IP on port */
if ( setsockopt( socket, SOL_SOCKET, SO_REUSEADDR, (int *)&reuse, sizeof(reuse) ) == -1) {
fprintf( stderr, "connect_to_multicast_group : REUSEADDR not set\n");
return -1;
}
printf("connect_to_multicast_group : set REUSEADDR\n");
/* subscribe to multicast group */
if ( setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &lmreq, sizeof(struct ip_mreq)) == -1) {
fprintf( stderr, "connect_to_multicast_group : error subscribing to multicast group %s", mgroup );
return -1;
}
printf("connect_to_multicast_group : SUBSCRIBE To MulticastGroup\n");
laddr.sin_family = AF_INET;
laddr.sin_port = htons(mport);
laddr.sin_addr.s_addr = /*inet_addr(mgroup);*/ htonl(INADDR_ANY);
/* bind socket to the right port to receive data */
if ( bind(socket, (struct sockaddr *)&laddr, sizeof(struct sockaddr_in)) == -1) {
fprintf( stderr, "connect_to_multicast_group : error binding\n");
return -1;
}
printf( "connect_to_multicast_group : Bind socket\n");
return 0;
}
/*
* --------------------------------------------------------------------------
* Loop over incoming packets sending them to pfun for handling
* --------------------------------------------------------------------------
*/
void netloop( pktproc_t pfun )
{
struct sockaddr_in addr;
int addrlen, sock, cnt;
struct ip_mreq mreq;
unsigned char message[1500];
/* set up socket */
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
exit(1);
}
connect_to_multicast_group( sock, g_mgroup, g_nic, g_mport);
while (1) {
cnt = recvfrom(sock, message, sizeof(message), 0, (struct sockaddr *) &addr, &addrlen);
if (cnt < 0) {
perror("recvfrom");
exit(1);
} else if (cnt == 0) {
break;
}
pfun(message, cnt);
}
}
/*
* --------------------------------------------------------------------------
* Usage
* --------------------------------------------------------------------------
*/
void usage( int argc, char *argv[], int optind )
{
char *what = (optind > 0) ? "error" : "usage";
fprintf( stderr, "%s %s :\n%s [-h] [-i nic] [-m group] [-p port] [-d fbdev] [-o file] [-w width] [-h height]\n", argv[0], what, argv[0]);
fprintf( stderr, "\t-?\t\tPrints this message.\n");
fprintf( stderr, "\t-i\t\tSets network interface to bind to (default %s).\n", g_nic);
fprintf( stderr, "\t-g\t\tSets multicast group to join (default %s).\n", g_mgroup);
fprintf( stderr, "\t-p\t\tSets port to use (default %d).\n", g_mport);
fprintf( stderr, "\t-d\t\tSets the framebuffer device (default %s).\n", g_fbdev);
fprintf( stderr, "\t-o\t\tSets the image output file.\n");
fprintf( stderr, "\t-w\t\tSets the width of the image.\n");
fprintf( stderr, "\t-h\t\tSets the height of the image.\n");
fprintf( stderr, "\t-x\t\tSets the x offset of the image when rendering to framebuffer.\n");
fprintf( stderr, "\t-y\t\tSets the y offset of the image when rendering to framebuffer.\n");
fprintf( stderr, "Options -o -w -h must be used together and are incompatible with -d.\n");
/* exit with error only if option parsng failed */
exit(optind > 0);
}
/* --------------------------------------------------------------------------
* Programme principal
* --------------------------------------------------------------------------*/
int main( int argc, char *argv[] )
{
int opt;
while ( (opt = getopt( argc, argv, "?i:g:p:d:o:w:h:x:y:")) != -1 ) {
switch( opt ) {
case '?': usage( argc, argv, 0);
case 'i': g_nic = optarg; break;
case 'g': g_mgroup = optarg; break;
case 'p': g_mport = atoi(optarg); break;
case 'd': g_fbdev = optarg; break;
case 'w': g_width = atoi(optarg); break;
case 'h': g_height = atoi(optarg); break;
case 'o': g_out = optarg; break;
case 'x': g_x = atoi(optarg); break;
case 'y': g_y = atoi(optarg); break;
default:
usage(argc, argv, optind);
}
}
/* Check arguments validity */
if ( g_out != NULL && (g_width < 0 || g_height < 0)) {
fprintf( stderr, "options '-w' and '-h' are mandatory when '-o' is used.\n");
exit(1);
}
if ( g_out == NULL && (g_width >= 0 || g_height >= 0)) {
fprintf( stderr, "options '-w' and '-h' cannot be used when rendering to framebuffer.\n");
exit(1);
}
if ( g_out != NULL && (g_x != 0 || g_y != 0)) {
fprintf( stderr, "options '-x' and '-y' cannot be used when '-o' is used.\n");
exit(1);
}
/* Create image for rendering */
if ( g_out == NULL ) {
int fbfd = initfb( g_fbdev, pim );
}
else {
int imfd = initout( g_out, pim );
}
/* rendering */
netloop(imgprocess);
}