client.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * netq/client.c
  3. *
  4. * Copyright (C) 2022 bzt (bztsrc@gitlab) MIT license
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief An example NetQ client for IPv4 and IPv6 UDP
  27. * https://gitlab.com/bztsrc/netq
  28. *
  29. * Compile with: gcc client.c -o client
  30. */
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <netdb.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #define NETQ_IMPLEMENTATION
  38. #define NETQ_SEND my_sender
  39. #include "netq.h"
  40. /* the transport layer context */
  41. typedef struct {
  42. int sock;
  43. struct sockaddr_storage peer_addr;
  44. socklen_t addrlen;
  45. } my_net_t;
  46. my_net_t my_net;
  47. /* the raw packet sender */
  48. int my_sender(void *net, const void *message, int length)
  49. {
  50. my_net_t *my_net = (my_net_t *)net;
  51. return sendto(my_net->sock, message, length, 0, (struct sockaddr*)&my_net->peer_addr, my_net->addrlen);
  52. }
  53. /* the raw packet receiver */
  54. int my_receiver(void *net, void *message, int length)
  55. {
  56. my_net_t *my_net = (my_net_t *)net;
  57. my_net->addrlen = sizeof(struct sockaddr_storage); /* <- the recvfrom() API is tricky */
  58. return recvfrom(my_net->sock, message, length, 0, (struct sockaddr*)&my_net->peer_addr, &my_net->addrlen);
  59. }
  60. /* the client needs only one queue */
  61. netq_t nq = { 0 };
  62. /**
  63. * Example client
  64. */
  65. int main(int argc, char **argv)
  66. {
  67. int ret, n = 1;
  68. struct sockaddr_storage bind_addr;
  69. struct addrinfo hints, *cur, *addr_list = NULL;
  70. char buf[NETQ_MTU];
  71. if(argc < 3) {
  72. printf("%s <server ip> <port>\n", argv[0]);
  73. return 1;
  74. }
  75. /* get binary ip address for hostname / ip string, the usual stuff, nothing NetQ specific here */
  76. memset(&bind_addr, 0, sizeof(bind_addr));
  77. memset(&my_net, 0, sizeof(my_net));
  78. memset(&hints, 0, sizeof(hints));
  79. hints.ai_family = AF_UNSPEC;
  80. hints.ai_socktype = SOCK_DGRAM;
  81. hints.ai_protocol = IPPROTO_UDP;
  82. if(getaddrinfo(argv[1], argv[2], &hints, &addr_list) != 0) {
  83. fprintf(stderr, "getaddrinfo failed.\n"); return 1; }
  84. for(cur = addr_list; cur != NULL && !my_net.addrlen; cur = cur->ai_next) {
  85. my_net.sock = (int)socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol); n = 1;
  86. if(my_net.sock < 0) continue;
  87. /* we don't care about our ip, any will do */
  88. memset(&bind_addr, 0, sizeof(bind_addr));
  89. bind_addr.ss_family = cur->ai_family;
  90. if(cur->ai_family == AF_INET) ((struct sockaddr_in*)&bind_addr)->sin_addr.s_addr = INADDR_ANY;
  91. else ((struct sockaddr_in6*)&bind_addr)->sin6_addr = in6addr_any;
  92. if(setsockopt(my_net.sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&n, sizeof(n)) != 0 ||
  93. bind(my_net.sock, (struct sockaddr*)&bind_addr, cur->ai_addrlen) != 0) { close(my_net.sock); continue; }
  94. memcpy(&my_net.peer_addr, cur->ai_addr, cur->ai_addrlen);
  95. my_net.addrlen = cur->ai_addrlen;
  96. }
  97. freeaddrinfo(addr_list);
  98. /**************************************************
  99. * do some testing by sending / receiving packets *
  100. **************************************************/
  101. for(n = 1; n < 5; n++) {
  102. printf("-------------------------------------------------------------------------------\n");
  103. /* send message to server */
  104. sprintf(buf, "Hello %d", n);
  105. if((ret = netq_send(&nq, buf, strlen(buf) + 1, &my_net)) < 0)
  106. printf("netq_send returned %d\n", ret);
  107. netq_dump(&nq);
  108. /* get server's response */
  109. if((ret = my_receiver(&my_net, buf, sizeof(buf))) > 0) {
  110. netq_push(&nq, buf, ret, &my_net);
  111. netq_dump(&nq);
  112. netq_pop(&nq, buf, sizeof(buf));
  113. netq_dump(&nq);
  114. } else
  115. printf("recvfrom returned %d\n", ret);
  116. }
  117. close(my_net.sock);
  118. return 0;
  119. }