lwip_sockets.c 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
 * File      : lwip_sockets.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2015, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2015-02-17     Bernard      First version
 */

#include <dfs.h>
#include <dfs_def.h>
#include <lwip/sockets.h>
#include <sys/socket.h>

#include "dfs_lwip.h"

int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
    int sock = dfs_lwip_getsocket(s);

    return lwip_accept(sock, addr, addrlen);
}

int bind(int s, const struct sockaddr *name, socklen_t namelen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_bind(sock, name, namelen);
}

int shutdown(int s, int how)
{
    int sock = dfs_lwip_getsocket(s);

    return lwip_shutdown(s, how);
}

int getpeername (int s, struct sockaddr *name, socklen_t *namelen)
{
    int sock = dfs_lwip_getsocket(s);

    return lwip_getpeername(sock, name, namelen);
}

int getsockname (int s, struct sockaddr *name, socklen_t *namelen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_getsockname(sock, name, namelen);
}

int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_getsockopt(sock, level, optname, optval, optlen);
}

int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_setsockopt(sock, level, optname, optval, optlen);
}

int connect(int s, const struct sockaddr *name, socklen_t namelen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_connect(sock, name, namelen);
}

int listen(int s, int backlog)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_listen(sock, backlog);
}

int recv(int s, void *mem, size_t len, int flags)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_recv(sock, mem, len, flags);
}

int recvfrom(int s, void *mem, size_t len, int flags,
      struct sockaddr *from, socklen_t *fromlen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_recvfrom(sock, mem, len, flags, from, fromlen);
}

int send(int s, const void *dataptr, size_t size, int flags)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_send(sock, dataptr, size, flags);
}

int sendto(int s, const void *dataptr, size_t size, int flags,
    const struct sockaddr *to, socklen_t tolen)
{
    int sock = dfs_lwip_getsocket(s);
    
    return lwip_sendto(sock, dataptr, size, flags, to, tolen);
}

int socket(int domain, int type, int protocol)
{
    /* create a BSD socket */
    int fd;
    int sock;
    struct dfs_fd *d;

    /* allocate a fd */
    fd = fd_new();
    if (fd < 0)
    {
        rt_set_errno(-DFS_STATUS_ENOMEM);

        return -1;
    }
    d = fd_get(fd);

    /* create socket in lwip and then put it to the dfs_fd */
    sock = lwip_socket(domain, type, protocol);
    if (sock > 0)
    {
        /* this is a socket fd */
        d->type = FT_SOCKET;
        d->path = RT_NULL;

        d->fs = dfs_lwip_get_fs();
        
        d->flags = DFS_O_RDWR; /* set flags as read and write */
        d->size = 0;
        d->pos  = 0;
        
        /* set socket to the data of dfs_fd */
        d->data = (void*) sock;
    }

    /* release the ref-count of fd */
    fd_put(d);

    return fd;
}