]> rtime.felk.cvut.cz Git - can-eth-gw.git/blob - utils/cegw/cegw.c
c5c27b8c7d27a81084034f389032577d2293054b
[can-eth-gw.git] / utils / cegw / cegw.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <libgen.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include <errno.h>
7 #include <sys/socket.h>
8 #include <net/if.h>
9 #include <libnetlink.h>
10 #include <linux/netlink.h>
11 #include <linux/rtnetlink.h>
12 #include <arpa/inet.h>
13 #include <linux/can.h>
14 #include <linux/types.h>
15 #include <unistd.h>
16 #include "canethgw.h"
17
18 /**
19  * ToDo:
20  * [ ] print usage, on -h and plain execution
21  * [ ] start/stop listening
22  * [ ] split to files
23  */
24
25 #define CEGW_CMD_ADD     1
26 #define CEGW_CMD_LIST    2
27 #define CEGW_CMD_FLUSH   4
28 #define CEGW_CMD_LISTEN  8
29
30 enum
31 {
32         IF_UNDEF,
33         IF_CAN,
34         IF_ETH_UDP
35 };
36
37 struct cegw_data
38 {
39         int content;
40         int src_if, dst_if;
41         int can_ifidx;
42         struct in_addr eth_addr;
43         unsigned short eth_port;
44         struct in_addr eth_listen_addr;
45         unsigned short eth_listen_port;
46 };
47
48 struct cegw_nlmsg 
49 {
50         struct nlmsghdr nh;
51         struct rtmsg rt;
52         char buf[768]; /* enough? */
53 };
54
55 struct list_item
56 {
57         int type;
58         int can;
59         struct in_addr ip;
60         unsigned short port;
61 };
62
63 unsigned int cegw_errno = 0;
64
65 enum 
66 {
67         CEGW_ERR_UNKNOWN,
68         CEGW_ERR_IF_UNSPEC,
69         CEGW_ERR_IF_SAME,
70         CEGW_ERR_IF_TYPE,
71         CEGW_ERR_IF_CAN,
72         CEGW_ERR_IF_ETH,
73         CEGW_ERR_COLON,
74         CEGW_ERR_ATON,
75         CEGW_ERR_PORT
76 };
77
78 char* cegw_errlist[] =
79 {
80         [ CEGW_ERR_UNKNOWN   ] = "",
81         [ CEGW_ERR_IF_UNSPEC ] = "source or destination not specified",
82         [ CEGW_ERR_IF_SAME   ] = "source and destination have same interface type",
83         [ CEGW_ERR_IF_TYPE   ] = "unknown interface type",
84         [ CEGW_ERR_IF_CAN    ] = "invalid can interface",
85         [ CEGW_ERR_IF_ETH    ] = "invalid eth interface",
86         [ CEGW_ERR_COLON     ] = "expected ':' (<ip>:<port>)",
87         [ CEGW_ERR_ATON      ] = "ip address mismatch",
88         [ CEGW_ERR_PORT      ] = "port number"
89 };
90
91 static void perr( char* s )
92 {
93         if( s )
94         {
95                 if( cegw_errno == 0 )
96                 {
97                         fprintf( stderr, "error: %s\n", s );
98
99                 } else
100                 {
101                         fprintf( stderr, "error: %s, %s\n", s,
102                                  cegw_errlist[ cegw_errno ] );
103                 }
104                 return;
105         }
106
107         fprintf( stderr, "error: %s\n", cegw_errlist[ cegw_errno ] );
108 }
109
110 /**
111  * read_addrport - parses @in for eth address. 
112  * Valid input is e.g. udp@127.0.0.1:10502 or can@vcan0.
113  *
114  * @param[in]  in   string to search in
115  * @param[out] addr ip address
116  * @param[out] port transport layer port
117  * @return 0 on success, -1 otherwise
118  */
119 int read_addrport( char* in, struct in_addr* addr, unsigned short* port )
120 {
121         char* delim = NULL;
122         char addrstr[16];
123         int addrlen;
124         
125         if( (delim = strchr( in, ':' )) == NULL )
126         {
127                 cegw_errno = CEGW_ERR_COLON;
128                 return -1;
129         }
130
131         /* get address */
132         addrlen = delim - in;
133         memcpy( addrstr, in, addrlen );
134         addrstr[addrlen] = '\0';
135         if( inet_aton( addrstr, addr ) == 0 )
136         {
137                 cegw_errno = CEGW_ERR_ATON;
138                 return -1;
139         }
140
141         /* get port */
142         if( sscanf( delim, ":%hu", port ) != 1 ) /* ToDo: handle overflow */
143         {
144                 cegw_errno = CEGW_ERR_PORT;
145                 return -1;
146         }
147
148         return 0;
149 }
150
151 /**
152  * read_iftype - reads @in for iftype
153  * Iftype type is e.g. "can@" or "udp@".
154  *
155  * @param[in] in string to search in
156  * @param[out] iftype iftype detected
157  * @return pointer to @in after iftype on success, NULL otherwise
158  */
159 char* read_iftype( char* in, int* iftype )
160 {
161         char* ret = in+4;
162         
163         if( strncmp( "udp@", optarg, 4 ) == 0 )
164         {
165                 *iftype = IF_ETH_UDP;
166                 return ret;
167         }
168         /*
169         if( strncmp( "tcp@", optarg, 4 ) == 0 )
170         {
171                 return NULL;
172         }
173         */
174         if( strncmp( "can@", optarg, 4 ) == 0 )
175         {
176                 *iftype = IF_CAN;
177                 return ret;
178         }
179         
180         cegw_errno = CEGW_ERR_IF_TYPE;
181         return NULL;
182 }
183
184 /* ToDo: move to common */
185
186 /**
187  * read_if - reads interface from @in
188  * Function analyzes @in for interface specification in format
189  * <if>@<ip>:<port>, where <if> is can or udp, <ip> is address in dotted
190  * format
191  *
192  * @param[in]  in string to search in
193  * @param[out] iftype interface type (IF_CAN or IF_ETH_UDP)
194  * @param[out] d ip and port is stored to @d
195  */
196 int read_if( char* in, int* iftype, struct cegw_data* d )
197 {
198         char* optstr = NULL;
199
200         if( (optstr = read_iftype( in, iftype )) == NULL )
201         {
202                 return -1;
203         }
204
205         switch( *iftype )
206         {
207                 case IF_CAN:
208                         d->can_ifidx = if_nametoindex( optstr );
209                         if( d->can_ifidx == 0 )
210                         {
211                                 cegw_errno = CEGW_ERR_IF_CAN;
212                                 return -1;
213                         }
214                         break;
215                 case IF_ETH_UDP:
216                         if( read_addrport( optstr, &d->eth_addr, &d->eth_port ) != 0 )
217                         {
218                                 return -1;
219                         }
220                         break;
221                 default:
222                         return -1;
223                         break;
224         }
225
226         return 0;
227 }
228
229 inline static int cegw_add( struct cegw_nlmsg* req, struct cegw_data* d )
230 {
231         int gwtype = 0;
232
233         req->nh.nlmsg_type  = RTM_NEWROUTE;
234         if( (d->src_if == 0 || d->dst_if == 0) )
235         {
236                 cegw_errno = CEGW_ERR_IF_UNSPEC;
237                 return -cegw_errno;
238         }
239
240         if( d->src_if == d->dst_if )
241         {
242                 cegw_errno = CEGW_ERR_IF_SAME;
243                 return -cegw_errno;
244         }
245
246         gwtype = (d->src_if == IF_CAN) ? CEGW_RULE_CAN_ETH : CEGW_RULE_ETH_CAN;
247         addattr_l( &req->nh, sizeof(*req), CEGW_CAN_IFINDEX, &d->can_ifidx, sizeof(d->can_ifidx) );
248         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_addr, sizeof(d->eth_addr) );
249         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_port, sizeof(d->eth_port) );
250         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, gwtype );
251
252         return 0;
253 }
254
255 inline static int cegw_listen( struct cegw_nlmsg* req, struct cegw_data* d )
256 {
257         req->nh.nlmsg_type = RTM_NEWROUTE;
258         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_LISTEN );
259         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_listen_addr, sizeof(d->eth_listen_addr) );
260         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_listen_port, sizeof(d->eth_listen_port) );
261
262         return 0;
263 }
264
265 inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
266 {
267         req->nh.nlmsg_type = RTM_GETROUTE;
268         req->nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
269         
270         return 0;
271 }
272
273 inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
274 {
275         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
276         req->nh.nlmsg_type  = RTM_DELROUTE;
277         return 0;
278 }
279
280 void print_list_item( struct list_item* li )
281 {
282         char ifname[IF_NAMESIZE];
283         const char src_width = 21;
284         char* dotaddr;
285         int tmp;
286
287         if( if_indextoname( li->can, ifname ) == NULL )
288         {
289                 strncpy( ifname, "unknown", IF_NAMESIZE );
290         }
291
292         /* ToDo listening at */
293         switch( li->type )
294         {
295                 case CEGW_RULE_CAN_ETH:
296                         printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
297                                  inet_ntoa(li->ip), li->port );
298                         break;
299                 case CEGW_RULE_ETH_CAN:
300                         dotaddr = inet_ntoa(li->ip);
301                         tmp = src_width - strlen(dotaddr) - 1;
302                         printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
303                                  tmp, li->port, ifname );
304                         break;
305         }
306 }
307
308 int main( int argc, char* argv[] )
309 {
310         int s;
311         int tmp = 0;
312         int cmd = 0;
313         char* optstr;
314         char opt;
315         struct sockaddr_nl nladdr;
316         int err = 0;
317         struct cegw_nlmsg req;
318         struct cegw_data d;
319         char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
320         int rsize = 0;
321         struct nlmsghdr* nlh;
322         struct nlmsgerr* rte;
323         struct rtattr* rta;
324         int len;
325         struct list_item li;
326
327         memset( &d, 0, sizeof(d) );
328
329         struct option long_opt[] =
330         {
331                 { "add"   , 0, NULL, 'A' },
332                 { "flush" , 0, NULL, 'F' },
333                 { "list"  , 0, NULL, 'L' },
334                 { "listen", 1, NULL, 'l' },
335                 { 0, 0, 0, 0 }
336         };
337
338         while( 1 )
339         {
340                 opt = getopt_long( argc, argv, "AFLl:s:d:", long_opt, NULL );
341                 if( opt == -1 )
342                         break;
343
344                 switch( opt )
345                 {
346                         case 'A':
347                                 cmd |= CEGW_CMD_ADD;
348                                 break;
349                         case 'F':
350                                 cmd |= CEGW_CMD_FLUSH;
351                                 break;
352                         case 'L':
353                                 cmd |= CEGW_CMD_LIST;
354                                 break;
355                         case 'l':
356                                 cmd |= CEGW_CMD_LISTEN;
357                                 if( (optstr = read_iftype( optarg, &tmp )) == NULL )
358                                 {
359                                         perr( "'--listen'" );
360                                         return -1;
361                                 }       
362                                 if( tmp != IF_ETH_UDP )
363                                 {
364                                         perr( "'--listen' expects udp interface" );
365                                         return -1;
366                                 }       
367                                 if( read_addrport( optstr, &d.eth_listen_addr, &d.eth_listen_port ) )
368                                 {
369                                         perr( "'--listen'" );
370                                         return -1;
371                                 }
372                                 break;
373                         case 's':
374                                 if( read_if( optarg, &d.src_if, &d ) != 0 )
375                                 {
376                                         perr( "'-s'" );
377                                         return -1;
378                                 }
379                                 break;
380                         case 'd':
381                                 if( read_if( optarg, &d.dst_if, &d ) != 0 )
382                                 {
383                                         perr( "'-d'" );
384                                         return -1;
385                                 }
386                                 break;
387                         case '?':
388                                 return -1;
389                                 break;
390                         default:
391                                 perr( "unknown option" );
392                                 return -1;
393                                 break;
394                 }                               
395         }
396
397         /* prepare netlink message */
398         req.nh.nlmsg_len   = NLMSG_LENGTH( sizeof(struct rtmsg) );
399         //req.nh.nlmsg_type;
400         req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
401         req.nh.nlmsg_seq   = 0;
402         req.nh.nlmsg_pid   = 0; /* ? */
403
404         memset( &req.rt, 0, sizeof(req.rt) );
405         req.rt.rtm_family = AF_CAN;
406
407         switch( cmd )
408         {
409                 case 0:
410                         perr( "command not specified" );
411                         return -1;
412                         break;
413                 case CEGW_CMD_ADD:
414                         if( cegw_add( &req, &d ) != 0 )
415                         {
416                                 perr( "'--add'" );
417                                 return -1;
418                         }
419                         break;
420                 case CEGW_CMD_FLUSH:
421                         cegw_flush( &req, &d );
422                         break;
423                 case CEGW_CMD_LIST:
424                         cegw_list( &req, &d );
425                         break;
426                 case CEGW_CMD_LISTEN:
427                         cegw_listen( &req, &d );
428                         break;
429                 default:
430                         perr( "command mismatch" );
431                         break;
432         }
433
434         /* send over netlink socket */
435         s = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); /* chck */
436         
437         memset( &nladdr, 0, sizeof(nladdr) );
438         nladdr.nl_family = AF_NETLINK;
439         nladdr.nl_pad = 0;
440         nladdr.nl_pid = 0;
441         nladdr.nl_groups = 0;
442
443         err = sendto( s, &req, req.nh.nlmsg_len, 0, 
444                       (struct sockaddr*)&nladdr, sizeof(nladdr) );
445         if( err < 0 )
446         {
447                 perror( "netlink sendto" );
448                 return -1;
449         }
450         
451         /* recv */
452         rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
453         if( rsize < 0 )
454         {
455                 perr( "recv" );
456                 return -1;
457         }
458         nlh = (struct nlmsghdr*)rxbuf;
459
460         if( nlh->nlmsg_type == NLMSG_ERROR )
461         {
462                 rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
463                 err = rte->error;
464
465                 if( err == 0  )
466                 {
467                         printf( "%s\n", strerror(abs(err)) );
468                         return 0;
469                 } else
470                 {
471                         printf( "netlink error: %s\n", strerror(abs(err)) );
472                         return -1;
473                 }
474         }
475
476         if( cmd & CEGW_CMD_LIST )
477         {
478                 /* ToDo recv while */
479                 printf( "%10ssource%20sdestination\n", "", "" );
480                 while( 1 )
481                 {
482                         if( !NLMSG_OK( nlh, rsize ) )
483                         {
484                                 break;
485                         }
486                         if( nlh->nlmsg_type == NLMSG_DONE )
487                         {
488                                 break;
489                         }
490                         /* ToDo: NLMSG_ERR */
491                         rta = NLMSG_DATA( nlh );
492                         len = NLMSG_PAYLOAD( nlh, 0 );
493                         for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
494                         {
495                                 switch( rta->rta_type )
496                                 {
497                                         case CEGW_TYPE:
498                                                 li.type = *(int*)RTA_DATA(rta);
499                                                 break;
500                                         case CEGW_CAN_IFINDEX:
501                                                 li.can = *(int*)RTA_DATA(rta);
502                                                 break;
503                                         case CEGW_ETH_IP:
504                                                 li.ip = *(struct in_addr*)RTA_DATA(rta);
505                                                 break;
506                                         case CEGW_ETH_PORT:
507                                                 li.port = *(unsigned short*)RTA_DATA(rta);
508                                                 break;
509                                         /* case CGW_ETH_PROTO */
510                                 }
511                         }
512
513                         print_list_item( &li );
514
515                         nlh = NLMSG_NEXT( nlh, rsize );
516                 }
517         }
518
519         return 0;
520 }
521