From 79e0d4f89c4d532189aae06cc5dfbc14e3269703 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Mon, 11 Feb 2019 10:44:29 +0100 Subject: [HICN-50] Added udp application connector. Change-Id: I0c5afad4b404ec485f50b1342b81e70ef85a5163 Signed-off-by: Mauro Sardara Signed-off-by: michele papalini --- hicn-light/src/io/udpConnection.c | 94 +++++++++++++-------------------------- 1 file changed, 30 insertions(+), 64 deletions(-) (limited to 'hicn-light/src/io/udpConnection.c') diff --git a/hicn-light/src/io/udpConnection.c b/hicn-light/src/io/udpConnection.c index 6c2e35392..3faf2bfac 100644 --- a/hicn-light/src/io/udpConnection.c +++ b/hicn-light/src/io/udpConnection.c @@ -19,6 +19,7 @@ * NB The Send() function may overflow the output buffer * */ +#include #include #include @@ -58,6 +59,7 @@ typedef struct udp_state { // Prototypes static bool _send(IoOperations *ops, const Address *nexthop, Message *message); +static bool _sendCommandResponse(IoOperations *ops, struct iovec *message); static const Address *_getRemoteAddress(const IoOperations *ops); static const AddressPair *_getAddressPair(const IoOperations *ops); static unsigned _getConnectionId(const IoOperations *ops); @@ -82,6 +84,7 @@ static const void *_streamConnection_Class(const IoOperations *ops) { static IoOperations _template = {.closure = NULL, .send = &_send, + .sendCommandResponse = &_sendCommandResponse, .getRemoteAddress = &_getRemoteAddress, .getAddressPair = &_getAddressPair, .getConnectionId = &_getConnectionId, @@ -239,38 +242,6 @@ static bool _send(IoOperations *ops, const Address *dummy, Message *message) { // in this particular connection we don't need natting beacause we send the // packet to the next hop using upd connection -#if 0 - if((hicnConnState->peerAddressLength == sizeof(struct sockaddr_in)) || (hicnConnState->localAddressLength == sizeof(struct sockaddr_in))) - return false; - - if(message_GetType(message) = MessagePacketType_ContentObject){ - //this is a data packet. We need to put the remote address in the destination field - messageHandler_SetDestination_IPv6((uint8_t *) message_FixedHeader(message), - &((struct sockaddr_in6 *) hicnConnState->peerAddress)->sin6_addr); - - } else if (message_GetType(message) == MessagePacketType_Interest) { - //this si an interest packet. We need to put the local address in the source field - messageHandler_SetSource_IPv6((uint8_t *) message_FixedHeader(message), - &((struct sockaddr_in6 *) hicnConnState->localAddress)->sin6_addr); - - //only in this case we may need to set the probeDestAddress - if(hicnConnState->refreshProbeDestAddress){ - _refreshProbeDestAddress(hicnConnState, message_FixedHeader(message)); - } - - } else if (message_GetType(message) == MessagePacketType_WldrNotification) { - //here we don't need to do anything for now - }else{ - //unkown packet - if (logger_IsLoggable(hicnConnState->logger, LoggerFacility_IO, PARCLogLevel_Debug)) { - logger_Log(hicnConnState->logger, LoggerFacility_IO, PARCLogLevel_Debug, __func__, - "connid %u can't parse the message", - hicnConnState->id); - } - return false; - } -#endif - ssize_t writeLength = sendto(udpConnState->udpListenerSocket, message_FixedHeader(message), (int)message_Length(message), 0, udpConnState->peerAddress, @@ -290,44 +261,39 @@ static bool _send(IoOperations *ops, const Address *dummy, Message *message) { return true; } +static bool _sendCommandResponse(IoOperations *ops, struct iovec *message){ + parcAssertNotNull(ops, "Parameter ops must be non-null"); + parcAssertNotNull(message, "Parameter message must be non-null"); + _UdpState *udpConnState = (_UdpState *)ioOperations_GetClosure(ops); + + // Perform connect before to establish association between this peer and + // the remote peer. This is required to use writev. + // Connection association can be changed at any time. + connect(udpConnState->udpListenerSocket, + udpConnState->peerAddress, + udpConnState->peerAddressLength); + + ssize_t writeLength = writev(udpConnState->udpListenerSocket, message, 2); + + struct sockaddr any_address = {0}; + any_address.sa_family = AF_UNSPEC; + connect(udpConnState->udpListenerSocket, + &any_address, (socklen_t)sizeof(any_address)); + + if (writeLength < 0) { + return false; + } + + return true; +} + static list_connections_type _getConnectionType(const IoOperations *ops) { return CONN_UDP; } static Ticks _sendProbe(IoOperations *ops, unsigned probeType, uint8_t *message) { -#if 0 - parcAssertNotNull(ops, "Parameter ops must be non-null"); - _MetisUdpState *udpConnState = (_MetisUdpState *) metisIoOperations_GetClosure(ops); - - - uint8_t *pkt; - size_t pkt_size = 8; - pkt = (uint8_t *) malloc(sizeof(uint8_t) * pkt_size); - for (unsigned i = 0; i < pkt_size; i++) { - pkt[i] = 0; - } - pkt[0] = 1; //type - pkt[1] = probeType; //packet type - pkt[6] = 8; //header len (16bit, network order) - - ssize_t writeLen = sendto(udpConnState->udpListenerSocket, pkt, pkt_size, 0, udpConnState->peerAddress, udpConnState->peerAddressLength); - - if (writeLen < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - free(pkt); - return 0; - } else { - //this print is for debugging - printf("Incorrect write length %zd, expected %zd: (%d) %s\n", writeLen, pkt_size, errno, strerror(errno)); - free(pkt); - return 0; - } - } - - free(pkt); - return metisForwarder_GetTicks(udpConnState->metis); -#endif + //TODO return 0; } -- cgit 1.2.3-korg