int lwip_bind(int s, struct sockaddr *name, socklen_t namelen) { struct lwip_socket *sock; struct ip_addr local_addr; u16_t local_port; err_t err; sock = get_socket(s); if (!sock) { set_errno(EBADF); return -1; } local_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr; local_port = ((struct sockaddr_in *)name)->sin_port; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s)); ip_addr_debug_print(SOCKETS_DEBUG, &local_addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(local_port))); err = netconn_bind(sock->conn, &local_addr, ntohs(local_port)); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s)); sock_set_errno(sock, 0); return 0; }
int lwip_send(int s, void *data, int size, unsigned int flags) { struct lwip_socket *sock; struct netbuf *buf; err_t err; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%d, flags=0x%x)\n", s, data, size, flags)); sock = get_socket(s); if (!sock) { set_errno(EBADF); return -1; } switch (netconn_type(sock->conn)) { case NETCONN_RAW: case NETCONN_UDP: case NETCONN_UDPLITE: case NETCONN_UDPNOCHKSUM: /* create a buffer */ buf = netbuf_new(); if (!buf) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ENOBUFS\n", s)); sock_set_errno(sock, ENOBUFS); return -1; } /* make the buffer point to the data that should be sent */ netbuf_ref(buf, data, size); /* send the data */ err = netconn_send(sock->conn, buf); /* deallocated the buffer */ netbuf_delete(buf); break; case NETCONN_TCP: err = netconn_write(sock->conn, data, size, NETCONN_COPY); break; default: err = ERR_ARG; break; } if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) ok size=%d\n", s, size)); sock_set_errno(sock, 0); return size; }
/*-----------------------------------------------------------------------------------*/ int lwip_connect(int s, struct sockaddr *name, socklen_t namelen) { struct lwip_socket *sock; err_t err; sock = get_socket(s); if (!sock) { return -1; } if (((struct sockaddr_in *)name)->sin_family == AF_UNSPEC) { DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s)); err = netconn_disconnect(sock->conn); } else { struct ip_addr remote_addr; u16_t remote_port; remote_addr.addr = ((struct sockaddr_in *)name)->sin_addr.s_addr; remote_port = ((struct sockaddr_in *)name)->sin_port; #if SOCKETS_DEBUG DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s)); ip_addr_debug_print(&remote_addr); DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port))); #endif err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port)); } if (err != ERR_OK) { DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s)); sock_set_errno(sock, 0); return 0; }
/*-----------------------------------------------------------------------------------*/ int lwip_listen(int s, int backlog) { struct lwip_socket *sock; err_t err; DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog)); sock = get_socket(s); if (!sock) { return -1; } err = netconn_listen(sock->conn); if (err != ERR_OK) { DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } sock_set_errno(sock, 0); return 0; }