int socket(socket_family_t family, socket_type_t type, socket_protocol_t protocol) { if (m_initialization_state == false) { (void) socket_init(); } VERIFY_MODULE_IS_INITIALIZED(); int ret_sock = -1; int sock = socket_allocate(); SOCKET_TRACE("Got value %d from allocate\r\n", (int)sock); if (sock >= 0) { socket_entry_t *p_socket_entry = &m_socket_table[sock]; p_socket_entry->handle.params.family = family; p_socket_entry->handle.params.protocol = protocol; p_socket_entry->handle.params.type = type; p_socket_entry->handler = NULL; if (family == AF_INET6) { #if SOCKET_IPV6_ENABLE == 1 || SOCKET_LWIP_ENABLE == 1 p_socket_entry->handler = &transport_handler; #else set_errno(EAFNOSUPPORT); #endif // } else if (family == AF_BLE) { // TODO: Handle BLE } else { set_errno(EAFNOSUPPORT); } if (p_socket_entry->handler != NULL) { uint32_t err_code = p_socket_entry->handler->create_handler(&p_socket_entry->handle); socket_set_errno(err_code); ret_sock = (err_code == NRF_SUCCESS) ? sock : ret_sock; } if (ret_sock < 0) { socket_free(sock); } } SOCKET_TRACE("Returning socket value %d\r\n", (int)ret_sock); return ret_sock; }
// the listen handler is pretty easy, so we can just stick it here int voicechat_hearthstone_listen_socket_read_handler(network_socket *s, int act) { struct sockaddr_in new_address; int slen = sizeof(struct sockaddr_in); char buffer[100] = {'N','/','A'}; network_socket * ns; int new_fd = (int)accept(s->fd, (struct sockaddr*)&new_address, &slen); if( new_fd < 0 ) { log_write(ERROR, "FATAL: accept() returned < %d", new_fd); abort(); return -1; } //inet_ntop(AF_INET, &new_address.sin_addr, buffer, 100); //log_write(DEBUG, "Incoming TCP connection from %s port %u", buffer, (int)ntohs(new_address.sin_port)); log_write(DEBUG, "Incoming TCP connection from %s port %u", inet_ntoa(new_address.sin_addr), (int)ntohs(new_address.sin_port)); // create the socket structure. ns = (network_socket*)vc_malloc(sizeof(network_socket)); network_init_socket(ns, new_fd, 5000); ns->event_handler = voicechat_socket_read_handler; ns->miscdata = (void*)socket_allocate(ns); // used for voicechat struct later ns->write_handler = default_tcp_write_handler; // add it if( network_add_socket(ns) < 0 ) { log_write(ERROR, "FD array is full!"); socket_free((ascnhalf_socket*)ns->miscdata); network_close(ns); } return 0; }