PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/ipx/stacks/stack_fnet.c

https://gitlab.com/fuggles/ucos
C | 166 lines | 122 code | 40 blank | 4 comment | 26 complexity | 122bbdb8ddc969221a96d2e55dae1937 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /* Headers */
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include "stacks/stack.h"
  5. #include "ipx_error.h"
  6. #include "log.h"
  7. #include "fnet.h"
  8. #include "fnet_socket.h"
  9. #include "fnet_socket_prv.h"
  10. /* Prototypes */
  11. static void ipx_dhcp_updated(fnet_netif_desc_t netif, void *param);
  12. static void ipx_dhcp_set(fnet_netif_desc_t netif, void *param);
  13. extern fnet_socket_t *fnet_socket_desc_find(SOCKET desc);
  14. /* Implementations */
  15. int ipx_init(void) {
  16. if(fnet_init_static() != FNET_OK) {
  17. return ipx_error_internal;
  18. }
  19. return ipx_error_success;
  20. }
  21. void *ipx_get_stack_context(unsigned int n) {
  22. return (void*)fnet_netif_get_by_number(n);
  23. }
  24. void ipx_poll(void) {
  25. fnet_poll_services();
  26. }
  27. int ipx_do_dhcp(void *iface, const ipx_stack_api_t *api) {
  28. struct fnet_dhcp_params dhcp_params;
  29. fnet_netif_desc_t netif = (fnet_netif_desc_t)iface;
  30. fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));
  31. if(fnet_dhcp_init(netif, &dhcp_params) != FNET_ERR) {
  32. fnet_dhcp_handler_updated_set(ipx_dhcp_updated, (void*)api);
  33. fnet_dhcp_handler_discover_set(ipx_dhcp_set, (void*)api);
  34. }
  35. else {
  36. LOG_ERROR("ERROR: DHCP initialization is failed!");
  37. return ipx_error_init_fail;
  38. }
  39. return ipx_error_success;
  40. }
  41. static void ipx_dhcp_updated(fnet_netif_desc_t netif, void *param) {
  42. ((const ipx_stack_api_t*)param)->dhcp_updated((void*)netif);
  43. }
  44. static void ipx_dhcp_set(fnet_netif_desc_t netif, void *param) {
  45. ((const ipx_stack_api_t*)param)->dhcp_set((void*)netif);
  46. }
  47. void *ipx_open_udp_socket(void *context, ip_port_t lport, int flags) {
  48. SOCKET sock;
  49. fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
  50. struct sockaddr laddr;
  51. memset((void*)&laddr, 0x00, sizeof(laddr));
  52. ((struct sockaddr_in*)(&laddr))->sin_family = AF_INET;
  53. ((struct sockaddr_in*)(&laddr))->sin_port = lport;
  54. ((struct sockaddr_in*)(&laddr))->sin_addr.s_addr = FNET_HTONL(fnet_netif_get_ip4_addr(netif));
  55. if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_INVALID) {
  56. return NULL;
  57. }
  58. if(bind(sock, (struct sockaddr*)&laddr, sizeof(laddr)) != 0) {
  59. return NULL;
  60. }
  61. return (void*)sock;
  62. }
  63. int ipx_udp_send(void *context, const uint8_t *buf, size_t len, const ip_host_t *host) {
  64. struct sockaddr dest_addr;
  65. memset((void*)&dest_addr, 0x00, sizeof(dest_addr));
  66. ((struct sockaddr_in*)(&dest_addr))->sin_family = AF_INET;
  67. ((struct sockaddr_in*)(&dest_addr))->sin_port = host->host.v4.port;
  68. ((struct sockaddr_in*)(&dest_addr))->sin_addr.s_addr = FNET_HTONL(host->host.v4.ip.addr.addr.word);
  69. if(sendto((SOCKET)context, (char*)buf, len, 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) == FNET_OK) {
  70. return ipx_error_success;
  71. }
  72. return ipx_error_internal;
  73. }
  74. size_t ipx_udp_recv_peek(void *context) {
  75. fnet_socket_t *sock = fnet_socket_desc_find((SOCKET)context);
  76. if(sock != NULL) {
  77. // Look into the buffer chain for a node w/ length > 0.
  78. fnet_netbuf_t *nb = sock->receive_buffer.net_buf_chain;
  79. if(nb != NULL && nb->next != NULL) {
  80. return (size_t)nb->next->total_length;
  81. }
  82. }
  83. return 0;
  84. }
  85. size_t ipx_udp_recv(void *context, uint8_t *buf, size_t len, ip_host_t *from) {
  86. struct sockaddr from_addr;
  87. unsigned int from_len = sizeof(from_addr);
  88. int result = recvfrom((SOCKET)context, (char*)buf, len, 0, &from_addr, &from_len);
  89. if(result > 0) {
  90. from->is_v6 = false;
  91. from->host.v4.port = ((struct sockaddr_in*)(&from_addr))->sin_port;
  92. from->host.v4.ip.addr.addr.word = ((struct sockaddr_in*)(&from_addr))->sin_addr.s_addr;
  93. return (size_t)result;
  94. }
  95. return 0;
  96. }
  97. unsigned int ipx_register_service(void (*handler)(void *context), void *context) {
  98. return (unsigned int)fnet_poll_service_register(handler, context);
  99. }
  100. bool ipx_get_mac_address(void *context, uint8_t *mac_buf) {
  101. fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
  102. return (fnet_netif_get_hw_addr(netif, (unsigned char*)mac_buf, IPX_INTERFACE_MAC_LEN) == FNET_OK);
  103. }
  104. bool ipx_get_ipv4_addressing(void *context, uint8_t *ip_buf, uint8_t *gw_buf, uint8_t *sn_buf) {
  105. fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
  106. uint32_t *ip = (uint32_t*)ip_buf;
  107. uint32_t *gw = (uint32_t*)gw_buf;
  108. uint32_t *sn = (uint32_t*)sn_buf;
  109. if(ip_buf != NULL) {
  110. *ip = fnet_netif_get_ip4_addr(netif);
  111. }
  112. if(gw_buf != NULL) {
  113. *gw = fnet_netif_get_ip4_gateway(netif);
  114. }
  115. if(sn_buf != NULL) {
  116. *sn = fnet_netif_get_ip4_subnet_mask(netif);
  117. }
  118. return (*ip != 0);
  119. }
  120. bool ipx_get_ipv6_addressing(void *context, uint8_t *ip_buf, uint8_t *gw_buf, uint8_t *sn_buf) {
  121. return false;
  122. }
  123. void ipx_get_name(void *context, uint8_t *buf, uint8_t len) {
  124. fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
  125. fnet_netif_get_name(netif, (char*)buf, len);
  126. }