summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--console.c57
-rw-r--r--console.h2
-rw-r--r--devices.c2
-rw-r--r--mactelnet.c4
-rw-r--r--mactelnet.h2
-rw-r--r--main.c73
-rw-r--r--mndp.c2
7 files changed, 122 insertions, 20 deletions
diff --git a/console.c b/console.c
index 2ac2068..3592eb6 100644
--- a/console.c
+++ b/console.c
@@ -1,14 +1,67 @@
+/*
+ Mac-Telnet - Connect to RouterOS routers via MAC address
+ Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
+
+ 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 2 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, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
+
+struct termios origTerm;
+
+int rawTerm() {
+ struct termios new;
+
+ if (tcgetattr(STDIN_FILENO, &origTerm) < 0) {
+ perror("tcgetattr");
+ return -1;
+ }
+
+ memcpy(&new, &origTerm, sizeof(struct termios) );
+
+ /* raw mode, from tcsetattr man page */
+ new.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
+ new.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
+ new.c_cflag &= ~(CSIZE|PARENB);
+ new.c_cflag |= CS8;
+
+ if (tcsetattr(STDIN_FILENO, TCSANOW, &new) < 0) {
+ perror("tcsetattr");
+ return -1;
+ }
+ return 0;
+}
+
+int resetTerm() {
+ if (tcsetattr(STDIN_FILENO, TCSANOW, &origTerm) < 0) {
+ perror("tcsetattr");
+ return -1;
+ }
+ return 0;
+}
int getTerminalSize(unsigned short *width, unsigned short *height) {
struct winsize ws;
- if (ioctl(0,TIOCGWINSZ,&ws) != 0) {
- fprintf(stderr,"TIOCGWINSZ:%s\n",strerror(errno));
+ if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != 0) {
+ perror("TIOCGWINSZ");
return -1;
}
diff --git a/console.h b/console.h
index 3a303fb..4f3c990 100644
--- a/console.h
+++ b/console.h
@@ -1 +1,3 @@
extern int getTerminalSize(unsigned short *width, unsigned short *height);
+extern int rawTerm();
+extern int resetTerm();
diff --git a/devices.c b/devices.c
index 1c69710..70daabe 100644
--- a/devices.c
+++ b/devices.c
@@ -1,5 +1,5 @@
/*
- Mac-Telnet - Connect to RouterOS clients via MAC address
+ Mac-Telnet - Connect to RouterOS routers via MAC address
Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
This program is free software; you can redistribute it and/or modify
diff --git a/mactelnet.c b/mactelnet.c
index 0d8de3d..e28b946 100644
--- a/mactelnet.c
+++ b/mactelnet.c
@@ -1,5 +1,5 @@
/*
- Mac-Telnet - Connect to RouterOS clients via MAC address
+ Mac-Telnet - Connect to RouterOS routers via MAC address
Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -116,7 +116,7 @@ int parseControlPacket(unsigned char *data, const int data_len, struct mt_mactel
cpkthdr->cptype = data[4];
/* Control packet data length */
- cpkthdr->length = data[5]<<24|data[6]<<16|data[7]<<8|data[8];
+ cpkthdr->length = data[5] << 24 | data[6] << 16 | data[7] << 8 | data[8];
/* Set pointer to actual data */
cpkthdr->data = data + 9;
diff --git a/mactelnet.h b/mactelnet.h
index 8275169..44d3a09 100644
--- a/mactelnet.h
+++ b/mactelnet.h
@@ -1,5 +1,5 @@
/*
- Mac-Telnet - Connect to RouterOS clients via MAC address
+ Mac-Telnet - Connect to RouterOS routers via MAC address
Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
This program is free software; you can redistribute it and/or modify
diff --git a/main.c b/main.c
index d332aca..833e81c 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
/*
- Mac-Telnet - Connect to RouterOS clients via MAC address
+ Mac-Telnet - Connect to RouterOS routers via MAC address
Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
This program is free software; you can redistribute it and/or modify
@@ -18,9 +18,11 @@
*/
#include <stdlib.h>
#include <stdio.h>
+#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ether.h>
+#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
@@ -36,6 +38,9 @@ int sockfd;
int deviceIndex;
int outcounter=0;
int sessionkey=0;
+int running = 1;
+
+unsigned char terminalMode = 0;
unsigned char srcmac[ETH_ALEN];
unsigned char dstmac[ETH_ALEN];
@@ -123,6 +128,7 @@ void handlePacket(unsigned char *data, int data_len) {
printf("Received encryption key of %d characters\n", cpkt.length);
}
+
else if (cpkt.cptype == MT_CPTYPE_PLAINDATA) {
cpkt.data[cpkt.length] = 0;
printf("%s", cpkt.data);
@@ -138,7 +144,8 @@ void handlePacket(unsigned char *data, int data_len) {
plen = initPacket(odata, MT_PTYPE_END, srcmac, dstmac, pkthdr.seskey, 0);
result = sendCustomUDP(sockfd, deviceIndex, srcmac, dstmac, &sourceip, 20561, &destip, 20561, odata, plen);
fprintf(stderr, "Connection closed.\n");
- exit(0);
+ /* exit */
+ running = 0;
} else {
fprintf(stderr, "Unhandeled packet type: %d received from server %s\n", pkthdr.ptype, ether_ntoa((struct ether_addr *)dstmac));
}
@@ -154,6 +161,9 @@ int main (int argc, char **argv) {
struct sockaddr_in si_me;
char buff[1500];
int plen = 0;
+ struct timeval timeout;
+ fd_set read_fds;
+
if (argc < 4) {
fprintf(stderr, "Usage: %s <ifname> <MAC> <username> <password>\n", argv[0]);
@@ -214,7 +224,10 @@ int main (int argc, char **argv) {
/* Sessioon key */
sessionkey = rand() % 65535;
- printf("Connecting to %s...\n", ether_ntoa((struct ether_addr *)dstmac));
+ /* stop output buffering */
+ setvbuf(stdout, (char*)NULL, _IONBF, 0);
+
+ printf("Connecting to %s...", ether_ntoa((struct ether_addr *)dstmac));
plen = initPacket(data, MT_PTYPE_SESSIONSTART, srcmac, dstmac, sessionkey, 0);
result = sendCustomUDP(sockfd, deviceIndex, srcmac, dstmac, &sourceip, 20561, &destip, 20561, data, plen);
@@ -223,7 +236,20 @@ int main (int argc, char **argv) {
if (DEBUG)
printf("Sessionkey: %d\n", sessionkey);
+ FD_ZERO(&read_fds);
+ FD_SET(insockfd, &read_fds);
+ timeout.tv_sec = 5;
+ timeout.tv_usec = 0;
+ select(insockfd+1, &read_fds, NULL, NULL, &timeout);
+
+ if (!FD_ISSET(insockfd, &read_fds)) {
+ fprintf(stderr, "Connection timed out\n");
+ exit(1);
+ }
+
result = recvfrom(insockfd, buff, 1400, 0, 0, 0);
+ printf("done\n");
+
handlePacket(buff, result);
/*
@@ -246,16 +272,37 @@ int main (int argc, char **argv) {
}
handlePacket(buff, result);
- memset(buff, 0, 1500);
- result = recvfrom(insockfd, buff, 1500, 0, 0, 0);
- handlePacket(buff, result);
-
-while(1) {
- memset(buff, 0, 1500);
- result = recvfrom(insockfd, buff, 1500, 0, 0, 0);
- handlePacket(buff, result);
-}
-
+ /* stop input buffering at all levels. Give full control of terminal to RouterOS */
+ rawTerm();
+ setvbuf(stdin, (char*)NULL, _IONBF, 0);
+
+ while (running) {
+ int reads;
+
+ FD_ZERO(&read_fds);
+ FD_SET(0, &read_fds);
+ FD_SET(insockfd, &read_fds);
+ timeout.tv_sec = 1;
+ timeout.tv_usec = 0;
+
+ reads = select(insockfd+1, &read_fds, NULL, NULL, &timeout);
+ if (reads > 0) {
+ if (FD_ISSET(insockfd, &read_fds)) {
+ memset(buff, 0, 1500);
+ result = recvfrom(insockfd, buff, 1500, 0, 0, 0);
+ handlePacket(buff, result);
+ }
+ if (FD_ISSET(0, &read_fds)) {
+ unsigned char key = getc(stdin);
+ memset(data, 0, sizeof(data));
+ plen = initPacket(data, MT_PTYPE_DATA, srcmac, dstmac, sessionkey, outcounter);
+ outcounter ++;
+ memcpy(data + plen, &key, 1);
+ result = sendCustomUDP(sockfd, deviceIndex, srcmac, dstmac, &sourceip, 20561, &destip, 20561, data, plen + 1);
+ }
+ }
+ }
+ resetTerm();
close(sockfd);
close(insockfd);
diff --git a/mndp.c b/mndp.c
index ec0a188..3e65192 100644
--- a/mndp.c
+++ b/mndp.c
@@ -1,5 +1,5 @@
/*
- Mac-Telnet - Connect to RouterOS clients via MAC address
+ Mac-Telnet - Connect to RouterOS routers via MAC address
Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com>
This program is free software; you can redistribute it and/or modify