# pppob.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       pppob.cc
    00003 ///             In the same vein as pppoe, used with pppd to create a
    00004 ///             pty tunnel and GPRS modem link.
    00005 ///
    00006
    00007 /*
    00008     Copyright (C) 2007-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00009
    00010     This program is free software; you can redistribute it and/or modify
    00011     it under the terms of the GNU General Public License as published by
    00012     the Free Software Foundation; either version 2 of the License, or
    00013     (at your option) any later version.
    00014
    00015     This program is distributed in the hope that it will be useful,
    00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00018
    00019     See the GNU General Public License in the COPYING file at the
    00020     root directory of this project for more details.
    00021 */
    00022
    00023 #include <barry/barry.h>
    00024 #include <iomanip>
    00025 #include <iostream>
    00026 #include <fstream>
    00027 #include <vector>
    00028 #include <string>
    00029 #include <memory>
    00030 #include <getopt.h>
    00031 #include <sys/select.h>
    00032 #include <sys/time.h>
    00033 #include <sys/types.h>
    00034 #include <unistd.h>
    00035 #include <signal.h>
    00036 #include "i18n.h"
    00037
    00038
    00039 using namespace std;
    00040 using namespace Barry;
    00041
    00042 bool data_dump = false;
    00043 volatile bool signal_end = false;
    00044
    00045 void Usage()
    00046 {
    00047    int major, minor;
    00048    const char *Version = Barry::Version(major, minor);
    00049
    00050    cerr
    00051    << "pppob - PPP over Barry\n"
    00052    << "        Copyright 2007-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
    00053    << "        Using: " << Version << "\n"
    00054    << "\n"
    00055    << "   -l file   Direct pppob log output to file (useful with -v)\n"
    00056    << "   -p pin    PIN of device to talk with\n"
    00057    << "             If only one device plugged in, this flag is optional\n"
    00058    << "   -P pass   Simplistic method to specify device password\n"
    00059    << "   -s        Use Serial mode instead of IpModem\n"
    00060    << "   -v        Dump protocol data during operation (debugging only!)\n"
    00061    << endl;
    00062 }
    00063
    00064 void signal_handler(int signum)
    00065 {
    00066         signal_end = true;
    00067 }
    00068
    00069 void SerialDataCallback(void *context, const unsigned char *data, int len)
    00070 {
    00071         if( len && data_dump )
    00072                 barryverbose("ReadThread:\n" << Data(data, len));
    00073
    00074         while( len ) {
    00075                 int written = write(1, data, len);
    00076                 if( written > 0 ) {
    00077                         len -= written;
    00078                         data += written;
    00079                 }
    00080                 else {
    00081                         barryverbose("Error in write()");
    00082                 }
    00083         }
    00084 }
    00085
    00086 void ProcessStdin(Modem &modem)
    00087 {
    00088         // Read from stdin and write to USB, until
    00089         // stdin is closed
    00090         Data data;
    00091         int bytes_read;
    00092         fd_set rfds;
    00093         struct timeval tv;
    00094         int ret;
    00095
    00096         // Handle interrupt signals from pppd
    00097         signal_end = false;
    00098         signal(SIGINT, &signal_handler);
    00099         signal(SIGHUP, &signal_handler);
    00100         signal(SIGTERM, &signal_handler);
    00101
    00102         FD_ZERO(&rfds);
    00103         while( signal_end == false ) {
    00104                 // Need to use select() here, so that pppd doesn't
    00105                 // hang when it tries to set the line discipline
    00106                 // on our stdin.
    00107
    00108                 FD_SET(0, &rfds);
    00109                 tv.tv_sec = 30;
    00110                 tv.tv_usec = 0;
    00111
    00112                 ret = select(1, &rfds, NULL, NULL, &tv);
    00113                 if( ret == -1 ) {
    00114                         perror("select()");
    00115                 }
    00116                 else if( ret && FD_ISSET(0, &rfds) ) {
    00117                         bytes_read = read(0, data.GetBuffer(), data.GetBufSize());
    00118                         if( bytes_read == 0 )
    00119                                 break;
    00120
    00121                         if( bytes_read > 0 ) {
    00122                                 data.ReleaseBuffer(bytes_read);
    00123                                 modem.Write(data);
    00124                         }
    00125                 }
    00126         }
    00127 }
    00128
    00129 int main(int argc, char *argv[])
    00130 {
    00131         INIT_I18N(PACKAGE);
    00132
    00133         cout.sync_with_stdio(true);     // leave this on, since libusb uses
    00134                                         // stdio for debug messages
    00135
    00136         try {
    00137
    00138                 uint32_t pin = 0;
    00139                 bool force_serial = false;
    00140                 std::string logfile;
    00141                 std::string password;
    00142
    00143                 // process command line options
    00144                 for(;;) {
    00145                         int cmd = getopt(argc, argv, "l:p:P:sv");
    00146                         if( cmd == -1 )
    00147                                 break;
    00148
    00149                         switch( cmd )
    00150                         {
    00151                         case 'l':       // Verbose log file
    00152                                 logfile = optarg;
    00153                                 break;
    00154
    00155                         case 'p':       // Blackberry PIN
    00156                                 pin = strtoul(optarg, NULL, 16);
    00157                                 break;
    00158
    00159                         case 'P':       // Device password
    00160                                 password = optarg;
    00161                                 break;
    00162
    00163                         case 's':       // Use Serial mode
    00164                                 force_serial = true;
    00165                                 break;
    00166
    00167                         case 'v':       // data dump on
    00168                                 data_dump = true;
    00169                                 break;
    00170
    00171                         case 'h':       // help
    00172                         default:
    00173                                 Usage();
    00174                                 return 0;
    00175                         }
    00176                 }
    00177
    00178                 // Initialize the barry library.  Must be called before
    00179                 // anything else.
    00180                 // Log to stderr, since stdout is for data in this program.
    00181                 std::auto_ptr<std::ofstream> log;
    00182                 if( logfile.size() ) {
    00183                         log.reset( new std::ofstream(logfile.c_str(), ios::app) );
    00184                         Barry::Init(data_dump, log.get());
    00185                 }
    00186                 else {
    00187                         Barry::Init(data_dump, &std::cerr);
    00188                 }
    00189
    00190                 // Display version if in data_dump mode
    00191                 if( data_dump ) {
    00192                         int major, minor;
    00193                         const char *Version = Barry::Version(major, minor);
    00194                         barryverbose(Version);
    00195                 }
    00196
    00197                 // Probe the USB bus for Blackberry devices and display.
    00198                 // If user has specified a PIN, search for it in the
    00199                 // available device list here as well
    00200                 Barry::Probe probe;
    00201                 int activeDevice = probe.FindActive(pin);
    00202                 if( activeDevice == -1 ) {
    00203                         if( pin )
    00204                                 cerr << "PIN " << setbase(16) << pin
    00205                                         << " not found" << endl;
    00206                         cerr << "No device selected" << endl;
    00207                         return 1;
    00208                 }
    00209
    00210                 const ProbeResult &device = probe.Get(activeDevice);
    00211
    00212                 if( !force_serial && device.HasIpModem() ) {
    00213                         barryverbose("Using IpModem mode...");
    00214
    00215                         // Create our controller object using our threaded router.
    00216                         Controller con(probe.Get(activeDevice));
    00217
    00218                         // Open serial mode... the callback handles reading from
    00219                         // USB and writing to stdout
    00220                         Mode::IpModem modem(con, SerialDataCallback, 0);
    00221                         modem.Open(password.c_str());
    00222
    00223                         ProcessStdin(modem);
    00224                         modem.Close();  // graceful close so we can restart without unplugging
    00225                 }
    00226                 else {
    00227                         if( force_serial ) {
    00228                                 barryverbose("Using Serial mode per command line...");
    00229                         }
    00230                         else {
    00231                                 barryverbose("No IpModem mode available, using Serial mode...");
    00232                         }
    00233
    00234                         // Create our socket router and start thread to handle
    00235                         // the USB reading, instead of creating our own thread.
    00236                         SocketRoutingQueue router;
    00237                         router.SpinoffSimpleReadThread();
    00238
    00239                         // Create our controller object using our threaded router.
    00240                         Controller con(probe.Get(activeDevice), router);
    00241
    00242                         // Open desktop mode... this handles the password side
    00243                         // of things
    00244                         Mode::Desktop desktop(con);
    00245                         desktop.Open(password.c_str());
    00246
    00247                         // Open serial connection
    00248                         Mode::Serial modem(con, SerialDataCallback, 0);
    00249                         modem.Open(password.c_str());
    00250
    00251                         ProcessStdin(modem);
    00252                 }
    00253
    00254                 barryverbose("Exiting");
    00255
    00256         }
    00257         catch( std::exception &e ) {
    00258                 cerr << "exception caught in main(): " << e.what() << endl;
    00259                 return 1;
    00260         }
    00261
    00262         return 0;
    00263 }
    00264