summaryrefslogtreecommitdiff
path: root/M4-RCs/qpid/cpp/src/qpid/sys/windows/SystemInfo.cpp
blob: b887cac58bbf2a03d0b0668ea5e572ad79b72dc8 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

/* GetNativeSystemInfo call requires _WIN32_WINNT 0x0501 or higher */
#ifndef _WIN32_WINNT
#  define _WIN32_WINNT 0x0501
#endif

#include "qpid/sys/IntegerTypes.h"
#include "qpid/sys/SystemInfo.h"

#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>

#ifndef HOST_NAME_MAX
#  define HOST_NAME_MAX 256
#endif

namespace qpid {
namespace sys {

long  SystemInfo::concurrency() {
    SYSTEM_INFO sys_info;
    ::GetSystemInfo (&sys_info);
    long activeProcessors = 0;
    DWORD_PTR mask = sys_info.dwActiveProcessorMask;
    while (mask != 0) {
        if (mask & 1)
            ++activeProcessors;
        mask >>= 1;
    }
    return activeProcessors;
}

bool SystemInfo::getLocalHostname (TcpAddress &address) {
    char name[HOST_NAME_MAX];
    if (::gethostname(name, sizeof(name)) != 0) {
        errno = WSAGetLastError();
        return false;
    }
    address.host = name;
    return true;
}

void SystemInfo::getLocalIpAddresses (uint16_t port,
                                      std::vector<Address> &addrList) {
    enum { MAX_URL_INTERFACES = 100 };
    static const std::string LOCALHOST("127.0.0.1");

    SOCKET s = socket (PF_INET, SOCK_STREAM, 0);
    if (s != INVALID_SOCKET) {
        INTERFACE_INFO interfaces[MAX_URL_INTERFACES];
        DWORD filledBytes = 0;
        WSAIoctl (s,
                  SIO_GET_INTERFACE_LIST,
                  0,
                  0,
                  interfaces,
                  sizeof (interfaces),
                  &filledBytes,
                  0,
                  0);
        unsigned int interfaceCount = filledBytes / sizeof (INTERFACE_INFO);
        for (unsigned int i = 0; i < interfaceCount; ++i) {
            if (interfaces[i].iiFlags & IFF_UP) {
                std::string addr(inet_ntoa(interfaces[i].iiAddress.AddressIn.sin_addr));
                if (addr != LOCALHOST)
                    addrList.push_back(TcpAddress(addr, port));
            }
        }
        closesocket (s);
    }
}

void SystemInfo::getSystemId (std::string &osName,
                              std::string &nodeName,
                              std::string &release,
                              std::string &version,
                              std::string &machine)
{
    osName = "Microsoft Windows";

    char node[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD nodelen = MAX_COMPUTERNAME_LENGTH + 1;
    GetComputerName (node, &nodelen);
    nodeName = node;

    OSVERSIONINFOEX vinfo;
    vinfo.dwOSVersionInfoSize = sizeof(vinfo);
    GetVersionEx ((OSVERSIONINFO *)&vinfo);

    SYSTEM_INFO sinfo;
    GetNativeSystemInfo(&sinfo);

    switch(vinfo.dwMajorVersion) {
    case 5:
        switch(vinfo.dwMinorVersion) {
        case 0:
            release  ="2000";
            break;
        case 1:
            release = "XP";
            break;
        case 2:
            if (sinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
                sinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
                release = "XP-64";
            else
                release = "Server 2003";
            break;
        default:
            release = "Windows";
        }
        break;
    case 6:
        if (vinfo.wProductType == VER_NT_SERVER)
            release = "Server 2008";
        else
            release = "Vista";
        break;
    default:
        release = "Microsoft Windows";
    }
    version = vinfo.szCSDVersion;

    switch(sinfo.wProcessorArchitecture) {
    case PROCESSOR_ARCHITECTURE_AMD64:
        machine = "x86-64";
        break;
    case PROCESSOR_ARCHITECTURE_IA64:
        machine = "IA64";
        break;
    case PROCESSOR_ARCHITECTURE_INTEL:
        machine = "x86";
        break;
    default:
        machine = "unknown";
        break;
    }
}

}} // namespace qpid::sys