Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
keithw committed Jul 13, 2012
0 parents commit d6f48c2
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 0 deletions.
34 changes: 34 additions & 0 deletions donat
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/perl -w

use strict;

my @script = ( q[iptables -t nat -F],
q[iptables -F],
q[iptables -t nat -A POSTROUTING -o rmnet1 -j MASQUERADE],
q[iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT -i rmnet1 -o rndis0],
q[iptables -A FORWARD -j ACCEPT -o rmnet1 -i rndis0],
q[ifconfig rndis0 10.100.DEVICENUM.2 netmask 255.255.255.0 up],
q[echo 1 > /proc/sys/net/ipv4/ip_forward] );

my @devs = ( 'x', 'usb0', 'usb1', 'usb2' );

for my $device ( qw[A B C] ) {
my ( $num ) = $device =~ tr{ABC}{123}r;
my $dev = $devs[ $num ];

for my $line ( @script ) {
my $linemod = $line;
$linemod =~ s{DEVICENUM}{$num}g;
my $str = qq{./runon $device shell su -c "$linemod"};
print "$str\n";
system( $str );
}
system( "sudo ifconfig $dev 10.100.$num.1 netmask 255.255.255.0" );
system( "sudo route add -net default gw 10.100.$num.2 metric 2" );
system( "echo 0 | sudo tee /proc/sys/net/ipv4/conf/$dev/rp_filter" );
system( "echo 1 | sudo tee /proc/sys/net/ipv4/conf/$dev/accept_local" );
}

# A: 8a:71:5f:ef:c7:5e
# B: 0a:8d:31:bb:7c:96
# C: 6a:f6:8e:83:a7:7e
15 changes: 15 additions & 0 deletions runon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/perl -w

use strict;

my $ADB = '/home/keithw/android/android-sdk-linux/platform-tools/adb';

my %devices = ( A => '0149A97C0F012017',
B => '0149A44C0C012007',
C => '0149A44C1000D00C' );

my ( $device, @command ) = @ARGV;

die unless exists $devices{ $device };

exec { $ADB } ( 'adb', '-s', $devices{ $device }, @command );
12 changes: 12 additions & 0 deletions runscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/perl -w

use strict;

my ( $device, $script ) = @ARGV;

open SCRIPT, $script or die;

while ( <SCRIPT> ) {
chomp;
system( './runon', $device, 'shell', $_ );
}
24 changes: 24 additions & 0 deletions sender/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
source = send1way.cc socket.cc
objects = socket.o
executables = send1way

CXX = g++
CXXFLAGS = -g -O3 -std=c++0x -ffast-math -pedantic -Werror -Wall -Wextra -Weffc++ -fno-default-inline -pipe -D_FILE_OFFSET_BITS=64 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE
LIBS = -lm

all: $(executables)

send1way: send1way.o $(objects)
$(CXX) $(CXXFLAGS) -o $@ $+ $(LIBS)

%.o: %.cc
$(CXX) $(CXXFLAGS) -c -o $@ $<

-include depend

depend: $(source)
$(CXX) $(INCLUDES) -MM $(source) > depend

.PHONY: clean
clean:
-rm -f $(executables) depend *.o *.rpo
37 changes: 37 additions & 0 deletions sender/send1way.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "socket.hh"

int main( void )
{
Socket lte1_socket, lte2_socket, lte3_socket, ethernet_socket;

Socket::Address ethernet_address( "128.30.76.255", 9000 );
Socket::Address lte1_local_address( "10.100.1.1", 9001 );
Socket::Address lte2_local_address( "10.100.2.1", 9002 );
Socket::Address lte3_local_address( "10.100.3.1", 9003 );

ethernet_socket.bind( ethernet_address );
lte1_socket.bind( lte1_local_address );
lte2_socket.bind( lte2_local_address );
lte3_socket.bind( lte3_local_address );

ethernet_socket.bind_to_device( "eth0" );
lte1_socket.bind_to_device( "usb0" );
lte2_socket.bind_to_device( "usb1" );
lte3_socket.bind_to_device( "usb2" );

lte1_socket.send( Socket::Packet( ethernet_address, "Hello." ) );

Socket::Packet received( ethernet_socket.recv() );

printf( "From %s got \"%s\".\n", received.addr.str().c_str(), received.payload.c_str() );

sleep( 2 );

ethernet_socket.send( Socket::Packet( received.addr, "Goodbye." ) );

Socket::Packet received2( lte1_socket.recv() );

printf( "From %s got \"%s\".\n", received2.addr.str().c_str(), received2.payload.c_str() );

return 0;
}
89 changes: 89 additions & 0 deletions sender/socket.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdio.h>

