/* GSocket_SetLocal: * GSocket_GetLocal: * GSocket_SetPeer: * GSocket_GetPeer: * Set or get the local or peer address for this socket. The 'set' * functions return GSOCK_NOERROR on success, an error code otherwise. * The 'get' functions return a pointer to a GAddress object on success, * or NULL otherwise, in which case they set the error code of the * corresponding GSocket. * * Error codes: * GSOCK_INVSOCK - the socket is not valid. * GSOCK_INVADDR - the address is not valid. */ GSocketError GSocket::SetLocal(GAddress *address) { assert(this); /* the socket must be initialized, or it must be a server */ if ((m_fd != INVALID_SOCKET && !m_server)) { m_error = GSOCK_INVSOCK; return GSOCK_INVSOCK; } /* check address */ if (address == NULL || address->m_family == GSOCK_NOFAMILY) { m_error = GSOCK_INVADDR; return GSOCK_INVADDR; } if (m_local) GAddress_destroy(m_local); m_local = GAddress_copy(address); return GSOCK_NOERROR; }
/* GSocket_SetLocal: * GSocket_GetLocal: * GSocket_SetPeer: * GSocket_GetPeer: * Set or get the local or peer address for this socket. The 'set' * functions return GSOCK_NOERROR on success, an error code otherwise. * The 'get' functions return a pointer to a GAddress object on success, * or NULL otherwise, in which case they set the error code of the * corresponding GSocket. * * Error codes: * GSOCK_INVSOCK - the socket is not valid. * GSOCK_INVADDR - the address is not valid. */ GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address) { assert(socket != NULL); /* the socket must be initialized, or it must be a server */ if ((socket->m_endpoint != kOTInvalidEndpointRef && !socket->m_server)) { socket->m_error = GSOCK_INVSOCK; return GSOCK_INVSOCK; } /* check address */ if (address == NULL || address->m_family == GSOCK_NOFAMILY) { socket->m_error = GSOCK_INVADDR; return GSOCK_INVADDR; } if (socket->m_local) GAddress_destroy(socket->m_local); socket->m_local = GAddress_copy(address); return GSOCK_NOERROR; }
GAddress *GSocket::GetPeer() { assert(this); /* try to get it from the m_peer var */ if (m_peer) return GAddress_copy(m_peer); return NULL; }
GAddress *GSocket_GetPeer(GSocket *socket) { assert(socket != NULL); /* try to get it from the m_peer var */ if (socket->m_peer) return GAddress_copy(socket->m_peer); return NULL; }
GAddress *GSocket_GetLocal(GSocket *socket) { GAddress *address = NULL ; GSocketError err; InetAddress loc ; assert(socket != NULL); /* try to get it from the m_local var first */ if (socket->m_local) return GAddress_copy(socket->m_local); /* else, if the socket is initialized, try getsockname */ if (socket->m_endpoint == kOTInvalidEndpointRef) { socket->m_error = GSOCK_INVSOCK; return NULL; } /* we do not support multihoming with this code at the moment OTGetProtAddress will have to be used then - but we don't have a handy method to use right now */ { InetInterfaceInfo info; OTInetGetInterfaceInfo(&info, kDefaultInetInterface); loc.fHost = info.fAddress ; loc.fPort = 0 ; loc.fAddressType = AF_INET ; } /* got a valid address from getsockname, create a GAddress object */ address = GAddress_new(); if (address == NULL) { socket->m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(address, &loc); if (err != GSOCK_NOERROR) { GAddress_destroy(address); socket->m_error = err; return NULL; } return address; }
GSocketError GSocket::SetPeer(GAddress *address) { assert(this); /* check address */ if (address == NULL || address->m_family == GSOCK_NOFAMILY) { m_error = GSOCK_INVADDR; return GSOCK_INVADDR; } if (m_peer) GAddress_destroy(m_peer); m_peer = GAddress_copy(address); return GSOCK_NOERROR; }
GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address) { assert(socket != NULL); /* check address */ if (address == NULL || address->m_family == GSOCK_NOFAMILY) { socket->m_error = GSOCK_INVADDR; return GSOCK_INVADDR; } if (socket->m_peer) GAddress_destroy(socket->m_peer); socket->m_peer = GAddress_copy(address); return GSOCK_NOERROR; }
GAddress *GSocket::GetLocal() { GAddress *address; struct sockaddr addr; SOCKLEN_T size = sizeof(addr); GSocketError err; assert(this); /* try to get it from the m_local var first */ if (m_local) return GAddress_copy(m_local); /* else, if the socket is initialized, try getsockname */ if (m_fd == INVALID_SOCKET) { m_error = GSOCK_INVSOCK; return NULL; } if (getsockname(m_fd, &addr, (SOCKLEN_T *) &size) < 0) { m_error = GSOCK_IOERR; return NULL; } /* got a valid address from getsockname, create a GAddress object */ address = GAddress_new(); if (address == NULL) { m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(address, &addr, size); if (err != GSOCK_NOERROR) { GAddress_destroy(address); m_error = err; return NULL; } return address; }