Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit d6f48c21349b5a103b3097842b71be7a8a8a382e @keithw committed Jul 13, 2012
Showing with 254 additions and 0 deletions.
  1. +34 −0 donat
  2. +15 −0 runon
  3. +12 −0 runscript
  4. +24 −0 sender/Makefile
  5. +37 −0 sender/send1way.cc
  6. +89 −0 sender/socket.cc
  7. +43 −0 sender/socket.hh
34 donat
@@ -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 runon
@@ -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 );
@@ -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', $_ );
+}
@@ -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
@@ -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;
+}
@@ -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 ) );
+}
@@ -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.