#include "socket.hh"

using namespace std;

Socket::Address::Address( string ip, uint16_t port )
: _sockaddr()
{
_sockaddr.sin_family = AF_INET;
_sockaddr.sin_port = htons( port );

if ( inet_aton( ip.c_str(), &_sockaddr.sin_addr ) == 0 ) {
fprintf( stderr, "Invalid IP address (%s)\n", ip.c_str() );
exit( 1 );
}
}

const string Socket::Address::str( void ) const
{
char tmp[ 64 ];
snprintf( tmp, 64, "%s:%d", inet_ntoa( _sockaddr.sin_addr ), ntohs( _sockaddr.sin_port ) );
return string( tmp );
}

Socket::Socket()
: sock( socket( AF_INET, SOCK_DGRAM, 0 ) )
{
if ( sock < 0 ) {
perror( "socket" );
exit( 1 );
}
}

void Socket::bind( const Socket::Address & addr ) const
{
if ( ::bind( sock, (sockaddr *)&addr.sockaddr(), sizeof( addr.sockaddr() ) ) < 0 ) {
fprintf( stderr, "Error binding to %s\n", addr.str().c_str() );
perror( "bind" );
exit( 1 );
}
}

void Socket::send( const Socket::Packet & packet ) const
{
ssize_t bytes_sent = sendto( sock, packet.payload.data(), packet.payload.size(), 0,
(sockaddr *)&packet.addr.sockaddr(), sizeof( packet.addr.sockaddr() ) );
if ( bytes_sent != static_cast<ssize_t>( packet.payload.size() ) ) {
perror( "sendto" );
}
}

void Socket::bind_to_device( const std::string & name ) const
{
if ( setsockopt( sock, SOL_SOCKET, SO_BINDTODEVICE, name.c_str(), name.size() ) < 0 ) {
fprintf( stderr, "Error binding to %s\n", name.c_str() );
perror( "setsockopt SO_BINDTODEVICE" );
exit( 1 );
}
}

Socket::Packet Socket::recv( void ) const
{
const unsigned int BUF_SIZE = 2048;
char buf[ BUF_SIZE ];

struct sockaddr_in remote_addr;
socklen_t addrlen = sizeof( remote_addr );

ssize_t received_len = recvfrom( sock, buf, BUF_SIZE, 0, (sockaddr *)&remote_addr, &addrlen );

if ( received_len < 0 ) {
perror( "recvfrom" );
exit( 1 );
}

if ( received_len > BUF_SIZE ) {
fprintf( stderr, "Received oversize datagram (size %d) and limit is %d\n",
static_cast<int>( received_len ), BUF_SIZE );
exit( 1 );
}

return Socket::Packet( Socket::Address( remote_addr ),
string( buf, received_len ) );
}
43 changes: 43 additions & 0 deletions sender/socket.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef SOCKET_HH
#define SOCKET_HH

#include <sys/socket.h>
#include <netinet/in.h>
#include <string>

class Socket {
public:
class Address {
private:
struct sockaddr_in _sockaddr;

public:
Address( std::string ip, uint16_t port );
Address( const struct sockaddr_in s_sockaddr ) : _sockaddr( s_sockaddr ) {}

const struct sockaddr_in & sockaddr( void ) const { return _sockaddr; }
const std::string str( void ) const;
};

class Packet {
public:
Address addr;
std::string payload;

Packet( const Address &s_addr, const std::string &s_payload )
: addr( s_addr ), payload( s_payload )
{}
};

private:
const int sock;

public:
Socket();
void bind( const Address & addr ) const;
void send( const Packet & payload ) const;
void bind_to_device( const std::string & name ) const;
Packet recv( void ) const;
};

#endif

0 comments on commit d6f48c2

Please sign in to comment.