pppob.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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-2010, 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
00089
00090 Data data;
00091 int bytes_read;
00092 fd_set rfds;
00093 struct timeval tv;
00094 int ret;
00095
00096
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
00105
00106
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);
00134
00135
00136 try {
00137
00138 uint32_t pin = 0;
00139 bool force_serial = false;
00140 std::string logfile;
00141 std::string password;
00142
00143
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':
00152 logfile = optarg;
00153 break;
00154
00155 case 'p':
00156 pin = strtoul(optarg, NULL, 16);
00157 break;
00158
00159 case 'P':
00160 password = optarg;
00161 break;
00162
00163 case 's':
00164 force_serial = true;
00165 break;
00166
00167 case 'v':
00168 data_dump = true;
00169 break;
00170
00171 case 'h':
00172 default:
00173 Usage();
00174 return 0;
00175 }
00176 }
00177
00178
00179
00180
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
00191 if( data_dump ) {
00192 int major, minor;
00193 const char *Version = Barry::Version(major, minor);
00194 barryverbose(Version);
00195 }
00196
00197
00198
00199
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
00216 Controller con(probe.Get(activeDevice));
00217
00218
00219
00220 Mode::IpModem modem(con, SerialDataCallback, 0);
00221 modem.Open(password.c_str());
00222
00223 ProcessStdin(modem);
00224 modem.Close();
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
00235
00236 SocketRoutingQueue router;
00237 router.SpinoffSimpleReadThread();
00238
00239
00240 Controller con(probe.Get(activeDevice), router);
00241
00242
00243
00244 Mode::Desktop desktop(con);
00245 desktop.Open(password.c_str());
00246
00247
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