int ecx_portt::setupnicSecondary(const char *ifname) { int rval = 0; /* secondary port struct available? */ if (this->redport) { /* when using secondary socket it is automatically a redundant setup */ pcap_t **psock = &(this->redport->sockhandle); *psock = NULL; this->redstate = ECT_RED_DOUBLE; this->redport->stack.sock = &(this->redport->sockhandle); this->redport->stack.txbuf = &(this->txbuf); this->redport->stack.txbuflength = &(this->txbuflength); this->redport->stack.tempbuf = &(this->redport->tempinbuf); this->redport->stack.rxbuf = &(this->redport->rxbuf); this->redport->stack.rxbufstat = &(this->redport->rxbufstat); this->redport->stack.rxsa = &(this->redport->rxsa); ecx_clear_rxbufstat(&(this->redport->rxbufstat[0])); rval = setupnic(ifname, psock); } else { /* fail */ } return rval; }
int ecx_portt::setupnicPrimary(const char *ifname) { InitializeCriticalSection(&(this->getindex_mutex)); InitializeCriticalSection(&(this->tx_mutex)); InitializeCriticalSection(&(this->rx_mutex)); this->sockhandle = NULL; this->lastidx = 0; this->redstate = ECT_RED_NONE; this->stack.sock = &(this->sockhandle); this->stack.txbuf = &(this->txbuf); this->stack.txbuflength = &(this->txbuflength); this->stack.tempbuf = &(this->tempinbuf); this->stack.rxbuf = &(this->rxbuf); this->stack.rxbufstat = &(this->rxbufstat); this->stack.rxsa = &(this->rxsa); ecx_clear_rxbufstat(&(this->rxbufstat[0])); return setupnic(ifname, &(this->sockhandle)); }
/** Basic setup to connect NIC to socket. * @param[in] port = port context struct * @param[in] ifname = Name of NIC device, f.e. "eth0" * @param[in] secondary = if >0 then use secondary stack instead of primary * @return >0 if succeeded */ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary) { int i; int rVal; int *psock; port->getindex_mutex = mtx_create(); port->tx_mutex = mtx_create(); port->rx_mutex = mtx_create(); rVal = bfin_EMAC_init((uint8_t *)priMAC); if (rVal != 0) return 0; if (secondary) { /* secondary port struct available? */ if (port->redport) { /* when using secondary socket it is automatically a redundant setup */ psock = &(port->redport->sockhandle); *psock = -1; port->redstate = ECT_RED_DOUBLE; port->redport->stack.sock = &(port->redport->sockhandle); port->redport->stack.txbuf = &(port->txbuf); port->redport->stack.txbuflength = &(port->txbuflength); port->redport->stack.tempbuf = &(port->redport->tempinbuf); port->redport->stack.rxbuf = &(port->redport->rxbuf); port->redport->stack.rxbufstat = &(port->redport->rxbufstat); port->redport->stack.rxsa = &(port->redport->rxsa); ecx_clear_rxbufstat(&(port->redport->rxbufstat[0])); } else { /* fail */ return 0; } } else { port->getindex_mutex = mtx_create(); port->tx_mutex = mtx_create(); port->rx_mutex = mtx_create(); port->sockhandle = -1; port->lastidx = 0; port->redstate = ECT_RED_NONE; port->stack.sock = &(port->sockhandle); port->stack.txbuf = &(port->txbuf); port->stack.txbuflength = &(port->txbuflength); port->stack.tempbuf = &(port->tempinbuf); port->stack.rxbuf = &(port->rxbuf); port->stack.rxbufstat = &(port->rxbufstat); port->stack.rxsa = &(port->rxsa); ecx_clear_rxbufstat(&(port->rxbufstat[0])); psock = &(port->sockhandle); } /* setup ethernet headers in tx buffers so we don't have to repeat it */ for (i = 0; i < EC_MAXBUF; i++) { ec_setupheader(&(port->txbuf[i])); port->rxbufstat[i] = EC_BUF_EMPTY; } ec_setupheader(&(port->txbuf2)); return 1; }
/** Basic setup to connect NIC to socket. * @param[in] port = port context struct * @param[in] ifname = Name of NIC device, f.e. "eth0" * @param[in] secondary = if >0 then use secondary stack instead of primary * @return >0 if succeeded */ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary) { int i; int r, rval, ifindex; struct timeval timeout; struct ifreq ifr; struct sockaddr_ll sll; int *psock; pthread_mutexattr_t mutexattr; rval = 0; if (secondary) { /* secondary port struct available? */ if (port->redport) { /* when using secondary socket it is automatically a redundant setup */ psock = &(port->redport->sockhandle); *psock = -1; port->redstate = ECT_RED_DOUBLE; port->redport->stack.sock = &(port->redport->sockhandle); port->redport->stack.txbuf = &(port->txbuf); port->redport->stack.txbuflength = &(port->txbuflength); port->redport->stack.tempbuf = &(port->redport->tempinbuf); port->redport->stack.rxbuf = &(port->redport->rxbuf); port->redport->stack.rxbufstat = &(port->redport->rxbufstat); port->redport->stack.rxsa = &(port->redport->rxsa); ecx_clear_rxbufstat(&(port->redport->rxbufstat[0])); } else { /* fail */ return 0; } } else { pthread_mutexattr_init(&mutexattr); pthread_mutexattr_setprotocol(&mutexattr , PTHREAD_PRIO_INHERIT); pthread_mutex_init(&(port->getindex_mutex), &mutexattr); pthread_mutex_init(&(port->tx_mutex) , &mutexattr); pthread_mutex_init(&(port->rx_mutex) , &mutexattr); port->sockhandle = -1; port->lastidx = 0; port->redstate = ECT_RED_NONE; port->stack.sock = &(port->sockhandle); port->stack.txbuf = &(port->txbuf); port->stack.txbuflength = &(port->txbuflength); port->stack.tempbuf = &(port->tempinbuf); port->stack.rxbuf = &(port->rxbuf); port->stack.rxbufstat = &(port->rxbufstat); port->stack.rxsa = &(port->rxsa); ecx_clear_rxbufstat(&(port->rxbufstat[0])); psock = &(port->sockhandle); } /* we use RAW packet socket, with packet type ETH_P_ECAT */ *psock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ECAT)); timeout.tv_sec = 0; timeout.tv_usec = 1; r = setsockopt(*psock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); r = setsockopt(*psock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); i = 1; r = setsockopt(*psock, SOL_SOCKET, SO_DONTROUTE, &i, sizeof(i)); /* connect socket to NIC by name */ strcpy(ifr.ifr_name, ifname); r = ioctl(*psock, SIOCGIFINDEX, &ifr); ifindex = ifr.ifr_ifindex; strcpy(ifr.ifr_name, ifname); ifr.ifr_flags = 0; /* reset flags of NIC interface */ r = ioctl(*psock, SIOCGIFFLAGS, &ifr); /* set flags of NIC interface, here promiscuous and broadcast */ ifr.ifr_flags = ifr.ifr_flags | IFF_PROMISC | IFF_BROADCAST; r = ioctl(*psock, SIOCSIFFLAGS, &ifr); /* bind socket to protocol, in this case RAW EtherCAT */ sll.sll_family = AF_PACKET; sll.sll_ifindex = ifindex; sll.sll_protocol = htons(ETH_P_ECAT); r = bind(*psock, (struct sockaddr *)&sll, sizeof(sll)); /* setup ethernet headers in tx buffers so we don't have to repeat it */ for (i = 0; i < EC_MAXBUF; i++) { ec_setupheader(&(port->txbuf[i])); port->rxbufstat[i] = EC_BUF_EMPTY; } ec_setupheader(&(port->txbuf2)); if (r == 0) rval = 1; return rval; }