summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--macping.c101
-rw-r--r--mactelnet.c8
-rw-r--r--mactelnetd.c38
-rw-r--r--protocol.c11
-rw-r--r--udp.c4
5 files changed, 94 insertions, 68 deletions
diff --git a/macping.c b/macping.c
index 7731176..2f37a90 100644
--- a/macping.c
+++ b/macping.c
@@ -99,14 +99,17 @@ static long long int toddiff(struct timeval *tod1, struct timeval *tod2)
static void display_results() {
int percent = (int)((100.f/ping_sent) * pong_received);
- if (percent > 100)
+ if (percent > 100) {
percent = 0;
+ }
- if (percent < 0)
+ if (percent < 0) {
percent = 0;
+ }
- if (min_ms == FLT_MAX)
+ if (min_ms == FLT_MAX) {
min_ms = 0;
+ }
printf("\n");
printf("%d packets transmitted, %d packets received, %d%% packet loss\n", ping_sent, pong_received, 100 - percent);
@@ -133,8 +136,9 @@ int main(int argc, char **argv) {
while (1) {
c = getopt(argc, argv, "fs:c:hv?");
- if (c == -1)
+ if (c == -1) {
break;
+ }
switch (c) {
case 'f':
@@ -289,51 +293,54 @@ int main(int argc, char **argv) {
while (waitforpacket) {
/* Wait for data or timeout */
reads = select(insockfd+1, &read_fds, NULL, NULL, &timeout);
- if (reads > 0) {
- unsigned char buff[1500];
- struct sockaddr_in saddress;
- unsigned int slen = sizeof(saddress);
- struct mt_mactelnet_hdr pkthdr;
-
- result = recvfrom(insockfd, buff, 1500, 0, (struct sockaddr *)&saddress, &slen);
- parse_packet(buff, &pkthdr);
-
- /* TODO: Check that we are the receiving host */
- if (pkthdr.ptype == MT_PTYPE_PONG) {
- struct timeval pongtimestamp;
- struct timeval nowtimestamp;
-
- waitforpacket = 0;
- gettimeofday(&nowtimestamp, NULL);
-
- memcpy(&pongtimestamp, pkthdr.data - 4, sizeof(pongtimestamp));
- if (memcmp(pkthdr.data - 4, pingdata, ping_size) == 0) {
- float diff = toddiff(&nowtimestamp, &pongtimestamp) / 1000.0f;
-
- if (diff < min_ms)
- min_ms = diff;
-
- if (diff > max_ms)
- max_ms = diff;
-
- avg_ms += diff;
-
- printf("%s %d byte, ping time %.2f ms%s\n", ether_ntoa((struct ether_addr *)&(pkthdr.srcaddr)), result, diff, (char *)(memcmp(&pongtimestamp,&lasttimestamp,sizeof(lasttimestamp)) == 0 ? " DUP" : ""));
- } else {
- printf("%s Reply of %d bytes of unequal data\n", ether_ntoa((struct ether_addr *)&(pkthdr.srcaddr)), result);
- }
- pong_received++;
- memcpy(&lasttimestamp, &pongtimestamp, sizeof(pongtimestamp));
- if (!fastmode) {
- sleep(1);
- }
- } else {
- /* Wait for the correct packet */
- continue;
- }
- } else {
+ if (reads <= 0) {
waitforpacket = 0;
fprintf(stderr, "%s ping timeout\n", ether_ntoa((struct ether_addr *)&dstmac));
+ break;
+ }
+
+ unsigned char buff[1500];
+ struct sockaddr_in saddress;
+ unsigned int slen = sizeof(saddress);
+ struct mt_mactelnet_hdr pkthdr;
+
+ result = recvfrom(insockfd, buff, 1500, 0, (struct sockaddr *)&saddress, &slen);
+ parse_packet(buff, &pkthdr);
+
+ /* TODO: Check that we are the receiving host */
+ if (pkthdr.ptype != MT_PTYPE_PONG) {
+ /* Wait for the correct packet */
+ continue;
+ }
+
+ struct timeval pongtimestamp;
+ struct timeval nowtimestamp;
+
+ waitforpacket = 0;
+ gettimeofday(&nowtimestamp, NULL);
+
+ memcpy(&pongtimestamp, pkthdr.data - 4, sizeof(pongtimestamp));
+ if (memcmp(pkthdr.data - 4, pingdata, ping_size) == 0) {
+ float diff = toddiff(&nowtimestamp, &pongtimestamp) / 1000.0f;
+
+ if (diff < min_ms) {
+ min_ms = diff;
+ }
+
+ if (diff > max_ms) {
+ max_ms = diff;
+ }
+
+ avg_ms += diff;
+
+ printf("%s %d byte, ping time %.2f ms%s\n", ether_ntoa((struct ether_addr *)&(pkthdr.srcaddr)), result, diff, (char *)(memcmp(&pongtimestamp,&lasttimestamp,sizeof(lasttimestamp)) == 0 ? " DUP" : ""));
+ } else {
+ printf("%s Reply of %d bytes of unequal data\n", ether_ntoa((struct ether_addr *)&(pkthdr.srcaddr)), result);
+ }
+ pong_received++;
+ memcpy(&lasttimestamp, &pongtimestamp, sizeof(pongtimestamp));
+ if (!fastmode) {
+ sleep(1);
}
}
}
diff --git a/mactelnet.c b/mactelnet.c
index cb792ee..2dfe04c 100644
--- a/mactelnet.c
+++ b/mactelnet.c
@@ -135,8 +135,9 @@ static int send_udp(struct mt_packet *packet, int retransmit) {
send_udp(packet, 0);
}
- if (terminal_mode)
+ if (terminal_mode) {
reset_term();
+ }
fprintf(stderr, "\nConnection timed out\n");
exit(1);
@@ -274,7 +275,7 @@ static int handle_packet(unsigned char *data, int data_len) {
}
}
else if (pkthdr.ptype == MT_PTYPE_ACK) {
- /* TODO: If we were resubmitting lost messages, stop resubmitting here if received counter is correct. */
+ /* Handled elsewhere */
}
/* The server wants to terminate the connection, we have to oblige */
@@ -381,8 +382,9 @@ int main (int argc, char **argv) {
while (1) {
c = getopt(argc, argv, "nt:u:p:vh?");
- if (c == -1)
+ if (c == -1) {
break;
+ }
switch (c) {
diff --git a/mactelnetd.c b/mactelnetd.c
index cb1dfcd..0654009 100644
--- a/mactelnetd.c
+++ b/mactelnetd.c
@@ -148,8 +148,9 @@ static void list_add_connection(struct mt_connection *conn) {
static void list_remove_connection(struct mt_connection *conn) {
struct mt_connection *p;
struct mt_connection *last;
- if (connections_head == NULL)
+ if (connections_head == NULL) {
return;
+ }
if (conn->state == STATE_ACTIVE && conn->ptsfd > 0) {
close(conn->ptsfd);
@@ -179,8 +180,9 @@ static void list_remove_connection(struct mt_connection *conn) {
static struct mt_connection *list_find_connection(unsigned short seskey, unsigned char *srcmac) {
struct mt_connection *p;
- if (connections_head == NULL)
+ if (connections_head == NULL) {
return NULL;
+ }
for (p = connections_head; p != NULL; p = p->next) {
if (p->seskey == seskey && memcmp(srcmac, p->srcmac, 6) == 0) {
@@ -195,8 +197,9 @@ static int find_socket(unsigned char *mac) {
int i;
for (i = 0; i < sockets_count; ++i) {
- if (memcmp(mac, sockets[i].mac, ETH_ALEN) == 0)
+ if (memcmp(mac, sockets[i].mac, ETH_ALEN) == 0) {
return i;
+ }
}
return -1;
}
@@ -316,8 +319,9 @@ static void uwtmp_login(struct mt_connection *conn) {
pid = getpid();
char *line = ttyname(conn->slavefd);
- if (strncmp(line, "/dev/", 5) == 0)
+ if (strncmp(line, "/dev/", 5) == 0) {
line += 5;
+ }
/* Setup utmp struct */
memset((void *) &utent, 0, sizeof(utent));
@@ -326,7 +330,7 @@ static void uwtmp_login(struct mt_connection *conn) {
strncpy(utent.ut_user, conn->username, sizeof(utent.ut_user));
strncpy(utent.ut_line, line, sizeof(utent.ut_line));
strncpy(utent.ut_id, utent.ut_line + 3, sizeof(utent.ut_id));
- strncpy(utent.ut_host,ether_ntoa((const struct ether_addr *)conn->srcmac), sizeof(utent.ut_host));
+ strncpy(utent.ut_host, ether_ntoa((const struct ether_addr *)conn->srcmac), sizeof(utent.ut_host));
time((time_t *)&(utent.ut_time));
/* Update utmp and/or wtmp */
@@ -401,8 +405,9 @@ static void user_login(struct mt_connection *curconn, struct mt_mactelnet_hdr *p
curconn->outcounter += add_control_packet(&pdata, MT_CPTYPE_END_AUTH, NULL, 0);
send_udp(curconn, &pdata);
- if (curconn->state == STATE_ACTIVE)
+ if (curconn->state == STATE_ACTIVE) {
return;
+ }
}
if (user == NULL || memcmp(md5sum, trypassword, 17) != 0) {
@@ -725,12 +730,14 @@ static void daemonize() {
pid = fork();
/* Error? */
- if (pid < 0)
+ if (pid < 0) {
exit(1);
+ }
/* Parent exit */
- if (pid > 0)
+ if (pid > 0) {
exit(0);
+ }
setsid();
close(0);
@@ -758,13 +765,16 @@ void mndp_broadcast() {
int i;
unsigned int uptime;
- if (uname(&s_uname) != 0)
+ if (uname(&s_uname) != 0) {
return;
-
- if (sysinfo(&s_sysinfo) != 0)
+ }
+
+ if (sysinfo(&s_sysinfo) != 0) {
return;
-
+ }
+
uptime = s_sysinfo.uptime;
+
/* Seems like ping uptime is transmitted as little endian? */
#if BYTE_ORDER == BIG_ENDIAN
uptime = (
@@ -775,7 +785,6 @@ void mndp_broadcast() {
);
#endif
-
for (i = 0; i < sockets_count; ++i) {
struct mt_mndp_hdr *header = (struct mt_mndp_hdr *)&(pdata.data);
struct mt_socket *socket = &(sockets[i]);
@@ -947,8 +956,9 @@ int main (int argc, char **argv) {
for (p = connections_head; p != NULL; p = p->next) {
if (p->state == STATE_ACTIVE && p->wait_for_ack == 0 && p->ptsfd > 0) {
FD_SET(p->ptsfd, &read_fds);
- if (p->ptsfd > maxfd)
+ if (p->ptsfd > maxfd) {
maxfd = p->ptsfd;
+ }
}
}
diff --git a/protocol.c b/protocol.c
index 2de5833..cd1a040 100644
--- a/protocol.c
+++ b/protocol.c
@@ -195,8 +195,9 @@ int parse_control_packet(unsigned char *packetdata, int data_len, struct mt_mact
and then several times for each control packets. Letting this function
control the data position. */
if (packetdata != NULL) {
- if (data_len <= 0)
+ if (data_len <= 0) {
return 0;
+ }
int_data = packetdata;
int_data_len = data_len;
@@ -204,8 +205,9 @@ int parse_control_packet(unsigned char *packetdata, int data_len, struct mt_mact
}
/* No more data to parse? */
- if (int_pos >= int_data_len)
+ if (int_pos >= int_data_len) {
return 0;
+ }
/* Set current position in data buffer */
data = int_data + int_pos;
@@ -309,6 +311,11 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
p += 4;
+ /* Check if len is invalid */
+ if (p + len > data + packet_len) {
+ break;
+ }
+
switch (type) {
case MT_MNDPTYPE_ADDRESS:
if (len >= ETH_ALEN) {
diff --git a/udp.c b/udp.c
index 1cd977b..4029943 100644
--- a/udp.c
+++ b/udp.c
@@ -43,8 +43,8 @@ unsigned short udp_sum_calc(unsigned char *src_addr,unsigned char *dst_addr, uns
/* Padding ? */
padd = (len % 2);
- if (padd){
- data[len]=0;
+ if (padd) {
+ data[len] = 0;
}
/* header+data */