Skip to content

Commit

Permalink
PeerVPN version 0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Volk authored and zoranzaric committed Sep 25, 2012
0 parents commit 81b47a3
Show file tree
Hide file tree
Showing 19 changed files with 3,925 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CC=gcc
CFLAGS=-Wall -O3
LDFLAGS=-lssl

all: peervpn
peervpn: peervpn.o

clean:
rm -f peervpn peervpn.o
238 changes: 238 additions & 0 deletions config.ic
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/***************************************************************************
* Copyright (C) 2008 by Tobias Volk *
* [email protected] *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/


static int parseConfigBoolean(char *str) {
if(strncmp(str,"true",4) == 0) {
return 1;
}
else if(strncmp(str,"1",1) == 0) {
return 1;
}
else if(strncmp(str,"yes",3) == 0) {
return 1;
}
else if(strncmp(str,"false",5) == 0) {
return 0;
}
else if(strncmp(str,"0",1) == 0) {
return 0;
}
else if(strncmp(str,"no",2) == 0) {
return 0;
}
else {
return -1;
}
}

static int parseConfigIsEOLChar(char c) {
switch(c) {
case '#':
case ';':
case '\0':
case '\r':
return 1;
default:
return 0;
}
}

static int parseConfigLineCheckCommand(char *line, int linelen, const char *cmd, int *vpos) {
int cmdlen = strlen(cmd);
if(linelen >= cmdlen) {
if(strncmp(line,cmd,cmdlen) == 0) {
if(parseConfigIsEOLChar(line[cmdlen])) {
*vpos = cmdlen;
return 1;
}
else if(isWhitespaceChar(line[cmdlen])) {
*vpos = cmdlen;
while(((*vpos)+1) < linelen) {
if(isWhitespaceChar(line[*vpos])) {
*vpos = (*vpos)+1;
}
else {
break;
}
}
return 1;
}
else {
return 0;
}
}
else {
return 0;
}
}
else {
return 0;
}
}

static int parseConfigLine(char *line, int len, struct s_initconfig *cs) {
int vpos,a;
if(!(len > 0)) {
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"echo",&vpos)) {
printf("%s\n",&line[vpos]);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"local",&vpos)) {
strncpy(cs->sourceip,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"port",&vpos)) {
strncpy(cs->sourceport,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"user",&vpos)) {
strncpy(cs->userstr,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"group",&vpos)) {
strncpy(cs->groupstr,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"chroot",&vpos)) {
strncpy(cs->chrootstr,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"networkname",&vpos)) {
strncpy(cs->networkname,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"interface",&vpos)) {
strncpy(cs->tapname,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"upcmd",&vpos)) {
strncpy(cs->upcmd,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"initpeers",&vpos)) {
strncpy(cs->initpeers,&line[vpos],CONFPARSER_NAMEBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"psk",&vpos)) {
cs->psklen = hexStringToByteArray(&line[vpos],cs->psk,CRYPTO_PSKBUF_SIZE);
return 1;
}
else if(parseConfigLineCheckCommand(line,len,"enableconsole",&vpos)) {
if((a = parseConfigBoolean(&line[vpos])) < 0) {
return -1;
}
else {
cs->enableconsole = a;
return 1;
}
}
else if(parseConfigLineCheckCommand(line,len,"enabletunneling",&vpos)) {
if((a = parseConfigBoolean(&line[vpos])) < 0) {
return -1;
}
else {
cs->enableeth = a;
return 1;
}
}
else if(parseConfigLineCheckCommand(line,len,"allowrelay",&vpos)) {
if((a = parseConfigBoolean(&line[vpos])) < 0) {
return -1;
}
else {
cs->enablerelay = a;
return 1;
}
}
else if(parseConfigLineCheckCommand(line,len,"allowindirect",&vpos)) {
if((a = parseConfigBoolean(&line[vpos])) < 0) {
return -1;
}
else {
cs->enableindirect = a;
return 1;
}
}
else if(parseConfigLineCheckCommand(line,len,"windowsize",&vpos)) {
if(sscanf(&line[vpos],"%d",&a) > 0) {
if(a > 32 && a < 65536) {
cs->windowsize = a;
return 1;
}
else {
return -1;
}
}
else {
return -1;
}
}
else if(parseConfigLineCheckCommand(line,len,"endconfig",&vpos)) {
return 0;
}
else {
return -1;
}
}

static void parseConfigFile(int fd, struct s_initconfig *cs) {
char line[CONFPARSER_LINEBUF_SIZE+1];
char c;
int linepos = 0;
int waiteol = 0;
int rc;
while((read(fd,&c,1)) > 0) {
if(c == '\n') {
while(linepos > 0) {
if(isWhitespaceChar(line[linepos-1])) {
linepos--;
}
else {
break;
}
}
line[linepos] = '\0';
rc = parseConfigLine(line,linepos,cs);
if(rc < 0) throwError("config file parse error!");
if(rc == 0) break;
linepos = 0;
waiteol = 0;
}
else {
if((!waiteol) && (!(linepos == 0 && isWhitespaceChar(c)))) {
if(parseConfigIsEOLChar(c)) {
line[linepos] = '\0';
waiteol = 1;
}
else {
if(linepos < (CONFPARSER_LINEBUF_SIZE)) {
line[linepos] = c;
linepos++;
}
else {
line[linepos] = '\0';
waiteol = 1;
}
}
}
}
}
}
97 changes: 97 additions & 0 deletions console.ic
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/***************************************************************************
* Copyright (C) 2008 by Tobias Volk *
* [email protected] *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/


// print table of MAC addresses
static void printMacTable() {
int i;
char macstring[MACTREE_KEYSIZE*2+1];
printf("MAC address overview:\n");
for(i=0; i<g_mactree.nodecount; i++) {
byteArrayToString(macstring,g_mactree.key[i],MACTREE_KEYSIZE);
printf(" #%04d: mac=%s, peerid=0x%02X\n", i+1, macstring, g_mactree.value[i][0]);
}
}


// print table of active peers
static void printActivePeerTable() {
int i,pos;
char ipstring[PEERADDR_LENGTH*2+1];
char clientstring[CLIENTID_LENGTH*2+1];
printf("List of active peers:\n");
for(i=0; i<g_peertable.fwdcount; i++) {
pos = g_peertable.fwdlookup[i];
byteArrayToString(ipstring,g_peertable.addr[pos].addr,PEERADDR_LENGTH);
byteArrayToString(clientstring,g_peertable.clientid[pos].id,CLIENTID_LENGTH);
printf(" #%03d: peerid=0x%02X, clientid=%s, addr=%s, lastseen=%03d\n", i+1, pos, clientstring, ipstring, getUptime() - g_peertable.lastseen[pos]);
}
}


// parse command
static void parseCMD(char *cmd, int cmdlen) {
if(cmd[0] == 'A' || cmd[0] == 'a') {
// ACTIVEPEERTABLE
printActivePeerTable();
}
if(cmd[0] == 'I' || cmd[0] == 'i') {
// INSERTPEER
char pa[1024];
char pb[1024];
char pc[1024];
struct s_peeraddr paddr;
sscanf(cmd,"%s %s %s",pa,pb,pc);
if(setPeerAddrByName(&paddr,pb,pc)) {
if(connectNewPeer(NULL,&paddr,1,0,0,0)) {
printf("new connection created.\n");
}
else {
printf("could not create new connection.\n");
}
}
else {
printf("could not get peer address.\n");
}

}
if(cmd[0] == 'M' || cmd[0] == 'm') {
#if DEBUG_LEVEL > 0
if(cmd[1] == 'E' || cmd[1] == 'e') {
// MEMORY
printMemoryOverview();
}
#endif
if(cmd[1] == 'A' || cmd[1] == 'a') {
// MACTABLE
printMacTable();
}
}
if(cmd[0] == 'P' || cmd[0] == 'p') {
// PEERTABLE
printActivePeerTable();
}
}


// decode console command
static void decodeConsole() {
char cmd[COMMAND_BUFSIZE] = {0,0};
int len = read(g_fd[FDID_CONSOLE].fd, cmd, COMMAND_BUFSIZE);
if(len > 0) parseCMD(cmd,len);
}
Loading

0 comments on commit 81b47a3

Please sign in to comment.