6 #include <netinet/in.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
14 #include <linux/can.h>
15 #include <linux/can/raw.h>
26 #define printdbg(x) printf(x)
36 #define GW_FLAG_LISTEN 4
37 #define GW_FLAG_FILTER 8
39 unsigned int opt_flag = 0;
42 struct can_filter flt;
43 struct sockaddr_in udp_dest;
47 * @return true if frame should be dropped
49 bool filter_check( struct can_frame* cf )
51 if( !(opt_flag & GW_FLAG_FILTER) )
54 return ( (cf->can_id & flt.can_mask) == (flt.can_id & flt.can_mask) );
61 read( sock_udp, &cf, sizeof(cf) );
63 if( filter_check( &cf ) )
65 printdbg( "filter: drop\n" );
69 write( sock_can, &cf, sizeof(cf) );
71 printdbg( "eth -> can\n" );
78 read( sock_can, &cf, sizeof(cf) );
80 if( filter_check( &cf ) )
82 printdbg( "filter: drop\n" );
86 sendto( sock_udp, &cf, sizeof(cf), 0, (struct sockaddr*) &udp_dest, sizeof(udp_dest) );
88 printdbg( "can -> eth\n" );
91 void signal_exit( int sig )
95 printf( "exiting\n" );
100 int main( int argc, char* argv[] )
102 struct sockaddr_in udp_addr;
103 struct sockaddr_can can_addr;
104 struct pollfd pfd[2];
107 struct cegw_if ceif[2], iflisten;
108 struct cegw_if* ifcan,* ifeth;
113 struct option longopt[] =
115 {"listen", 1, NULL, 'l'},
119 while( (opt = getopt_long( argc, argv, "s:d:l:f:i:", longopt, NULL )) != -1 )
124 if( read_if(optarg, &ceif[0]) != 0 )
129 opt_flag |= GW_FLAG_SRC;
132 if( read_if(optarg, &ceif[1]) != 0 )
137 opt_flag |= GW_FLAG_DST;
140 if( read_if(optarg, &iflisten) != 0 )
145 if( iflisten.type != IF_ETH_UDP )
147 perr( "'-l' udp interface expected" );
150 opt_flag |= GW_FLAG_LISTEN;
153 if( sscanf( optarg, "%x:%x", &flt.can_id,
154 &flt.can_mask ) != 2 )
156 perr( "bad filter format");
159 opt_flag |= GW_FLAG_FILTER;
167 if( !((opt_flag & GW_FLAG_SRC) && (opt_flag & GW_FLAG_DST)
168 && (opt_flag & GW_FLAG_LISTEN)) )
170 perr( "'s', 'd' and 'l' are mandatory" );
175 if( !(ceif[0].type == IF_CAN && ceif[1].type == IF_ETH_UDP) &&
176 !(ceif[0].type == IF_ETH_UDP && ceif[1].type == IF_CAN ) )
178 perr( "'-s' and '-d' should be different interface type" );
182 if( ceif[0].type == IF_CAN )
192 signal( SIGINT, signal_exit );
194 /* prepare udp destination */
195 udp_dest.sin_family = AF_INET;
196 udp_dest.sin_port = htons(ifeth->eth.port);
197 udp_dest.sin_addr = ifeth->eth.ip;
200 sock_udp = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP );
203 perr( "udp socket creation failed" );
207 udp_addr.sin_family = AF_INET;
208 udp_addr.sin_port = htons(iflisten.eth.port);
209 udp_addr.sin_addr = iflisten.eth.ip;
211 if( bind( sock_udp, (struct sockaddr*) &udp_addr, sizeof(udp_addr) ) < 0 )
213 perr( "udp socket binding failed" );
219 sock_can = socket( PF_CAN, SOCK_RAW, CAN_RAW );
222 perr( "can socket creation failed");
227 can_addr.can_family = AF_CAN;
228 can_addr.can_ifindex = ifcan->can.ifindex;
230 if( bind( sock_can, (struct sockaddr*) &can_addr, sizeof(can_addr) ) < 0 )
232 perr( "can socket binding failed" );
237 pfd[GW_POLL_ETH].fd = sock_udp;
238 pfd[GW_POLL_ETH].events = POLLIN | POLLPRI;
239 pfd[GW_POLL_ETH].revents = 0;
241 pfd[GW_POLL_CAN].fd = sock_can;
242 pfd[GW_POLL_CAN].events = POLLIN | POLLPRI;
243 pfd[GW_POLL_CAN].revents = 0;
245 printf( "canethgw is running\n" );
249 printdbg( "polling\n" );
250 polret = poll( pfd, 2, -1 );
253 perr( "poll(..) failed" );
259 if( pfd[GW_POLL_ETH].revents != 0 )
263 if( pfd[GW_POLL_CAN].revents != 0 )
268 pfd[GW_POLL_ETH].revents = 0;
269 pfd[GW_POLL_CAN].revents = 0;