glue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright (c) 2019 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #define ENET_IMPLEMENTATION
  20. #include "enet.h"
  21. #include "brl.mod/blitz.mod/blitz.h"
  22. ENetAddress * bmx_enet_address_create_ipv4(int host_ip, int host_port) {
  23. ENetAddress * address = calloc(1, sizeof(ENetAddress));
  24. address->host.s6_addr[5] = 0xffff;
  25. memcpy(&address->host.s6_addr[6], &host_ip, sizeof(int));
  26. address->port = host_port;
  27. return address;
  28. }
  29. ENetAddress * bmx_enet_address_create_any(int host_port) {
  30. ENetAddress * address = calloc(1, sizeof(ENetAddress));
  31. address->host = ENET_HOST_ANY;
  32. address->port = host_port;
  33. return address;
  34. }
  35. ENetAddress * bmx_enet_address_create_ipv6(BBString * host_ip, int host_port, int ipv6) {
  36. ENetAddress * address = calloc(1, sizeof(ENetAddress));
  37. char * s = bbStringToUTF8String(host_ip);
  38. if (ipv6) {
  39. struct in6_addr addr;
  40. inet_pton(AF_INET6, s, &addr);
  41. address->host = addr;
  42. } else {
  43. struct in_addr addr4;
  44. inet_pton(AF_INET, s, &addr4);
  45. struct in6_addr addr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}};
  46. addr.s6_addr[12] = ((uint8_t *)&addr4.s_addr)[0];
  47. addr.s6_addr[13] = ((uint8_t *)&addr4.s_addr)[1];
  48. addr.s6_addr[14] = ((uint8_t *)&addr4.s_addr)[2];
  49. addr.s6_addr[15] = ((uint8_t *)&addr4.s_addr)[3];
  50. address->host = addr;
  51. }
  52. bbMemFree(s);
  53. address->port = host_port;
  54. return address;
  55. }
  56. void bmx_enet_address_destroy(ENetAddress * address) {
  57. free(address);
  58. }
  59. enet_uint8 * bmx_enet_packet_data(ENetPacket * packet) {
  60. return packet->data;
  61. }
  62. size_t bmx_enet_packet_size(ENetPacket * packet) {
  63. return packet->dataLength;
  64. }
  65. ENetEvent * bmx_enet_enetevent_new() {
  66. return calloc(1, sizeof(ENetEvent));
  67. }
  68. void bmx_enet_enetevent_free(ENetEvent * event) {
  69. free(event);
  70. }
  71. ENetPeer * bmx_enet_enetevent_peer(ENetEvent * event) {
  72. return event->peer;
  73. }
  74. int bmx_enet_enetevent_event(ENetEvent * event) {
  75. return event->type;
  76. }
  77. ENetPacket * bmx_enet_enetevent_packet(ENetEvent * event) {
  78. return event->packet;
  79. }
  80. enet_uint8 bmx_enet_enetevent_channelid(ENetEvent * event) {
  81. return event->channelID;
  82. }
  83. enet_uint32 bmx_enet_enetevent_data(ENetEvent * event) {
  84. return event->data;
  85. }