Exemple #1
0
int main( int argc, char **argv )
{
#if US_ENABLE_RAW_ETHERNET==1
#if defined (WIN32)
#define sleep(x) Sleep(x*1000)
    const char *if_name="\\Device\\NPF_{BD3BC03E-E00B-490E-97C4-7A01467918D4}";
#else
    const char *if_name="eth0";
#endif
    uint8_t dest_mac[6] = { 0x91, 0xe0, 0xf0, 0x01, 0x00, 0x00 };
    int fd;
    us_rawnet_context_t sock;
    if( argc>1 )
    {
        if_name=argv[1];
    }
    fd=us_rawnet_socket( &sock, 0x22f0, if_name, dest_mac );
    if( fd>=0 )
    {
        int i=0;
        int rep=0;
        for( rep=0; rep<10; ++rep )
        {
            uint8_t buf[1500];
            size_t buflen=sizeof(buf);
            for( i=0; i<buflen; ++i )
            {
                buf[i] = (uint8_t)i&0xff;
            }
            if( us_rawnet_send( &sock, dest_mac, buf, buflen )>=0 )
            {
                printf( "sent packet %d\n", rep );
            }
            sleep(1);
        }
    }
#endif

}
static ssize_t rawnet_multi_send_data( struct us_socket_collection_s *self,
                                       void *context,
                                       int fd,
                                       struct sockaddr const *to_addr,
                                       socklen_t to_addrlen,
                                       uint8_t const *buf_,
                                       size_t len )
{

    us_socket_collection_rawnet_multi_t *self_multi = (us_socket_collection_rawnet_multi_t *)self;
    us_rawnet_context_t *rawnet = (us_rawnet_context_t *)context;
    uint8_t *buf = (uint8_t *)buf_;
    uint8_t const *destaddr = 0;
    int destination_if = -1;
    ssize_t r = -1;

    (void)self;
    (void)fd;

    // if we were asked to send this to a specific address, then get the address
    if ( to_addr != 0 && to_addrlen >= sizeof( us_sockaddr_dl ) )
    {
        if ( to_addr->sa_family == US_AF_LINK )
        {
            destaddr = us_sockaddr_dl_get_mac( to_addr );
        }
        // look up the interface to use
        destination_if = us_rawnet_multi_route_find( self_multi->rawnet_multi, destaddr );
    }

    // only actually send it if it is either the correct interface to use or the message is multicast
    if ( destination_if == -1 || destination_if == rawnet->m_interface_id )
    {
        r = us_rawnet_send( rawnet, destaddr, buf, len );
    }
    return r;
}
static ssize_t rawnet_send_data( struct us_socket_collection_s *self,
                                 void *context,
                                 int fd,
                                 struct sockaddr const *to_addr,
                                 socklen_t to_addrlen,
                                 uint8_t const *buf_,
                                 size_t len )
{
    us_rawnet_context_t *rawnet = (us_rawnet_context_t *)context;
    uint8_t *buf = (uint8_t *)buf_;
    uint8_t const *destaddr = 0;

    (void)self;
    (void)fd;
    if ( to_addr != 0 && to_addrlen >= sizeof( us_sockaddr_dl ) )
    {
        if ( to_addr->sa_family == US_AF_LINK )
        {
            destaddr = us_sockaddr_dl_get_mac( to_addr );
        }
    }

    return us_rawnet_send( rawnet, destaddr, buf, len );
}