diff --git a/.travis/check-posix-app-pty b/.travis/check-posix-app-pty index 228954733..747ae314e 100755 --- a/.travis/check-posix-app-pty +++ b/.travis/check-posix-app-pty @@ -80,6 +80,15 @@ check() { OT_CLI_CMD="$(pwd)/$(ls output/posix/*linux*/bin/ot-cli) ${OT_NCP_PATH} ${CORE_PTY}" fi + # Cover setting a valid network interface name. + readonly VALID_NETIF_NAME="wan$(date +%H%M%S)" + sudo ${OT_CLI_CMD} -I "${VALID_NETIF_NAME}" -n + + # Cover setting a too long(max is 15 characters) network interface name. + # Expect exit code to be 2(OT_EXIT_INVALID_ARGUMENTS). + readonly INVALID_NETIF_NAME="wan0123456789123" + sudo ${OT_CLI_CMD} -I "${INVALID_NETIF_NAME}" -n || test $? = 2 + if [[ "${DAEMON}" = 1 ]]; then sudo ${OT_CLI_CMD} panid 0xface | grep 'Done' || die 'failed to set panid with ot-ctl' fi diff --git a/src/posix/main.c b/src/posix/main.c index eaa955bd2..284bc51a1 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -180,7 +180,7 @@ static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode) static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig) { - memset(aConfig, 0, sizeof(PosixConfig)); + memset(aConfig, 0, sizeof(*aConfig)); aConfig->mPlatformConfig.mSpeedUpFactor = 1; aConfig->mPlatformConfig.mResetRadio = true; diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index b2b9d2507..d88d12bad 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -384,7 +384,7 @@ exit: return; } -void platformNetifInit(otInstance *aInstance) +void platformNetifInit(otInstance *aInstance, const char *aInterfaceName) { struct ifreq ifr; @@ -410,7 +410,17 @@ void platformNetifInit(otInstance *aInstance) memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - strncpy(ifr.ifr_name, "wpan%d", IFNAMSIZ); + + if (aInterfaceName) + { + VerifyOrDie(strlen(aInterfaceName) < IFNAMSIZ, OT_EXIT_INVALID_ARGUMENTS); + + strncpy(ifr.ifr_name, aInterfaceName, IFNAMSIZ); + } + else + { + strncpy(ifr.ifr_name, "wpan%d", IFNAMSIZ); + } VerifyOrExit(ioctl(sTunFd, TUNSETIFF, static_cast(&ifr)) == 0, otLogCritPlat("Unable to configure tun device %s", OPENTHREAD_POSIX_TUN_DEVICE)); diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index d61040dae..aed45eef2 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -276,9 +276,10 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co * This function initializes platform netif. * * @param[in] aInstance A pointer to the OpenThread instance. + * @param[in] aInterfaceName A pointer to Thread network interface name. * */ -void platformNetifInit(otInstance *aInstance); +void platformNetifInit(otInstance *aInstance, const char *aInterfaceName); /** * This function updates the file descriptor sets with file descriptors used by platform netif module. diff --git a/src/posix/platform/system.cpp b/src/posix/platform/system.cpp index eeaec5835..8d85d7cc4 100644 --- a/src/posix/platform/system.cpp +++ b/src/posix/platform/system.cpp @@ -59,7 +59,7 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig) assert(instance != NULL); #if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE - platformNetifInit(instance); + platformNetifInit(instance, aPlatformConfig->mInterfaceName); #elif OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE platformUdpInit(aPlatformConfig->mInterfaceName); #endif