diff options
-rw-r--r-- | interfaces.c | 123 | ||||
-rw-r--r-- | mactelnetd.c | 32 | ||||
-rw-r--r-- | protocol.c | 22 |
3 files changed, 86 insertions, 91 deletions
diff --git a/interfaces.c b/interfaces.c index 0874f90..e701f3f 100644 --- a/interfaces.c +++ b/interfaces.c @@ -64,7 +64,7 @@ struct net_interface *net_get_interface_ptr(struct net_interface *interfaces, in for (i = 0; i < max_devices; ++i) { if (!interfaces[i].in_use) break; - + if (strncmp(interfaces[i].name, name, 254) == 0) { return interfaces + i; } @@ -110,7 +110,7 @@ static void net_update_mac(struct net_interface *interfaces, int max_devices) { static int get_device_index(char *device_name) { struct ifreq ifr; int tmpsock; - + tmpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); /* Find interface index from device_name */ @@ -126,7 +126,7 @@ static int get_device_index(char *device_name) { int net_get_interfaces(struct net_interface *interfaces, int max_devices) { static struct ifaddrs *int_addrs; - static const struct ifaddrs *ifaddrsp; + static const struct ifaddrs *ifaddrsp; const struct sockaddr_in *dl_addr; int found = 0; @@ -135,16 +135,16 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) { exit(1); } - for (ifaddrsp = int_addrs; ifaddrsp; ifaddrsp = ifaddrsp->ifa_next) { - dl_addr = (const struct sockaddr_in *) ifaddrsp->ifa_addr; + for (ifaddrsp = int_addrs; ifaddrsp; ifaddrsp = ifaddrsp->ifa_next) { + dl_addr = (const struct sockaddr_in *) ifaddrsp->ifa_addr; - if (ifaddrsp->ifa_addr == NULL) + if (ifaddrsp->ifa_addr == NULL) continue; - if (ifaddrsp->ifa_addr->sa_family == AF_INET) { - struct net_interface *interface = - net_get_interface_ptr(interfaces, max_devices, - ifaddrsp->ifa_name, 1); + if (ifaddrsp->ifa_addr->sa_family == AF_INET) { + struct net_interface *interface = + net_get_interface_ptr(interfaces, max_devices, + ifaddrsp->ifa_name, 1); if (interface != NULL) { found++; memcpy(interface->ipv4_addr, &dl_addr->sin_addr, IPV4_ALEN); @@ -156,15 +156,15 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) { #ifndef __linux__ { unsigned char emptymac[] = {0, 0, 0, 0, 0, 0}; - struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifaddrsp->ifa_addr; + struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifaddrsp->ifa_addr; if (sdl->sdl_alen == ETH_ALEN) { - struct net_interface *interface = - net_get_interface_ptr(interfaces, max_devices, - ifaddrsp->ifa_name, 1); + struct net_interface *interface = + net_get_interface_ptr(interfaces, max_devices, + ifaddrsp->ifa_name, 1); memcpy(interface->mac_addr, LLADDR(sdl), ETH_ALEN); - if (interface != NULL && - memcmp(interface->mac_addr, &emptymac, ETH_ALEN) != 0) { + if (interface != NULL && + memcmp(interface->mac_addr, &emptymac, ETH_ALEN) != 0) { interface->has_mac = 1; } } @@ -179,16 +179,16 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) { #if 0 { - int i; + int i; for (i = 0; i < max_devices; ++i) { if (interfaces[i].in_use) { - struct in_addr *addr = - (struct in_addr *)interfaces[i].ipv4_addr; + struct in_addr *addr = + (struct in_addr *)interfaces[i].ipv4_addr; printf("Interface %s:\n", interfaces[i].name); printf("\tIP: %s\n", inet_ntoa(*addr)); - printf("\tMAC: %s\n", - ether_ntoa((struct ether_addr *)interfaces[i].mac_addr)); + printf("\tMAC: %s\n", + ether_ntoa((struct ether_addr *)interfaces[i].mac_addr)); #ifdef __linux__ printf("\tIfIndex: %d\n", interfaces[i].ifindex); #endif @@ -206,17 +206,17 @@ unsigned short in_cksum(unsigned short *addr, int len) int sum = 0; unsigned short *w = addr; unsigned short answer = 0; - + while (nleft > 1) { sum += *w++; nleft -= 2; } - + if (nleft == 1) { *(unsigned char *) (&answer) = *(unsigned char *) w; sum += answer; } - + sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); answer = ~sum; @@ -297,21 +297,21 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c * Create a buffer for the full ethernet frame * and align header pointers to the correct positions. */ - void* buffer = (void*)malloc(ETH_FRAME_LEN); + static unsigned char stackbuf[ETH_FRAME_LEN]; + void* buffer = (void*)&stackbuf; #if defined(__FreeBSD__) - struct ether_header *eh = (struct ether_header *)buffer; - struct ip *ip = (struct ip *)(buffer + 14); + struct ether_header *eh = (struct ether_header *)buffer; + struct ip *ip = (struct ip *)(buffer + 14); #else struct ethhdr *eh = (struct ethhdr *)buffer; struct iphdr *ip = (struct iphdr *)(buffer + 14); #endif struct udphdr *udp = (struct udphdr *)(buffer + 14 + 20); - unsigned char *rest = - (unsigned char *)(buffer + 20 + 14 + sizeof(struct udphdr)); + unsigned char *rest = + (unsigned char *)(buffer + 20 + 14 + sizeof(struct udphdr)); if (((void *)rest - (void*)buffer) + datalen > ETH_FRAME_LEN) { fprintf(stderr, _("packet size too large\n")); - free(buffer); return 0; } @@ -326,9 +326,9 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c /* Init ethernet header */ #if defined(__FreeBSD__) - memcpy(eh->ether_shost, sourcemac, ETH_ALEN); - memcpy(eh->ether_dhost, destmac, ETH_ALEN); - eh->ether_type = htons(ETHERTYPE_IP); + memcpy(eh->ether_shost, sourcemac, ETH_ALEN); + memcpy(eh->ether_dhost, destmac, ETH_ALEN); + eh->ether_type = htons(ETHERTYPE_IP); #else memcpy(eh->h_source, sourcemac, ETH_ALEN); memcpy(eh->h_dest, destmac, ETH_ALEN); @@ -351,17 +351,17 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c /* Init IP Header */ #if defined(__FreeBSD__) - ip->ip_v = 4; - ip->ip_hl = 5; - ip->ip_tos = 0x10; - ip->ip_len = htons(datalen + 8 + 20); - ip->ip_id = htons(id++); - ip->ip_off = htons(0x4000); - ip->ip_ttl = 64; - ip->ip_p = 17; /* UDP */ - ip->ip_sum = 0; - ip->ip_src.s_addr = sourceip->s_addr; - ip->ip_dst.s_addr = destip->s_addr; + ip->ip_v = 4; + ip->ip_hl = 5; + ip->ip_tos = 0x10; + ip->ip_len = htons(datalen + 8 + 20); + ip->ip_id = htons(id++); + ip->ip_off = htons(0x4000); + ip->ip_ttl = 64; + ip->ip_p = 17; /* UDP */ + ip->ip_sum = 0; + ip->ip_src.s_addr = sourceip->s_addr; + ip->ip_dst.s_addr = destip->s_addr; #else ip->version = 4; ip->ihl = 5; @@ -378,17 +378,17 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c /* Calculate checksum for IP header */ #if defined(__FreeBSD__) - ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip)); + ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip)); #else ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr)); #endif /* Init UDP Header */ #if defined(__FreeBSD__) - udp->uh_sport = htons(sourceport); - udp->uh_dport = htons(destport); - udp->uh_ulen = htons(sizeof(struct udphdr) + datalen); - udp->uh_sum = 0; + udp->uh_sport = htons(sourceport); + udp->uh_dport = htons(destport); + udp->uh_ulen = htons(sizeof(struct udphdr) + datalen); + udp->uh_sum = 0; #else udp->source = htons(sourceport); udp->dest = htons(destport); @@ -401,23 +401,23 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c /* Add UDP checksum */ #if defined(__FreeBSD__) - udp->uh_sum = udp_sum_calc((unsigned char *)&(ip->ip_src.s_addr), - (unsigned char *)&(ip->ip_dst.s_addr), - (unsigned char *)udp, - sizeof(struct udphdr) + datalen); - udp->uh_sum = htons(udp->uh_sum); + udp->uh_sum = udp_sum_calc((unsigned char *)&(ip->ip_src.s_addr), + (unsigned char *)&(ip->ip_dst.s_addr), + (unsigned char *)udp, + sizeof(struct udphdr) + datalen); + udp->uh_sum = htons(udp->uh_sum); #else - udp->check = udp_sum_calc((unsigned char *)&(ip->saddr), - (unsigned char *)&(ip->daddr), - (unsigned char *)udp, - sizeof(struct udphdr) + datalen); + udp->check = udp_sum_calc((unsigned char *)&(ip->saddr), + (unsigned char *)&(ip->daddr), + (unsigned char *)udp, + sizeof(struct udphdr) + datalen); udp->check = htons(udp->check); #endif #ifdef __linux__ /* Send the packet */ - send_result = sendto(fd, buffer, datalen + 8 + 14 + 20, 0, - (struct sockaddr*)&socket_address, sizeof(socket_address)); + send_result = sendto(fd, buffer, datalen + 8 + 14 + 20, 0, + (struct sockaddr*)&socket_address, sizeof(socket_address)); if (send_result == -1) perror("sendto"); #else @@ -436,8 +436,7 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c if (send_result == -1) perror("bpf_write"); #endif - free(buffer); - + /* Return amount of _data_ bytes sent */ if (send_result - 8 - 14 - 20 < 0) { return 0; diff --git a/mactelnetd.c b/mactelnetd.c index 5bb1e9d..20ee760 100644 --- a/mactelnetd.c +++ b/mactelnetd.c @@ -322,19 +322,19 @@ static void display_nologin() { putchar(c); } fclose(fp); - } + } } static void uwtmp_login(struct mt_connection *conn) { #if defined(__FreeBSD__) - struct utmpx utent; + struct utmpx utent; #else struct utmp utent; #endif pid_t pid; pid = getpid(); - + char *line = ttyname(conn->slavefd); if (strncmp(line, "/dev/", 5) == 0) { line += 5; @@ -351,11 +351,11 @@ static void uwtmp_login(struct mt_connection *conn) { ether_ntoa((const struct ether_addr *)conn->srcmac), sizeof(utent.ut_host)); #if defined(__FreeBSD__) - gettimeofday(&utent.ut_tv, NULL); + gettimeofday(&utent.ut_tv, NULL); #else time((time_t *)&(utent.ut_time)); #endif - + /* Update utmp and/or wtmp */ #if defined(__FreeBSD__) setutxent(); @@ -398,8 +398,8 @@ static void uwtmp_logout(struct mt_connection *conn) { utent.ut_tv.tv_sec = time(NULL); #if defined(__FreeBSD__) - pututxline(&utent); - endutxent(); + pututxline(&utent); + endutxent(); #else pututline(&utent); endutent(); @@ -411,7 +411,7 @@ static void uwtmp_logout(struct mt_connection *conn) { static void abort_connection(struct mt_connection *curconn, struct mt_mactelnet_hdr *pkthdr, char *message) { struct mt_packet pdata; - + init_packet(&pdata, MT_PTYPE_DATA, pkthdr->dstaddr, pkthdr->srcaddr, pkthdr->seskey, curconn->outcounter); add_control_packet(&pdata, MT_CPTYPE_PLAINDATA, message, strlen(message)); send_udp(curconn, &pdata); @@ -534,14 +534,14 @@ static void user_login(struct mt_connection *curconn, struct mt_mactelnet_hdr *p if (!interfaces[i].in_use) { break; } - + } setsid(); /* Don't let shell process inherit slavefd */ fcntl (curconn->slavefd, F_SETFD, FD_CLOEXEC); close(curconn->ptsfd); - + /* Redirect STDIN/STDIO/STDERR */ close(0); dup(curconn->slavefd); @@ -627,7 +627,7 @@ static void handle_data_packet(struct mt_connection *curconn, struct mt_mactelne } else if (cpkt.cptype == MT_CPTYPE_TERM_WIDTH) { unsigned short width; - + memcpy(&width, cpkt.data, 2); curconn->terminal_width = le16toh(width); got_width_packet = 1; @@ -666,11 +666,11 @@ static void handle_data_packet(struct mt_connection *curconn, struct mt_mactelne /* Parse next control packet */ success = parse_control_packet(NULL, 0, &cpkt); } - + if (got_user_packet && got_pass_packet) { user_login(curconn, pkthdr); } - + if (curconn->state == STATE_ACTIVE && (got_width_packet || got_height_packet)) { set_terminal_size(curconn->ptsfd, curconn->terminal_width, curconn->terminal_height); @@ -809,7 +809,7 @@ static void daemonize() { close(0); close(1); close(2); - + fd = open("/dev/null",O_RDWR); dup(fd); dup(fd); @@ -1090,7 +1090,7 @@ int main (int argc, char **argv) { interface_count++; } } - + if (interface_count == 0) { syslog(LOG_ERR, _("Unable to find any valid network interfaces\n")); exit(1); @@ -1184,7 +1184,7 @@ int main (int argc, char **argv) { /* Handle select() timeout */ } time(&now); - + if (now - last_mndp_time > MT_MNDP_BROADCAST_INTERVAL) { pings = 0; mndp_broadcast(); @@ -239,7 +239,7 @@ int parse_control_packet(unsigned char *packetdata, int data_len, struct mt_mact /* Control packet data length */ memcpy(&(cpkthdr->length), data + 5, sizeof(cpkthdr->length)); cpkthdr->length = ntohl(cpkthdr->length); - + /* We want no buffer overflows */ if (cpkthdr->length >= MT_PACKET_LEN - 22 - int_pos) { cpkthdr->length = MT_PACKET_LEN - 1 - 22 - int_pos; @@ -274,9 +274,9 @@ int mndp_init_packet(struct mt_packet *packet, unsigned char version, unsigned c header->version = version; header->ttl = ttl; header->cksum = 0; - + packet->size = sizeof(*header); - + return sizeof(*header); } @@ -307,7 +307,8 @@ int mndp_add_attribute(struct mt_packet *packet, enum mt_mndp_attrtype attrtype, struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len) { const unsigned char *p; - static struct mt_mndp_info *packetp; + static struct mt_mndp_info packet; + struct mt_mndp_info *packetp = &packet; struct mt_mndp_hdr *mndp_hdr; /* Check for valid packet length */ @@ -315,11 +316,6 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len) return NULL; } - packetp = malloc(sizeof(*packetp)); - if (packetp == NULL) { - fprintf(stderr, "ERROR %s: malloc() failed\n", __func__); - return NULL; - } bzero(packetp, sizeof(*packetp)); mndp_hdr = (struct mt_mndp_hdr*)data; @@ -400,7 +396,7 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len) if (len > MT_MNDP_MAX_STRING_LENGTH) { len = MT_MNDP_MAX_STRING_LENGTH; } - + memcpy(packetp->softid, p, len); packetp->softid[len] = '\0'; break; @@ -409,10 +405,10 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len) Unhandled MNDP type */ } - + p += len; } - + return packetp; } @@ -476,7 +472,7 @@ int query_mndp(const char *identity, unsigned char *mac) { timeout.tv_sec = fastlookup ? MT_MNDP_TIMEOUT : MT_MNDP_LONGTIMEOUT; timeout.tv_usec = 0; - + select(sock + 1, &read_fds, NULL, NULL, &timeout); if (!FD_ISSET(sock, &read_fds)) { goto done; |