#include "util.h" #include "rlocs.h" #include "packet.h" #include #include #include /* * Entry point. Expects an invocation like: * hide-eid * * hide-eid wraps packets that come to it unwrapped, and unwraps packets that * come to it wrapped. This makes it handy for all-in-one boxes */ int main(int argc, char** argv) { struct session session; struct recv_pkt recv_pkt; struct rsp_data to_send; ssize_t count; int result = 0; if ( argc < 4 ) { warn( "Usage: %s [ ]n", argv[0] ); return 1; } rlocs_init(); if ( !session_setup( &session, argv[1], argv[2], argv[3] ) ) { warn( "Failed to set up session, exiting" ); return 1; } memset( &recv_pkt, 0, sizeof( struct recv_pkt ) ); memset( &to_send, 0, sizeof( struct rsp_data ) ); warn( "TODO: Write BGP interventions to file" ); info( "Processing packets" ); while(1) { if ( ( count = read( session.listen_if, &recv_pkt, sizeof( struct recv_pkt ) ) ) < 0 ) { warn( "Failed to get a packet (%s)", strerror( errno ) ); break; } info( "Got a packet \\o/. %zu bytes", count ); switch( recv_pkt.hdr.ip.version ) { case 0x04 : if ( recv_pkt.hdr.ip.protocol == IPPROTO_HIDE_EID ) { result = unwrap_ipv4_packet( session.rlocs, &recv_pkt, &to_send ); } else { result = wrap_ipv4_packet( session.rlocs, &recv_pkt, &to_send ); } break; case 0x06 : /* TODO: ip6.protocol doesn't exist. And we're not wrapping * IPv6 packets yet anyway. if ( recv_pkt.hdr.ip6.protocol == IPPROTO_HIDE_EID ) { result = unwrap_ipv6_packet( wrap.rlocs, &recv_pkt, &to_send ); } else { result = wrap_ipv6_packet( wrap.rlocs, &recv_pkt, &to_send ); } */ warn( "TODO: wrap/unwrap IPv6 packets" ); break; default: warn( "Unknown IP version: %i", recv_pkt.hdr.ip.version ); } warn( "TODO: select wrap / unwrap functionality based on destination IP" ); if ( !result ) { warn( "Failed to process received packet, dropping." ); continue; } // no failure, but nothing to forward. if ( to_send.count == 0 ) { continue; } if ( ( count = writev( session.output_if, to_send.iovs, to_send.count ) ) < 0 ) { warn( "Error writing processed packet to output: %s", strerror(errno) ); } } info( "Finished, cleaning up" ); session_teardown( &session ); return 0; }