* @ingroup netconn_common * Bind a netconn to a specific local IP address and port. * Binding one netconn twice might not always be checked correctly! * * @param conn the netconn to bind * @param addr the local IP address to bind the netconn to * (use IP4_ADDR_ANY/IP6_ADDR_ANY to bind to all addresses) * @param port the local port to bind the netconn to (not used for RAW) * @re
| 304 | * @return ERR_OK if bound, any other err_t on failure |
| 305 | */ |
| 306 | err_t |
| 307 | netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port) |
| 308 | { |
| 309 | API_MSG_VAR_DECLARE(msg); |
| 310 | err_t err; |
| 311 | |
| 312 | LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;); |
| 313 | |
| 314 | #if LWIP_IPV4 |
| 315 | /* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */ |
| 316 | if (addr == NULL) { |
| 317 | addr = IP4_ADDR_ANY; |
| 318 | } |
| 319 | #endif /* LWIP_IPV4 */ |
| 320 | |
| 321 | #if LWIP_IPV4 && LWIP_IPV6 |
| 322 | /* "Socket API like" dual-stack support: If IP to bind to is IP6_ADDR_ANY, |
| 323 | * and NETCONN_FLAG_IPV6_V6ONLY is 0, use IP_ANY_TYPE to bind |
| 324 | */ |
| 325 | if ((netconn_get_ipv6only(conn) == 0) && |
| 326 | ip_addr_cmp(addr, IP6_ADDR_ANY)) { |
| 327 | addr = IP_ANY_TYPE; |
| 328 | } |
| 329 | #endif /* LWIP_IPV4 && LWIP_IPV6 */ |
| 330 | |
| 331 | API_MSG_VAR_ALLOC(msg); |
| 332 | API_MSG_VAR_REF(msg).conn = conn; |
| 333 | API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr); |
| 334 | API_MSG_VAR_REF(msg).msg.bc.port = port; |
| 335 | err = netconn_apimsg(lwip_netconn_do_bind, &API_MSG_VAR_REF(msg)); |
| 336 | API_MSG_VAR_FREE(msg); |
| 337 | |
| 338 | return err; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @ingroup netconn_common |