static void ssdp_send_notify(const char *nts) { int fd, i = 0; netif_t *ni; struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = 0; if((ni = net_get_interfaces()) == NULL) return; while(ni[i].ifname[0]) { if((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1) { sin.sin_addr.s_addr = htonl(ni[i].ipv4); if(bind(fd, (struct sockaddr *)&sin, sizeof(sin)) != -1) { ssdp_send_all(fd, ni[i].ipv4, NULL, nts); } close(fd); } i++; } free(ni); }
static JSBool js_sysipaddr(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { netif_t *ni = net_get_interfaces(); if(ni) { char buf[32]; uint32_t myaddr = ni[0].ipv4; free(ni); snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (uint8_t)(myaddr >> 24), (uint8_t)(myaddr >> 16), (uint8_t)(myaddr >> 8), (uint8_t)(myaddr)); *rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buf)); } else {
static int find_interface() { fd_set read_fds; struct mt_packet data; struct sockaddr_in myip; unsigned char emptymac[ETH_ALEN]; int i, testsocket; struct timeval timeout; int optval = 1; /* TODO: reread interfaces on HUP */ bzero(&interfaces, sizeof(struct net_interface) * MAX_INTERFACES); bzero(emptymac, ETH_ALEN); if (net_get_interfaces(interfaces, MAX_INTERFACES) <= 0) { fprintf(stderr, "Error: No suitable devices found\n"); exit(1); } for (i = 0; i < MAX_INTERFACES; ++i) { if (!interfaces[i].in_use) { break; } /* Skip loopback interfaces */ if (memcmp("lo", interfaces[i].name, 2) == 0) { continue; } /* Initialize receiving socket on the device chosen */ myip.sin_family = AF_INET; memcpy((void *)&myip.sin_addr, interfaces[i].ipv4_addr, IPV4_ALEN); myip.sin_port = htons(sourceport); /* Initialize socket and bind to udp port */ if ((testsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { continue; } setsockopt(testsocket, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)); setsockopt(testsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); if (bind(testsocket, (struct sockaddr *)&myip, sizeof(struct sockaddr_in)) == -1) { close(testsocket); continue; } /* Ensure that we have mac-address for this interface */ if (!interfaces[i].has_mac) { close(testsocket); continue; } /* Set the global socket handle and source mac address for send_udp() */ send_socket = testsocket; memcpy(srcmac, interfaces[i].mac_addr, ETH_ALEN); active_interface = &interfaces[i]; /* Send a SESSIONSTART message with the current device */ init_packet(&data, MT_PTYPE_SESSIONSTART, srcmac, dstmac, sessionkey, 0); send_udp(&data, 0); timeout.tv_sec = connect_timeout; timeout.tv_usec = 0; FD_ZERO(&read_fds); FD_SET(insockfd, &read_fds); select(insockfd + 1, &read_fds, NULL, NULL, &timeout); if (FD_ISSET(insockfd, &read_fds)) { /* We got a response, this is the correct device to use */ return 1; } close(testsocket); } return 0; }
/** * mc is set if packet arrived on our multicast listening socket */ static void ssdp_input(int fd, int mc) { char buf[2000]; int r, cmd, self; struct http_header_list args; uint32_t myaddr; const char *usn; struct sockaddr_in si; #if defined(IP_RECVDSTADDR) struct msghdr msg; struct cmsghdr *cmsg; struct iovec iov; char ctrl[500]; iov.iov_base = buf; iov.iov_len = sizeof(buf); msg.msg_name = (struct sockaddr *)&si; msg.msg_namelen = sizeof(struct sockaddr_in); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = ctrl; msg.msg_controllen = sizeof(ctrl); r = recvmsg(fd, &msg, 0); if(r < 1) return; buf[r] = 0; myaddr = 0; for(cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg,cmsg)) { if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) { struct in_addr *ia = (struct in_addr *)CMSG_DATA(cmsg); myaddr = ntohl(ia->s_addr); break; } } #else socklen_t slen = sizeof(struct sockaddr_in); netif_t *ni; r = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&si, &slen); if(r < 1) return; buf[r] = 0; ni = net_get_interfaces(); myaddr = ni ? ni[0].ipv4 : 0; free(ni); #endif if(!myaddr) return; LIST_INIT(&args); cmd = ssdp_parse(buf, &args); usn = http_header_get(&args, "usn"); self = usn != NULL && !strncmp(usn, "uuid:", 5) && !strncmp(usn + 5, ssdp_uuid, strlen(ssdp_uuid)); if(!self) { if(cmd == SSDP_NOTIFY && mc) ssdp_recv_notify(&args); if(cmd == SSDP_RESPONSE && !mc) ssdp_response(&args); if(cmd == SSDP_SEARCH && mc) ssdp_send_all(ssdp_fdu, myaddr, &si, NULL); } http_headers_free(&args); }
int main(int argc, char **argv) { int optval = 1; int print_help = 0; int send_packets = 5; int fastmode = 0; int c; struct sockaddr_in si_me; struct mt_packet packet; int i; while (1) { c = getopt(argc, argv, "fs:c:hv?"); if (c == -1) { break; } switch (c) { case 'f': fastmode = 1; break; case 's': ping_size = atoi(optarg) - 18; break; case 'v': print_version(); exit(0); break; case 'c': send_packets = atoi(optarg); break; case 'h': case '?': print_help = 1; break; } } /* We don't want people to use this for the wrong reasons */ if (fastmode && (send_packets == 0 || send_packets > 100)) { fprintf(stderr, "Number of packets to send must be more than 0 and less than 100 in fast mode.\n"); return 1; } if (argc - optind < 1 || print_help) { print_version(); fprintf(stderr, "Usage: %s <MAC> [-h] [-f] [-c <count>] [-s <packet size>]\n", argv[0]); if (print_help) { fprintf(stderr, "\nParameters:\n"); fprintf(stderr, " MAC MAC-Address of the RouterOS/mactelnetd device.\n"); fprintf(stderr, " -f Fast mode, do not wait before sending next ping request.\n"); fprintf(stderr, " -s Specify size of ping packet.\n"); fprintf(stderr, " -c Number of packets to send. (0 = unlimited)\n"); fprintf(stderr, " -h This help.\n"); fprintf(stderr, "\n"); } return 1; } if (ping_size > ETH_FRAME_LEN - 42) { fprintf(stderr, "Packet size must be between 18 and %d\n", ETH_FRAME_LEN - 42 + 18); exit(1); } /* Mikrotik RouterOS does not answer unless the packet has the correct recipient mac-address in * the ethernet frame. Unlike real MacTelnet connections where the OS is ok with it being a * broadcast mac address. */ if (geteuid() != 0) { fprintf(stderr, "You need to have root privileges to use %s.\n", argv[0]); return 1; } /* Get mac-address from string, or check for hostname via mndp */ if (!query_mndp_or_mac(argv[optind], dstmac, 1)) { /* No valid mac address found, abort */ return 1; } sockfd = net_init_raw_socket(); insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (insockfd < 0) { perror("insockfd"); return 1; } /* Set initialize address/port */ memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(MT_MACTELNET_PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); setsockopt(insockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval)); /* Bind to specified address/port */ if (bind(insockfd, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { fprintf(stderr, "Error binding to %s:%d\n", inet_ntoa(si_me.sin_addr), MT_MNDP_PORT); return 1; } /* Listen address*/ inet_pton(AF_INET, (char *)"0.0.0.0", &sourceip); /* Set up global info about the connection */ inet_pton(AF_INET, (char *)"255.255.255.255", &destip); srand(time(NULL)); /* Enumerate available interfaces */ net_get_interfaces(interfaces, MAX_INTERFACES); if (ping_size < sizeof(struct timeval)) { ping_size = sizeof(struct timeval); } signal(SIGINT, display_results); for (i = 0; i < send_packets || send_packets == 0; ++i) { fd_set read_fds; static struct timeval lasttimestamp; int reads, result; struct timeval timeout; int ii; int sent = 0; int waitforpacket; struct timeval timestamp; unsigned char pingdata[1500]; gettimeofday(×tamp, NULL); memcpy(pingdata, ×tamp, sizeof(timestamp)); for (ii = sizeof(timestamp); ii < ping_size; ++ii) { pingdata[ii] = rand() % 256; } for (ii = 0; ii < MAX_INTERFACES; ++ii) { struct net_interface *interface = &interfaces[ii]; if (!interface->in_use) { break; } if (!interface->has_mac) { continue; } init_pingpacket(&packet, interface->mac_addr, dstmac); add_packetdata(&packet, pingdata, ping_size); result = net_send_udp(sockfd, interface, interface->mac_addr, dstmac, &sourceip, MT_MACTELNET_PORT, &destip, MT_MACTELNET_PORT, packet.data, packet.size); if (result > 0) { sent++; } } if (sent == 0) { fprintf(stderr, "Error sending packet.\n"); continue; } ping_sent++; FD_ZERO(&read_fds); FD_SET(insockfd, &read_fds); timeout.tv_sec = 1; timeout.tv_usec = 0; waitforpacket = 1; while (waitforpacket) { /* Wait for data or timeout */ reads = select(insockfd+1, &read_fds, NULL, NULL, &timeout); 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); } } } /* Display statistics and exit */ display_results(); return 0; }