mbed socket API

Dependents:   EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more

Deprecated

This is an mbed 2 sockets library. For mbed 5, network sockets have been revised to better support additional network stacks and thread safety here.

UDPSocket.cpp

Committer:
emilmont
Date:
2012-07-25
Revision:
3:e6474399e057
Parent:
2:b227d242f3c7
Child:
5:300e7ad2dc1d

File content as of revision 3:e6474399e057:

/* Copyright (C) 2012 mbed.org, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "UDPSocket.h"

#include <cstring>

using std::memset;

UDPSocket::UDPSocket() {
}

// Server initialization
int UDPSocket::bind(int port) {
    if (init(SOCK_DGRAM) < 0)
        return -1;
    
    struct sockaddr_in localHost;
    std::memset(&localHost, 0, sizeof(localHost));
    
    localHost.sin_family = AF_INET;
    localHost.sin_port = htons(port);
    localHost.sin_addr.s_addr = INADDR_ANY;
    
    if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
        close();
        return -1;
    }
    
    return 0;
}

// -1 if unsuccessful, else number of bytes written
int UDPSocket::sendTo(char* data, int length, char* host, int port, int timeout) {
    if (_sock_fd < 0)
        return -1;
    
    struct sockaddr_in remoteHost;
    
    //Populate m_remoteHost
    std::memset(&remoteHost, 0, sizeof(struct sockaddr_in));
    
    //Resolve DNS address or populate hard-coded IP address
    struct hostent *server = ::gethostbyname(host);
    if (server == NULL) {
        return -1; //Could not resolve address
    }
    std::memcpy((char*) &remoteHost.sin_addr.s_addr,
            (char*) server->h_addr_list[0], server->h_length);
    
    remoteHost.sin_family = AF_INET;
    remoteHost.sin_port = htons(port);
    
    size_t writtenLen = 0;
    set_timeout(timeout);
    while (writtenLen < length) {
        //Wait for socket to be writeable
        //Creating FS set
        if (wait_writable() != 0)
            return writtenLen; //Timeout -- FIXME should we return -1 or writtenLength ?
        
        int ret = lwip_sendto(_sock_fd, data + writtenLen, length - writtenLen, 0,
                (const struct sockaddr *) &remoteHost, sizeof(remoteHost));
        if (ret > 0) {
            writtenLen += ret;
        } else if (ret == 0) {
            return writtenLen; //Connection was closed by server -- FIXME how do we signal that the connection was closed ?
        } else {
            return -1; //Connnection error
        }
    }
    
    return writtenLen;
}

// -1 if unsuccessful, else number of bytes received
int UDPSocket::receiveFrom(char* data, int length, char** host, int* port, int timeout) {
    if (_sock_fd < 0)
        return -1;
    
    set_timeout(timeout);
    if (wait_readable() != 0)
        return -1;
    
    struct sockaddr_in remoteHost;
    std::memset(&remoteHost, 0, sizeof(struct sockaddr_in));
    
    socklen_t remoteHostLen = sizeof(remoteHost);
    int n = lwip_recvfrom(_sock_fd, data, length, 0, (struct sockaddr*) &remoteHost, &remoteHostLen);
    if (n < 0)
        return -1;
    
    static char hostBuf[16];
    inet_ntoa_r(remoteHost.sin_addr, hostBuf, sizeof(hostBuf));
    
    *host = hostBuf;
    *port = ntohs(remoteHost.sin_port);
    
    return n;
}

UDPSocket::~UDPSocket() {
    close(); //Don't want to leak
}