]> rtime.felk.cvut.cz Git - can-eth-gw.git/blob - utils/cegw/cegw.c
improved rtnl processing, some error checks added
[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         printf( "listen at: %x, %hu\n", d->eth_listen_addr.s_addr, d->eth_listen_port );
262
263         return 0;
264 }
265
266 inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
267 {
268         req->nh.nlmsg_type = RTM_GETROUTE;
269         req->nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
270         
271         return 0;
272 }
273
274 inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
275 {
276         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
277         req->nh.nlmsg_type  = RTM_DELROUTE;
278         return 0;
279 }
280
281 void print_list_item( struct list_item* li )
282 {
283         char ifname[IF_NAMESIZE];
284         const char src_width = 21;
285         char* dotaddr;
286         int tmp;
287
288         if( if_indextoname( li->can, ifname ) == NULL )
289         {
290                 strncpy( ifname, "unknown", IF_NAMESIZE );
291         }
292
293         /* ToDo listening at */
294         switch( li->type )
295         {
296                 case CEGW_RULE_CAN_ETH:
297                         printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
298                                  inet_ntoa(li->ip), li->port );
299                         break;
300                 case CEGW_RULE_ETH_CAN:
301                         dotaddr = inet_ntoa(li->ip);
302                         tmp = src_width - strlen(dotaddr) - 1;
303                         printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
304                                  tmp, li->port, ifname );
305                         break;
306         }
307 }
308
309 int main( int argc, char* argv[] )
310 {
311         int s;
312         int tmp = 0;
313         int cmd = 0;
314         char* optstr;
315         char opt;
316         struct sockaddr_nl nladdr;
317         int err = 0;
318         struct cegw_nlmsg req;
319         struct cegw_data d;
320         char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
321         int rsize = 0;
322         struct nlmsghdr* nlh;
323         struct nlmsgerr* rte;
324         struct rtattr* rta;
325         int len;
326         struct list_item li;
327
328         memset( &d, 0, sizeof(d) );
329
330         struct option long_opt[] =
331         {
332                 { "add"   , 0, NULL, 'A' },
333                 { "flush" , 0, NULL, 'F' },
334                 { "list"  , 0, NULL, 'L' },
335                 { "listen", 1, NULL, 'l' },
336                 { 0, 0, 0, 0 }
337         };
338
339         while( 1 )
340         {
341                 opt = getopt_long( argc, argv, "AFLl:s:d:", long_opt, NULL );
342                 if( opt == -1 )
343                         break;
344
345                 switch( opt )
346                 {
347                         case 'A':
348                                 cmd |= CEGW_CMD_ADD;
349                                 break;
350                         case 'F':
351                                 cmd |= CEGW_CMD_FLUSH;
352                                 break;
353                         case 'L':
354                                 cmd |= CEGW_CMD_LIST;
355                                 break;
356                         case 'l':
357                                 cmd |= CEGW_CMD_LISTEN;
358                                 if( (optstr = read_iftype( optarg, &tmp )) == NULL )
359                                 {
360                                         perr( "'--listen'" );
361                                         return -1;
362                                 }       
363                                 if( tmp != IF_ETH_UDP )
364                                 {
365                                         perr( "'--listen' expects udp interface" );
366                                         return -1;
367                                 }       
368                                 if( read_addrport( optstr, &d.eth_listen_addr, &d.eth_listen_port ) )
369                                 {
370                                         perr( "'--listen'" );
371                                         return -1;
372                                 }
373                                 break;
374                         case 's':
375                                 if( read_if( optarg, &d.src_if, &d ) != 0 )
376                                 {
377                                         perr( "'-s'" );
378                                         return -1;
379                                 }
380                                 break;
381                         case 'd':
382                                 if( read_if( optarg, &d.dst_if, &d ) != 0 )
383                                 {
384                                         perr( "'-d'" );
385                                         return -1;
386                                 }
387                                 break;
388                         case '?':
389                                 return -1;
390                                 break;
391                         default:
392                                 perr( "unknown option" );
393                                 return -1;
394                                 break;
395                 }                               
396         }
397
398         /* prepare netlink message */
399         req.nh.nlmsg_len   = NLMSG_LENGTH( sizeof(struct rtmsg) );
400         //req.nh.nlmsg_type;
401         req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
402         req.nh.nlmsg_seq   = 0;
403         req.nh.nlmsg_pid   = 0; /* ? */
404
405         memset( &req.rt, 0, sizeof(req.rt) );
406         req.rt.rtm_family = AF_CAN;
407
408         switch( cmd )
409         {
410                 case 0:
411                         perr( "command not specified" );
412                         return -1;
413                         break;
414                 case CEGW_CMD_ADD:
415                         if( cegw_add( &req, &d ) != 0 )
416                         {
417                                 perr( "'--add'" );
418                                 return -1;
419                         }
420                         break;
421                 case CEGW_CMD_FLUSH:
422                         cegw_flush( &req, &d );
423                         break;
424                 case CEGW_CMD_LIST:
425                         cegw_list( &req, &d );
426                         break;
427                 case CEGW_CMD_LISTEN:
428                         cegw_listen( &req, &d );
429                         break;
430                 default:
431                         perr( "command mismatch" );
432                         break;
433         }
434
435         /* send over netlink socket */
436         s = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); /* chck */
437         
438         memset( &nladdr, 0, sizeof(nladdr) );
439         nladdr.nl_family = AF_NETLINK;
440         nladdr.nl_pad = 0;
441         nladdr.nl_pid = 0;
442         nladdr.nl_groups = 0;
443
444         err = sendto( s, &req, req.nh.nlmsg_len, 0, 
445                       (struct sockaddr*)&nladdr, sizeof(nladdr) );
446         if( err < 0 )
447         {
448                 perror( "netlink sendto" );
449                 return err;
450         }
451         
452         /* recv */
453         rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
454         printf( "recv size=%d\n", rsize );
455         if( rsize < 0 )
456         {
457                 perr( "recv" );
458                 return -1;
459         }
460         nlh = (struct nlmsghdr*)rxbuf;
461
462         if( cmd & CEGW_CMD_LIST )
463         {
464                 printf( "recv nlmsg_type=%d\n", nlh->nlmsg_type );
465                 if( nlh->nlmsg_type == NLMSG_ERROR )
466                 {
467                         struct nlmsgerr* nlerr = NLMSG_DATA( nlh );
468                         int err = nlerr->error;
469                         printf( "nlerror: %d,%s\n", err, strerror(abs(err)) );
470                 }
471                 /* ToDo recv while */
472                 printf( "%10ssource%20sdestination\n", "", "" );
473                 while( 1 )
474                 {
475                         if( !NLMSG_OK( nlh, rsize ) )
476                         {
477                                 puts( "NLMSG_OK\n" );
478                                 break;
479                         }
480                         if( nlh->nlmsg_type == NLMSG_DONE )
481                         {
482                                 puts( "NLMSG_DONE" );
483                                 break;
484                         }
485                         /* ToDo: NLMSG_ERR */
486
487                         rta = NLMSG_DATA( nlh );
488                         //rta = (struct rtattr*)( ((char *)rtm) + NLMSG_ALIGN(sizeof(struct rtmsg)) );
489                         len = NLMSG_PAYLOAD( nlh, 0 );
490                         for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
491                         {
492                                 switch( rta->rta_type )
493                                 {
494                                         case CEGW_TYPE:
495                                                 li.type = *(int*)RTA_DATA(rta);
496                                                 break;
497                                         case CEGW_CAN_IFINDEX:
498                                                 li.can = *(int*)RTA_DATA(rta);
499                                                 break;
500                                         case CEGW_ETH_IP:
501                                                 li.ip = *(struct in_addr*)RTA_DATA(rta);
502                                                 break;
503                                         case CEGW_ETH_PORT:
504                                                 li.port = *(unsigned short*)RTA_DATA(rta);
505                                                 break;
506                                         /* case CGW_ETH_PROTO */
507                                 }
508                         }
509
510                         print_list_item( &li );
511
512                         nlh = NLMSG_NEXT( nlh, rsize );
513                 }
514         } else
515         {
516                 if( nlh->nlmsg_type != NLMSG_ERROR )
517                 {
518                         fprintf( stderr, "error: unexpected netlink answer=%d\n", nlh->nlmsg_type );
519                         return -1;
520                 }
521                 rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
522                 err = rte->error;
523                 if( err < 0 )
524                         fprintf( stderr, "error: netlink(%d); %s\n", err, strerror(abs(err)) );
525         }
526         
527         return 0;
528 }
529