aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/hwaddressretriever/HardwareAddressRetriever_win.c
blob: bcd83bebd31f41bb7e794a21e1bd922fe349ad0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

/**
 * \file HardwareAddressRetriever_win.c
 * \brief Hardware address retriever (Windows specific code).
 * \author Sebastien Vincent
 * \date 2010
 */

#if defined(_WIN32) || defined(_WIN64)

/**
 * \def WIN32_LEAN_AND_MEAN
 * \brief Exclude not commonly used headers from win32 API.
 *
 * It excludes some unused stuff from windows headers and
 * by the way code compiles faster.
 */
#define WIN32_LEAN_AND_MEAN

#include <stdlib.h>
#include <windows.h>

#include <ws2tcpip.h>
#include <iphlpapi.h>

#include "HardwareAddressRetriever.h"

jbyteArray getHardwareAddress(JNIEnv* env, jstring ifName)
{
    MIB_IFTABLE* ifTable = NULL;
    ULONG size = 15000;
    int found = 0;
    jbyteArray hwaddr = NULL;
    DWORD ret = 0;
    DWORD i = 0;
    WCHAR* wname = NULL;
    MIB_IFROW ifi;
    jclass clazz = (*env)->GetObjectClass(env, ifName);
    jmethodID method = (*env)->GetMethodID(env, clazz, "compareTo", "(Ljava/lang/String;)I");

    if(method == NULL)
    {
        return NULL;
    }

    memset(&ifi, 0x00, sizeof(MIB_IFROW));

    do
    {
        ifTable = malloc(size);

        if(!ifTable)
        {
            /* out of memory */
            return NULL;
        }

        ret = GetIfTable(ifTable, &size, 1);

    }while(ret == ERROR_INSUFFICIENT_BUFFER);

    if(ret != ERROR_SUCCESS)
    {
        free(ifTable);
        return NULL;
    }

    for(i = 0 ; i < ifTable->dwNumEntries ; i++)
    {
        jstring tmp = NULL;
        ifi = ifTable->table[i];

        if(ifi.dwType == IF_TYPE_OTHER)
        {
            continue;
        }

        /* jstring created by NewStringUTF will be garbage collected at 
         * the end of the function 
         */
        tmp = (*env)->NewStringUTF(env, ifi.bDescr);

        if(!tmp)
        {
            /* printf("error\n"); */
            continue;
        }

        if((*env)->CallIntMethod(env, ifName, method, tmp) == 0)
        {
            found = 1;
            break;
        }
    }

    if(found)
    {
        DWORD hwlen = ifi.dwPhysAddrLen;

        if(hwlen > 0)
        {
            hwaddr = (*env)->NewByteArray(env, hwlen);

            if(hwaddr)
            {
                /* copy the hardware address and return it */
                (*env)->SetByteArrayRegion(env, hwaddr, 0, hwlen, ifi.bPhysAddr);
            }
        }
    }

    /* cleanup */
    free(ifTable);

    return hwaddr;
}

#endif