//#--------------------------------------------------------------------------------------------#// //# hm2_test: // .c: int rtapi_app_main(void) { hm2_test_t *me; hm2_lowlevel_io_t *this; int r = 0; LL_PRINT("loading HostMot2 test driver with test pattern %d\n", test_pattern); comp_id = hal_init(HM2_LLIO_NAME); }
static bool use_iptables() { if(iptables_state == -1) { if(geteuid() != 0) return (iptables_state = 0); if(!chain_exists()) { int res = shell("/sbin/iptables -N " CHAIN); if(res != EXIT_SUCCESS) { LL_PRINT("ERROR: Failed to create iptables chain "CHAIN); return (iptables_state = 0); } } // now add a jump to our chain at the start of the OUTPUT chain if it isn't in the chain already int res = shell("/sbin/iptables -C OUTPUT -j " CHAIN " || /sbin/iptables -I OUTPUT 1 -j " CHAIN); if(res != EXIT_SUCCESS) { LL_PRINT("ERROR: Failed to insert rule in OUTPUT chain"); return (iptables_state = 0); } return (iptables_state = 1); } return iptables_state; }
static int install_iptables_rule(const char *fmt, ...) { char commandbuf[1024], *ptr = commandbuf, *ebuf = commandbuf + sizeof(commandbuf); ptr = seprintf(ptr, ebuf, IPTABLES" -A "CHAIN" "); va_list ap; va_start(ap, fmt); ptr = vseprintf(ptr, ebuf, fmt, ap); va_end(ap); if(ptr == ebuf) { LL_PRINT("ERROR: commandbuf too small\n"); return -ENOSPC; } int res = shell(commandbuf); if(res == EXIT_SUCCESS) return 0; LL_PRINT("ERROR: Failed to execute '%s'\n", commandbuf); return -EINVAL; }
static int close_net(void) { if(use_iptables()) clear_iptables(); if(req.arp_flags & ATF_PERM) { int ret = ioctl(sockfd, SIOCDARP, &req); if(ret < 0) perror("ioctl SIOCDARP"); } int ret = shutdown(sockfd, SHUT_RDWR); if (ret < 0) LL_PRINT("ERROR: can't close socket: %s\n", strerror(errno)); return ret < 0 ? -errno : 0; }
static int eshellf(char *fmt, ...) { char commandbuf[1024]; va_list ap; va_start(ap, fmt); vsnprintf(commandbuf, sizeof(commandbuf), fmt, ap); va_end(ap); int res = shell(commandbuf); if(res == EXIT_SUCCESS) return 0; LL_PRINT("ERROR: Failed to execute '%s'\n", commandbuf); return -EINVAL; }
static int fetch_hwaddr(int sockfd, unsigned char buf[6]) { lbp16_cmd_addr packet; unsigned char response[6]; LBP16_INIT_PACKET4(packet, 0x4983, 0x0002); int res = eth_socket_send(sockfd, &packet, sizeof(packet), 0); if(res < 0) return -errno; int i=0; do { res = eth_socket_recv(sockfd, &response, sizeof(response), 0); } while(++i < 10 && res < 0 && errno == EAGAIN); if(res < 0) return -errno; // eeprom order is backwards from arp AF_LOCAL order for(i=0; i<6; i++) buf[i] = response[5-i]; LL_PRINT("Hardware address: %02x:%02x:%02x:%02x:%02x:%02x\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); return 0; }
static int init_net(void) { int ret; sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); if (sockfd < 0) { LL_PRINT("ERROR: can't open socket: %s\n", strerror(errno)); return -errno; } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(LBP16_UDP_PORT); server_addr.sin_addr.s_addr = inet_addr(board_ip); local_addr.sin_family = AF_INET; local_addr.sin_addr.s_addr = INADDR_ANY; ret = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in)); if (ret < 0) { LL_PRINT("ERROR: can't connect: %s\n", strerror(errno)); return -errno; } if(use_iptables()) { LL_PRINT("Using iptables for exclusive access to network interface\n") // firewall has to be open in order to successfully arp the board clear_iptables(); } else { LL_PRINT(\ "WARNING: Unable to restrict other access to the hm2-eth device.\n" "This means that other software using the same network interface can violate\n" "realtime guarantees. See hm2_eth(9) for more information.\n"); } struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = RECV_TIMEOUT_US; ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)); if (ret < 0) { LL_PRINT("ERROR: can't set socket option: %s\n", strerror(errno)); return -errno; } timeout.tv_usec = SEND_TIMEOUT_US; setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)); if (ret < 0) { LL_PRINT("ERROR: can't set socket option: %s\n", strerror(errno)); return -errno; } memset(&req, 0, sizeof(req)); struct sockaddr_in *sin; sin = (struct sockaddr_in *) &req.arp_pa; sin->sin_family = AF_INET; sin->sin_addr.s_addr = inet_addr(board_ip); req.arp_ha.sa_family = AF_LOCAL; req.arp_flags = ATF_PERM | ATF_COM; ret = fetch_hwaddr( sockfd, (void*)&req.arp_ha.sa_data ); if(ret < 0) { LL_PRINT("ERROR: Could not retrieve mac address\n"); return ret; } ret = ioctl(sockfd, SIOCSARP, &req); if(ret < 0) { perror("ioctl SIOCSARP"); req.arp_flags &= ~ATF_PERM; return -errno; } if(use_iptables()) { ret = install_iptables(sockfd); if(ret < 0) return ret; } return 0; }
int rtapi_app_main(void) { int r = 0; LL_PRINT("loading Mesa AnyIO HostMot2 driver version " HM2_PCI_VERSION "\n"); comp_id = hal_init(HM2_LLIO_NAME); }
//#--------------------------------------------------------------------------------------------#// //# hm2_eth: // .c int rtapi_app_main(void) { int ret; LL_PRINT("loading Mesa AnyIO HostMot2 ethernet driver version " HM2_ETH_VERSION "\n"); ret = hal_init(HM2_LLIO_NAME); }