BNetwork.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @file BNetwork.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifdef BADVPN_USE_WINAPI
  23. #include <windows.h>
  24. #include <winsock2.h>
  25. #include <mswsock.h>
  26. #else
  27. #include <string.h>
  28. #include <signal.h>
  29. #endif
  30. #include <misc/debug.h>
  31. #include <base/BLog.h>
  32. #include <system/BNetwork.h>
  33. #include <generated/blog_channel_BNetwork.h>
  34. int bnetwork_initialized = 0;
  35. int BNetwork_GlobalInit (void)
  36. {
  37. ASSERT(!bnetwork_initialized)
  38. #ifdef BADVPN_USE_WINAPI
  39. WORD requested = MAKEWORD(2, 2);
  40. WSADATA wsadata;
  41. if (WSAStartup(requested, &wsadata) != 0) {
  42. BLog(BLOG_ERROR, "WSAStartup failed");
  43. goto fail0;
  44. }
  45. if (wsadata.wVersion != requested) {
  46. BLog(BLOG_ERROR, "WSAStartup returned wrong version");
  47. goto fail1;
  48. }
  49. #else
  50. struct sigaction act;
  51. memset(&act, 0, sizeof(act));
  52. act.sa_handler = SIG_IGN;
  53. sigemptyset(&act.sa_mask);
  54. act.sa_flags = 0;
  55. if (sigaction(SIGPIPE, &act, NULL) < 0) {
  56. BLog(BLOG_ERROR, "sigaction failed");
  57. goto fail0;
  58. }
  59. #endif
  60. bnetwork_initialized = 1;
  61. return 1;
  62. #ifdef BADVPN_USE_WINAPI
  63. fail1:
  64. WSACleanup();
  65. #endif
  66. fail0:
  67. return 0;
  68. }
  69. void BNetwork_Assert (void)
  70. {
  71. ASSERT(bnetwork_initialized)
  72. }