📄 main.cpp
字号:
#include "global.h"#include "dot1x.h"#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <signal.h>using namespace std;using namespace NetworkOperation;extern char *optarg;
extern int optind;
char* prog_name;
void usage ();
int check_instance();void init_daemon();void handler(int signum)
{
system("pkill dhclient");
exit(0);
}int main(int argc, char **argv){ InterfaceInfo interfaces[MAXINTERFACES]; int interfacenum; char *user; int next; /* next opting character */ int dFlag, kFlag, rFlag,uFlag; const char* const s_options = "drhku:"; const struct option l_options[] = { {"daemon",0,NULL,'d'}, {"renew",0,NULL,'r'}, {"help",0,NULL,'h'}, {"kill",0,NULL,'k'}, {"user",1,NULL,'u'}, {NULL,0,NULL,0} }; prog_name = argv[0]; user = NULL; dFlag = rFlag = kFlag = uFlag = 0; if(!strcmp(prog_name,"h3cclient")) { cout << "Please rename this program to 'h3cclient'." << endl; exit(0); } do{ next = getopt_long(argc, argv, s_options, l_options, NULL); switch(next){ case 'd': dFlag = 1; break; case 'r': rFlag = 1; break; case 'k': kFlag = 1; break; case 'u': uFlag = 1; user = (char*)malloc(strlen(optarg) + 1); strcpy(user, optarg); break; case 'h': usage(); case '?': usage(); case -1: break; default : abort(); } }while(next != -1);// disconnect by killing the program if(kFlag) {// HuaweiNetwork dot1x(username, passwd, ifname, ip, interfaces[curinterfaceid].macaddr,rFlag,dFlag); // rFlag dFlag added.// dot1x.DisConnect(); system("pkill -10 h3cclient");
exit(0); }// check instance/* if(0 != check_instance()) { cout << "An instance already exists. You can use 'h3cclient -k' to stop it." << endl; exit(0); }*/// get interface name if (optind >= argc) { cout << "You must specify a interface, e.g. eth0." << endl; exit(0); } system("pkill dhclient"); string ifname = argv[optind]; if (dFlag) {
init_daemon();
} (void)signal(SIGUSR1, handler); if ((interfacenum = GetSystemInterfaceInfo(interfaces)) < 1) {
cout << "Can't find any interface." << endl;
exit(1);
};// get username and password string username, passwd; if(uFlag) { char *p = strtok(user, "/");
username = p;
p = strtok(0, "/");
if (p == NULL) { /* no passwd */
usage();
} else
passwd = p; } else { cout << "You must specify username and password." <<endl; exit(0); }// get IP address char ipaddress[20];
int curinterfaceid = -1;
for (int i = 0; i < interfacenum; i++) {
//if (strcmp(ifname.c_str, interfaces[i].name.c_str()) == 0)
if (ifname == interfaces[i].name) {
curinterfaceid = i;
break;
}
}
if (curinterfaceid < 0) {
printf( "can't find the specified interface.\n");
exit(1);
}
strcpy(ipaddress,interfaces[curinterfaceid].ipaddr.c_str());
int ip[4];
char * p = strtok(ipaddress,".");
ip[0] = atoi(p);
for (int i=1;i<4;i++) {
p = strtok(0,".");
ip[i] = atoi(p);
} HuaweiNetwork dot1x(username, passwd, ifname, ip, interfaces[curinterfaceid].macaddr,rFlag); // rFlag dFlag added. if (dot1x.Connect())
dot1x.run();
else {
cout << "Send login failed." << endl;
exit(1);
} return 0;}void usage(){ printf("usage: %s [-d] [-r] [-h] [-k] -u username/password interface\n" "\t\t-d --daemon\trun as daemon.\n" "\t\t-r --renew\trenew IP address.\n" "\t\t-h --help\tdiaplay usage.\n" "\t\t-k --kill\tdisconnect.\n" "\t\t-u --user\tusername/password\n" "You can update this program by visiting http://www.flyx.cn/\n",prog_name); exit(0);}// check that is there a instance of this program
// return 0 for there is a running instance while non-zero means noneint check_instance(){ return system("pgrep h3cclient > /dev/null");}// run as daemonvoid init_daemon(){ int i, numfiles;
pid_t pid;
cout << "Running as daemon..." << endl;
pid = fork();
if (pid > 0) /* parent process */
exit(0);
else if (pid < 0) /* fork failed */
exit(1);
/* here is the child */
setsid(); /* so it is the leader of session and process group */
pid = fork();
if (pid > 0)
exit(0);
else if (pid < 0)
exit(1);
/* reconfigure environment */
numfiles = getdtablesize();
for (i = 0; i < numfiles; i++)
close(i);
umask(0);
chdir("/tmp");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -