示例#1
0
文件: win.c 项目: hermixy/qtun
size_t enum_devices(enum_device_t devices[MAX_DEVICE_COUNT])
{
    ULONG len = 0;
    IP_ADAPTER_INFO* adapters = NULL;
    IP_INTERFACE_INFO* interfaces = NULL;
    HANDLE handle = INVALID_HANDLE_VALUE;
    size_t idx = 0;

    if (GetAdaptersInfo(NULL, &len) == ERROR_NO_DATA) goto failed;

    adapters = malloc(sizeof(IP_ADAPTER_INFO) * len);
    if (adapters == NULL) goto failed;

    if (GetAdaptersInfo(adapters, &len) == ERROR_SUCCESS)
    {
        IP_ADAPTER_INFO* adp = adapters;
        while (adp)
        {
            if (adp->Type == MIB_IF_TYPE_ETHERNET)
            {
                if (strncmp(adp->Description, "QTun", sizeof("QTun") - 1) == 0)
                {
                    devices[idx].index = adp->Index;
                    strcpy(devices[idx].dev_path, "\\\\.\\");
                    strcat(devices[idx].dev_path, adp->AdapterName);
                    strcat(devices[idx].dev_path, ".tun");
                    ++idx;
                }
            }
            adp = adp->Next;
        }
    }

    MprConfigServerConnect(NULL, &handle);

    len = 0;
    if (GetInterfaceInfo(NULL, &len) == ERROR_NO_DATA) goto failed;

    interfaces = malloc(sizeof(IP_INTERFACE_INFO) * len);
    if (interfaces == NULL) goto failed;

    if (GetInterfaceInfo(interfaces, &len) == ERROR_SUCCESS)
    {
        LONG i;
        size_t j;
        for (i = 0; i < interfaces->NumAdapters; ++i)
        {
            for (j = 0; j < idx; ++j)
            {
                if (devices[j].index == interfaces->Adapter[i].Index)
                {
                    WCHAR w_name[128] = { 0 };
                    int name_len = 0;
                    MprConfigGetFriendlyName(handle, interfaces->Adapter[i].Name, w_name, sizeof(w_name));
                    name_len = WideCharToMultiByte(CP_ACP, 0, w_name, -1, NULL, 0, NULL, FALSE);
                    WideCharToMultiByte(CP_ACP, 0, w_name, -1, devices[j].dev_name, name_len, NULL, FALSE);
                    break;
                }
            }
        }
    }

    if (adapters) free(adapters);
    if (interfaces) free(interfaces);
    return idx;
failed:
    if (adapters) free(adapters);
    if (interfaces) free(interfaces);
    return 0;
}
示例#2
0
DWORD
CE_Initialize (
    PCONFIGURATION_ENTRY    pce,
    HANDLE                  hMgrNotificationEvent,
    PSUPPORT_FUNCTIONS      psfSupportFunctions,
    PXORPRTM_GLOBAL_CONFIG pigc)
{
    DWORD   dwErr               = NO_ERROR;
    pipe_instance_t *pp;
    int i, pipefail;

    do {
        pce->ulActivityCount    = 0;

	pce->hMprConfig = NULL;
	dwErr = MprConfigServerConnect(NULL, &pce->hMprConfig);
	if (dwErr != NO_ERROR) {
            TRACE0(CONFIGURATION, "could not obtain mpr config handle");
	}

        /* Router Manager Information */
        pce->hMgrNotificationEvent   = hMgrNotificationEvent;
        if (psfSupportFunctions)
            pce->sfSupportFunctions      = *psfSupportFunctions;

	pipefail = 0;
	for (i = 0; i < PIPE_INSTANCES; i++) {
	    pp = pipe_new();
	    if (pp == NULL) {
		pipefail = 1;
		break;
	    } else {
		pipe_listen(pp);
		pce->pipes[i] = pp;
	    }
	}

	if (pipefail) {
            TRACE0(CONFIGURATION, "failed to allocate all pipes");
	    break;
	}
	TRACE0(ANY, "Listening on pipes ok.");

        pce->reiRtmEntity.RtmInstanceId = 0;
#ifdef IPV6_DLL
        pce->reiRtmEntity.AddressFamily = AF_INET6;
#else
        pce->reiRtmEntity.AddressFamily = AF_INET;
#endif
        pce->reiRtmEntity.EntityId.EntityProtocolId = PROTO_IP_XORPRTM;
        pce->reiRtmEntity.EntityId.EntityInstanceId = 0;

        dwErr = RtmRegisterEntity(
            &pce->reiRtmEntity,
            NULL,
            RTM_CallbackEvent,
            TRUE,
            &pce->rrpRtmProfile,
            &pce->hRtmHandle);
        if (dwErr != NO_ERROR) {
            TRACE1(CONFIGURATION, "Error %u registering with RTM", dwErr);
            break;
        }
	TRACE0(ANY, "registered entity ok.");

        dwErr = RtmRegisterForChangeNotification(
            pce->hRtmHandle,
            RTM_VIEW_MASK_UCAST,
            RTM_CHANGE_TYPE_ALL,
            NULL,
            &pce->hRtmNotificationHandle);
        if (dwErr != NO_ERROR) {
            TRACE1(CONFIGURATION,
                   "Error %u registering for change with RTM", dwErr);
            break;
        }
	TRACE0(ANY, "registered rtm changes ok.");

        pce->iscStatus = XORPRTM_STATUS_RUNNING;
    } while (FALSE);

    if (dwErr != NO_ERROR) {
	TRACE0(ANY, "init failed, cleaning up.");
        CE_Cleanup(pce);
    } else {
	TRACE0(ANY, "Leaving init ok ");
    }

    return dwErr;
}