/platform/ipx/stacks/stack_fnet.c
C | 166 lines | 122 code | 40 blank | 4 comment | 26 complexity | 122bbdb8ddc969221a96d2e55dae1937 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
- /* Headers */
- #include <stdint.h>
- #include <string.h>
- #include "stacks/stack.h"
- #include "ipx_error.h"
- #include "log.h"
- #include "fnet.h"
- #include "fnet_socket.h"
- #include "fnet_socket_prv.h"
- /* Prototypes */
- static void ipx_dhcp_updated(fnet_netif_desc_t netif, void *param);
- static void ipx_dhcp_set(fnet_netif_desc_t netif, void *param);
- extern fnet_socket_t *fnet_socket_desc_find(SOCKET desc);
- /* Implementations */
- int ipx_init(void) {
- if(fnet_init_static() != FNET_OK) {
- return ipx_error_internal;
- }
- return ipx_error_success;
- }
- void *ipx_get_stack_context(unsigned int n) {
- return (void*)fnet_netif_get_by_number(n);
- }
- void ipx_poll(void) {
- fnet_poll_services();
- }
- int ipx_do_dhcp(void *iface, const ipx_stack_api_t *api) {
- struct fnet_dhcp_params dhcp_params;
- fnet_netif_desc_t netif = (fnet_netif_desc_t)iface;
- fnet_memset_zero(&dhcp_params, sizeof(struct fnet_dhcp_params));
- if(fnet_dhcp_init(netif, &dhcp_params) != FNET_ERR) {
- fnet_dhcp_handler_updated_set(ipx_dhcp_updated, (void*)api);
- fnet_dhcp_handler_discover_set(ipx_dhcp_set, (void*)api);
- }
- else {
- LOG_ERROR("ERROR: DHCP initialization is failed!");
- return ipx_error_init_fail;
- }
- return ipx_error_success;
- }
- static void ipx_dhcp_updated(fnet_netif_desc_t netif, void *param) {
- ((const ipx_stack_api_t*)param)->dhcp_updated((void*)netif);
- }
- static void ipx_dhcp_set(fnet_netif_desc_t netif, void *param) {
- ((const ipx_stack_api_t*)param)->dhcp_set((void*)netif);
- }
- void *ipx_open_udp_socket(void *context, ip_port_t lport, int flags) {
- SOCKET sock;
- fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
- struct sockaddr laddr;
- memset((void*)&laddr, 0x00, sizeof(laddr));
- ((struct sockaddr_in*)(&laddr))->sin_family = AF_INET;
- ((struct sockaddr_in*)(&laddr))->sin_port = lport;
- ((struct sockaddr_in*)(&laddr))->sin_addr.s_addr = FNET_HTONL(fnet_netif_get_ip4_addr(netif));
- if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_INVALID) {
- return NULL;
- }
- if(bind(sock, (struct sockaddr*)&laddr, sizeof(laddr)) != 0) {
- return NULL;
- }
- return (void*)sock;
- }
- int ipx_udp_send(void *context, const uint8_t *buf, size_t len, const ip_host_t *host) {
- struct sockaddr dest_addr;
- memset((void*)&dest_addr, 0x00, sizeof(dest_addr));
- ((struct sockaddr_in*)(&dest_addr))->sin_family = AF_INET;
- ((struct sockaddr_in*)(&dest_addr))->sin_port = host->host.v4.port;
- ((struct sockaddr_in*)(&dest_addr))->sin_addr.s_addr = FNET_HTONL(host->host.v4.ip.addr.addr.word);
- if(sendto((SOCKET)context, (char*)buf, len, 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) == FNET_OK) {
- return ipx_error_success;
- }
- return ipx_error_internal;
- }
- size_t ipx_udp_recv_peek(void *context) {
- fnet_socket_t *sock = fnet_socket_desc_find((SOCKET)context);
- if(sock != NULL) {
- // Look into the buffer chain for a node w/ length > 0.
- fnet_netbuf_t *nb = sock->receive_buffer.net_buf_chain;
- if(nb != NULL && nb->next != NULL) {
- return (size_t)nb->next->total_length;
- }
- }
- return 0;
- }
- size_t ipx_udp_recv(void *context, uint8_t *buf, size_t len, ip_host_t *from) {
- struct sockaddr from_addr;
- unsigned int from_len = sizeof(from_addr);
- int result = recvfrom((SOCKET)context, (char*)buf, len, 0, &from_addr, &from_len);
- if(result > 0) {
- from->is_v6 = false;
- from->host.v4.port = ((struct sockaddr_in*)(&from_addr))->sin_port;
- from->host.v4.ip.addr.addr.word = ((struct sockaddr_in*)(&from_addr))->sin_addr.s_addr;
- return (size_t)result;
- }
- return 0;
- }
- unsigned int ipx_register_service(void (*handler)(void *context), void *context) {
- return (unsigned int)fnet_poll_service_register(handler, context);
- }
- bool ipx_get_mac_address(void *context, uint8_t *mac_buf) {
- fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
- return (fnet_netif_get_hw_addr(netif, (unsigned char*)mac_buf, IPX_INTERFACE_MAC_LEN) == FNET_OK);
- }
- bool ipx_get_ipv4_addressing(void *context, uint8_t *ip_buf, uint8_t *gw_buf, uint8_t *sn_buf) {
- fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
- uint32_t *ip = (uint32_t*)ip_buf;
- uint32_t *gw = (uint32_t*)gw_buf;
- uint32_t *sn = (uint32_t*)sn_buf;
- if(ip_buf != NULL) {
- *ip = fnet_netif_get_ip4_addr(netif);
- }
- if(gw_buf != NULL) {
- *gw = fnet_netif_get_ip4_gateway(netif);
- }
- if(sn_buf != NULL) {
- *sn = fnet_netif_get_ip4_subnet_mask(netif);
- }
- return (*ip != 0);
- }
- bool ipx_get_ipv6_addressing(void *context, uint8_t *ip_buf, uint8_t *gw_buf, uint8_t *sn_buf) {
- return false;
- }
- void ipx_get_name(void *context, uint8_t *buf, uint8_t len) {
- fnet_netif_desc_t netif = (fnet_netif_desc_t)context;
- fnet_netif_get_name(netif, (char*)buf, len);
- }