]> rtime.felk.cvut.cz Git - can-eth-gw.git/blob - utils/cegw/cegw.c
reworking kernel canethgw
[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 /**
185  * read_if - reads interface from @in
186  * Function analyzes @in for interface specification in format
187  * <if>@<ip>:<port>, where <if> is can or udp, <ip> is address in dotted
188  * format
189  *
190  * @param[in]  in string to search in
191  * @param[out] iftype interface type (IF_CAN or IF_ETH_UDP)
192  * @param[out] d ip and port is stored to @d
193  */
194 int read_if( char* in, int* iftype, struct cegw_data* d )
195 {
196         char* optstr = NULL;
197
198         if( (optstr = read_iftype( in, iftype )) == NULL )
199         {
200                 return -1;
201         }
202
203         switch( *iftype )
204         {
205                 case IF_CAN:
206                         d->can_ifidx = if_nametoindex( optstr );
207                         if( d->can_ifidx == 0 )
208                         {
209                                 cegw_errno = CEGW_ERR_IF_CAN;
210                                 return -1;
211                         }
212                         break;
213                 case IF_ETH_UDP:
214                         if( read_addrport( optstr, &d->eth_addr, &d->eth_port ) != 0 )
215                         {
216                                 return -1;
217                         }
218                         break;
219                 default:
220                         return -1;
221                         break;
222         }
223
224         return 0;
225 }
226
227 inline static int cegw_add( struct cegw_nlmsg* req, struct cegw_data* d )
228 {
229         int gwtype = 0;
230
231         req->nh.nlmsg_type  = RTM_NEWROUTE;
232         if( (d->src_if == 0 || d->dst_if == 0) )
233         {
234                 cegw_errno = CEGW_ERR_IF_UNSPEC;
235                 return -cegw_errno;
236         }
237
238         if( d->src_if == d->dst_if )
239         {
240                 cegw_errno = CEGW_ERR_IF_SAME;
241                 return -cegw_errno;
242         }
243
244         gwtype = (d->src_if == IF_CAN) ? CEGW_RULE_CAN_ETH : CEGW_RULE_ETH_CAN;
245         addattr_l( &req->nh, sizeof(*req), CEGW_CAN_IFINDEX, &d->can_ifidx, sizeof(d->can_ifidx) );
246
247         switch( gwtype )
248         {
249                 case CEGW_RULE_CAN_ETH:
250                         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_addr, sizeof(d->eth_addr) );
251                         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_port, sizeof(d->eth_port) );
252                         break;
253                 case CEGW_RULE_ETH_CAN:
254                         break;
255                 default:
256                         break;
257         }
258
259         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, gwtype );
260
261         return 0;
262 }
263
264 inline static int cegw_listen( struct cegw_nlmsg* req, struct cegw_data* d )
265 {
266         req->nh.nlmsg_type = RTM_NEWROUTE;
267         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_LISTEN );
268         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_listen_addr, sizeof(d->eth_listen_addr) );
269         addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_listen_port, sizeof(d->eth_listen_port) );
270         printf( "listen at: %x, %hu\n", d->eth_listen_addr.s_addr, d->eth_listen_port );
271
272         return 0;
273 }
274
275 inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
276 {
277         req->nh.nlmsg_type = RTM_GETROUTE;
278         req->nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
279         
280         return 0;
281 }
282
283 inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
284 {
285         addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
286         req->nh.nlmsg_type  = RTM_DELROUTE;
287         return 0;
288 }
289
290 void print_list_item( struct list_item* li )
291 {
292         char ifname[IF_NAMESIZE];
293         const char src_width = 21;
294         char* dotaddr;
295         int tmp;
296
297         if( if_indextoname( li->can, ifname ) == NULL )
298         {
299                 strncpy( ifname, "unknown", IF_NAMESIZE );
300         }
301
302         /* ToDo listening at */
303         switch( li->type )
304         {
305                 case CEGW_RULE_CAN_ETH:
306                         printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
307                                  inet_ntoa(li->ip), li->port );
308                         break;
309                 case CEGW_RULE_ETH_CAN:
310                         dotaddr = inet_ntoa(li->ip);
311                         tmp = src_width - strlen(dotaddr) - 1;
312                         printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
313                                  tmp, li->port, ifname );
314                         break;
315         }
316 }
317
318 int main( int argc, char* argv[] )
319 {
320         int s;
321         int tmp = 0;
322         int cmd = 0;
323         char* optstr;
324         char opt;
325         struct sockaddr_nl nladdr;
326         int err = 0;
327         struct cegw_nlmsg req;
328         struct cegw_data d;
329         char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
330         int rsize = 0;
331         struct nlmsghdr* nlh;
332         struct nlmsgerr* rte;
333         struct rtattr* rta;
334         int len;
335         struct list_item li;
336
337         memset( &d, 0, sizeof(d) );
338
339         struct option long_opt[] =
340         {
341                 { "add"   , 0, NULL, 'A' },
342                 { "flush" , 0, NULL, 'F' },
343                 { "list"  , 0, NULL, 'L' },
344                 { "listen", 1, NULL, 'l' },
345                 { 0, 0, 0, 0 }
346         };
347
348         while( 1 )
349         {
350                 opt = getopt_long( argc, argv, "AFLl:s:d:", long_opt, NULL );
351                 if( opt == -1 )
352                         break;
353
354                 switch( opt )
355                 {
356                         case 'A':
357                                 cmd |= CEGW_CMD_ADD;
358                                 break;
359                         case 'F':
360                                 cmd |= CEGW_CMD_FLUSH;
361                                 break;
362                         case 'L':
363                                 cmd |= CEGW_CMD_LIST;
364                                 break;
365                         case 'l':
366                                 cmd |= CEGW_CMD_LISTEN;
367                                 if( (optstr = read_iftype( optarg, &tmp )) == NULL )
368                                 {
369                                         perr( "'--listen'" );
370                                         return -1;
371                                 }       
372                                 if( tmp != IF_ETH_UDP )
373                                 {
374                                         perr( "'--listen' expects udp interface" );
375                                         return -1;
376                                 }       
377                                 if( read_addrport( optstr, &d.eth_listen_addr, &d.eth_listen_port ) )
378                                 {
379                                         perr( "'--listen'" );
380                                         return -1;
381                                 }
382                                 break;
383                         case 's':
384                                 if( read_if( optarg, &d.src_if, &d ) != 0 )
385                                 {
386                                         perr( "'-s'" );
387                                         return -1;
388                                 }
389                                 break;
390                         case 'd':
391                                 if( read_if( optarg, &d.dst_if, &d ) != 0 )
392                                 {
393                                         perr( "'-d'" );
394                                         return -1;
395                                 }
396                                 break;
397                         case '?':
398                                 return -1;
399                                 break;
400                         default:
401                                 perr( "unknown option" );
402                                 return -1;
403                                 break;
404                 }                               
405         }
406
407         /* prepare netlink message */
408         req.nh.nlmsg_len   = NLMSG_LENGTH( sizeof(struct rtmsg) );
409         //req.nh.nlmsg_type;
410         req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
411         req.nh.nlmsg_seq   = 0;
412         req.nh.nlmsg_pid   = 0; /* ? */
413
414         memset( &req.rt, 0, sizeof(req.rt) );
415         req.rt.rtm_family = AF_CAN;
416
417         switch( cmd )
418         {
419                 case 0:
420                         perr( "command not specified" );
421                         return -1;
422                         break;
423                 case CEGW_CMD_ADD:
424                         if( cegw_add( &req, &d ) != 0 )
425                         {
426                                 perr( "'--add'" );
427                                 return -1;
428                         }
429                         break;
430                 case CEGW_CMD_FLUSH:
431                         cegw_flush( &req, &d );
432                         break;
433                 case CEGW_CMD_LIST:
434                         cegw_list( &req, &d );
435                         break;
436                 case CEGW_CMD_LISTEN:
437                         cegw_listen( &req, &d );
438                         break;
439                 default:
440                         perr( "command mismatch" );
441                         break;
442         }
443
444         /* send over netlink socket */
445         s = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); /* chck */
446         
447         memset( &nladdr, 0, sizeof(nladdr) );
448         nladdr.nl_family = AF_NETLINK;
449         nladdr.nl_pad = 0;
450         nladdr.nl_pid = 0;
451         nladdr.nl_groups = 0;
452
453         err = sendto( s, &req, req.nh.nlmsg_len, 0, 
454                       (struct sockaddr*)&nladdr, sizeof(nladdr) );
455         if( err < 0 )
456         {
457                 perror( "netlink sendto" );
458                 return err;
459         }
460         
461         /* recv */
462         rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
463         printf( "recv size=%d\n", rsize );
464         if( rsize < 0 )
465         {
466                 perr( "recv" );
467                 return -1;
468         }
469         nlh = (struct nlmsghdr*)rxbuf;
470
471         if( cmd & CEGW_CMD_LIST )
472         {
473                 printf( "recv nlmsg_type=%d\n", nlh->nlmsg_type );
474                 if( nlh->nlmsg_type == NLMSG_ERROR )
475                 {
476                         struct nlmsgerr* nlerr = NLMSG_DATA( nlh );
477                         int err = nlerr->error;
478                         printf( "nlerror: %d,%s\n", err, strerror(abs(err)) );
479                 }
480                 /* ToDo recv while */
481                 printf( "%10ssource%20sdestination\n", "", "" );
482                 while( 1 )
483                 {
484                         if( !NLMSG_OK( nlh, rsize ) )
485                         {
486                                 puts( "NLMSG_OK\n" );
487                                 break;
488                         }
489                         if( nlh->nlmsg_type == NLMSG_DONE )
490                         {
491                                 puts( "NLMSG_DONE" );
492                                 break;
493                         }
494                         /* ToDo: NLMSG_ERR */
495
496                         rta = NLMSG_DATA( nlh );
497                         //rta = (struct rtattr*)( ((char *)rtm) + NLMSG_ALIGN(sizeof(struct rtmsg)) );
498                         len = NLMSG_PAYLOAD( nlh, 0 );
499                         for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
500                         {
501                                 switch( rta->rta_type )
502                                 {
503                                         case CEGW_TYPE:
504                                                 li.type = *(int*)RTA_DATA(rta);
505                                                 break;
506                                         case CEGW_CAN_IFINDEX:
507                                                 li.can = *(int*)RTA_DATA(rta);
508                                                 break;
509                                         case CEGW_ETH_IP:
510                                                 li.ip = *(struct in_addr*)RTA_DATA(rta);
511                                                 break;
512                                         case CEGW_ETH_PORT:
513                                                 li.port = *(unsigned short*)RTA_DATA(rta);
514                                                 break;
515                                         /* case CGW_ETH_PROTO */
516                                 }
517                         }
518
519                         print_list_item( &li );
520
521                         nlh = NLMSG_NEXT( nlh, rsize );
522                 }
523         } else
524         {
525                 if( nlh->nlmsg_type != NLMSG_ERROR )
526                 {
527                         fprintf( stderr, "error: unexpected netlink answer=%d\n", nlh->nlmsg_type );
528                         return -1;
529                 }
530                 rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
531                 err = rte->error;
532                 if( err < 0 )
533                         fprintf( stderr, "error: netlink(%d); %s\n", err, strerror(abs(err)) );
534         }
535         
536         return 0;
537 }
538