/*-------------------------------------------------------------------------------------*/ char * get_ip_addr( Socket socket ) /*-------------------------------------------------------------------------------------*/ { // parameter check check_socket_ptr("get_ip_addr() failed", socket); MySocket *s = (MySocket *)socket; check_ip_addr("get_ip_addr() failed", s->ip_addr); return s->ip_addr; }
/*-------------------------------------------------------------------------------------*/ Socket tcp_active_open( int remote_port, char *remote_ip ) /*-------------------------------------------------------------------------------------*/ { // parameter check check_port("tcp_open_client() failed", remote_port); check_ip_addr("tcp_open_client() failed", remote_ip); MySocket *client = (MySocket *)malloc( sizeof(MySocket) ); if ( client == NULL ) die("tcp_open_client() failed: mem alloc error"); struct sockaddr_in addr; int length; char *p; client->sd = socket(PROTOCOLFAMILY, TYPE, PROTOCOL); check_sd("tcp_open_client() failed: socket creation error", client->sd); /* Construct the server address structure */ memset(&addr, 0, sizeof(struct sockaddr_in)); addr.sin_family = PROTOCOLFAMILY; if ( inet_aton(remote_ip, (struct in_addr *) &addr.sin_addr.s_addr) == 0 ) die("tcp_open_client failed(): invalid ip address"); addr.sin_port = htons(remote_port); if ( connect(client->sd, (struct sockaddr *) &addr, sizeof(addr) ) < 0 ) die("tcp_open_client failed(): connect () failed"); memset(&addr, 0, sizeof(struct sockaddr_in)); length = sizeof(addr); if ( getsockname(client->sd, (struct sockaddr *)&addr, (socklen_t *)&length) != 0 ) die("tcp_open_client failed(): getsockname() failed"); p = inet_ntoa(addr.sin_addr); //returns addr to statically allocated buffer client->ip_addr = (char *)malloc( sizeof(char)*CHAR_IP_ADDR_LENGTH); if ( client->ip_addr == NULL ) die("tcp_open_client failed(): mem alloc error"); client->ip_addr = strcpy( client->ip_addr, p ); client->port = ntohs(addr.sin_port); return (Socket)client; }
/* Called when data needs to be sent to network */ int net_send_data(struct net_pkt *pkt) { int status; if (!pkt || !pkt->frags) { return -ENODATA; } if (!net_pkt_iface(pkt)) { return -EINVAL; } #if defined(CONFIG_NET_STATISTICS) switch (net_pkt_family(pkt)) { case AF_INET: net_stats_update_ipv4_sent(); break; case AF_INET6: net_stats_update_ipv6_sent(); break; } #endif status = check_ip_addr(pkt); if (status < 0) { return status; } else if (status > 0) { /* Packet is destined back to us so send it directly * to RX processing. */ NET_DBG("Loopback pkt %p back to us", pkt); processing_data(pkt, true); return 0; } if (net_if_send_data(net_pkt_iface(pkt), pkt) == NET_DROP) { return -EIO; } return 0; }