int32_t socket_base_recvfrom(int s, void *buf, uint32_t len, int flags, sockaddr6_t *from, uint32_t *fromlen) { if (udp_socket_compliancy(s)) { return udp_recvfrom(s, buf, len, flags, from, fromlen); } else if (tcp_socket_compliancy(s)) { return tcp_recv(s, buf, len, flags); } printf("Socket Type not supported!\n"); return -1; }
int socket_base_close(int s) { socket_internal_t *current_socket = socket_base_get_socket(s); if (udp_socket_compliancy(s)) { memset(current_socket, 0, sizeof(socket_internal_t)); return 0; } else if (tcp_socket_compliancy(s)) { return tcp_teardown(current_socket); } printf("Socket Type not supported!\n"); return -1; }
int32_t socket_base_sendto(int s, const void *buf, uint32_t len, int flags, sockaddr6_t *to, uint32_t tolen) { if (udp_socket_compliancy(s)) { return udp_sendto(s, buf, len, flags, to, tolen); } else if (tcp_socket_compliancy(s)) { return tcp_send(s, buf, len, flags); } else { printf("Socket Type not supported!\n"); return -1; } return -1; }
socket_internal_t *get_udp_socket(udp_hdr_t *udp_header) { uint8_t i = 1; while (i < MAX_SOCKETS + 1) { if (udp_socket_compliancy(i) && (socket_base_get_socket(i)->socket_values.local_address.sin6_port == udp_header->dst_port)) { return socket_base_get_socket(i); } i++; } return NULL; }
int udp_bind_socket(int s, sockaddr6_t *name, int namelen, uint8_t pid) { int i; if (!socket_base_exists_socket(s)) { return -1; } for (i = 1; i < MAX_SOCKETS + 1; i++) { if (udp_socket_compliancy(i) && (socket_base_get_socket(i)->socket_values.local_address.sin6_port == name->sin6_port)) { return -1; } } memcpy(&socket_base_get_socket(s)->socket_values.local_address, name, namelen); socket_base_get_socket(s)->recv_pid = pid; return 0; }
int32_t udp_sendto(int s, const void *buf, uint32_t len, int flags, sockaddr6_t *to, uint32_t tolen) { (void) flags; (void) tolen; if (udp_socket_compliancy(s) && (socket_base_get_socket(s)->socket_values.foreign_address.sin6_port == 0)) { uint8_t send_buffer[BUFFER_SIZE]; ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); udp_hdr_t *current_udp_packet = ((udp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); uint8_t *payload = &send_buffer[IPV6_HDR_LEN + UDP_HDR_LEN]; memcpy(&(temp_ipv6_header->destaddr), &to->sin6_addr, 16); ipv6_net_if_get_best_src_addr(&(temp_ipv6_header->srcaddr), &(temp_ipv6_header->destaddr)); current_udp_packet->src_port = socket_base_get_free_source_port(IPPROTO_UDP); current_udp_packet->dst_port = to->sin6_port; current_udp_packet->checksum = 0; memcpy(payload, buf, len); current_udp_packet->length = HTONS(UDP_HDR_LEN + len); temp_ipv6_header->length = UDP_HDR_LEN + len; current_udp_packet->checksum = ~ipv6_csum(temp_ipv6_header, (uint8_t *) current_udp_packet, UDP_HDR_LEN + len, IPPROTO_UDP); return ipv6_sendto(&to->sin6_addr, IPPROTO_UDP, (uint8_t *)(current_udp_packet), NTOHS(current_udp_packet->length), NULL); } else { return -1; } }