Free Electron
NetUtil.h
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 #ifndef __NETUTIL_H__
6 #define __NETUTIL_H__
7 
8 #include "osheader.h"
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 class NetUtil
16 {
17 public:
18  static NetUtil &Instance()
19  {
20  static NetUtil instance;
21  return instance;
22  }
23 
24  ~NetUtil()
25  {
26  Cleanup();
27  }
28 
29  bool IsNetworkInit()
30  {
31 #ifdef _WIN32
32  SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
33  if (s == INVALID_SOCKET && WSAGetLastError() == WSANOTINITIALISED)
34  {
35  return false;
36  }
37 
38  closesocket(s);
39 #endif
40  return true;
41  }
42 
43  int GetLastError()
44  {
45 #ifdef _WIN32
46  return WSAGetLastError();
47 #else
48  return errno;
49 #endif
50  }
51 
52 protected:
53  NetUtil() { Init(); }
54 
55  bool Init()
56  {
57 #ifdef _WIN32
58  WSADATA wsaData;
59  WORD version = MAKEWORD(2, 2);
60 
61  if (WSAStartup(version, &wsaData) != 0)
62  {
63  feLog("winsock initialization error\n");
64  return false;
65  }
66 #endif
67  return true;
68  }
69 
70  void Cleanup()
71  {
72 #ifdef _WIN32
73  WSACleanup();
74 #endif
75  }
76 };
77 
78 } // namespace ext
79 } // namespace fe
80 
81 #endif
kernel
Definition: namespace.dox:3