char *version = "0.1";
char *devtun = "tun://tun0";
-char *loctun = "tun://1.2.0.1";
-char *remtun = "tun://1.2.0.2";
-char *locudp = "udp:///1.1.0.1:1234";
-char *remudp = "udp://1.1.0.2:1234";
+char *loctun = "tun://10.2.0.1";
+char *remtun = "tun://10.2.0.2";
+char *locudp = "udp://10.1.0.1:1234";
+char *remudp = "udp://10.1.0.2:1235";
int stop = 0;
#define MAXPAYLOAD 1500
+DECLARE_VERBOSE_LEVEL (ulvpn, INFO);
+
void sig_handler (int sig)
{
switch (sig) {
case SIGINT:
- //stop = 1;
exit (0);
break;
case SIGTERM:
- exit (0);
+ stop = 1;
break;
}
}
printf (" -r: remote tun (%s)\n", remtun);
printf (" -t: local udp (%s)\n", locudp);
printf (" -u: remote udp (%s)\n", remudp);
- printf (" -v: verbose level [%d..%d] (%d)\n", ERROR, TRACE, verbose);
+ printf (" -v: program verbose level [%d..%d] (%d)\n", ERROR, TRACE, GET_VERBOSE_LEVEL (ulvpn));
+ printf (" -V: library verbose level [%d..%d] (%d)\n", ERROR, TRACE, GET_VERBOSE_LEVEL (mapec));
printf ("%s version %s\n", progname, version);
-'
+}
+
int main (int argc, char **argv)
{
while (argc-- > 1) {
char *arg = *(++argv);
if (arg[0] != '-') {
- filename = arg;
- continue;
+ usage ();
+ return 1;
}
char c = arg[1];
switch (c) {
case 'd':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (mapec, ERROR, PRINTF ("%s: dev tun not specified\n", progname));
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: dev tun not specified\n", progname));
return 1;
}
devtun = arg;
case 'l':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (mapec, ERROR, PRINTF ("%s: local tun not specified\n", progname));
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: local tun not specified\n", progname));
return 1;
}
loctun = arg;
case 'r':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (mapec, ERROR, PRINTF ("%s: remove tun not specified\n", progname));
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: remove tun not specified\n", progname));
return 1;
}
remtun = arg;
case 't':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (mapec, ERROR, PRINTF ("%s: local udp not specified\n", progname));
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: local udp not specified\n", progname));
return 1;
}
locudp = arg;
case 'u':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (mapec, ERROR, PRINTF ("%s: remove udp not specified\n", progname));
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: remove udp not specified\n", progname));
return 1;
}
remudp = arg;
case 'v':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (mapec, ERROR, PRINTF ("%s: verbose level not specified\n", progname));
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: verbose level not specified\n", progname));
+ return 1;
+ }
+ CHANGE_VERBOSE_LEVEL (ulvpn, atoi (arg));
+ break;
+ case 'V':
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ VERBOSE (ulvpn, ERROR, PRINTF ("%s: verbose level not specified\n", progname));
return 1;
}
CHANGE_VERBOSE_LEVEL (mapec, atoi (arg));
}
/* init communication channel */
- int tun_fd = MAPEC_Connect (devtun, locudp);
- if (tun_fd > 0) {
- VERBOSE (mapec, ERROR, PRINTF ("can't open communication for %s %s\n", devtun, loctun));
+ int tun_fd = MAPEC_Connect (devtun, loctun);
+ if (tun_fd < 0) {
+ VERBOSE (ulvpn, ERROR, PRINTF ("can't open communication for %s %s\n", devtun, loctun));
return -1;
}
int udp_fd = MAPEC_Connect (locudp, remudp);
- if (udp_fd > 0) {
- VERBOSE (mapec, ERROR, PRINTF ("can't open communication for %s %s\n", locudp, remudp));
+ if (udp_fd < 0) {
+ VERBOSE (ulvpn, ERROR, PRINTF ("can't open communication for %s %s\n", locudp, remudp));
return -1;
}
signal(SIGTERM, sig_handler);
/* main loop */
- while (stop) {
+ int rc;
+ while (!stop) {
+ rc = 2;
fd_set readset;
FD_ZERO (&readset);
}
uint8_t buffer[MAXPAYLOAD] = {0};
- int len = 0;
if (FD_ISSET (tun_fd, &readset)) {
- int r = MAPEC_Receive (tun_fd, buffer, &len);
+ int r = MAPEC_Receive (tun_fd, buffer, sizeof (buffer));
if (r < 0) {
// TODO: ignore some errno
VERBOSE (ulvpn, ERROR, PRINTF ("error when receiving from tun\n"));
- break;
- }
-
- //encrypt it
-
- r = MAPEC_Send (udp_fs, buffer, r);
- if (r < 0) {
- // TODO: ignore some errno
- VERBOSE (ulvpn, ERROR, PRINTF ("error when sending to udp\n"));
- break;
+ //break;
+ } else {
+ VERBOSE (ulvpn, DEBUG, PRINTF ("received from tun %d bytes\n", r));
+
+ //encrypt it
+
+ r = MAPEC_Send (udp_fd, buffer, r);
+ if (r < 0) {
+ // TODO: ignore some errno
+ VERBOSE (ulvpn, ERROR, PRINTF ("error when sending to udp\n"));
+ //break;
+ } else {
+ VERBOSE (ulvpn, DEBUG, PRINTF ("sent to udp %d bytes\n", r));
+ }
}
} else if (FD_ISSET (udp_fd, &readset)) {
- int r = MAPEC_Receive (udp_fd, buffer, &len);
+ int r = MAPEC_Receive (udp_fd, buffer, sizeof (buffer));
if (r < 0) {
// TODO: ignore some errno
VERBOSE (ulvpn, ERROR, PRINTF ("error when receiving from udp\n"));
- break;
- }
-
- //decrypt it
-
- r = MAPEC_Send (tun_fs, buffer, r);
- if (r < 0) {
- // TODO: ignore some errno
- VERBOSE (ulvpn, ERROR, PRINTF ("error when sending to tun\n"));
- break;
+ //break;
+ } else {
+ VERBOSE (ulvpn, DEBUG, PRINTF ("received from udp %d bytes\n", r));
+
+ //decrypt it
+
+ r = MAPEC_Send (tun_fd, buffer, r);
+ if (r < 0) {
+ // TODO: ignore some errno
+ VERBOSE (ulvpn, ERROR, PRINTF ("error when sending to tun\n"));
+ //break;
+ } else {
+ VERBOSE (ulvpn, DEBUG, PRINTF ("sent to tun %d bytes\n", r));
+ }
}
} else {
VERBOSE (ulvpn, ERROR, PRINTF ("select error\n"));
break;
}
+
+ rc = 0;
}
/* cleaning */
close (tun_fd);
close (udp_fd);
- return rc + GET_VERBOSE_ERRORS (mapec);
+ return rc;
}
/* test: mapec_valid.exe -h | grep usage */