Changes in / [ee1c2d9:0453261] in mainline


Ignore:
Files:
47 added
17 deleted
63 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/synch/futex.c

    ree1c2d9 r0453261  
    400400                return (sysarg_t) ENOENT;
    401401
    402 #ifdef CONFIG_UDEBUG
    403         udebug_stoppable_begin();
    404 #endif
    405 
    406402        int rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
    407 
    408 #ifdef CONFIG_UDEBUG
    409         udebug_stoppable_end();
    410 #endif
    411403
    412404        return (sysarg_t) rc;
  • uspace/Makefile

    ree1c2d9 r0453261  
    7777        app/usbinfo \
    7878        app/vuhid \
     79        app/netecho \
     80        app/netspeed \
     81        app/nettest1 \
     82        app/nettest2 \
     83        app/nettest3 \
    7984        app/nic \
    8085        app/ping \
     
    229234        lib/draw \
    230235        lib/math \
    231         lib/nettl \
     236        lib/net \
    232237        lib/nic \
    233238        lib/ext4 \
  • uspace/Makefile.common

    ree1c2d9 r0453261  
    142142LIBHOUND_PREFIX = $(LIB_PREFIX)/hound
    143143LIBPCM_PREFIX = $(LIB_PREFIX)/pcm
     144LIBNET_PREFIX = $(LIB_PREFIX)/net
    144145LIBNIC_PREFIX = $(LIB_PREFIX)/nic
    145146LIBIEEE80211_PREFIX = $(LIB_PREFIX)/ieee80211
     
    157158LIBMBR_PREFIX = $(LIB_PREFIX)/mbr
    158159LIBGPT_PREFIX = $(LIB_PREFIX)/gpt
    159 LIBNETTL_PREFIX = $(LIB_PREFIX)/nettl
    160160
    161161LIBURCU_PREFIX = $(LIB_PREFIX)/urcu
     
    264264
    265265ifeq ($(CONFIG_LINE_DEBUG),y)
    266         GCC_CFLAGS += -ggdb
     266        GCC_CFLAGS += -g
    267267        ICC_CFLAGS += -g
    268268        CLANG_CFLAGS += -g
  • uspace/app/download/main.c

    ree1c2d9 r0453261  
    3636 */
    3737
    38 #include <errno.h>
    3938#include <stdio.h>
    4039#include <stdlib.h>
     
    4342#include <task.h>
    4443#include <macros.h>
     44
     45#include <net/in.h>
     46#include <net/inet.h>
     47#include <net/socket.h>
    4548
    4649#include <http/http.h>
  • uspace/app/nterm/conn.c

    ree1c2d9 r0453261  
    3838#include <fibril.h>
    3939#include <inet/dnsr.h>
    40 #include <inet/endpoint.h>
    41 #include <inet/tcp.h>
     40#include <net/inet.h>
     41#include <net/socket.h>
    4242#include <stdio.h>
    4343#include <stdlib.h>
     
    4848#include "nterm.h"
    4949
    50 static tcp_t *tcp;
    51 static tcp_conn_t *conn;
     50static int conn_fd;
     51static fid_t rcv_fid;
    5252
    5353#define RECV_BUF_SIZE 1024
    5454static uint8_t recv_buf[RECV_BUF_SIZE];
    5555
    56 static void conn_conn_reset(tcp_conn_t *);
    57 static void conn_data_avail(tcp_conn_t *);
    58 
    59 static tcp_cb_t conn_cb = {
    60         .conn_reset = conn_conn_reset,
    61         .data_avail = conn_data_avail
    62 };
    63 
    64 static void conn_conn_reset(tcp_conn_t *conn)
     56static int rcv_fibril(void *arg)
    6557{
    66         printf("\n[Connection reset]\n");
    67 }
    68 
    69 static void conn_data_avail(tcp_conn_t *conn)
    70 {
    71         int rc;
    72         size_t nrecv;
     58        ssize_t nr;
    7359
    7460        while (true) {
    75                 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv);
    76                 if (rc != EOK) {
    77                         printf("\n[Receive error %d]\n", rc);
     61                nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0);
     62                if (nr < 0)
    7863                        break;
    79                 }
    8064
    81                 nterm_received(recv_buf, nrecv);
     65                nterm_received(recv_buf, nr);
     66        }
    8267
    83                 if (nrecv != RECV_BUF_SIZE)
    84                         break;
    85         }
     68        if (nr == ENOTCONN)
     69                printf("\n[Other side has closed the connection]\n");
     70        else
     71                printf("'\n[Receive errror (%s)]\n", str_error(nr));
     72
     73        exit(0);
     74        return 0;
    8675}
    8776
    8877int conn_open(const char *host, const char *port_s)
    8978{
    90         inet_ep2_t epp;
    91 
     79        int conn_fd = -1;
     80        struct sockaddr *saddr = NULL;
     81        socklen_t saddrlen;
     82       
    9283        /* Interpret as address */
    9384        inet_addr_t iaddr;
    9485        int rc = inet_addr_parse(host, &iaddr);
    95 
     86       
    9687        if (rc != EOK) {
    9788                /* Interpret as a host name */
    9889                dnsr_hostinfo_t *hinfo = NULL;
    9990                rc = dnsr_name2host(host, &hinfo, ip_any);
    100 
     91               
    10192                if (rc != EOK) {
    10293                        printf("Error resolving host '%s'.\n", host);
    10394                        goto error;
    10495                }
    105 
     96               
    10697                iaddr = hinfo->addr;
    10798        }
    108 
     99       
    109100        char *endptr;
    110101        uint16_t port = strtol(port_s, &endptr, 10);
     
    113104                goto error;
    114105        }
    115 
    116         inet_ep2_init(&epp);
    117         epp.remote.addr = iaddr;
    118         epp.remote.port = port;
    119 
     106       
     107        rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen);
     108        if (rc != EOK) {
     109                assert(rc == ENOMEM);
     110                printf("Out of memory.\n");
     111                return ENOMEM;
     112        }
     113       
    120114        printf("Connecting to host %s port %u\n", host, port);
    121 
    122         rc = tcp_create(&tcp);
     115       
     116        conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0);
     117        if (conn_fd < 0)
     118                goto error;
     119       
     120        rc = connect(conn_fd, saddr, saddrlen);
    123121        if (rc != EOK)
    124122                goto error;
    125 
    126         rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
    127         if (rc != EOK)
     123       
     124        rcv_fid = fibril_create(rcv_fibril, NULL);
     125        if (rcv_fid == 0)
    128126                goto error;
    129 
    130         rc = tcp_conn_wait_connected(conn);
    131         if (rc != EOK)
    132                 goto error;
    133 
     127       
     128        fibril_add_ready(rcv_fid);
     129       
     130        free(saddr);
    134131        return EOK;
    135132error:
    136         tcp_conn_destroy(conn);
    137         tcp_destroy(tcp);
    138 
     133        if (conn_fd >= 0) {
     134                closesocket(conn_fd);
     135                conn_fd = -1;
     136        }
     137        free(saddr);
     138       
    139139        return EIO;
    140140}
     
    142142int conn_send(void *data, size_t size)
    143143{
    144         int rc = tcp_conn_send(conn, data, size);
     144        int rc = send(conn_fd, data, size, 0);
    145145        if (rc != EOK)
    146146                return EIO;
    147 
     147       
    148148        return EOK;
    149149}
  • uspace/app/websrv/websrv.c

    ree1c2d9 r0453261  
    4444#include <task.h>
    4545
    46 #include <inet/addr.h>
    47 #include <inet/endpoint.h>
    48 #include <inet/tcp.h>
     46#include <net/in.h>
     47#include <net/inet.h>
     48#include <net/socket.h>
    4949
    5050#include <arg_parse.h>
     
    5656
    5757#define DEFAULT_PORT  8080
     58#define BACKLOG_SIZE  3
    5859
    5960#define WEB_ROOT  "/data/web"
     
    6162/** Buffer for receiving the request. */
    6263#define BUFFER_SIZE  1024
    63 
    64 static void websrv_new_conn(tcp_listener_t *, tcp_conn_t *);
    65 
    66 static tcp_listen_cb_t listen_cb = {
    67         .new_conn = websrv_new_conn
    68 };
    69 
    70 static tcp_cb_t conn_cb = {
    71         .connected = NULL
    72 };
    7364
    7465static uint16_t port = DEFAULT_PORT;
     
    131122
    132123/** Receive one character (with buffering) */
    133 static int recv_char(tcp_conn_t *conn, char *c)
    134 {
    135         size_t nrecv;
    136         int rc;
    137        
     124static int recv_char(int fd, char *c)
     125{
    138126        if (rbuf_out == rbuf_in) {
    139127                rbuf_out = 0;
    140128                rbuf_in = 0;
    141129               
    142                 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv);
    143                 if (rc != EOK) {
    144                         fprintf(stderr, "tcp_conn_recv() failed (%d)\n", rc);
     130                ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);
     131                if (rc <= 0) {
     132                        fprintf(stderr, "recv() failed (%zd)\n", rc);
    145133                        return rc;
    146134                }
    147135               
    148                 rbuf_in = nrecv;
     136                rbuf_in = rc;
    149137        }
    150138       
     
    154142
    155143/** Receive one line with length limit */
    156 static int recv_line(tcp_conn_t *conn)
     144static int recv_line(int fd)
    157145{
    158146        char *bp = lbuf;
     
    161149        while (bp < lbuf + BUFFER_SIZE) {
    162150                char prev = c;
    163                 int rc = recv_char(conn, &c);
     151                int rc = recv_char(fd, &c);
    164152               
    165153                if (rc != EOK)
     
    199187}
    200188
    201 static int send_response(tcp_conn_t *conn, const char *msg)
     189static int send_response(int conn_sd, const char *msg)
    202190{
    203191        size_t response_size = str_size(msg);
     
    206194            fprintf(stderr, "Sending response\n");
    207195       
    208         int rc = tcp_conn_send(conn, (void *) msg, response_size);
    209         if (rc != EOK) {
    210                 fprintf(stderr, "tcp_conn_send() failed\n");
     196        ssize_t rc = send(conn_sd, (void *) msg, response_size, 0);
     197        if (rc < 0) {
     198                fprintf(stderr, "send() failed\n");
    211199                return rc;
    212200        }
     
    215203}
    216204
    217 static int uri_get(const char *uri, tcp_conn_t *conn)
     205static int uri_get(const char *uri, int conn_sd)
    218206{
    219207        if (str_cmp(uri, "/") == 0)
     
    227215        int fd = open(fname, O_RDONLY);
    228216        if (fd < 0) {
    229                 rc = send_response(conn, msg_not_found);
     217                rc = send_response(conn_sd, msg_not_found);
    230218                free(fname);
    231219                return rc;
     
    234222        free(fname);
    235223       
    236         rc = send_response(conn, msg_ok);
     224        rc = send_response(conn_sd, msg_ok);
    237225        if (rc != EOK)
    238226                return rc;
     
    248236                }
    249237               
    250                 rc = tcp_conn_send(conn, fbuf, nr);
    251                 if (rc != EOK) {
    252                         fprintf(stderr, "tcp_conn_send() failed\n");
     238                rc = send(conn_sd, fbuf, nr, 0);
     239                if (rc < 0) {
     240                        fprintf(stderr, "send() failed\n");
    253241                        close(fd);
    254242                        return rc;
     
    261249}
    262250
    263 static int req_process(tcp_conn_t *conn)
    264 {
    265         int rc = recv_line(conn);
     251static int req_process(int conn_sd)
     252{
     253        int rc = recv_line(conn_sd);
    266254        if (rc != EOK) {
    267255                fprintf(stderr, "recv_line() failed\n");
     
    273261       
    274262        if (str_lcmp(lbuf, "GET ", 4) != 0) {
    275                 rc = send_response(conn, msg_not_implemented);
     263                rc = send_response(conn_sd, msg_not_implemented);
    276264                return rc;
    277265        }
     
    289277       
    290278        if (!uri_is_valid(uri)) {
    291                 rc = send_response(conn, msg_bad_request);
    292                 return rc;
    293         }
    294        
    295         return uri_get(uri, conn);
     279                rc = send_response(conn_sd, msg_bad_request);
     280                return rc;
     281        }
     282       
     283        return uri_get(uri, conn_sd);
    296284}
    297285
     
    358346}
    359347
    360 static void websrv_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
    361 {
    362         int rc;
    363        
    364         if (verbose)
    365                 fprintf(stderr, "New connection, waiting for request\n");
    366        
    367         rbuf_out = 0;
    368         rbuf_in = 0;
    369        
    370         rc = req_process(conn);
    371         if (rc != EOK) {
    372                 fprintf(stderr, "Error processing request (%s)\n",
    373                     str_error(rc));
    374                 return;
    375         }
    376 
    377         rc = tcp_conn_send_fin(conn);
    378         if (rc != EOK) {
    379                 fprintf(stderr, "Error sending FIN.\n");
    380                 return;
    381         }
    382 }
    383 
    384348int main(int argc, char *argv[])
    385349{
    386         inet_ep_t ep;
    387         tcp_listener_t *lst;
    388         tcp_t *tcp;
    389         int rc;
    390        
    391350        /* Parse command line arguments */
    392351        for (int i = 1; i < argc; i++) {
    393352                if (argv[i][0] == '-') {
    394                         rc = parse_option(argc, argv, &i);
     353                        int rc = parse_option(argc, argv, &i);
    395354                        if (rc != EOK)
    396355                                return rc;
     
    401360        }
    402361       
     362        struct sockaddr_in addr;
     363       
     364        addr.sin_family = AF_INET;
     365        addr.sin_port = htons(port);
     366       
     367        int rc = inet_pton(AF_INET, "127.0.0.1", (void *)
     368            &addr.sin_addr.s_addr);
     369        if (rc != EOK) {
     370                fprintf(stderr, "Error parsing network address (%s)\n",
     371                    str_error(rc));
     372                return 1;
     373        }
     374       
    403375        printf("%s: HelenOS web server\n", NAME);
    404376
    405377        if (verbose)
    406                 fprintf(stderr, "Creating listener\n");
    407        
    408         inet_ep_init(&ep);
    409         ep.port = port;
    410 
    411         rc = tcp_create(&tcp);
     378                fprintf(stderr, "Creating socket\n");
     379       
     380        int listen_sd = socket(PF_INET, SOCK_STREAM, 0);
     381        if (listen_sd < 0) {
     382                fprintf(stderr, "Error creating listening socket (%s)\n",
     383                    str_error(listen_sd));
     384                return 2;
     385        }
     386       
     387        rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
    412388        if (rc != EOK) {
    413                 fprintf(stderr, "Error initializing TCP.\n");
    414                 return 1;
    415         }
    416 
    417         rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
    418             &lst);
     389                fprintf(stderr, "Error binding socket (%s)\n",
     390                    str_error(rc));
     391                return 3;
     392        }
     393       
     394        rc = listen(listen_sd, BACKLOG_SIZE);
    419395        if (rc != EOK) {
    420                 fprintf(stderr, "Error creating listener.\n");
    421                 return 2;
     396                fprintf(stderr, "listen() failed (%s)\n", str_error(rc));
     397                return 4;
    422398        }
    423399       
     
    426402
    427403        task_retval(0);
    428         async_manager();
     404
     405        while (true) {
     406                struct sockaddr_in raddr;
     407                socklen_t raddr_len = sizeof(raddr);
     408                int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr,
     409                    &raddr_len);
     410               
     411                if (conn_sd < 0) {
     412                        fprintf(stderr, "accept() failed (%s)\n", str_error(rc));
     413                        continue;
     414                }
     415               
     416                if (verbose) {
     417                        fprintf(stderr, "Connection accepted (sd=%d), "
     418                            "waiting for request\n", conn_sd);
     419                }
     420               
     421                rbuf_out = 0;
     422                rbuf_in = 0;
     423               
     424                rc = req_process(conn_sd);
     425                if (rc != EOK)
     426                        fprintf(stderr, "Error processing request (%s)\n",
     427                            str_error(rc));
     428               
     429                rc = closesocket(conn_sd);
     430                if (rc != EOK) {
     431                        fprintf(stderr, "Error closing connection socket (%s)\n",
     432                            str_error(rc));
     433                        closesocket(listen_sd);
     434                        return 5;
     435                }
     436               
     437                if (verbose)
     438                        fprintf(stderr, "Connection closed\n");
     439        }
    429440       
    430441        /* Not reached */
  • uspace/drv/bus/usb/usbhub/usbhub.c

    ree1c2d9 r0453261  
    191191                if (!tries--) {
    192192                        usb_log_error("Can't remove hub, still running.\n");
    193                         return EBUSY;
     193                        return EINPROGRESS;
    194194                }
    195195        }
  • uspace/lib/c/Makefile

    ree1c2d9 r0453261  
    9797        generic/futex.c \
    9898        generic/inet/addr.c \
    99         generic/inet/endpoint.c \
    100         generic/inet/tcp.c \
    101         generic/inet/udp.c \
    10299        generic/inet.c \
    103100        generic/inetcfg.c \
     
    140137        generic/adt/list.c \
    141138        generic/adt/hash_table.c \
     139        generic/adt/dynamic_fifo.c \
     140        generic/adt/char_map.c \
    142141        generic/adt/prodcons.c \
    143142        generic/time.c \
     
    146145        generic/vfs/vfs.c \
    147146        generic/vfs/canonify.c \
     147        generic/net/inet.c \
     148        generic/net/socket_client.c \
     149        generic/net/socket_parse.c \
    148150        generic/setjmp.c \
    149151        generic/stack.c \
  • uspace/lib/c/generic/fibril_synch.c

    ree1c2d9 r0453261  
    461461                        if (rc == ETIMEOUT && timer->state == fts_active) {
    462462                                timer->state = fts_fired;
    463                                 timer->handler_fid = fibril_get_id();
     463                                timer->handler_running = true;
    464464                                fibril_mutex_unlock(timer->lockp);
    465465                                timer->fun(timer->arg);
    466466                                fibril_mutex_lock(timer->lockp);
    467                                 timer->handler_fid = 0;
     467                                timer->handler_running = false;
    468468                        }
    469469                        break;
     
    477477        /* Acknowledge timer fibril has finished cleanup. */
    478478        timer->state = fts_clean;
    479         fibril_condvar_broadcast(&timer->cv);
    480479        fibril_mutex_unlock(timer->lockp);
     480        free(timer);
    481481
    482482        return 0;
     
    525525        timer->state = fts_cleanup;
    526526        fibril_condvar_broadcast(&timer->cv);
    527 
    528         /* Wait for timer fibril to terminate */
    529         while (timer->state != fts_clean)
    530                 fibril_condvar_wait(&timer->cv, timer->lockp);
    531527        fibril_mutex_unlock(timer->lockp);
    532 
    533         free(timer);
    534528}
    535529
     
    614608        assert(fibril_mutex_is_locked(timer->lockp));
    615609
    616         while (timer->handler_fid != 0) {
    617                 if (timer->handler_fid == fibril_get_id()) {
    618                         printf("Deadlock detected.\n");
    619                         stacktrace_print();
    620                         printf("Fibril %zx is trying to clear timer %p from "
    621                             "inside its handler %p.\n",
    622                             fibril_get_id(), timer, timer->fun);
    623                         abort();
    624                 }
    625 
     610        while (timer->handler_running)
    626611                fibril_condvar_wait(&timer->cv, timer->lockp);
    627         }
    628612
    629613        old_state = timer->state;
  • uspace/lib/c/generic/inet.c

    ree1c2d9 r0453261  
    177177       
    178178        dgram.tos = IPC_GET_ARG1(*icall);
    179         dgram.iplink = IPC_GET_ARG2(*icall);
    180179       
    181180        ipc_callid_t callid;
  • uspace/lib/c/generic/inet/addr.c

    ree1c2d9 r0453261  
    11/*
    22 * Copyright (c) 2013 Jiri Svoboda
    3  * Copyright (c) 2013 Martin Decky
    43 * All rights reserved.
    54 *
     
    3736#include <errno.h>
    3837#include <unistd.h>
     38#include <net/socket_codes.h>
    3939#include <inet/addr.h>
     40#include <net/inet.h>
    4041#include <stdio.h>
    4142#include <malloc.h>
     
    4344
    4445#define INET_PREFIXSTRSIZE  5
    45 
    46 #define INET6_ADDRSTRLEN (8 * 4 + 7 + 1)
    4746
    4847#if !(defined(__BE__) ^ defined(__LE__))
     
    181180}
    182181
     182/** Determine address version.
     183 *
     184 * @param text Address in common notation.
     185 * @param af   Place to store address version.
     186 *
     187 * @return EOK on success, EINVAL if input is not in valid format.
     188 *
     189 */
     190static int inet_addr_version(const char *text, ip_ver_t *ver)
     191{
     192        char *dot = str_chr(text, '.');
     193        if (dot != NULL) {
     194                *ver = ip_v4;
     195                return EOK;
     196        }
     197
     198        char *collon = str_chr(text, ':');
     199        if (collon != NULL) {
     200                *ver = ip_v6;
     201                return EOK;
     202        }
     203
     204        return EINVAL;
     205}
     206
     207static int ipver_af(ip_ver_t ver)
     208{
     209        switch (ver) {
     210        case ip_any:
     211                return AF_NONE;
     212        case ip_v4:
     213                return AF_INET;
     214        case ip_v6:
     215                return AF_INET6;
     216        default:
     217                assert(false);
     218                return EINVAL;
     219        }
     220}
     221
     222ip_ver_t ipver_from_af(int af)
     223{
     224        switch (af) {
     225        case AF_NONE:
     226                return ip_any;
     227        case AF_INET:
     228                return ip_v4;
     229        case AF_INET6:
     230                return ip_v6;
     231        default:
     232                assert(false);
     233                return EINVAL;
     234        }
     235}
     236
    183237void inet_naddr_addr(const inet_naddr_t *naddr, inet_addr_t *addr)
    184238{
     
    234288        if (naddr->version != addr->version)
    235289                return 0;
    236 
     290       
    237291        switch (naddr->version) {
    238292        case ip_v4:
     
    261315                if (naddr->prefix > 128)
    262316                        return 0;
    263 
     317               
    264318                size_t pos = 0;
    265319                for (size_t i = 0; i < 16; i++) {
     
    267321                        if (naddr->prefix < pos)
    268322                                break;
    269 
     323                       
    270324                        if (naddr->prefix - pos > 8) {
    271325                                /* Comparison without masking */
     
    279333                                        return 0;
    280334                        }
    281 
     335                       
    282336                        pos += 8;
    283337                }
    284 
     338               
    285339                return 1;
    286340        default:
    287341                return 0;
    288342        }
    289 }
    290 
    291 static int inet_addr_parse_v4(const char *str, inet_addr_t *raddr,
    292     int *prefix)
    293 {
    294         uint32_t a = 0;
    295         uint8_t b;
    296         char *cur = (char *)str;
    297         size_t i = 0;
    298 
    299         while (i < 4) {
    300                 int rc = str_uint8_t(cur, (const char **)&cur, 10, false, &b);
    301                 if (rc != EOK)
    302                         return rc;
    303 
    304                 a = (a << 8) + b;
    305 
    306                 i++;
    307 
    308                 if (*cur == '\0')
    309                         break;
    310 
    311                 if (*cur != '.')
    312                         return EINVAL;
    313 
    314                 if (i < 4)
    315                         cur++;
    316         }
    317 
    318         if (prefix != NULL) {
    319                 *prefix = strtoul(cur, &cur, 10);
    320                 if (*prefix > 32)
    321                         return EINVAL;
    322         }
    323 
    324         if (i != 4 || (*cur != '\0'))
    325                 return EINVAL;
    326 
    327         raddr->version = ip_v4;
    328         raddr->addr = a;
    329 
    330         return EOK;
    331 }
    332 
    333 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix)
    334 {
    335         uint8_t data[16];
    336 
    337         memset(data, 0, 16);
    338 
    339         const char *cur = str;
    340         size_t i = 0;
    341         size_t wildcard_pos = (size_t) -1;
    342         size_t wildcard_size = 0;
    343 
    344         /* Handle initial wildcard */
    345         if ((str[0] == ':') && (str[1] == ':')) {
    346                 cur = str + 2;
    347                 wildcard_pos = 0;
    348                 wildcard_size = 16;
    349 
    350                 /* Handle the unspecified address */
    351                 if (*cur == '\0')
    352                         goto success;
    353         }
    354 
    355         while (i < 16) {
    356                 uint16_t bioctet;
    357                 int rc = str_uint16_t(cur, &cur, 16, false, &bioctet);
    358                 if (rc != EOK)
    359                         return rc;
    360 
    361                 data[i] = (bioctet >> 8) & 0xff;
    362                 data[i + 1] = bioctet & 0xff;
    363 
    364                 if (wildcard_pos != (size_t) -1) {
    365                         if (wildcard_size < 2)
    366                                 return EINVAL;
    367 
    368                         wildcard_size -= 2;
    369                 }
    370 
    371                 i += 2;
    372 
    373                 if (*cur != ':')
    374                         break;
    375 
    376                 if (i < 16) {
    377                         cur++;
    378 
    379                         /* Handle wildcard */
    380                         if (*cur == ':') {
    381                                 if (wildcard_pos != (size_t) -1)
    382                                         return EINVAL;
    383 
    384                                 wildcard_pos = i;
    385                                 wildcard_size = 16 - i;
    386                                 cur++;
    387 
    388                                 if (*cur == '\0' || *cur == '/')
    389                                         break;
    390                         }
    391                 }
    392         }
    393 
    394         if (prefix != NULL) {
    395                 if (*cur != '/')
    396                         return EINVAL;
    397                 cur++;
    398 
    399                 *prefix = strtoul(cur, (char **)&cur, 10);
    400                 if (*prefix > 128)
    401                         return EINVAL;
    402         }
    403 
    404         if (*cur != '\0')
    405                 return EINVAL;
    406 
    407         /* Create wildcard positions */
    408         if ((wildcard_pos != (size_t) -1) && (wildcard_size > 0)) {
    409                 size_t wildcard_shift = 16 - wildcard_size;
    410 
    411                 for (i = wildcard_pos + wildcard_shift; i > wildcard_pos; i--) {
    412                         size_t j = i - 1;
    413                         data[j + wildcard_size] = data[j];
    414                         data[j] = 0;
    415                 }
    416         }
    417 
    418 success:
    419         raddr->version = ip_v6;
    420         memcpy(raddr->addr6, data, 16);
    421         return EOK;
    422343}
    423344
     
    432353int inet_addr_parse(const char *text, inet_addr_t *addr)
    433354{
    434         int rc;
    435 
    436         rc = inet_addr_parse_v4(text, addr, NULL);
    437         if (rc == EOK)
    438                 return EOK;
    439 
    440         rc = inet_addr_parse_v6(text, addr, NULL);
    441         if (rc == EOK)
    442                 return EOK;
    443 
    444         return EINVAL;
     355        int rc = inet_addr_version(text, &addr->version);
     356        if (rc != EOK)
     357                return rc;
     358       
     359        uint8_t buf[16];
     360        rc = inet_pton(ipver_af(addr->version), text, buf);
     361        if (rc != EOK)
     362                return rc;
     363       
     364        switch (addr->version) {
     365        case ip_v4:
     366                addr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) |
     367                    buf[3];
     368                break;
     369        case ip_v6:
     370                memcpy(addr->addr6, buf, 16);
     371                break;
     372        default:
     373                return EINVAL;
     374        }
     375       
     376        return EOK;
    445377}
    446378
     
    455387int inet_naddr_parse(const char *text, inet_naddr_t *naddr)
    456388{
    457         int rc;
    458         inet_addr_t addr;
    459         int prefix;
    460 
    461         rc = inet_addr_parse_v4(text, &addr, &prefix);
    462         if (rc == EOK) {
    463                 inet_addr_naddr(&addr, prefix, naddr);
    464                 return EOK;
    465         }
    466 
    467         rc = inet_addr_parse_v6(text, &addr, &prefix);
    468         if (rc == EOK) {
    469                 inet_addr_naddr(&addr, prefix, naddr);
    470                 return EOK;
    471         }
    472 
    473         return EINVAL;
    474 }
    475 
    476 static int inet_addr_format_v4(addr32_t addr, char **bufp)
    477 {
    478         int rc;
    479 
    480         rc = asprintf(bufp, "%u.%u.%u.%u", (addr >> 24) & 0xff,
    481             (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
    482         if (rc < 0)
    483                 return ENOMEM;
    484 
    485         return EOK;
    486 }
    487 
    488 static int inet_addr_format_v6(const addr128_t addr, char **bufp)
    489 {
    490         *bufp = (char *) malloc(INET6_ADDRSTRLEN);
    491         if (*bufp == NULL)
    492                 return ENOMEM;
    493 
    494         /* Find the longest zero subsequence */
    495 
    496         uint16_t zeroes[8];
    497         uint16_t bioctets[8];
    498 
    499         for (size_t i = 8; i > 0; i--) {
    500                 size_t j = i - 1;
    501 
    502                 bioctets[j] = (addr[j << 1] << 8) | addr[(j << 1) + 1];
    503 
    504                 if (bioctets[j] == 0) {
    505                         zeroes[j] = 1;
    506                         if (j < 7)
    507                                 zeroes[j] += zeroes[j + 1];
    508                 } else
    509                         zeroes[j] = 0;
    510         }
    511 
    512         size_t wildcard_pos = (size_t) -1;
    513         size_t wildcard_size = 0;
    514 
    515         for (size_t i = 0; i < 8; i++) {
    516                 if (zeroes[i] > wildcard_size) {
    517                         wildcard_pos = i;
    518                         wildcard_size = zeroes[i];
    519                 }
    520         }
    521 
    522         char *cur = *bufp;
    523         size_t rest = INET6_ADDRSTRLEN;
    524         bool tail_zero = false;
    525         int ret;
    526 
    527         for (size_t i = 0; i < 8; i++) {
    528                 if ((i == wildcard_pos) && (wildcard_size > 1)) {
    529                         ret = snprintf(cur, rest, ":");
    530                         i += wildcard_size - 1;
    531                         tail_zero = true;
    532                 } else if (i == 0) {
    533                         ret = snprintf(cur, rest, "%" PRIx16, bioctets[i]);
    534                         tail_zero = false;
    535                 } else {
    536                         ret = snprintf(cur, rest, ":%" PRIx16, bioctets[i]);
    537                         tail_zero = false;
    538                 }
    539 
    540                 if (ret < 0)
     389        char *slash = str_chr(text, '/');
     390        if (slash == NULL)
     391                return EINVAL;
     392       
     393        *slash = 0;
     394       
     395        int rc = inet_addr_version(text, &naddr->version);
     396        if (rc != EOK)
     397                return rc;
     398       
     399        uint8_t buf[16];
     400        rc = inet_pton(ipver_af(naddr->version), text, buf);
     401        *slash = '/';
     402       
     403        if (rc != EOK)
     404                return rc;
     405       
     406        slash++;
     407        uint8_t prefix;
     408       
     409        switch (naddr->version) {
     410        case ip_v4:
     411                prefix = strtoul(slash, &slash, 10);
     412                if (prefix > 32)
    541413                        return EINVAL;
    542 
    543                 cur += ret;
    544                 rest -= ret;
    545         }
    546 
    547         if (tail_zero)
    548                 (void) snprintf(cur, rest, ":");
    549 
     414               
     415                naddr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) |
     416                    buf[3];
     417                naddr->prefix = prefix;
     418               
     419                break;
     420        case ip_v6:
     421                prefix = strtoul(slash, &slash, 10);
     422                if (prefix > 128)
     423                        return EINVAL;
     424               
     425                memcpy(naddr->addr6, buf, 16);
     426                naddr->prefix = prefix;
     427               
     428                break;
     429        default:
     430                return ENOTSUP;
     431        }
     432       
    550433        return EOK;
    551434}
     
    563446int inet_addr_format(const inet_addr_t *addr, char **bufp)
    564447{
    565         int rc;
    566 
    567         rc = ENOTSUP;
    568 
     448        int rc = 0;
     449       
    569450        switch (addr->version) {
    570451        case ip_any:
    571452                rc = asprintf(bufp, "none");
    572                 if (rc < 0)
     453                break;
     454        case ip_v4:
     455                rc = asprintf(bufp, "%u.%u.%u.%u", (addr->addr >> 24) & 0xff,
     456                    (addr->addr >> 16) & 0xff, (addr->addr >> 8) & 0xff,
     457                    addr->addr & 0xff);
     458                break;
     459        case ip_v6:
     460                *bufp = (char *) malloc(INET6_ADDRSTRLEN);
     461                if (*bufp == NULL)
    573462                        return ENOMEM;
    574                 rc = EOK;
    575                 break;
    576         case ip_v4:
    577                 rc = inet_addr_format_v4(addr->addr, bufp);
    578                 break;
    579         case ip_v6:
    580                 rc = inet_addr_format_v6(addr->addr6, bufp);
    581                 break;
    582         }
    583 
    584         return rc;
     463               
     464                return inet_ntop(AF_INET6, addr->addr6, *bufp, INET6_ADDRSTRLEN);
     465        default:
     466                return ENOTSUP;
     467        }
     468       
     469        if (rc < 0)
     470                return ENOMEM;
     471       
     472        return EOK;
    585473}
    586474
     
    597485int inet_naddr_format(const inet_naddr_t *naddr, char **bufp)
    598486{
    599         int rc;
    600         char *astr;
    601 
    602         rc = ENOTSUP;
    603 
     487        int rc = 0;
     488        char prefix[INET_PREFIXSTRSIZE];
     489       
    604490        switch (naddr->version) {
    605491        case ip_any:
    606492                rc = asprintf(bufp, "none");
    607                 if (rc < 0)
     493                break;
     494        case ip_v4:
     495                rc = asprintf(bufp, "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
     496                    "/%" PRIu8, (naddr->addr >> 24) & 0xff,
     497                    (naddr->addr >> 16) & 0xff, (naddr->addr >> 8) & 0xff,
     498                    naddr->addr & 0xff, naddr->prefix);
     499                break;
     500        case ip_v6:
     501                *bufp = (char *) malloc(INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE);
     502                if (*bufp == NULL)
    608503                        return ENOMEM;
    609                 rc = EOK;
    610                 break;
    611         case ip_v4:
    612                 rc = inet_addr_format_v4(naddr->addr, &astr);
    613                 if (rc != EOK)
    614                         return ENOMEM;
    615 
    616                 rc = asprintf(bufp, "%s/%" PRIu8, astr, naddr->prefix);
     504               
     505                rc = inet_ntop(AF_INET6, naddr->addr6, *bufp,
     506                    INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE);
     507                if (rc != EOK) {
     508                        free(*bufp);
     509                        return rc;
     510                }
     511               
     512                rc = snprintf(prefix, INET_PREFIXSTRSIZE, "/%" PRIu8,
     513                    naddr->prefix);
    617514                if (rc < 0) {
    618                         free(astr);
     515                        free(*bufp);
    619516                        return ENOMEM;
    620517                }
    621 
    622                 rc = EOK;
    623                 break;
    624         case ip_v6:
    625                 rc = inet_addr_format_v6(naddr->addr6, &astr);
    626                 if (rc != EOK)
    627                         return ENOMEM;
    628 
    629                 rc = asprintf(bufp, "%s/%" PRIu8, astr, naddr->prefix);
    630                 if (rc < 0) {
    631                         free(astr);
    632                         return ENOMEM;
    633                 }
    634 
    635                 rc = EOK;
    636                 break;
    637         }
    638 
    639         return rc;
     518               
     519                str_append(*bufp, INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE, prefix);
     520               
     521                break;
     522        default:
     523                return ENOTSUP;
     524        }
     525       
     526        if (rc < 0)
     527                return ENOMEM;
     528       
     529        return EOK;
    640530}
    641531
     
    696586}
    697587
     588void inet_sockaddr_in_addr(const sockaddr_in_t *sockaddr_in, inet_addr_t *addr)
     589{
     590        addr->version = ip_v4;
     591        addr->addr = uint32_t_be2host(sockaddr_in->sin_addr.s_addr);
     592}
     593
    698594void inet_addr_set6(addr128_t v6, inet_addr_t *addr)
    699595{
     
    709605}
    710606
     607void inet_sockaddr_in6_addr(const sockaddr_in6_t *sockaddr_in6,
     608    inet_addr_t *addr)
     609{
     610        addr->version = ip_v6;
     611        addr128_t_be2host(sockaddr_in6->sin6_addr.s6_addr, addr->addr6);
     612}
     613
     614uint16_t inet_addr_sockaddr_in(const inet_addr_t *addr,
     615    sockaddr_in_t *sockaddr_in, sockaddr_in6_t *sockaddr_in6)
     616{
     617        switch (addr->version) {
     618        case ip_v4:
     619                if (sockaddr_in != NULL) {
     620                        sockaddr_in->sin_family = AF_INET;
     621                        sockaddr_in->sin_addr.s_addr = host2uint32_t_be(addr->addr);
     622                }
     623                break;
     624        case ip_v6:
     625                if (sockaddr_in6 != NULL) {
     626                        sockaddr_in6->sin6_family = AF_INET6;
     627                        host2addr128_t_be(addr->addr6, sockaddr_in6->sin6_addr.s6_addr);
     628                }
     629                break;
     630        default:
     631                assert(false);
     632                break;
     633        }
     634
     635        return ipver_af(addr->version);
     636}
     637
     638int inet_addr_sockaddr(const inet_addr_t *addr, uint16_t port,
     639    sockaddr_t **nsockaddr, socklen_t *naddrlen)
     640{
     641        sockaddr_in_t *sa4;
     642        sockaddr_in6_t *sa6;
     643
     644        switch (addr->version) {
     645        case ip_v4:
     646                sa4 = calloc(1, sizeof(sockaddr_in_t));
     647                if (sa4 == NULL)
     648                        return ENOMEM;
     649
     650                sa4->sin_family = AF_INET;
     651                sa4->sin_port = host2uint16_t_be(port);
     652                sa4->sin_addr.s_addr = host2uint32_t_be(addr->addr);
     653                if (nsockaddr != NULL)
     654                        *nsockaddr = (sockaddr_t *)sa4;
     655                if (naddrlen != NULL)
     656                        *naddrlen = sizeof(*sa4);
     657                break;
     658        case ip_v6:
     659                sa6 = calloc(1, sizeof(sockaddr_in6_t));
     660                if (sa6 == NULL)
     661                        return ENOMEM;
     662
     663                sa6->sin6_family = AF_INET6;
     664                sa6->sin6_port = host2uint16_t_be(port);
     665                host2addr128_t_be(addr->addr6, sa6->sin6_addr.s6_addr);
     666                if (nsockaddr != NULL)
     667                        *nsockaddr = (sockaddr_t *)sa6;
     668                if (naddrlen != NULL)
     669                        *naddrlen = sizeof(*sa6);
     670                break;
     671        default:
     672                assert(false);
     673                break;
     674        }
     675
     676        return EOK;
     677}
     678
    711679/** @}
    712680 */
  • uspace/lib/c/generic/iplink.c

    ree1c2d9 r0453261  
    4747static void iplink_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
    4848
    49 int iplink_open(async_sess_t *sess, iplink_ev_ops_t *ev_ops, void *arg,
     49int iplink_open(async_sess_t *sess, iplink_ev_ops_t *ev_ops,
    5050    iplink_t **riplink)
    5151{
     
    5656        iplink->sess = sess;
    5757        iplink->ev_ops = ev_ops;
    58         iplink->arg = arg;
    5958       
    6059        async_exch_t *exch = async_exchange_begin(sess);
     
    235234       
    236235        return (int) retval;
    237 }
    238 
    239 void *iplink_get_userptr(iplink_t *iplink)
    240 {
    241         return iplink->arg;
    242236}
    243237
  • uspace/lib/c/include/errno.h

    ree1c2d9 r0453261  
    6868#define ENAK (-303)
    6969
     70/** An API function is called while another blocking function is in progress. */
     71#define EINPROGRESS  (-10036)
     72
     73/** The socket identifier is not valid. */
     74#define ENOTSOCK  (-10038)
     75
     76/** The destination address required. */
     77#define EDESTADDRREQ  (-10039)
     78
     79/** Protocol is not supported.  */
     80#define EPROTONOSUPPORT  (-10043)
     81
     82/** Socket type is not supported. */
     83#define ESOCKTNOSUPPORT  (-10044)
     84
     85/** Protocol family is not supported. */
     86#define EPFNOSUPPORT  (-10046)
     87
     88/** Address family is not supported. */
     89#define EAFNOSUPPORT  (-10047)
     90
     91/** Address is already in use. */
     92#define EADDRINUSE  (-10048)
     93
     94/** The socket is not connected or bound. */
     95#define ENOTCONN  (-10057)
     96
     97#define ECONNREFUSED  (-10058)
     98
     99#define ECONNABORTED  (-10059)
     100
    70101/** The requested operation was not performed. Try again later. */
    71102#define EAGAIN  (-11002)
     103
     104/** No data. */
     105#define NO_DATA (-11004)
    72106
    73107#endif
  • uspace/lib/c/include/fibril_synch.h

    ree1c2d9 r0453261  
    135135        fid_t fibril;
    136136        fibril_timer_state_t state;
    137         /** FID of fibril executing handler or 0 if handler is not running */
    138         fid_t handler_fid;
     137        bool handler_running;
    139138
    140139        suseconds_t delay;
  • uspace/lib/c/include/inet/addr.h

    ree1c2d9 r0453261  
    3737
    3838#include <stdint.h>
     39#include <net/in.h>
     40#include <net/in6.h>
     41#include <net/socket.h>
    3942
    4043typedef uint32_t addr32_t;
     
    123126extern void inet_addr_set(addr32_t, inet_addr_t *);
    124127extern void inet_naddr_set(addr32_t, uint8_t, inet_naddr_t *);
     128extern void inet_sockaddr_in_addr(const sockaddr_in_t *, inet_addr_t *);
    125129
    126130extern void inet_addr_set6(addr128_t, inet_addr_t *);
    127131extern void inet_naddr_set6(addr128_t, uint8_t, inet_naddr_t *);
     132extern void inet_sockaddr_in6_addr(const sockaddr_in6_t *, inet_addr_t *);
     133
     134extern uint16_t inet_addr_sockaddr_in(const inet_addr_t *, sockaddr_in_t *,
     135    sockaddr_in6_t *);
     136
     137extern ip_ver_t ipver_from_af(int af);
     138extern int inet_addr_sockaddr(const inet_addr_t *, uint16_t, sockaddr_t **,
     139    socklen_t *);
    128140
    129141#endif
  • uspace/lib/c/include/inet/iplink.h

    ree1c2d9 r0453261  
    4444        async_sess_t *sess;
    4545        struct iplink_ev_ops *ev_ops;
    46         void *arg;
    4746} iplink_t;
    4847
     
    8281} iplink_ev_ops_t;
    8382
    84 extern int iplink_open(async_sess_t *, iplink_ev_ops_t *, void *, iplink_t **);
     83extern int iplink_open(async_sess_t *, iplink_ev_ops_t *, iplink_t **);
    8584extern void iplink_close(iplink_t *);
    8685extern int iplink_send(iplink_t *, iplink_sdu_t *);
     
    9190extern int iplink_get_mac48(iplink_t *, addr48_t *);
    9291extern int iplink_set_mac48(iplink_t *, addr48_t);
    93 extern void *iplink_get_userptr(iplink_t *);
    9492
    9593#endif
  • uspace/lib/c/include/ipc/services.h

    ree1c2d9 r0453261  
    4949        SERVICE_IRC        = FOURCC('i', 'r', 'c', ' '),
    5050        SERVICE_CLIPBOARD  = FOURCC('c', 'l', 'i', 'p'),
     51        SERVICE_UDP        = FOURCC('u', 'd', 'p', ' '),
     52        SERVICE_TCP        = FOURCC('t', 'c', 'p', ' ')
    5153} services_t;
    5254
     
    5961#define SERVICE_NAME_INETPING6  "net/inetping6"
    6062#define SERVICE_NAME_NETCONF    "net/netconf"
    61 #define SERVICE_NAME_UDP        "net/udp"
    62 #define SERVICE_NAME_TCP        "net/tcp"
    6363
    6464#endif
  • uspace/lib/http/include/http/http.h

    ree1c2d9 r0453261  
    3737#define HTTP_HTTP_H_
    3838
     39#include <net/socket.h>
    3940#include <adt/list.h>
    4041#include <inet/addr.h>
    41 #include <inet/tcp.h>
    4242
    4343#include "receive-buffer.h"
     
    4848        inet_addr_t addr;
    4949
    50         tcp_t *tcp;
    51         tcp_conn_t *conn;
    52 
     50        bool connected;
     51        int conn_sd;
     52       
    5353        size_t buffer_size;
    5454        receive_buffer_t recv_buffer;
  • uspace/lib/http/src/http.c

    ree1c2d9 r0453261  
    3434 */
    3535
    36 #include <errno.h>
    3736#include <stdio.h>
    3837#include <stdlib.h>
     
    4039#include <macros.h>
    4140
     41#include <net/socket.h>
    4242#include <inet/dnsr.h>
    43 #include <inet/tcp.h>
    4443
    4544#include <http/http.h>
     
    4948{
    5049        http_t *http = client_data;
    51         size_t nrecv;
    52         int rc;
    53 
    54         rc = tcp_conn_recv_wait(http->conn, buf, buf_size, &nrecv);
    55         if (rc != EOK)
    56                 return rc;
    57 
    58         return nrecv;
     50        return recv(http->conn_sd, buf, buf_size, 0);
    5951}
    6052
     
    8577int http_connect(http_t *http)
    8678{
    87         if (http->conn != NULL)
     79        if (http->connected)
    8880                return EBUSY;
    8981       
     
    10395        }
    10496       
    105         inet_ep2_t epp;
     97        struct sockaddr *saddr;
     98        socklen_t saddrlen;
    10699       
    107         inet_ep2_init(&epp);
    108         epp.remote.addr = http->addr;
    109         epp.remote.port = http->port;
     100        rc = inet_addr_sockaddr(&http->addr, http->port, &saddr, &saddrlen);
     101        if (rc != EOK) {
     102                assert(rc == ENOMEM);
     103                return ENOMEM;
     104        }
    110105       
    111         rc = tcp_create(&http->tcp);
    112         if (rc != EOK)
    113                 return rc;
     106        http->conn_sd = socket(saddr->sa_family, SOCK_STREAM, 0);
     107        if (http->conn_sd < 0)
     108                return http->conn_sd;
    114109       
    115         rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn);
    116         if (rc != EOK)
    117                 return rc;
    118        
    119         rc = tcp_conn_wait_connected(http->conn);
    120         if (rc != EOK)
    121                 return rc;
     110        rc = connect(http->conn_sd, saddr, saddrlen);
     111        free(saddr);
    122112       
    123113        return rc;
     
    126116int http_close(http_t *http)
    127117{
    128         if (http->conn == NULL)
     118        if (!http->connected)
    129119                return EINVAL;
    130120       
    131         tcp_conn_destroy(http->conn);
    132         tcp_destroy(http->tcp);
    133         return EOK;
     121        return closesocket(http->conn_sd);
    134122}
    135123
  • uspace/lib/http/src/request.c

    ree1c2d9 r0453261  
    3434 */
    3535
    36 #include <errno.h>
    3736#include <stdio.h>
    3837#include <stdlib.h>
     
    4039#include <macros.h>
    4140
    42 #include <inet/tcp.h>
     41#include <net/socket.h>
    4342
    4443#include <http/http.h>
     
    150149                return rc;
    151150       
    152         rc = tcp_conn_send(http->conn, buf, buf_size);
     151        rc = send(http->conn_sd, buf, buf_size, 0);
    153152        free(buf);
    154153       
  • uspace/lib/posix/include/posix/errno.h

    ree1c2d9 r0453261  
    6969extern int *__posix_errno(void);
    7070
    71 #define __TOP_ERRNO (-EAGAIN)
     71#define __TOP_ERRNO (-NO_DATA)
    7272
    7373enum {
    7474        POSIX_E2BIG = __TOP_ERRNO + 1,
    7575        POSIX_EACCES = __TOP_ERRNO + 2,
     76        POSIX_EADDRINUSE = -EADDRINUSE,
    7677        POSIX_EADDRNOTAVAIL = -EADDRNOTAVAIL,
     78        POSIX_EAFNOSUPPORT = -EAFNOSUPPORT,
    7779        POSIX_EAGAIN = -EAGAIN,
    7880        POSIX_EALREADY = __TOP_ERRNO + 3,
     
    8688        POSIX_ECONNRESET = __TOP_ERRNO + 9,
    8789        POSIX_EDEADLK = __TOP_ERRNO + 10,
     90        POSIX_EDESTADDRREQ = -EDESTADDRREQ,
    8891        POSIX_EDOM = __TOP_ERRNO + 11,
    8992        POSIX_EDQUOT = __TOP_ERRNO + 12,
     
    9497        POSIX_EIDRM = __TOP_ERRNO + 16,
    9598        POSIX_EILSEQ = __TOP_ERRNO + 17,
     99        POSIX_EINPROGRESS = -EINPROGRESS,
    96100        POSIX_EINTR = -EINTR,
    97101        POSIX_EINVAL = -EINVAL,
     
    110114        POSIX_ENFILE = __TOP_ERRNO + 25,
    111115        POSIX_ENOBUFS = __TOP_ERRNO + 26,
     116        POSIX_ENODATA = -NO_DATA,
    112117        POSIX_ENODEV = __TOP_ERRNO + 27,
    113118        POSIX_ENOENT = -ENOENT,
     
    122127        POSIX_ENOSTR = __TOP_ERRNO + 34,
    123128        POSIX_ENOSYS = __TOP_ERRNO + 35,
     129        POSIX_ENOTCONN = -ENOTCONN,
    124130        POSIX_ENOTDIR = -ENOTDIR,
    125131        POSIX_ENOTEMPTY = -ENOTEMPTY,
    126132        POSIX_ENOTRECOVERABLE = __TOP_ERRNO + 36,
     133        POSIX_ENOTSOCK = -ENOTSOCK,
    127134        POSIX_ENOTSUP = -ENOTSUP,
    128135        POSIX_ENOTTY = __TOP_ERRNO + 37,
     
    134141        POSIX_EPIPE = __TOP_ERRNO + 41,
    135142        POSIX_EPROTO = __TOP_ERRNO + 42,
     143        POSIX_EPROTONOSUPPORT = -EPROTONOSUPPORT,
    136144        POSIX_EPROTOTYPE = __TOP_ERRNO + 43,
    137145        POSIX_ERANGE = -ERANGE,
     
    145153        POSIX_EWOULDBLOCK = __TOP_ERRNO + 51,
    146154        POSIX_EXDEV = -EXDEV,
    147         POSIX_EINPROGRESS = __TOP_ERRNO + 52,
    148         POSIX_ENOTSOCK = __TOP_ERRNO + 53,
    149         POSIX_EDESTADDRREQ = __TOP_ERRNO + 54,
    150         POSIX_EPROTONOSUPPORT = __TOP_ERRNO + 55,
    151         POSIX_EAFNOSUPPORT = __TOP_ERRNO + 56,
    152         POSIX_EADDRINUSE = __TOP_ERRNO + 57,
    153         POSIX_ENOTCONN = __TOP_ERRNO + 58,
    154         POSIX_ENODATA = __TOP_ERRNO + 59,
    155155};
    156156
  • uspace/srv/hid/remcons/remcons.c

    ree1c2d9 r0453261  
    4444#include <fibril_synch.h>
    4545#include <task.h>
    46 #include <inet/addr.h>
    47 #include <inet/endpoint.h>
    48 #include <inet/tcp.h>
     46#include <net/in.h>
     47#include <net/inet.h>
     48#include <net/socket.h>
    4949#include <io/console.h>
    5050#include <inttypes.h>
     
    9999};
    100100
    101 static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn);
    102 
    103 static tcp_listen_cb_t listen_cb = {
    104         .new_conn = remcons_new_conn
    105 };
    106 
    107 static tcp_cb_t conn_cb = {
    108         .connected = NULL
    109 };
    110 
    111101static telnet_user_t *srv_to_user(con_srv_t *srv)
    112102{
     
    121111
    122112        /* Force character mode. */
    123         (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
    124             telnet_force_character_mode_command_count);
     113        send(user->socket, (void *)telnet_force_character_mode_command,
     114            telnet_force_character_mode_command_count, 0);
    125115
    126116        return EOK;
     
    282272}
    283273
    284 /** Handle network connection.
    285  *
    286  * @param lst  Listener
    287  * @param conn Connection
    288  */
    289 static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
    290 {
    291         telnet_user_t *user = telnet_user_create(conn);
    292         assert(user);
    293 
    294         con_srvs_init(&user->srvs);
    295         user->srvs.ops = &con_ops;
    296         user->srvs.sarg = user;
    297         user->srvs.abort_timeout = 1000;
    298 
    299         telnet_user_add(user);
     274/** Fibril for each accepted socket.
     275 *
     276 * @param arg Corresponding @c telnet_user_t structure.
     277 */
     278static int network_user_fibril(void *arg)
     279{
     280        telnet_user_t *user = arg;
    300281
    301282        int rc = loc_service_register(user->service_name, &user->service_id);
     
    303284                telnet_user_error(user, "Unable to register %s with loc: %s.",
    304285                    user->service_name, str_error(rc));
    305                 return;
     286                return EOK;
    306287        }
    307288
    308289        telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
    309290            user->service_name, user->service_id);
    310 
     291       
    311292        fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
    312293        assert(spawn_fibril);
    313294        fibril_add_ready(spawn_fibril);
    314 
     295       
    315296        /* Wait for all clients to exit. */
    316297        fibril_mutex_lock(&user->guard);
    317298        while (!user_can_be_destroyed_no_lock(user)) {
    318299                if (user->task_finished) {
    319                         user->conn = NULL;
     300                        closesocket(user->socket);
    320301                        user->socket_closed = true;
    321302                        user->srvs.aborted = true;
     
    329310        }
    330311        fibril_mutex_unlock(&user->guard);
    331 
     312       
    332313        rc = loc_service_unregister(user->service_id);
    333314        if (rc != EOK) {
     
    339320        telnet_user_log(user, "Destroying...");
    340321        telnet_user_destroy(user);
     322
     323        return EOK;
    341324}
    342325
    343326int main(int argc, char *argv[])
    344327{
    345         int rc;
    346         tcp_listener_t *lst;
    347         tcp_t *tcp;
    348         inet_ep_t ep;
    349 
     328        int port = 2223;
     329       
    350330        async_set_client_connection(client_connection);
    351         rc = loc_server_register(NAME);
     331        int rc = loc_server_register(NAME);
    352332        if (rc != EOK) {
    353333                fprintf(stderr, "%s: Unable to register server\n", NAME);
    354334                return rc;
    355335        }
    356 
    357         rc = tcp_create(&tcp);
    358         if (rc != EOK) {
    359                 fprintf(stderr, "%s: Error initializing TCP.\n", NAME);
    360                 return rc;
    361         }
    362 
    363         inet_ep_init(&ep);
    364         ep.port = 2223;
    365 
    366         rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
    367             &lst);
    368         if (rc != EOK) {
    369                 fprintf(stderr, "%s: Error creating listener.\n", NAME);
    370                 return rc;
     336       
     337        struct sockaddr_in addr;
     338       
     339        addr.sin_family = AF_INET;
     340        addr.sin_port = htons(port);
     341       
     342        rc = inet_pton(AF_INET, "127.0.0.1", (void *)
     343            &addr.sin_addr.s_addr);
     344        if (rc != EOK) {
     345                fprintf(stderr, "Error parsing network address: %s.\n",
     346                    str_error(rc));
     347                return 2;
     348        }
     349
     350        int listen_sd = socket(PF_INET, SOCK_STREAM, 0);
     351        if (listen_sd < 0) {
     352                fprintf(stderr, "Error creating listening socket: %s.\n",
     353                    str_error(listen_sd));
     354                return 3;
     355        }
     356
     357        rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
     358        if (rc != EOK) {
     359                fprintf(stderr, "Error binding socket: %s.\n",
     360                    str_error(rc));
     361                return 4;
     362        }
     363
     364        rc = listen(listen_sd, BACKLOG_SIZE);
     365        if (rc != EOK) {
     366                fprintf(stderr, "listen() failed: %s.\n", str_error(rc));
     367                return 5;
    371368        }
    372369
    373370        printf("%s: HelenOS Remote console service\n", NAME);
    374371        task_retval(0);
    375         async_manager();
    376 
    377         /* Not reached */
     372
     373        while (true) {
     374                struct sockaddr_in raddr;
     375                socklen_t raddr_len = sizeof(raddr);
     376                int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr,
     377                    &raddr_len);
     378
     379                if (conn_sd < 0) {
     380                        fprintf(stderr, "accept() failed: %s.\n",
     381                            str_error(rc));
     382                        continue;
     383                }
     384
     385                telnet_user_t *user = telnet_user_create(conn_sd);
     386                assert(user);
     387
     388                con_srvs_init(&user->srvs);
     389                user->srvs.ops = &con_ops;
     390                user->srvs.sarg = user;
     391                user->srvs.abort_timeout = 1000;
     392
     393                telnet_user_add(user);
     394
     395                fid_t fid = fibril_create(network_user_fibril, user);
     396                assert(fid);
     397                fibril_add_ready(fid);
     398        }
     399
    378400        return 0;
    379401}
  • uspace/srv/hid/remcons/remcons.h

    ree1c2d9 r0453261  
    3838#define NAME       "remcons"
    3939#define NAMESPACE  "term"
     40#define BACKLOG_SIZE  5
    4041
    4142#endif
  • uspace/srv/hid/remcons/user.c

    ree1c2d9 r0453261  
    4444#include <fibril_synch.h>
    4545#include <task.h>
    46 #include <inet/tcp.h>
     46#include <net/in.h>
     47#include <net/inet.h>
     48#include <net/socket.h>
    4749#include <io/console.h>
    4850#include <inttypes.h>
     
    5658/** Create new telnet user.
    5759 *
    58  * @param conn Incoming connection.
     60 * @param socket Socket the user communicates through.
    5961 * @return New telnet user or NULL when out of memory.
    6062 */
    61 telnet_user_t *telnet_user_create(tcp_conn_t *conn)
     63telnet_user_t *telnet_user_create(int socket)
    6264{
    6365        static int telnet_user_id_counter = 0;
     
    7678        }
    7779
    78         user->conn = conn;
     80        user->socket = socket;
    7981        user->service_id = (service_id_t) -1;
    8082        prodcons_initialize(&user->in_events);
     
    191193        /* No more buffered data? */
    192194        if (user->socket_buffer_len <= user->socket_buffer_pos) {
    193                 int rc;
    194                 size_t recv_length;
    195 
    196                 rc = tcp_conn_recv_wait(user->conn, user->socket_buffer,
    197                     BUFFER_SIZE, &recv_length);
    198                 if (rc != EOK)
    199                         return rc;
    200 
    201                 if (recv_length == 0) {
     195                int recv_length = recv(user->socket, user->socket_buffer, BUFFER_SIZE, 0);
     196                if ((recv_length == 0) || (recv_length == ENOTCONN)) {
    202197                        user->socket_closed = true;
    203198                        user->srvs.aborted = true;
    204199                        return ENOENT;
    205200                }
    206 
     201                if (recv_length < 0) {
     202                        return recv_length;
     203                }
    207204                user->socket_buffer_len = recv_length;
    208205                user->socket_buffer_pos = 0;
     
    362359
    363360
    364         int rc = tcp_conn_send(user->conn, converted, converted_size);
     361        int rc = send(user->socket, converted, converted_size, 0);
    365362        free(converted);
    366363
  • uspace/srv/hid/remcons/user.h

    ree1c2d9 r0453261  
    3838#include <adt/prodcons.h>
    3939#include <fibril_synch.h>
    40 #include <inet/tcp.h>
    4140#include <inttypes.h>
    4241#include <io/con_srv.h>
     
    5251        /** Internal id, used for creating locfs entries. */
    5352        int id;
    54         /** Associated connection. */
    55         tcp_conn_t *conn;
     53        /** Associated socket. */
     54        int socket;
    5655        /** Location service id assigned to the virtual terminal. */
    5756        service_id_t service_id;
     
    8180} telnet_user_t;
    8281
    83 extern telnet_user_t *telnet_user_create(tcp_conn_t *);
     82extern telnet_user_t *telnet_user_create(int);
    8483extern void telnet_user_add(telnet_user_t *);
    8584extern void telnet_user_destroy(telnet_user_t *);
  • uspace/srv/hid/rfb/main.c

    ree1c2d9 r0453261  
    150150}
    151151
     152static int socket_fibril(void *unused)
     153{
     154        rfb_accept(&rfb);
     155       
     156        /* Not reached */
     157        return EOK;
     158}
     159
    152160int main(int argc, char **argv)
    153161{
     
    259267        }
    260268       
     269        fid_t fib = fibril_create(socket_fibril, NULL);
     270        if (!fib) {
     271                fprintf(stderr, NAME ": Unable to create socket fibril.\n");
     272                return 2;
     273        }
     274       
     275        fibril_add_ready(fib);
     276       
    261277        printf("%s: Accepting connections\n", NAME);
    262278        task_retval(0);
  • uspace/srv/hid/rfb/rfb.c

    ree1c2d9 r0453261  
    3131#include <stdlib.h>
    3232#include <fibril_synch.h>
    33 #include <inet/addr.h>
    34 #include <inet/endpoint.h>
    35 #include <inet/tcp.h>
    3633#include <inttypes.h>
    3734#include <str.h>
     
    4138#include <io/log.h>
    4239
     40#include <net/in.h>
     41#include <net/inet.h>
     42#include <net/socket.h>
     43
    4344#include "rfb.h"
    44 
    45 static void rfb_new_conn(tcp_listener_t *, tcp_conn_t *);
    46 
    47 static tcp_listen_cb_t listen_cb = {
    48         .new_conn = rfb_new_conn
    49 };
    50 
    51 static tcp_cb_t conn_cb = {
    52         .connected = NULL
    53 };
    5445
    5546/** Buffer for receiving the request. */
     
    6253
    6354/** Receive one character (with buffering) */
    64 static int recv_char(tcp_conn_t *conn, char *c)
    65 {
    66         size_t nrecv;
    67         int rc;
    68 
     55static int recv_char(int fd, char *c)
     56{
    6957        if (rbuf_out == rbuf_in) {
    7058                rbuf_out = 0;
    7159                rbuf_in = 0;
    7260               
    73                 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv);
    74                 if (rc != EOK)
     61                ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);
     62                if (rc <= 0)
    7563                        return rc;
    7664               
    77                 rbuf_in = nrecv;
     65                rbuf_in = rc;
    7866        }
    7967       
     
    8371
    8472/** Receive count characters (with buffering) */
    85 static int recv_chars(tcp_conn_t *conn, char *c, size_t count)
     73static int recv_chars(int fd, char *c, size_t count)
    8674{
    8775        for (size_t i = 0; i < count; i++) {
    88                 int rc = recv_char(conn, c);
     76                int rc = recv_char(fd, c);
    8977                if (rc != EOK)
    9078                        return rc;
     
    9482}
    9583
    96 static int recv_skip_chars(tcp_conn_t *conn, size_t count)
     84static int recv_skip_chars(int fd, size_t count)
    9785{
    9886        for (size_t i = 0; i < count; i++) {
    9987                char c;
    100                 int rc = recv_char(conn, &c);
     88                int rc = recv_char(fd, &c);
    10189                if (rc != EOK)
    10290                        return rc;
     
    216204}
    217205
    218 static int recv_message(tcp_conn_t *conn, char type, void *buf, size_t size)
     206static int recv_message(int conn_sd, char type, void *buf, size_t size)
    219207{
    220208        memcpy(buf, &type, 1);
    221         return recv_chars(conn, ((char *) buf) + 1, size -1);
     209        return recv_chars(conn_sd, ((char *) buf) + 1, size -1);
    222210}
    223211
     
    489477}
    490478
    491 static int rfb_send_framebuffer_update(rfb_t *rfb, tcp_conn_t *conn,
    492     bool incremental)
     479static int rfb_send_framebuffer_update(rfb_t *rfb, int conn_sd, bool incremental)
    493480{
    494481        fibril_mutex_lock(&rfb->lock);
     
    553540       
    554541        if (!rfb->pixel_format.true_color) {
    555                 int rc = tcp_conn_send(conn, send_palette, send_palette_size);
     542                int rc = send(conn_sd, send_palette, send_palette_size, 0);
    556543                if (rc != EOK) {
    557544                        free(buf);
     
    560547        }
    561548       
    562         int rc = tcp_conn_send(conn, buf, buf_size);
     549        int rc = send(conn_sd, buf, buf_size, 0);
    563550        free(buf);
    564551       
     
    593580}
    594581
    595 static void rfb_socket_connection(rfb_t *rfb, tcp_conn_t *conn)
     582static void rfb_socket_connection(rfb_t *rfb, int conn_sd)
    596583{
    597584        /* Version handshake */
    598         int rc = tcp_conn_send(conn, "RFB 003.008\n", 12);
     585        int rc = send(conn_sd, "RFB 003.008\n", 12, 0);
    599586        if (rc != EOK) {
    600587                log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server version %d", rc);
     
    603590       
    604591        char client_version[12];
    605         rc = recv_chars(conn, client_version, 12);
     592        rc = recv_chars(conn_sd, client_version, 12);
    606593        if (rc != EOK) {
    607594                log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client version: %d", rc);
     
    620607        sec_types[0] = 1; /* length */
    621608        sec_types[1] = RFB_SECURITY_NONE;
    622         rc = tcp_conn_send(conn, sec_types, 2);
     609        rc = send(conn_sd, sec_types, 2, 0);
    623610        if (rc != EOK) {
    624611                log_msg(LOG_DEFAULT, LVL_WARN,
     
    628615       
    629616        char selected_sec_type = 0;
    630         rc = recv_char(conn, &selected_sec_type);
     617        rc = recv_char(conn_sd, &selected_sec_type);
    631618        if (rc != EOK) {
    632619                log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving security type: %d", rc);
     
    639626        }
    640627        uint32_t security_result = RFB_SECURITY_HANDSHAKE_OK;
    641         rc = tcp_conn_send(conn, &security_result, sizeof(uint32_t));
     628        rc = send(conn_sd, &security_result, sizeof(uint32_t), 0);
    642629        if (rc != EOK) {
    643630                log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending security result: %d", rc);
     
    647634        /* Client init */
    648635        char shared_flag;
    649         rc = recv_char(conn, &shared_flag);
     636        rc = recv_char(conn_sd, &shared_flag);
    650637        if (rc != EOK) {
    651638                log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client init: %d", rc);
     
    670657        memcpy(server_init->name, rfb->name, name_length);
    671658        fibril_mutex_unlock(&rfb->lock);
    672         rc = tcp_conn_send(conn, server_init, msg_length);
     659        rc = send(conn_sd, server_init, msg_length, 0);
    673660        if (rc != EOK) {
    674661                log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server init: %d", rc);
     
    678665        while (true) {
    679666                char message_type = 0;
    680                 rc = recv_char(conn, &message_type);
     667                rc = recv_char(conn_sd, &message_type);
    681668                if (rc != EOK) {
    682669                        log_msg(LOG_DEFAULT, LVL_WARN,
     
    693680                switch (message_type) {
    694681                case RFB_CMSG_SET_PIXEL_FORMAT:
    695                         recv_message(conn, message_type, &spf, sizeof(spf));
     682                        recv_message(conn_sd, message_type, &spf, sizeof(spf));
    696683                        rfb_pixel_format_to_host(&spf.pixel_format, &spf.pixel_format);
    697684                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetPixelFormat message");
     
    703690                        break;
    704691                case RFB_CMSG_SET_ENCODINGS:
    705                         recv_message(conn, message_type, &se, sizeof(se));
     692                        recv_message(conn_sd, message_type, &se, sizeof(se));
    706693                        rfb_set_encodings_to_host(&se, &se);
    707694                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetEncodings message");
    708695                        for (uint16_t i = 0; i < se.count; i++) {
    709696                                int32_t encoding = 0;
    710                                 rc = recv_chars(conn, (char *) &encoding, sizeof(int32_t));
     697                                rc = recv_chars(conn_sd, (char *) &encoding, sizeof(int32_t));
    711698                                if (rc != EOK)
    712699                                        return;
     
    720707                        break;
    721708                case RFB_CMSG_FRAMEBUFFER_UPDATE_REQUEST:
    722                         recv_message(conn, message_type, &fbur, sizeof(fbur));
     709                        recv_message(conn_sd, message_type, &fbur, sizeof(fbur));
    723710                        rfb_framebuffer_update_request_to_host(&fbur, &fbur);
    724711                        log_msg(LOG_DEFAULT, LVL_DEBUG2,
    725712                            "Received FramebufferUpdateRequest message");
    726                         rfb_send_framebuffer_update(rfb, conn, fbur.incremental);
     713                        rfb_send_framebuffer_update(rfb, conn_sd, fbur.incremental);
    727714                        break;
    728715                case RFB_CMSG_KEY_EVENT:
    729                         recv_message(conn, message_type, &ke, sizeof(ke));
     716                        recv_message(conn_sd, message_type, &ke, sizeof(ke));
    730717                        rfb_key_event_to_host(&ke, &ke);
    731718                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received KeyEvent message");
    732719                        break;
    733720                case RFB_CMSG_POINTER_EVENT:
    734                         recv_message(conn, message_type, &pe, sizeof(pe));
     721                        recv_message(conn_sd, message_type, &pe, sizeof(pe));
    735722                        rfb_pointer_event_to_host(&pe, &pe);
    736723                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received PointerEvent message");
    737724                        break;
    738725                case RFB_CMSG_CLIENT_CUT_TEXT:
    739                         recv_message(conn, message_type, &cct, sizeof(cct));
     726                        recv_message(conn_sd, message_type, &cct, sizeof(cct));
    740727                        rfb_client_cut_text_to_host(&cct, &cct);
    741728                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received ClientCutText message");
    742                         recv_skip_chars(conn, cct.length);
     729                        recv_skip_chars(conn_sd, cct.length);
    743730                        break;
    744731                default:
     
    750737}
    751738
    752 int rfb_listen(rfb_t *rfb, uint16_t port)
    753 {
    754         tcp_t *tcp = NULL;
    755         tcp_listener_t *lst = NULL;
    756         inet_ep_t ep;
    757         int rc;
    758        
    759         rc = tcp_create(&tcp);
    760         if (rc != EOK) {
    761                 log_msg(LOG_DEFAULT, LVL_ERROR, "Error initializing TCP.");
    762                 goto error;
    763         }
    764        
    765         inet_ep_init(&ep);
    766         ep.port = port;
    767        
    768         rc = tcp_listener_create(tcp, &ep, &listen_cb, rfb, &conn_cb, rfb,
    769             &lst);
    770         if (rc != EOK) {
    771                 log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listener.");
    772                 goto error;
    773         }
    774        
    775         rfb->tcp = tcp;
    776         rfb->lst = lst;
     739int rfb_listen(rfb_t *rfb, uint16_t port) {
     740        struct sockaddr_in addr;
     741       
     742        addr.sin_family = AF_INET;
     743        addr.sin_port = htons(port);
     744       
     745        int rc = inet_pton(AF_INET, "127.0.0.1", (void *)
     746            &addr.sin_addr.s_addr);
     747        if (rc != EOK) {
     748                log_msg(LOG_DEFAULT, LVL_ERROR, "Error parsing network address (%s)",
     749                    str_error(rc));
     750                return rc;
     751        }
     752
     753        int listen_sd = socket(PF_INET, SOCK_STREAM, 0);
     754        if (listen_sd < 0) {
     755                log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listening socket (%s)",
     756                    str_error(listen_sd));
     757                return rc;
     758        }
     759       
     760        rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
     761        if (rc != EOK) {
     762                log_msg(LOG_DEFAULT, LVL_ERROR, "Error binding socket (%s)",
     763                    str_error(rc));
     764                return rc;
     765        }
     766       
     767        rc = listen(listen_sd, 2);
     768        if (rc != EOK) {
     769                log_msg(LOG_DEFAULT, LVL_ERROR, "listen() failed (%s)", str_error(rc));
     770                return rc;
     771        }
     772       
     773        rfb->listen_sd = listen_sd;
    777774       
    778775        return EOK;
    779 error:
    780         tcp_listener_destroy(lst);
    781         tcp_destroy(tcp);
    782         return rc;
    783 }
    784 
    785 static void rfb_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
    786 {
    787         rfb_t *rfb = (rfb_t *)tcp_listener_userptr(lst);
    788         log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted");
    789 
    790         rbuf_out = 0;
    791         rbuf_in = 0;
    792 
    793         rfb_socket_connection(rfb, conn);
    794 }
     776}
     777
     778void rfb_accept(rfb_t *rfb)
     779{
     780        while (true) {
     781                struct sockaddr_in raddr;
     782                socklen_t raddr_len = sizeof(raddr);
     783                int conn_sd = accept(rfb->listen_sd, (struct sockaddr *) &raddr,
     784                    &raddr_len);
     785               
     786                if (conn_sd < 0) {
     787                        log_msg(LOG_DEFAULT, LVL_WARN, "accept() failed (%s)",
     788                            str_error(conn_sd));
     789                        continue;
     790                }
     791               
     792                log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted");
     793               
     794                rbuf_out = 0;
     795                rbuf_in = 0;
     796               
     797                rfb_socket_connection(rfb, conn_sd);
     798                closesocket(conn_sd);
     799        }
     800}
  • uspace/srv/hid/rfb/rfb.h

    ree1c2d9 r0453261  
    3030#define RFB_H__
    3131
    32 #include <inet/tcp.h>
    3332#include <io/pixelmap.h>
    3433#include <fibril_synch.h>
     
    152151        rfb_pixel_format_t pixel_format;
    153152        const char *name;
    154         tcp_t *tcp;
    155         tcp_listener_t *lst;
     153        int listen_sd;
    156154        pixelmap_t framebuffer;
    157155        rfb_rectangle_t damage_rect;
     
    167165extern int rfb_set_size(rfb_t *, uint16_t, uint16_t);
    168166extern int rfb_listen(rfb_t *, uint16_t);
     167extern void rfb_accept(rfb_t *);
    169168
    170169#endif
  • uspace/srv/net/dhcp/dhcp.c

    ree1c2d9 r0453261  
    3737#include <adt/list.h>
    3838#include <bitops.h>
    39 #include <byteorder.h>
    4039#include <errno.h>
    4140#include <fibril_synch.h>
     
    563562        }
    564563
     564        /* XXX Work around multiple simultaneous sessions issue */
     565        dhcp_transport_fini(&dlink->dt);
     566
    565567        log_msg(LOG_DEFAULT, LVL_NOTE, "%s: Successfully configured.",
    566568            dlink->link_info.name);
  • uspace/srv/net/dhcp/transport.c

    ree1c2d9 r0453261  
    3636
    3737#include <bitops.h>
    38 #include <errno.h>
    3938#include <inet/addr.h>
    4039#include <inet/dnsr.h>
     
    4241#include <io/log.h>
    4342#include <loc.h>
     43#include <net/in.h>
     44#include <net/inet.h>
     45#include <net/socket.h>
    4446#include <stdio.h>
    4547#include <stdlib.h>
     
    6567} dhcp_offer_t;
    6668
    67 static void dhcp_recv_msg(udp_assoc_t *, udp_rmsg_t *);
    68 static void dhcp_recv_err(udp_assoc_t *, udp_rerr_t *);
    69 static void dhcp_link_state(udp_assoc_t *, udp_link_state_t);
    70 
    71 static udp_cb_t dhcp_transport_cb = {
    72         .recv_msg = dhcp_recv_msg,
    73         .recv_err = dhcp_recv_err,
    74         .link_state = dhcp_link_state
    75 };
     69static int dhcp_recv_fibril(void *);
    7670
    7771int dhcp_send(dhcp_transport_t *dt, void *msg, size_t size)
    7872{
    79         inet_ep_t ep;
     73        struct sockaddr_in addr;
    8074        int rc;
    8175
    82         inet_ep_init(&ep);
    83         ep.port = dhcp_server_port;
    84         inet_addr_set(addr32_broadcast_all_hosts, &ep.addr);
     76        addr.sin_family = AF_INET;
     77        addr.sin_port = htons(dhcp_server_port);
     78        addr.sin_addr.s_addr = htonl(addr32_broadcast_all_hosts);
    8579
    86         rc = udp_assoc_send_msg(dt->assoc, &ep, msg, size);
     80        rc = sendto(dt->fd, msg, size, 0,
     81            (struct sockaddr *)&addr, sizeof(addr));
    8782        if (rc != EOK) {
    88                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed sending message");
     83                log_msg(LOG_DEFAULT, LVL_ERROR, "Sending failed");
    8984                return rc;
    9085        }
     
    9388}
    9489
    95 static void dhcp_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
     90static int dhcp_recv_msg(dhcp_transport_t *dt, void **rmsg, size_t *rsize)
    9691{
    97         dhcp_transport_t *dt;
    98         size_t s;
     92        struct sockaddr_in src_addr;
     93        socklen_t src_addr_size;
     94        size_t recv_size;
    9995        int rc;
    10096
    101         log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg()");
    102 
    103         dt = (dhcp_transport_t *)udp_assoc_userptr(assoc);
    104         s = udp_rmsg_size(rmsg);
    105         if (s > MAX_MSG_SIZE)
    106                 s = MAX_MSG_SIZE; /* XXX */
    107 
    108         rc = udp_rmsg_read(rmsg, 0, msgbuf, s);
    109         if (rc != EOK) {
    110                 log_msg(LOG_DEFAULT, LVL_ERROR, "Error receiving message.");
    111                 return;
     97        if (dt->fd < 0) {
     98                /* Terminated */
     99                return EIO;
    112100        }
    113101
    114         log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg() - call recv_cb");
    115         dt->recv_cb(dt->cb_arg, msgbuf, s);
    116 }
     102        src_addr_size = sizeof(src_addr);
     103        rc = recvfrom(dt->fd, msgbuf, MAX_MSG_SIZE, 0,
     104            (struct sockaddr *)&src_addr, &src_addr_size);
     105        if (rc < 0) {
     106                log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom failed (%d)", rc);
     107                return rc;
     108        }
    117109
    118 static void dhcp_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr)
    119 {
    120         log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error");
    121 }
     110        recv_size = (size_t)rc;
     111        *rmsg = msgbuf;
     112        *rsize = recv_size;
    122113
    123 static void dhcp_link_state(udp_assoc_t *assoc, udp_link_state_t ls)
    124 {
    125         log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change");
     114        return EOK;
    126115}
    127116
     
    129118    dhcp_recv_cb_t recv_cb, void *arg)
    130119{
    131         udp_t *udp = NULL;
    132         udp_assoc_t *assoc = NULL;
    133         inet_ep2_t epp;
     120        int fd;
     121        struct sockaddr_in laddr;
     122        int fid;
    134123        int rc;
    135124
    136         log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_transport_init()");
     125        log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcptransport_init()");
    137126
    138         inet_ep2_init(&epp);
    139         epp.local.addr.version = ip_v4;
    140         epp.local.port = dhcp_client_port;
    141         epp.local_link = link_id;
     127        laddr.sin_family = AF_INET;
     128        laddr.sin_port = htons(dhcp_client_port);
     129        laddr.sin_addr.s_addr = INADDR_ANY;
    142130
    143         rc = udp_create(&udp);
     131        fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
     132        if (fd < 0) {
     133                rc = EIO;
     134                goto error;
     135        }
     136
     137        log_msg(LOG_DEFAULT, LVL_DEBUG, "Bind socket.");
     138        rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
    144139        if (rc != EOK) {
    145140                rc = EIO;
     
    147142        }
    148143
    149         rc = udp_assoc_create(udp, &epp, &dhcp_transport_cb, dt, &assoc);
     144        log_msg(LOG_DEFAULT, LVL_DEBUG, "Set socket options");
     145        rc = setsockopt(fd, SOL_SOCKET, SO_IPLINK, &link_id, sizeof(link_id));
    150146        if (rc != EOK) {
    151147                rc = EIO;
     
    153149        }
    154150
    155         dt->udp = udp;
    156         dt->assoc = assoc;
     151        dt->fd = fd;
    157152        dt->recv_cb = recv_cb;
    158153        dt->cb_arg = arg;
    159154
     155        fid = fibril_create(dhcp_recv_fibril, dt);
     156        if (fid == 0) {
     157                rc = ENOMEM;
     158                goto error;
     159        }
     160
     161        dt->recv_fid = fid;
     162        fibril_add_ready(fid);
     163
    160164        return EOK;
    161165error:
    162         udp_assoc_destroy(assoc);
    163         udp_destroy(udp);
     166        closesocket(fd);
    164167        return rc;
    165168}
     
    167170void dhcp_transport_fini(dhcp_transport_t *dt)
    168171{
    169         udp_assoc_destroy(dt->assoc);
    170         udp_destroy(dt->udp);
     172        closesocket(dt->fd);
     173        dt->fd = -1;
     174}
     175
     176static int dhcp_recv_fibril(void *arg)
     177{
     178        dhcp_transport_t *dt = (dhcp_transport_t *)arg;
     179        void *msg;
     180        size_t size = (size_t) -1;
     181        int rc;
     182
     183        while (true) {
     184                rc = dhcp_recv_msg(dt, &msg, &size);
     185                if (rc != EOK)
     186                        break;
     187
     188                assert(size != (size_t) -1);
     189
     190                dt->recv_cb(dt->cb_arg, msg, size);
     191        }
     192
     193        return EOK;
    171194}
    172195
  • uspace/srv/net/dhcp/transport.h

    ree1c2d9 r0453261  
    3838#define TRANSPORT_H
    3939
    40 #include <inet/udp.h>
    4140#include <ipc/loc.h>
    4241#include <sys/types.h>
     
    4847
    4948struct dhcp_transport {
    50         /** UDP */
    51         udp_t *udp;
    52         /** UDP association */
    53         udp_assoc_t *assoc;
     49        /** Transport socket */
     50        int fd;
    5451        /** Receive callback */
    5552        dhcp_recv_cb_t recv_cb;
    5653        /** Callback argument */
    5754        void *cb_arg;
     55        /** Receive fibril ID */
     56        int recv_fid;
    5857};
    5958
  • uspace/srv/net/dnsrsrv/transport.c

    ree1c2d9 r0453261  
    3737#include <errno.h>
    3838#include <fibril_synch.h>
    39 #include <inet/addr.h>
    40 #include <inet/endpoint.h>
    41 #include <inet/udp.h>
    4239#include <io/log.h>
     40#include <net/in.h>
     41#include <net/inet.h>
     42#include <net/socket.h>
    4343#include <stdbool.h>
    4444#include <stdlib.h>
     
    7272
    7373static uint8_t recv_buf[RECV_BUF_SIZE];
    74 static udp_t *transport_udp;
    75 static udp_assoc_t *transport_assoc;
     74static fid_t recv_fid;
     75static int transport_fd = -1;
    7676
    7777/** Outstanding requests */
     
    7979static FIBRIL_MUTEX_INITIALIZE(treq_lock);
    8080
    81 static void transport_recv_msg(udp_assoc_t *, udp_rmsg_t *);
    82 static void transport_recv_err(udp_assoc_t *, udp_rerr_t *);
    83 static void transport_link_state(udp_assoc_t *, udp_link_state_t);
    84 
    85 static udp_cb_t transport_cb = {
    86         .recv_msg = transport_recv_msg,
    87         .recv_err = transport_recv_err,
    88         .link_state = transport_link_state
    89 };
     81static int transport_recv_fibril(void *arg);
    9082
    9183int transport_init(void)
    9284{
    93         inet_ep2_t epp;
     85        struct sockaddr_in laddr;
     86        int fd;
     87        fid_t fid;
    9488        int rc;
    9589
    96         inet_ep2_init(&epp);
    97 
    98         rc = udp_create(&transport_udp);
    99         if (rc != EOK) {
     90        laddr.sin_family = AF_INET;
     91        laddr.sin_port = htons(12345);
     92        laddr.sin_addr.s_addr = INADDR_ANY;
     93
     94        fd = -1;
     95
     96        fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
     97        if (fd < 0) {
    10098                rc = EIO;
    10199                goto error;
    102100        }
    103101
    104         rc = udp_assoc_create(transport_udp, &epp, &transport_cb, NULL,
    105             &transport_assoc);
    106         if (rc != EOK) {
    107                 rc = EIO;
    108                 goto error;
    109         }
    110 
     102        rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
     103        if (rc != EOK)
     104                goto error;
     105
     106        transport_fd = fd;
     107
     108        fid = fibril_create(transport_recv_fibril, NULL);
     109        if (fid == 0)
     110                goto error;
     111
     112        fibril_add_ready(fid);
     113        recv_fid = fid;
    111114        return EOK;
    112115error:
    113         log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network.");
    114         udp_assoc_destroy(transport_assoc);
    115         udp_destroy(transport_udp);
     116        log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network socket.");
     117        if (fd >= 0)
     118                closesocket(fd);
    116119        return rc;
    117120}
     
    119122void transport_fini(void)
    120123{
    121         udp_assoc_destroy(transport_assoc);
    122         udp_destroy(transport_udp);
     124        if (transport_fd >= 0)
     125                closesocket(transport_fd);
    123126}
    124127
     
    179182{
    180183        trans_req_t *treq = NULL;
    181         inet_ep_t ep;
    182 
     184        struct sockaddr *saddr = NULL;
     185        socklen_t saddrlen;
     186       
    183187        void *req_data;
    184188        size_t req_size;
     
    186190        if (rc != EOK)
    187191                goto error;
    188 
    189         inet_ep_init(&ep);
    190         ep.addr = dns_server_addr;
    191         ep.port = DNS_SERVER_PORT;
    192 
     192       
     193        rc = inet_addr_sockaddr(&dns_server_addr, DNS_SERVER_PORT,
     194            &saddr, &saddrlen);
     195        if (rc != EOK) {
     196                assert(rc == ENOMEM);
     197                goto error;
     198        }
     199       
    193200        size_t ntry = 0;
    194 
     201       
    195202        while (ntry < REQ_RETRY_MAX) {
    196                 rc = udp_assoc_send_msg(transport_assoc, &ep, req_data,
    197                     req_size);
     203                rc = sendto(transport_fd, req_data, req_size, 0,
     204                    saddr, saddrlen);
    198205                if (rc != EOK)
    199206                        goto error;
    200 
     207               
    201208                treq = treq_create(req);
    202209                if (treq == NULL) {
     
    204211                        goto error;
    205212                }
    206 
     213               
    207214                fibril_mutex_lock(&treq->done_lock);
    208215                while (treq->done != true) {
     
    214221                        }
    215222                }
    216 
     223               
    217224                fibril_mutex_unlock(&treq->done_lock);
    218 
     225               
    219226                if (rc != ETIMEOUT)
    220227                        break;
    221228        }
    222 
     229       
    223230        if (ntry >= REQ_RETRY_MAX) {
    224231                rc = EIO;
    225232                goto error;
    226233        }
    227 
     234       
    228235        if (treq->status != EOK) {
    229236                rc = treq->status;
    230237                goto error;
    231238        }
    232 
     239       
    233240        *rresp = treq->resp;
    234241        treq_destroy(treq);
    235242        free(req_data);
     243        free(saddr);
    236244        return EOK;
    237 
     245       
    238246error:
    239247        if (treq != NULL)
    240248                treq_destroy(treq);
    241 
     249       
    242250        free(req_data);
     251        free(saddr);
    243252        return rc;
    244253}
    245254
    246 static void transport_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
     255static int transport_recv_msg(dns_message_t **rresp)
     256{
     257        struct sockaddr_in src_addr;
     258        socklen_t src_addr_size;
     259        size_t recv_size;
     260        dns_message_t *resp;
     261        int rc;
     262
     263        src_addr_size = sizeof(src_addr);
     264        rc = recvfrom(transport_fd, recv_buf, RECV_BUF_SIZE, 0,
     265            (struct sockaddr *)&src_addr, &src_addr_size);
     266        if (rc < 0) {
     267                log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom returns error - %d", rc);
     268                goto error;
     269        }
     270
     271        recv_size = (size_t)rc;
     272
     273        rc = dns_message_decode(recv_buf, recv_size, &resp);
     274        if (rc != EOK) {
     275                rc = EIO;
     276                goto error;
     277        }
     278
     279        *rresp = resp;
     280        return EOK;
     281
     282error:
     283        return rc;
     284}
     285
     286static int transport_recv_fibril(void *arg)
    247287{
    248288        dns_message_t *resp = NULL;
    249289        trans_req_t *treq;
    250         size_t size;
    251         inet_ep_t remote_ep;
    252290        int rc;
    253291
    254         size = udp_rmsg_size(rmsg);
    255         if (size > RECV_BUF_SIZE)
    256                 size = RECV_BUF_SIZE; /* XXX */
    257 
    258         rc = udp_rmsg_read(rmsg, 0, recv_buf, size);
    259         if (rc != EOK) {
    260                 log_msg(LOG_DEFAULT, LVL_ERROR, "Error reading message.");
    261                 return;
    262         }
    263 
    264         udp_rmsg_remote_ep(rmsg, &remote_ep);
    265         /* XXX */
    266 
    267         rc = dns_message_decode(recv_buf, size, &resp);
    268         if (rc != EOK) {
    269                 log_msg(LOG_DEFAULT, LVL_ERROR, "Error decoding message.");
    270                 return;
    271         }
    272 
    273         assert(resp != NULL);
    274 
    275         fibril_mutex_lock(&treq_lock);
    276         treq = treq_match_resp(resp);
    277         if (treq == NULL) {
     292        while (true) {
     293                rc = transport_recv_msg(&resp);
     294                if (rc != EOK)
     295                        continue;
     296
     297                assert(resp != NULL);
     298
     299                fibril_mutex_lock(&treq_lock);
     300                treq = treq_match_resp(resp);
     301                if (treq == NULL) {
     302                        fibril_mutex_unlock(&treq_lock);
     303                        continue;
     304                }
     305
     306                list_remove(&treq->lreq);
    278307                fibril_mutex_unlock(&treq_lock);
    279                 return;
    280         }
    281 
    282         list_remove(&treq->lreq);
    283         fibril_mutex_unlock(&treq_lock);
    284 
    285         treq_complete(treq, resp);
    286 }
    287 
    288 static void transport_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr)
    289 {
    290         log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error");
    291 }
    292 
    293 static void transport_link_state(udp_assoc_t *assoc, udp_link_state_t ls)
    294 {
    295         log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change");
    296 }
    297 
     308
     309                treq_complete(treq, resp);
     310        }
     311
     312        return 0;
     313}
    298314
    299315/** @}
  • uspace/srv/net/inetsrv/inet_link.c

    ree1c2d9 r0453261  
    7373{
    7474        memcpy(ip_addr, link_local_node_ip, 16);
    75 
     75       
    7676        ip_addr[8] = mac_addr[0] ^ 0x02;
    7777        ip_addr[9] = mac_addr[1];
     
    8585{
    8686        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()");
    87 
     87       
    8888        int rc;
    8989        inet_packet_t packet;
    90         inet_link_t *ilink;
    91 
    92         ilink = (inet_link_t *)iplink_get_userptr(iplink);
    93 
     90       
    9491        switch (ver) {
    9592        case ip_v4:
    96                 rc = inet_pdu_decode(sdu->data, sdu->size, ilink->svc_id,
    97                     &packet);
     93                rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
    9894                break;
    9995        case ip_v6:
    100                 rc = inet_pdu_decode6(sdu->data, sdu->size, ilink->svc_id,
    101                     &packet);
     96                rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
    10297                break;
    10398        default:
     
    105100                return EINVAL;
    106101        }
    107 
     102       
    108103        if (rc != EOK) {
    109104                log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
    110105                return rc;
    111106        }
    112 
    113         log_msg(LOG_DEFAULT, LVL_NOTE, "inet_iplink_recv: link_id=%zu", packet.link_id);
     107       
    114108        log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
    115109        rc = inet_recv_packet(&packet);
    116110        log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
    117111        free(packet.data);
    118 
     112       
    119113        return rc;
    120114}
     
    183177        }
    184178
    185         rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, ilink, &ilink->iplink);
     179        rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
    186180        if (rc != EOK) {
    187181                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
  • uspace/srv/net/inetsrv/inetsrv.c

    ree1c2d9 r0453261  
    469469{
    470470        async_exch_t *exch = async_exchange_begin(client->sess);
    471 
     471       
    472472        ipc_call_t answer;
    473 
    474         log_msg(LOG_DEFAULT, LVL_NOTE, "inet_ev_recv: iplink=%zu",
    475             dgram->iplink);
    476 
    477         aid_t req = async_send_2(exch, INET_EV_RECV, dgram->tos,
    478             dgram->iplink, &answer);
    479 
     473        aid_t req = async_send_1(exch, INET_EV_RECV, dgram->tos, &answer);
     474       
    480475        int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
    481476        if (rc != EOK) {
     
    484479                return rc;
    485480        }
    486 
     481       
    487482        rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
    488483        if (rc != EOK) {
     
    491486                return rc;
    492487        }
    493 
     488       
    494489        rc = async_data_write_start(exch, dgram->data, dgram->size);
    495 
     490       
    496491        async_exchange_end(exch);
    497 
     492       
    498493        if (rc != EOK) {
    499494                async_forget(req);
    500495                return rc;
    501496        }
    502 
     497       
    503498        sysarg_t retval;
    504499        async_wait_for(req, &retval);
    505 
     500       
    506501        return (int) retval;
    507502}
     
    516511        if (proto == IP_PROTO_ICMP)
    517512                return icmp_recv(dgram);
    518 
     513       
    519514        if (proto == IP_PROTO_ICMPV6)
    520515                return icmpv6_recv(dgram);
     
    545540                if (packet->offs == 0 && !packet->mf) {
    546541                        /* It is complete deliver it immediately */
    547                         dgram.iplink = packet->link_id;
    548542                        dgram.src = packet->src;
    549543                        dgram.dest = packet->dest;
  • uspace/srv/net/inetsrv/inetsrv.h

    ree1c2d9 r0453261  
    7575
    7676typedef struct {
    77         /** Local link ID */
    78         service_id_t link_id;
    7977        /** Source address */
    8078        inet_addr_t src;
  • uspace/srv/net/inetsrv/pdu.c

    ree1c2d9 r0453261  
    298298/** Decode IPv4 datagram
    299299 *
    300  * @param data    Serialized IPv4 datagram
    301  * @param size    Length of serialized IPv4 datagram
    302  * @param link_id Link on which PDU was received
    303  * @param packet  IP datagram structure to be filled
     300 * @param data   Serialized IPv4 datagram
     301 * @param size   Length of serialized IPv4 datagram
     302 * @param packet IP datagram structure to be filled
    304303 *
    305304 * @return EOK on success
     
    308307 *
    309308 */
    310 int inet_pdu_decode(void *data, size_t size, service_id_t link_id,
    311     inet_packet_t *packet)
     309int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet)
    312310{
    313311        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode()");
     
    368366       
    369367        memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
    370         packet->link_id = link_id;
    371368       
    372369        return EOK;
     
    375372/** Decode IPv6 datagram
    376373 *
    377  * @param data    Serialized IPv6 datagram
    378  * @param size    Length of serialized IPv6 datagram
    379  * @param link_id Link on which PDU was received
    380  * @param packet  IP datagram structure to be filled
     374 * @param data   Serialized IPv6 datagram
     375 * @param size   Length of serialized IPv6 datagram
     376 * @param packet IP datagram structure to be filled
    381377 *
    382378 * @return EOK on success
     
    385381 *
    386382 */
    387 int inet_pdu_decode6(void *data, size_t size, service_id_t link_id,
    388     inet_packet_t *packet)
     383int inet_pdu_decode6(void *data, size_t size, inet_packet_t *packet)
    389384{
    390385        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode6()");
     
    462457       
    463458        memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
    464         packet->link_id = link_id;
     459       
    465460        return EOK;
    466461}
  • uspace/srv/net/inetsrv/pdu.h

    ree1c2d9 r0453261  
    3838#define INET_PDU_H_
    3939
    40 #include <loc.h>
    4140#include <sys/types.h>
    4241#include "inetsrv.h"
     
    5150extern int inet_pdu_encode6(inet_packet_t *, addr128_t, addr128_t, size_t,
    5251    size_t, void **, size_t *, size_t *);
    53 extern int inet_pdu_decode(void *, size_t, service_id_t, inet_packet_t *);
    54 extern int inet_pdu_decode6(void *, size_t, service_id_t, inet_packet_t *);
     52extern int inet_pdu_decode(void *, size_t, inet_packet_t *);
     53extern int inet_pdu_decode6(void *, size_t, inet_packet_t *);
    5554
    5655extern int ndp_pdu_decode(inet_dgram_t *, ndp_packet_t *);
  • uspace/srv/net/inetsrv/reass.c

    ree1c2d9 r0453261  
    325325                return ENOMEM;
    326326
    327         /* XXX What if different fragments came from different link? */
    328         dgram.iplink = frag->packet.link_id;
    329327        dgram.size = dgram_size;
    330328        dgram.src = frag->packet.src;
  • uspace/srv/net/tcp/Makefile

    ree1c2d9 r0453261  
    2828
    2929USPACE_PREFIX = ../../..
    30 
    31 LIBS = \
    32         $(LIBNETTL_PREFIX)/libnettl.a
    33 
    34 EXTRA_CFLAGS += \
    35         -I$(LIBNETTL_PREFIX)/include
    36 
     30LIBS = $(LIBNET_PREFIX)/libnet.a
     31EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include
    3732BINARY = tcp
    3833
     
    4439        rqueue.c \
    4540        segment.c \
    46         service.c \
    4741        seq_no.c \
     42        sock.c \
    4843        tcp.c \
    4944        test.c \
  • uspace/srv/net/tcp/conn.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636
    3737#include <adt/list.h>
     38#include <stdbool.h>
    3839#include <errno.h>
    39 #include <inet/endpoint.h>
    4040#include <io/log.h>
    4141#include <macros.h>
    42 #include <nettl/amap.h>
    43 #include <stdbool.h>
    4442#include <stdlib.h>
    4543#include "conn.h"
     
    5755#define TIME_WAIT_TIMEOUT       (2*MAX_SEGMENT_LIFETIME)
    5856
    59 static LIST_INITIALIZE(conn_list);
    60 /** Taken after tcp_conn_t lock */
    61 static FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
    62 static amap_t *amap;
     57LIST_INITIALIZE(conn_list);
     58FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
    6359
    6460static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg);
     
    6662static void tcp_conn_tw_timer_clear(tcp_conn_t *conn);
    6763
    68 /** Initialize connections. */
    69 int tcp_conns_init(void)
    70 {
    71         int rc;
    72 
    73         rc = amap_create(&amap);
    74         if (rc != EOK) {
    75                 assert(rc == ENOMEM);
    76                 return ENOMEM;
    77         }
    78 
    79         return EOK;
    80 }
    81 
    8264/** Create new connection structure.
    8365 *
    84  * @param epp           Endpoint pair (will be deeply copied)
     66 * @param lsock         Local socket (will be deeply copied)
     67 * @param fsock         Foreign socket (will be deeply copied)
    8568 * @return              New connection or NULL
    8669 */
    87 tcp_conn_t *tcp_conn_new(inet_ep2_t *epp)
     70tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
    8871{
    8972        tcp_conn_t *conn = NULL;
     
    138121        fibril_condvar_initialize(&conn->cstate_cv);
    139122
    140         conn->cb = NULL;
     123        conn->cstate_cb = NULL;
    141124
    142125        conn->cstate = st_listen;
     
    145128        conn->ap = ap_passive;
    146129        conn->fin_is_acked = false;
    147         if (epp != NULL)
    148                 conn->ident = *epp;
     130        conn->ident.local = *lsock;
     131        if (fsock != NULL)
     132                conn->ident.foreign = *fsock;
    149133
    150134        return conn;
     
    200184void tcp_conn_addref(tcp_conn_t *conn)
    201185{
    202         log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p) before=%zu",
    203             conn->name, conn, atomic_get(&conn->refcnt));
     186        log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p)", conn->name, conn);
    204187        atomic_inc(&conn->refcnt);
    205188}
     
    213196void tcp_conn_delref(tcp_conn_t *conn)
    214197{
    215         log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p) before=%zu",
    216             conn->name, conn, atomic_get(&conn->refcnt));
     198        log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p)", conn->name, conn);
    217199
    218200        if (atomic_predec(&conn->refcnt) == 0)
     
    255237
    256238        assert(conn->deleted == false);
    257         conn->deleted = true;
    258         conn->cb = NULL;
    259         conn->cb_arg = NULL;
    260239        tcp_conn_delref(conn);
    261240}
     
    265244 * Add connection to the connection map.
    266245 */
    267 int tcp_conn_add(tcp_conn_t *conn)
    268 {
    269         inet_ep2_t aepp;
    270         int rc;
    271 
     246void tcp_conn_add(tcp_conn_t *conn)
     247{
    272248        tcp_conn_addref(conn);
    273249        fibril_mutex_lock(&conn_list_lock);
    274 
    275         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_add: conn=%p", conn);
    276 
    277         rc = amap_insert(amap, &conn->ident, conn, af_allow_system, &aepp);
    278         if (rc != EOK) {
    279                 tcp_conn_delref(conn);
    280                 fibril_mutex_unlock(&conn_list_lock);
    281                 return rc;
    282         }
    283 
    284         conn->ident = aepp;
    285250        list_append(&conn->link, &conn_list);
    286251        fibril_mutex_unlock(&conn_list_lock);
    287 
    288         return EOK;
    289252}
    290253
     
    296259{
    297260        fibril_mutex_lock(&conn_list_lock);
    298         amap_remove(amap, &conn->ident);
    299261        list_remove(&conn->link);
    300262        fibril_mutex_unlock(&conn_list_lock);
     
    313275
    314276        /* Run user callback function */
    315         if (conn->cb != NULL && conn->cb->cstate_change != NULL) {
     277        if (conn->cstate_cb != NULL) {
    316278                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - run user CB");
    317                 conn->cb->cstate_change(conn, conn->cb_arg, old_state);
     279                conn->cstate_cb(conn, conn->cstate_cb_arg);
    318280        } else {
    319281                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - no user CB");
     
    322284        assert(old_state != st_closed);
    323285        if (nstate == st_closed) {
    324                 tcp_conn_remove(conn);
    325286                /* Drop one reference for now being in closed state */
    326287                tcp_conn_delref(conn);
     
    371332}
    372333
    373 /** Find connection structure for specified endpoint pair.
    374  *
    375  * A connection is uniquely identified by a endpoint pair. Look up our
    376  * connection map and return connection structure based on endpoint pair.
     334/** Match socket with pattern. */
     335static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt)
     336{
     337        log_msg(LOG_DEFAULT, LVL_DEBUG2,
     338            "tcp_socket_match(sock=(%u), pat=(%u))", sock->port, patt->port);
     339       
     340        if ((!inet_addr_is_any(&patt->addr)) &&
     341            (!inet_addr_compare(&patt->addr, &sock->addr)))
     342                return false;
     343
     344        if ((patt->port != TCP_PORT_ANY) &&
     345            (patt->port != sock->port))
     346                return false;
     347
     348        log_msg(LOG_DEFAULT, LVL_DEBUG2, " -> match");
     349
     350        return true;
     351}
     352
     353/** Match socket pair with pattern. */
     354static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern)
     355{
     356        log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_sockpair_match(%p, %p)", sp, pattern);
     357
     358        if (!tcp_socket_match(&sp->local, &pattern->local))
     359                return false;
     360
     361        if (!tcp_socket_match(&sp->foreign, &pattern->foreign))
     362                return false;
     363
     364        return true;
     365}
     366
     367/** Find connection structure for specified socket pair.
     368 *
     369 * A connection is uniquely identified by a socket pair. Look up our
     370 * connection map and return connection structure based on socket pair.
    377371 * The connection reference count is bumped by one.
    378372 *
    379  * @param epp   Endpoint pair
     373 * @param sp    Socket pair
    380374 * @return      Connection structure or NULL if not found.
    381375 */
    382 tcp_conn_t *tcp_conn_find_ref(inet_ep2_t *epp)
    383 {
    384         int rc;
    385         void *arg;
    386         tcp_conn_t *conn;
    387 
    388         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", epp);
    389 
     376tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp)
     377{
     378        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
     379       
     380        log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%u), l:(%u))",
     381            sp->foreign.port, sp->local.port);
     382       
    390383        fibril_mutex_lock(&conn_list_lock);
    391 
    392         rc = amap_find_match(amap, epp, &arg);
    393         if (rc != EOK) {
    394                 assert(rc == ENOENT);
    395                 fibril_mutex_unlock(&conn_list_lock);
    396                 return NULL;
    397         }
    398 
    399         conn = (tcp_conn_t *)arg;
    400         tcp_conn_addref(conn);
    401 
     384       
     385        list_foreach(conn_list, link, tcp_conn_t, conn) {
     386                tcp_sockpair_t *csp = &conn->ident;
     387               
     388                log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%u), l:(%u))",
     389                    csp->foreign.port, csp->local.port);
     390               
     391                if (tcp_sockpair_match(sp, csp)) {
     392                        tcp_conn_addref(conn);
     393                        fibril_mutex_unlock(&conn_list_lock);
     394                        return conn;
     395                }
     396        }
     397       
    402398        fibril_mutex_unlock(&conn_list_lock);
    403         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref: got conn=%p",
    404             conn);
    405         return conn;
     399        return NULL;
    406400}
    407401
     
    413407{
    414408        log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
     409        tcp_conn_state_set(conn, st_closed);
    415410        conn->reset = true;
    416         tcp_conn_state_set(conn, st_closed);
    417411
    418412        tcp_conn_tw_timer_clear(conn);
     
    883877        if (conn->fin_is_acked) {
    884878                log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
     879                tcp_conn_remove(conn);
    885880                tcp_conn_state_set(conn, st_closed);
    886881                return cp_done;
     
    10111006
    10121007        /* Signal to the receive function that new data has arrived */
    1013         if (xfer_size > 0) {
    1014                 fibril_condvar_broadcast(&conn->rcv_buf_cv);
    1015                 if (conn->cb != NULL && conn->cb->recv_data != NULL)
    1016                         conn->cb->recv_data(conn, conn->cb_arg);
    1017         }
     1008        fibril_condvar_broadcast(&conn->rcv_buf_cv);
    10181009
    10191010        log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
     
    11071098                conn->rcv_buf_fin = true;
    11081099                fibril_condvar_broadcast(&conn->rcv_buf_cv);
    1109                 if (conn->cb != NULL && conn->cb->recv_data != NULL)
    1110                         conn->cb->recv_data(conn, conn->cb_arg);
    11111100
    11121101                tcp_segment_delete(seg);
     
    11791168 *
    11801169 * @param conn          Connection
    1181  * @param epp           Endpoint pair on which segment was received
    1182  * @param seg           Segment
    1183  */
    1184 void tcp_conn_segment_arrived(tcp_conn_t *conn, inet_ep2_t *epp,
    1185     tcp_segment_t *seg)
    1186 {
    1187         inet_ep2_t aepp;
    1188         inet_ep2_t oldepp;
    1189         int rc;
    1190 
     1170 * @param seg           Segment
     1171 */
     1172void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
     1173{
    11911174        log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
    11921175            conn->name, seg);
    11931176
    1194         tcp_conn_lock(conn);
    1195 
    1196         if (conn->cstate == st_closed) {
    1197                 log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
    1198                 tcp_unexpected_segment(epp, seg);
    1199                 tcp_conn_unlock(conn);
    1200                 return;
    1201         }
    1202 
    1203         if (inet_addr_is_any(&conn->ident.remote.addr) ||
    1204             conn->ident.remote.port == inet_port_any ||
    1205             inet_addr_is_any(&conn->ident.local.addr)) {
    1206 
    1207                 log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_conn_segment_arrived: "
    1208                     "Changing connection ID, updating amap.");
    1209                 oldepp = conn->ident;
    1210 
    1211                 /* Need to remove and re-insert connection with new identity */
    1212                 fibril_mutex_lock(&conn_list_lock);
    1213 
    1214                 if (inet_addr_is_any(&conn->ident.remote.addr))
    1215                         conn->ident.remote.addr = epp->remote.addr;
    1216 
    1217                 if (conn->ident.remote.port == inet_port_any)
    1218                         conn->ident.remote.port = epp->remote.port;
    1219 
    1220                 if (inet_addr_is_any(&conn->ident.local.addr))
    1221                         conn->ident.local.addr = epp->local.addr;
    1222 
    1223                 rc = amap_insert(amap, &conn->ident, conn, af_allow_system, &aepp);
    1224                 if (rc != EOK) {
    1225                         assert(rc != EEXISTS);
    1226                         assert(rc == ENOMEM);
    1227                         log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
    1228                         fibril_mutex_unlock(&conn_list_lock);
    1229                         tcp_conn_unlock(conn);
    1230                         return;
    1231                 }
    1232 
    1233                 amap_remove(amap, &oldepp);
    1234                 fibril_mutex_unlock(&conn_list_lock);
    1235 
    1236                 conn->name = (char *) "a";
    1237         }
    1238 
    12391177        switch (conn->cstate) {
    12401178        case st_listen:
    1241                 tcp_conn_sa_listen(conn, seg);
    1242                 break;
     1179                tcp_conn_sa_listen(conn, seg); break;
    12431180        case st_syn_sent:
    1244                 tcp_conn_sa_syn_sent(conn, seg);
    1245                 break;
     1181                tcp_conn_sa_syn_sent(conn, seg); break;
    12461182        case st_syn_received:
    12471183        case st_established:
     
    12531189        case st_time_wait:
    12541190                /* Process segments in order of sequence number */
    1255                 tcp_conn_sa_queue(conn, seg);
    1256                 break;
     1191                tcp_conn_sa_queue(conn, seg); break;
    12571192        case st_closed:
    12581193                log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
    12591194                assert(false);
    12601195        }
    1261 
    1262         tcp_conn_unlock(conn);
    12631196}
    12641197
     
    12831216
    12841217        log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
     1218        tcp_conn_remove(conn);
    12851219        tcp_conn_state_set(conn, st_closed);
    12861220
     
    13291263}
    13301264
    1331 /** Handle unexpected segment received on an endpoint pair.
     1265/** Handle unexpected segment received on a socket pair.
    13321266 *
    13331267 * We reply with an RST unless the received segment has RST.
    13341268 *
    1335  * @param sp            Endpoint pair which received the segment
     1269 * @param sp            Socket pair which received the segment
    13361270 * @param seg           Unexpected segment
    13371271 */
    1338 void tcp_unexpected_segment(inet_ep2_t *epp, tcp_segment_t *seg)
    1339 {
    1340         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", epp,
    1341             seg);
     1272void tcp_unexpected_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
     1273{
     1274        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_unexpected_segment(%p, %p)", sp, seg);
    13421275
    13431276        if ((seg->ctrl & CTL_RST) == 0)
    1344                 tcp_reply_rst(epp, seg);
    1345 }
    1346 
    1347 /** Compute flipped endpoint pair for response.
    1348  *
    1349  * Flipped endpoint pair has local and remote endpoints exchanged.
    1350  *
    1351  * @param epp           Endpoint pair
    1352  * @param fepp          Place to store flipped endpoint pair
    1353  */
    1354 void tcp_ep2_flipped(inet_ep2_t *epp, inet_ep2_t *fepp)
    1355 {
    1356         fepp->local = epp->remote;
    1357         fepp->remote = epp->local;
     1277                tcp_reply_rst(sp, seg);
     1278}
     1279
     1280/** Compute flipped socket pair for response.
     1281 *
     1282 * Flipped socket pair has local and foreign sockets exchanged.
     1283 *
     1284 * @param sp            Socket pair
     1285 * @param fsp           Place to store flipped socket pair
     1286 */
     1287void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
     1288{
     1289        fsp->local = sp->foreign;
     1290        fsp->foreign = sp->local;
    13581291}
    13591292
    13601293/** Send RST in response to an incoming segment.
    13611294 *
    1362  * @param epp           Endpoint pair which received the segment
     1295 * @param sp            Socket pair which received the segment
    13631296 * @param seg           Incoming segment
    13641297 */
    1365 void tcp_reply_rst(inet_ep2_t *epp, tcp_segment_t *seg)
     1298void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
    13661299{
    13671300        tcp_segment_t *rseg;
    13681301
    1369         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", epp, seg);
     1302        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
    13701303
    13711304        rseg = tcp_segment_make_rst(seg);
    1372         tcp_transmit_segment(epp, rseg);
     1305        tcp_transmit_segment(sp, rseg);
    13731306}
    13741307
  • uspace/srv/net/tcp/conn.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define CONN_H
    3737
    38 #include <inet/endpoint.h>
    3938#include <stdbool.h>
    4039#include "tcp_type.h"
    4140
    42 extern int tcp_conns_init(void);
    43 extern tcp_conn_t *tcp_conn_new(inet_ep2_t *);
     41extern tcp_conn_t *tcp_conn_new(tcp_sock_t *, tcp_sock_t *);
    4442extern void tcp_conn_delete(tcp_conn_t *);
    45 extern int tcp_conn_add(tcp_conn_t *);
     43extern void tcp_conn_add(tcp_conn_t *);
    4644extern void tcp_conn_remove(tcp_conn_t *);
    4745extern void tcp_conn_reset(tcp_conn_t *conn);
     
    4947extern void tcp_conn_fin_sent(tcp_conn_t *);
    5048extern void tcp_conn_ack_of_fin_rcvd(tcp_conn_t *);
    51 extern tcp_conn_t *tcp_conn_find_ref(inet_ep2_t *);
     49extern tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *);
    5250extern void tcp_conn_addref(tcp_conn_t *);
    5351extern void tcp_conn_delref(tcp_conn_t *);
     
    5553extern void tcp_conn_unlock(tcp_conn_t *);
    5654extern bool tcp_conn_got_syn(tcp_conn_t *);
    57 extern void tcp_conn_segment_arrived(tcp_conn_t *, inet_ep2_t *,
    58     tcp_segment_t *);
     55extern void tcp_conn_segment_arrived(tcp_conn_t *, tcp_segment_t *);
    5956extern void tcp_conn_trim_seg_to_wnd(tcp_conn_t *, tcp_segment_t *);
    60 extern void tcp_unexpected_segment(inet_ep2_t *, tcp_segment_t *);
    61 extern void tcp_ep2_flipped(inet_ep2_t *, inet_ep2_t *);
    62 extern void tcp_reply_rst(inet_ep2_t *, tcp_segment_t *);
     57extern void tcp_unexpected_segment(tcp_sockpair_t *, tcp_segment_t *);
     58extern void tcp_sockpair_flipped(tcp_sockpair_t *, tcp_sockpair_t *);
     59extern void tcp_reply_rst(tcp_sockpair_t *, tcp_segment_t *);
    6360
    6461#endif
  • uspace/srv/net/tcp/ncsim.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4242#include <async.h>
    4343#include <errno.h>
    44 #include <inet/endpoint.h>
    4544#include <io/log.h>
    4645#include <stdlib.h>
     
    6665/** Bounce segment through simulator into receive queue.
    6766 *
    68  * @param epp   Endpoint pair, oriented for transmission
     67 * @param sp    Socket pair, oriented for transmission
    6968 * @param seg   Segment
    7069 */
    71 void tcp_ncsim_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
     70void tcp_ncsim_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
    7271{
    7372        tcp_squeue_entry_t *sqe;
     
    7675
    7776        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_bounce_seg()");
    78         tcp_rqueue_bounce_seg(epp, seg);
     77        tcp_rqueue_bounce_seg(sp, seg);
    7978        return;
    8079
     
    9392
    9493        sqe->delay = random() % (1000 * 1000);
    95         sqe->epp = *epp;
     94        sqe->sp = *sp;
    9695        sqe->seg = seg;
    9796
     
    148147
    149148                log_msg(LOG_DEFAULT, LVL_DEBUG, "NCSim - End Sleep");
    150                 tcp_rqueue_bounce_seg(&sqe->epp, sqe->seg);
     149                tcp_rqueue_bounce_seg(&sqe->sp, sqe->seg);
    151150                free(sqe);
    152151        }
  • uspace/srv/net/tcp/ncsim.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define NCSIM_H
    3737
    38 #include <inet/endpoint.h>
    3938#include "tcp_type.h"
    4039
    4140extern void tcp_ncsim_init(void);
    42 extern void tcp_ncsim_bounce_seg(inet_ep2_t *, tcp_segment_t *);
     41extern void tcp_ncsim_bounce_seg(tcp_sockpair_t *, tcp_segment_t *);
    4342extern void tcp_ncsim_fibril_start(void);
    4443
  • uspace/srv/net/tcp/pdu.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3838#include <byteorder.h>
    3939#include <errno.h>
    40 #include <inet/endpoint.h>
    4140#include <mem.h>
    4241#include <stdlib.h>
     
    126125}
    127126
    128 static void tcp_header_setup(inet_ep2_t *epp, tcp_segment_t *seg, tcp_header_t *hdr)
     127static void tcp_header_setup(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_header_t *hdr)
    129128{
    130129        uint16_t doff_flags;
    131130        uint16_t doff;
    132131
    133         hdr->src_port = host2uint16_t_be(epp->local.port);
    134         hdr->dest_port = host2uint16_t_be(epp->remote.port);
     132        hdr->src_port = host2uint16_t_be(sp->local.port);
     133        hdr->dest_port = host2uint16_t_be(sp->foreign.port);
    135134        hdr->seq = host2uint32_t_be(seg->seq);
    136135        hdr->ack = host2uint32_t_be(seg->ack);
     
    191190}
    192191
    193 static int tcp_header_encode(inet_ep2_t *epp, tcp_segment_t *seg,
     192static int tcp_header_encode(tcp_sockpair_t *sp, tcp_segment_t *seg,
    194193    void **header, size_t *size)
    195194{
     
    200199                return ENOMEM;
    201200
    202         tcp_header_setup(epp, seg, hdr);
     201        tcp_header_setup(sp, seg, hdr);
    203202        *header = hdr;
    204203        *size = sizeof(tcp_header_t);
     
    294293
    295294/** Decode incoming PDU */
    296 int tcp_pdu_decode(tcp_pdu_t *pdu, inet_ep2_t *epp, tcp_segment_t **seg)
     295int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg)
    297296{
    298297        tcp_segment_t *nseg;
     
    308307        hdr = (tcp_header_t *)pdu->header;
    309308
    310         epp->local.port = uint16_t_be2host(hdr->dest_port);
    311         epp->local.addr = pdu->dest;
    312         epp->remote.port = uint16_t_be2host(hdr->src_port);
    313         epp->remote.addr = pdu->src;
     309        sp->local.port = uint16_t_be2host(hdr->dest_port);
     310        sp->local.addr = pdu->dest;
     311        sp->foreign.port = uint16_t_be2host(hdr->src_port);
     312        sp->foreign.addr = pdu->src;
    314313
    315314        *seg = nseg;
     
    318317
    319318/** Encode outgoing PDU */
    320 int tcp_pdu_encode(inet_ep2_t *epp, tcp_segment_t *seg, tcp_pdu_t **pdu)
     319int tcp_pdu_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu)
    321320{
    322321        tcp_pdu_t *npdu;
     
    328327                return ENOMEM;
    329328
    330         npdu->src = epp->local.addr;
    331         npdu->dest = epp->remote.addr;
    332         tcp_header_encode(epp, seg, &npdu->header, &npdu->header_size);
     329        npdu->src = sp->local.addr;
     330        npdu->dest = sp->foreign.addr;
     331        tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size);
    333332
    334333        text_size = tcp_segment_text_size(seg);
  • uspace/srv/net/tcp/pdu.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define PDU_H
    3737
    38 #include <inet/endpoint.h>
    3938#include <sys/types.h>
    4039#include "std.h"
     
    4342extern tcp_pdu_t *tcp_pdu_create(void *, size_t, void *, size_t);
    4443extern void tcp_pdu_delete(tcp_pdu_t *);
    45 extern int tcp_pdu_decode(tcp_pdu_t *, inet_ep2_t *, tcp_segment_t **);
    46 extern int tcp_pdu_encode(inet_ep2_t *, tcp_segment_t *, tcp_pdu_t **);
     44extern int tcp_pdu_decode(tcp_pdu_t *, tcp_sockpair_t *, tcp_segment_t **);
     45extern int tcp_pdu_encode(tcp_sockpair_t *, tcp_segment_t *, tcp_pdu_t **);
    4746
    4847#endif
  • uspace/srv/net/tcp/rqueue.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    6767 * This is for testing purposes only.
    6868 *
    69  * @param sp    Endpoint pair, oriented for transmission
     69 * @param sp    Socket pair, oriented for transmission
    7070 * @param seg   Segment
    7171 */
    72 void tcp_rqueue_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
     72void tcp_rqueue_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
    7373{
    74         inet_ep2_t rident;
     74        tcp_sockpair_t rident;
    7575
    7676        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_bounce_seg()");
     
    8080        tcp_segment_t *dseg;
    8181
    82         if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
     82        if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
    8383                log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
    8484                return;
     
    9797#else
    9898        /* Reverse the identification */
    99         tcp_ep2_flipped(epp, &rident);
     99        tcp_sockpair_flipped(sp, &rident);
    100100
    101101        /* Insert segment back into rqueue */
     
    106106/** Insert segment into receive queue.
    107107 *
    108  * @param epp   Endpoint pair, oriented for reception
     108 * @param sp    Socket pair, oriented for reception
    109109 * @param seg   Segment
    110110 */
    111 void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
     111void tcp_rqueue_insert_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
    112112{
    113113        tcp_rqueue_entry_t *rqe;
     
    122122        }
    123123
    124         rqe->epp = *epp;
     124        rqe->sp = *sp;
    125125        rqe->seg = seg;
    126126
     
    140140                rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
    141141
    142                 tcp_as_segment_arrived(&rqe->epp, rqe->seg);
     142                tcp_as_segment_arrived(&rqe->sp, rqe->seg);
    143143                free(rqe);
    144144        }
  • uspace/srv/net/tcp/rqueue.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define RQUEUE_H
    3737
    38 #include <inet/endpoint.h>
    3938#include "tcp_type.h"
    4039
    4140extern void tcp_rqueue_init(void);
    42 extern void tcp_rqueue_bounce_seg(inet_ep2_t *, tcp_segment_t *);
    43 extern void tcp_rqueue_insert_seg(inet_ep2_t *, tcp_segment_t *);
     41extern void tcp_rqueue_bounce_seg(tcp_sockpair_t *, tcp_segment_t *);
     42extern void tcp_rqueue_insert_seg(tcp_sockpair_t *, tcp_segment_t *);
    4443extern void tcp_rqueue_handler(void *);
    4544extern void tcp_rqueue_fibril_start(void);
  • uspace/srv/net/tcp/tcp.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4242#include <io/log.h>
    4343#include <stdio.h>
    44 #include <stdlib.h>
    4544#include <task.h>
    4645
    47 #include "conn.h"
    4846#include "ncsim.h"
    4947#include "pdu.h"
    5048#include "rqueue.h"
    51 #include "service.h"
     49#include "sock.h"
    5250#include "std.h"
    5351#include "tcp.h"
     
    161159{
    162160        tcp_segment_t *dseg;
    163         inet_ep2_t rident;
     161        tcp_sockpair_t rident;
    164162
    165163        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
     
    179177
    180178        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_init()");
    181 
    182         rc = tcp_conns_init();
    183         if (rc != EOK) {
    184                 assert(rc == ENOMEM);
    185                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing connections");
    186                 return ENOMEM;
    187         }
    188179
    189180        tcp_rqueue_init();
     
    201192        }
    202193
    203         rc = tcp_service_init();
     194        rc = tcp_sock_init();
    204195        if (rc != EOK) {
    205                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing service.");
     196                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
    206197                return ENOENT;
    207198        }
  • uspace/srv/net/tcp/tcp_type.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4141#include <fibril.h>
    4242#include <fibril_synch.h>
     43#include <socket_core.h>
    4344#include <sys/types.h>
    4445#include <inet/addr.h>
    45 #include <inet/endpoint.h>
    4646
    4747struct tcp_conn;
     
    9090        /* Connection reset */
    9191        TCP_ERESET,
    92         /* Remote endpoint unspecified */
     92        /* Foreign socket unspecified */
    9393        TCP_EUNSPEC,
    9494        /* Insufficient resources */
     
    9797        TCP_EINVPREC,
    9898        /* Security/compartment not allowed */
    99         TCP_EINVCOMP,
    100         TCP_EAGAIN
     99        TCP_EINVCOMP
    101100} tcp_error_t;
    102101
     
    113112} tcp_control_t;
    114113
     114typedef struct {
     115        inet_addr_t addr;
     116        uint16_t port;
     117} tcp_sock_t;
     118
     119enum tcp_port {
     120        TCP_PORT_ANY = 0
     121};
     122
     123typedef struct {
     124        tcp_sock_t local;
     125        tcp_sock_t foreign;
     126} tcp_sockpair_t;
     127
    115128/** Connection incoming segments queue */
    116129typedef struct {
     
    142155typedef void (*tcp_cstate_cb_t)(tcp_conn_t *, void *);
    143156
    144 /** Connection callbacks */
    145 typedef struct {
    146         void (*cstate_change)(tcp_conn_t *, void *, tcp_cstate_t);
    147         void (*recv_data)(tcp_conn_t *, void *);
    148 } tcp_cb_t;
    149 
    150157/** Connection */
    151158struct tcp_conn {
     
    153160        link_t link;
    154161
    155         /** Connection callbacks function */
    156         tcp_cb_t *cb;
     162        /** Connection state change callback function */
     163        tcp_cstate_cb_t cstate_cb;
    157164        /** Argument to @c cstate_cb */
    158         void *cb_arg;
    159 
    160         /** Connection identification (local and remote endpoint) */
    161         inet_ep2_t ident;
     165        void *cstate_cb_arg;
     166
     167        /** Connection identification (local and foreign socket) */
     168        tcp_sockpair_t ident;
    162169
    163170        /** Active or passive connection */
     
    267274typedef struct {
    268275        link_t link;
    269         inet_ep2_t epp;
     276        tcp_sockpair_t sp;
    270277        tcp_segment_t *seg;
    271278} tcp_rqueue_entry_t;
     
    275282        link_t link;
    276283        suseconds_t delay;
    277         inet_ep2_t epp;
     284        tcp_sockpair_t sp;
    278285        tcp_segment_t *seg;
    279286} tcp_squeue_entry_t;
     
    312319} tcp_pdu_t;
    313320
    314 /** TCP client connection */
    315 typedef struct tcp_cconn {
     321typedef struct {
     322        async_sess_t *sess;
     323        socket_cores_t sockets;
     324} tcp_client_t;
     325
     326#define TCP_SOCK_FRAGMENT_SIZE 1024
     327
     328typedef struct tcp_sockdata {
     329        /** Lock */
     330        fibril_mutex_t lock;
     331        /** Socket core */
     332        socket_core_t *sock_core;
     333        /** Client */
     334        tcp_client_t *client;
    316335        /** Connection */
    317336        tcp_conn_t *conn;
    318         /** Connection ID for the client */
    319         sysarg_t id;
    320         /** Client */
    321         struct tcp_client *client;
    322         link_t lclient;
    323 } tcp_cconn_t;
    324 
    325 /** TCP client listener */
    326 typedef struct tcp_clst {
    327         /** Local endpoint */
    328         inet_ep_t elocal;
    329         /** Connection */
     337        /** Local address */
     338        inet_addr_t laddr;
     339        /** Backlog size */
     340        int backlog;
     341        /** Array of listening connections, @c backlog elements */
     342        struct tcp_sock_lconn **lconn;
     343        /** List of connections (from lconn) that are ready to be accepted */
     344        list_t ready;
     345        /** Receiving fibril */
     346        fid_t recv_fibril;
     347        uint8_t recv_buffer[TCP_SOCK_FRAGMENT_SIZE];
     348        size_t recv_buffer_used;
     349        fibril_mutex_t recv_buffer_lock;
     350        fibril_condvar_t recv_buffer_cv;
     351        tcp_error_t recv_error;
     352} tcp_sockdata_t;
     353
     354typedef struct tcp_sock_lconn {
    330355        tcp_conn_t *conn;
    331         /** Listener ID for the client */
    332         sysarg_t id;
    333         /** Client */
    334         struct tcp_client *client;
    335         /** Link to tcp_client_t.clst */
    336         link_t lclient;
    337 } tcp_clst_t;
    338 
    339 /** TCP client */
    340 typedef struct tcp_client {
    341         /** Client callbac session */
    342         async_sess_t *sess;
    343         /** Client's connections */
    344         list_t cconn; /* of tcp_cconn_t */
    345         /** Client's listeners */
    346         list_t clst;
    347 } tcp_client_t;
     356        tcp_sockdata_t *socket;
     357        int index;
     358        link_t ready_list;
     359} tcp_sock_lconn_t;
     360
    348361
    349362#endif
  • uspace/srv/net/tcp/test.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5050{
    5151        tcp_conn_t *conn;
    52         inet_ep2_t epp;
     52        tcp_sock_t lsock;
     53        tcp_sock_t fsock;
    5354        char rcv_buf[RCV_BUF_SIZE + 1];
    5455        size_t rcvd;
     
    5657
    5758        printf("test_srv()\n");
    58 
    59         inet_ep2_init(&epp);
    60 
    61         inet_addr(&epp.local.addr, 127, 0, 0, 1);
    62         epp.local.port = 80;
    63 
    64         inet_addr(&epp.remote.addr, 127, 0, 0, 1);
    65         epp.remote.port = 1024;
    66 
     59       
     60        inet_addr(&lsock.addr, 127, 0, 0, 1);
     61        lsock.port = 80;
     62       
     63        inet_addr(&fsock.addr, 127, 0, 0, 1);
     64        fsock.port = 1024;
     65       
    6766        printf("S: User open...\n");
    68         tcp_uc_open(&epp, ap_passive, 0, &conn);
     67        tcp_uc_open(&lsock, &fsock, ap_passive, 0, &conn);
    6968        conn->name = (char *) "S";
    7069
     
    9493{
    9594        tcp_conn_t *conn;
    96         inet_ep2_t epp;
     95        tcp_sock_t lsock;
     96        tcp_sock_t fsock;
    9797        const char *msg = "Hello World!";
    9898
    9999        printf("test_cli()\n");
    100 
    101         inet_ep2_init(&epp);
    102 
    103         inet_addr(&epp.local.addr, 127, 0, 0, 1);
    104         epp.local.port = 1024;
    105 
    106         inet_addr(&epp.remote.addr, 127, 0, 0, 1);
    107         epp.remote.port = 80;
     100       
     101        inet_addr(&lsock.addr, 127, 0, 0, 1);
     102        lsock.port = 1024;
     103       
     104        inet_addr(&fsock.addr, 127, 0, 0, 1);
     105        fsock.port = 80;
    108106
    109107        async_usleep(1000*1000*3);
    110108        printf("C: User open...\n");
    111         tcp_uc_open(&epp, ap_active, 0, &conn);
     109        tcp_uc_open(&lsock, &fsock, ap_active, 0, &conn);
    112110        conn->name = (char *) "C";
    113111
  • uspace/srv/net/tcp/tqueue.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    282282}
    283283
    284 void tcp_transmit_segment(inet_ep2_t *epp, tcp_segment_t *seg)
     284void tcp_transmit_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
    285285{
    286286        log_msg(LOG_DEFAULT, LVL_DEBUG,
    287             "tcp_transmit_segment(l:(%u),f:(%u), %p)",
    288             epp->local.port, epp->remote.port, seg);
    289 
     287            "tcp_transmit_segment(f:(%u),l:(%u), %p)",
     288            sp->local.port, sp->foreign.port, seg);
     289       
    290290        log_msg(LOG_DEFAULT, LVL_DEBUG, "SEG.SEQ=%" PRIu32 ", SEG.WND=%" PRIu32,
    291291            seg->seq, seg->wnd);
     
    301301        tcp_pdu_t *pdu;
    302302
    303         if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
     303        if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
    304304                log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
    305305                return;
     
    351351
    352352        /* Reset retransmission timer */
    353         fibril_timer_set_locked(conn->retransmit.timer, RETRANSMIT_TIMEOUT,
    354             retransmit_timeout_func, (void *) conn);
     353        tcp_tqueue_timer_set(tqe->conn);
    355354
    356355        tcp_conn_unlock(conn);
     356        tcp_conn_delref(conn);
    357357
    358358        log_msg(LOG_DEFAULT, LVL_DEBUG, "### %s: retransmit_timeout_func(%p) end", conn->name, conn);
  • uspace/srv/net/tcp/tqueue.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define TQUEUE_H
    3737
    38 #include <inet/endpoint.h>
    3938#include "std.h"
    4039#include "tcp_type.h"
     
    4948extern void tcp_prepare_transmit_segment(tcp_conn_t *, tcp_segment_t *);
    5049extern void tcp_conn_transmit_segment(tcp_conn_t *, tcp_segment_t *);
    51 extern void tcp_transmit_segment(inet_ep2_t *, tcp_segment_t *);
     50extern void tcp_transmit_segment(tcp_sockpair_t *, tcp_segment_t *);
    5251extern void tcp_header_setup(tcp_conn_t *, tcp_segment_t *, tcp_header_t *);
    5352extern void tcp_phdr_setup(tcp_conn_t *, tcp_segment_t *, tcp_phdr_t *);
  • uspace/srv/net/tcp/ucall.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3535 */
    3636
    37 #include <errno.h>
    3837#include <fibril_synch.h>
    3938#include <io/log.h>
     
    5150/** OPEN user call
    5251 *
    53  * @param epp           Endpoint pair
     52 * @param lsock         Local socket
     53 * @param fsock         Foreign socket
    5454 * @param acpass        Active/passive
    5555 * @param oflags        Open flags
     
    6565 * establishment.
    6666 */
    67 tcp_error_t tcp_uc_open(inet_ep2_t *epp, acpass_t acpass,
     67tcp_error_t tcp_uc_open(tcp_sock_t *lsock, tcp_sock_t *fsock, acpass_t acpass,
    6868    tcp_open_flags_t oflags, tcp_conn_t **conn)
    6969{
    7070        tcp_conn_t *nconn;
    71         int rc;
    72 
    73         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %s, %s, %p)",
    74             epp, acpass == ap_active ? "active" : "passive",
     71
     72        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %p, %s, %s, %p)",
     73            lsock, fsock, acpass == ap_active ? "active" : "passive",
    7574            oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
    7675
    77         nconn = tcp_conn_new(epp);
    78         rc = tcp_conn_add(nconn);
    79         if (rc != EOK) {
    80                 tcp_conn_delete(nconn);
    81                 return TCP_EEXISTS;
    82         }
    83 
     76        nconn = tcp_conn_new(lsock, fsock);
     77        tcp_conn_add(nconn);
    8478        tcp_conn_lock(nconn);
    8579
     
    194188        /* Wait for data to become available */
    195189        while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
    196                 tcp_conn_unlock(conn);
    197                 return TCP_EAGAIN;
    198190                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
    199191                fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
     
    258250                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - listen/syn_sent");
    259251                tcp_conn_reset(conn);
     252                tcp_conn_remove(conn);
    260253                tcp_conn_unlock(conn);
    261254                return TCP_EOK;
     
    301294}
    302295
    303 void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
    304 {
    305         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
     296void tcp_uc_set_cstate_cb(tcp_conn_t *conn, tcp_cstate_cb_t cb, void *arg)
     297{
     298        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_ctate_cb(%p, %p, %p)",
    306299            conn, cb, arg);
    307300
    308         conn->cb = cb;
    309         conn->cb_arg = arg;
    310 }
    311 
    312 void *tcp_uc_get_userptr(tcp_conn_t *conn)
    313 {
    314         return conn->cb_arg;
     301        conn->cstate_cb = cb;
     302        conn->cstate_cb_arg = arg;
    315303}
    316304
     
    320308
    321309/** Segment arrived */
    322 void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
     310void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
    323311{
    324312        tcp_conn_t *conn;
     
    326314        log_msg(LOG_DEFAULT, LVL_DEBUG,
    327315            "tcp_as_segment_arrived(f:(%u), l:(%u))",
    328             epp->remote.port, epp->local.port);
    329 
    330         conn = tcp_conn_find_ref(epp);
     316            sp->foreign.port, sp->local.port);
     317
     318        conn = tcp_conn_find_ref(sp);
    331319        if (conn == NULL) {
    332320                log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
    333                 tcp_unexpected_segment(epp, seg);
     321                tcp_unexpected_segment(sp, seg);
    334322                return;
    335323        }
    336324
    337         tcp_conn_segment_arrived(conn, epp, seg);
     325        tcp_conn_lock(conn);
     326
     327        if (conn->cstate == st_closed) {
     328                log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
     329                tcp_unexpected_segment(sp, seg);
     330                tcp_conn_unlock(conn);
     331                tcp_conn_delref(conn);
     332                return;
     333        }
     334
     335        if (inet_addr_is_any(&conn->ident.foreign.addr))
     336                conn->ident.foreign.addr = sp->foreign.addr;
     337       
     338        if (conn->ident.foreign.port == TCP_PORT_ANY)
     339                conn->ident.foreign.port = sp->foreign.port;
     340       
     341        if (inet_addr_is_any(&conn->ident.local.addr))
     342                conn->ident.local.addr = sp->local.addr;
     343
     344        tcp_conn_segment_arrived(conn, seg);
     345
     346        tcp_conn_unlock(conn);
    338347        tcp_conn_delref(conn);
    339348}
  • uspace/srv/net/tcp/ucall.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2011 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define UCALL_H
    3737
    38 #include <inet/endpoint.h>
    3938#include <sys/types.h>
    4039#include "tcp_type.h"
     
    4342 * User calls
    4443 */
    45 extern tcp_error_t tcp_uc_open(inet_ep2_t *, acpass_t,
     44extern tcp_error_t tcp_uc_open(tcp_sock_t *, tcp_sock_t *, acpass_t,
    4645    tcp_open_flags_t, tcp_conn_t **);
    4746extern tcp_error_t tcp_uc_send(tcp_conn_t *, void *, size_t, xflags_t);
     
    5150extern void tcp_uc_status(tcp_conn_t *, tcp_conn_status_t *);
    5251extern void tcp_uc_delete(tcp_conn_t *);
    53 extern void tcp_uc_set_cb(tcp_conn_t *, tcp_cb_t *, void *);
    54 extern void *tcp_uc_get_userptr(tcp_conn_t *);
     52extern void tcp_uc_set_cstate_cb(tcp_conn_t *, tcp_cstate_cb_t, void *);
    5553
    5654/*
    5755 * Arriving segments
    5856 */
    59 extern void tcp_as_segment_arrived(inet_ep2_t *, tcp_segment_t *);
     57extern void tcp_as_segment_arrived(tcp_sockpair_t *, tcp_segment_t *);
    6058
    6159/*
  • uspace/srv/net/udp/Makefile

    ree1c2d9 r0453261  
    2828
    2929USPACE_PREFIX = ../../..
    30 
    31 LIBS = \
    32         $(LIBNETTL_PREFIX)/libnettl.a
    33 
    34 EXTRA_CFLAGS += \
    35         -I$(LIBNETTL_PREFIX)/include
    36 
     30LIBS = $(LIBNET_PREFIX)/libnet.a
     31EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include
    3732BINARY = udp
    3833
     
    4035        assoc.c \
    4136        msg.c \
     37        sock.c \
    4238        pdu.c \
    43         service.c \
     39        ucall.c \
    4440        udp.c \
    4541        udp_inet.c
  • uspace/srv/net/udp/assoc.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636
    3737#include <adt/list.h>
    38 #include <errno.h>
    3938#include <stdbool.h>
    4039#include <fibril_synch.h>
    41 #include <inet/endpoint.h>
    4240#include <io/log.h>
    43 #include <nettl/amap.h>
    4441#include <stdlib.h>
    4542
     
    4744#include "msg.h"
    4845#include "pdu.h"
     46#include "ucall.h"
    4947#include "udp_inet.h"
    5048#include "udp_type.h"
    5149
    52 static LIST_INITIALIZE(assoc_list);
    53 static FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
    54 static amap_t *amap;
    55 
    56 static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *);
    57 static int udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *);
    58 
    59 /** Initialize associations. */
    60 int udp_assocs_init(void)
    61 {
    62         int rc;
    63 
    64         rc = amap_create(&amap);
    65         if (rc != EOK) {
    66                 assert(rc == ENOMEM);
    67                 return ENOMEM;
    68         }
    69 
    70         return EOK;
    71 }
     50LIST_INITIALIZE(assoc_list);
     51FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
     52
     53static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *);
     54static int udp_assoc_queue_msg(udp_assoc_t *, udp_sockpair_t *, udp_msg_t *);
     55static bool udp_socket_match(udp_sock_t *, udp_sock_t *);
     56static bool udp_sockpair_match(udp_sockpair_t *, udp_sockpair_t *);
    7257
    7358/** Create new association structure.
    7459 *
    75  * @param epp           Endpoint pair (will be copied)
    76  * @param cb            Callbacks
    77  * @param cb_arg        Callback argument
     60 * @param lsock         Local socket (will be deeply copied)
     61 * @param fsock         Foreign socket (will be deeply copied)
    7862 * @return              New association or NULL
    7963 */
    80 udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg)
     64udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock)
    8165{
    8266        udp_assoc_t *assoc = NULL;
     
    9680        fibril_condvar_initialize(&assoc->rcv_queue_cv);
    9781
    98         if (epp != NULL)
    99                 assoc->ident = *epp;
    100 
    101         assoc->cb = cb;
    102         assoc->cb_arg = cb_arg;
     82        if (lsock != NULL)
     83                assoc->ident.local = *lsock;
     84       
     85        if (fsock != NULL)
     86                assoc->ident.foreign = *fsock;
     87
    10388        return assoc;
    10489error:
     
    181166 * Add association to the association map.
    182167 */
    183 int udp_assoc_add(udp_assoc_t *assoc)
    184 {
    185         inet_ep2_t aepp;
    186         int rc;
    187 
     168void udp_assoc_add(udp_assoc_t *assoc)
     169{
    188170        udp_assoc_addref(assoc);
    189171        fibril_mutex_lock(&assoc_list_lock);
    190 
    191         rc = amap_insert(amap, &assoc->ident, assoc, af_allow_system, &aepp);
    192         if (rc != EOK) {
    193                 udp_assoc_delref(assoc);
    194                 fibril_mutex_unlock(&assoc_list_lock);
    195                 return rc;
    196         }
    197 
    198         assoc->ident = aepp;
    199172        list_append(&assoc->link, &assoc_list);
    200173        fibril_mutex_unlock(&assoc_list_lock);
    201 
    202         return EOK;
    203174}
    204175
     
    210181{
    211182        fibril_mutex_lock(&assoc_list_lock);
    212         amap_remove(amap, &assoc->ident);
    213183        list_remove(&assoc->link);
    214184        fibril_mutex_unlock(&assoc_list_lock);
     
    226196            assoc, iplink);
    227197        fibril_mutex_lock(&assoc->lock);
    228         assoc->ident.local_link = iplink;
     198        assoc->ident.iplink = iplink;
     199        fibril_mutex_unlock(&assoc->lock);
     200}
     201
     202/** Set foreign socket in association.
     203 *
     204 * @param assoc         Association
     205 * @param fsock         Foreign socket (deeply copied)
     206 */
     207void udp_assoc_set_foreign(udp_assoc_t *assoc, udp_sock_t *fsock)
     208{
     209        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_foreign(%p, %p)", assoc, fsock);
     210        fibril_mutex_lock(&assoc->lock);
     211        assoc->ident.foreign = *fsock;
     212        fibril_mutex_unlock(&assoc->lock);
     213}
     214
     215/** Set local socket in association.
     216 *
     217 * @param assoc Association
     218 * @param lsock Local socket (deeply copied)
     219 *
     220 */
     221void udp_assoc_set_local(udp_assoc_t *assoc, udp_sock_t *lsock)
     222{
     223        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %p)", assoc, lsock);
     224        fibril_mutex_lock(&assoc->lock);
     225        assoc->ident.local = *lsock;
     226        fibril_mutex_unlock(&assoc->lock);
     227}
     228
     229/** Set local port in association.
     230 *
     231 * @param assoc Association
     232 * @param lport Local port
     233 *
     234 */
     235void udp_assoc_set_local_port(udp_assoc_t *assoc, uint16_t lport)
     236{
     237        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %" PRIu16 ")", assoc, lport);
     238        fibril_mutex_lock(&assoc->lock);
     239        assoc->ident.local.port = lport;
    229240        fibril_mutex_unlock(&assoc->lock);
    230241}
     
    233244 *
    234245 * @param assoc         Association
    235  * @param remote        Remote endpoint or NULL not to override @a assoc
     246 * @param fsock         Foreign socket or NULL not to override @a assoc
    236247 * @param msg           Message
    237248 *
    238249 * @return              EOK on success
    239  *                      EINVAL if remote endpoint is not set
     250 *                      EINVAL if foreign socket is not set
    240251 *                      ENOMEM if out of resources
    241252 *                      EIO if no route to destination exists
    242253 */
    243 int udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg)
     254int udp_assoc_send(udp_assoc_t *assoc, udp_sock_t *fsock, udp_msg_t *msg)
    244255{
    245256        udp_pdu_t *pdu;
    246         inet_ep2_t epp;
     257        udp_sockpair_t sp;
    247258        int rc;
    248259
    249260        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
    250             assoc, remote, msg);
    251 
    252         /* @a remote can be used to override the remote endpoint */
    253         epp = assoc->ident;
    254         if (remote != NULL)
    255                 epp.remote = *remote;
    256 
    257         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check addr any");
    258 
    259         if ((inet_addr_is_any(&epp.remote.addr)) ||
    260             (epp.remote.port == inet_port_any))
     261            assoc, fsock, msg);
     262
     263        /* @a fsock can be used to override the foreign socket */
     264        sp = assoc->ident;
     265        if (fsock != NULL)
     266                sp.foreign = *fsock;
     267
     268        if ((inet_addr_is_any(&sp.foreign.addr)) ||
     269            (sp.foreign.port == UDP_PORT_ANY))
    261270                return EINVAL;
    262271
    263         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check version");
    264 
    265         if (epp.remote.addr.version != epp.local.addr.version)
    266                 return EINVAL;
    267 
    268         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - encode pdu");
    269 
    270         rc = udp_pdu_encode(&epp, msg, &pdu);
     272        rc = udp_pdu_encode(&sp, msg, &pdu);
    271273        if (rc != EOK)
    272274                return ENOMEM;
    273275
    274         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - transmit");
    275 
    276276        rc = udp_transmit_pdu(pdu);
    277277        udp_pdu_delete(pdu);
     
    280280                return EIO;
    281281
    282         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - success");
    283282        return EOK;
    284283}
     
    288287 * Pull one message from the association's receive queue.
    289288 */
    290 int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
     289int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, udp_sock_t *fsock)
    291290{
    292291        link_t *link;
     
    304303                log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset");
    305304                fibril_mutex_unlock(&assoc->lock);
    306                 return ENXIO;
     305                return ECONNABORTED;
    307306        }
    308307
     
    314313
    315314        *msg = rqe->msg;
    316         *remote = rqe->epp.remote;
     315        *fsock = rqe->sp.foreign;
    317316        free(rqe);
    318317
     
    324323 * Find the association to which the message belongs and queue it.
    325324 */
    326 void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
     325void udp_assoc_received(udp_sockpair_t *rsp, udp_msg_t *msg)
    327326{
    328327        udp_assoc_t *assoc;
    329328        int rc;
    330329
    331         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", repp, msg);
    332 
    333         assoc = udp_assoc_find_ref(repp);
     330        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", rsp, msg);
     331
     332        assoc = udp_assoc_find_ref(rsp);
    334333        if (assoc == NULL) {
    335334                log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
     
    340339        }
    341340
    342         if (0) {
    343                 rc = udp_assoc_queue_msg(assoc, repp, msg);
    344                 if (rc != EOK) {
    345                         log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
     341        rc = udp_assoc_queue_msg(assoc, rsp, msg);
     342        if (rc != EOK) {
     343                log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
    346344                /* XXX Generate ICMP error? */
    347                 }
    348         }
    349 
    350         log_msg(LOG_DEFAULT, LVL_DEBUG, "call assoc->cb->recv_msg");
    351         assoc->cb->recv_msg(assoc->cb_arg, repp, msg);
    352         udp_assoc_delref(assoc);
     345        }
    353346}
    354347
     
    366359}
    367360
    368 static int udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
     361static int udp_assoc_queue_msg(udp_assoc_t *assoc, udp_sockpair_t *sp,
    369362    udp_msg_t *msg)
    370363{
     
    372365
    373366        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
    374             assoc, epp, msg);
     367            assoc, sp, msg);
    375368
    376369        rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
     
    379372
    380373        link_initialize(&rqe->link);
    381         rqe->epp = *epp;
     374        rqe->sp = *sp;
    382375        rqe->msg = msg;
    383376
     
    391384}
    392385
    393 /** Find association structure for specified endpoint pair.
    394  *
    395  * An association is uniquely identified by an endpoint pair. Look up our
    396  * association map and return association structure based on endpoint pair.
     386/** Match socket with pattern. */
     387static bool udp_socket_match(udp_sock_t *sock, udp_sock_t *patt)
     388{
     389        log_msg(LOG_DEFAULT, LVL_DEBUG,
     390            "udp_socket_match(sock=(%u), pat=(%u))", sock->port, patt->port);
     391       
     392        if ((!inet_addr_is_any(&patt->addr)) &&
     393            (!inet_addr_compare(&patt->addr, &sock->addr)))
     394                return false;
     395       
     396        if ((patt->port != UDP_PORT_ANY) &&
     397            (patt->port != sock->port))
     398                return false;
     399       
     400        log_msg(LOG_DEFAULT, LVL_DEBUG, " -> match");
     401       
     402        return true;
     403}
     404
     405/** Match socket pair with pattern. */
     406static bool udp_sockpair_match(udp_sockpair_t *sp, udp_sockpair_t *pattern)
     407{
     408        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sockpair_match(%p, %p)", sp, pattern);
     409
     410        if (!udp_socket_match(&sp->local, &pattern->local))
     411                return false;
     412
     413        if (!udp_socket_match(&sp->foreign, &pattern->foreign))
     414                return false;
     415
     416        log_msg(LOG_DEFAULT, LVL_DEBUG, "Socket pair matched.");
     417        return true;
     418}
     419
     420
     421/** Find association structure for specified socket pair.
     422 *
     423 * An association is uniquely identified by a socket pair. Look up our
     424 * association map and return association structure based on socket pair.
    397425 * The association reference count is bumped by one.
    398426 *
    399  * @param epp   Endpoint pair
     427 * @param sp    Socket pair
    400428 * @return      Association structure or NULL if not found.
    401429 */
    402 static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp)
    403 {
    404         int rc;
    405         void *arg;
    406         udp_assoc_t *assoc;
    407 
    408         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", epp);
     430static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *sp)
     431{
     432        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", sp);
     433       
    409434        fibril_mutex_lock(&assoc_list_lock);
    410 
    411         rc = amap_find_match(amap, epp, &arg);
    412         if (rc != EOK) {
    413                 assert(rc == ENOMEM);
    414                 fibril_mutex_unlock(&assoc_list_lock);
    415                 return NULL;
    416         }
    417 
    418         assoc = (udp_assoc_t *)arg;
    419         udp_assoc_addref(assoc);
    420 
     435       
     436        list_foreach(assoc_list, link, udp_assoc_t, assoc) {
     437                udp_sockpair_t *asp = &assoc->ident;
     438               
     439                /* Skip unbound associations */
     440                if (asp->local.port == UDP_PORT_ANY)
     441                        continue;
     442               
     443                if (udp_sockpair_match(sp, asp)) {
     444                        log_msg(LOG_DEFAULT, LVL_DEBUG, "Returning assoc %p", assoc);
     445                        udp_assoc_addref(assoc);
     446                        fibril_mutex_unlock(&assoc_list_lock);
     447                        return assoc;
     448                }
     449        }
     450       
    421451        fibril_mutex_unlock(&assoc_list_lock);
    422         return assoc;
     452        return NULL;
    423453}
    424454
  • uspace/srv/net/udp/assoc.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define ASSOC_H
    3737
    38 #include <inet/endpoint.h>
    3938#include <ipc/loc.h>
    4039#include <sys/types.h>
    4140#include "udp_type.h"
    4241
    43 extern int udp_assocs_init(void);
    44 extern udp_assoc_t *udp_assoc_new(inet_ep2_t *, udp_assoc_cb_t *, void *);
     42extern udp_assoc_t *udp_assoc_new(udp_sock_t *, udp_sock_t *);
    4543extern void udp_assoc_delete(udp_assoc_t *);
    46 extern int udp_assoc_add(udp_assoc_t *);
     44extern void udp_assoc_add(udp_assoc_t *);
    4745extern void udp_assoc_remove(udp_assoc_t *);
    4846extern void udp_assoc_addref(udp_assoc_t *);
    4947extern void udp_assoc_delref(udp_assoc_t *);
    5048extern void udp_assoc_set_iplink(udp_assoc_t *, service_id_t);
    51 extern int udp_assoc_send(udp_assoc_t *, inet_ep_t *, udp_msg_t *);
    52 extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, inet_ep_t *);
    53 extern void udp_assoc_received(inet_ep2_t *, udp_msg_t *);
     49extern void udp_assoc_set_foreign(udp_assoc_t *, udp_sock_t *);
     50extern void udp_assoc_set_local(udp_assoc_t *, udp_sock_t *);
     51extern void udp_assoc_set_local_port(udp_assoc_t *, uint16_t);
     52extern int udp_assoc_send(udp_assoc_t *, udp_sock_t *, udp_msg_t *);
     53extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, udp_sock_t *);
     54extern void udp_assoc_received(udp_sockpair_t *, udp_msg_t *);
    5455extern void udp_assoc_reset(udp_assoc_t *);
    5556
  • uspace/srv/net/udp/msg.c

    ree1c2d9 r0453261  
    5050void udp_msg_delete(udp_msg_t *msg)
    5151{
    52         free(msg->data);
    5352        free(msg);
    5453}
  • uspace/srv/net/udp/pdu.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4141#include <stdlib.h>
    4242#include <inet/addr.h>
     43#include <net/socket_codes.h>
    4344#include "msg.h"
    4445#include "pdu.h"
     
    162163
    163164/** Decode incoming PDU */
    164 int udp_pdu_decode(udp_pdu_t *pdu, inet_ep2_t *epp, udp_msg_t **msg)
     165int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
    165166{
    166167        udp_msg_t *nmsg;
     
    179180        hdr = (udp_header_t *)pdu->data;
    180181
    181         epp->local_link = pdu->iplink;
    182         epp->remote.port = uint16_t_be2host(hdr->src_port);
    183         epp->remote.addr = pdu->src;
    184         epp->local.port = uint16_t_be2host(hdr->dest_port);
    185         epp->local.addr = pdu->dest;
     182        sp->foreign.port = uint16_t_be2host(hdr->src_port);
     183        sp->foreign.addr = pdu->src;
     184        sp->local.port = uint16_t_be2host(hdr->dest_port);
     185        sp->local.addr = pdu->dest;
    186186
    187187        length = uint16_t_be2host(hdr->length);
     
    197197                return ENOMEM;
    198198
     199        nmsg->data = text;
    199200        nmsg->data_size = length - sizeof(udp_header_t);
    200         nmsg->data = malloc(nmsg->data_size);
    201         if (nmsg->data == NULL)
    202                 return ENOMEM;
    203 
    204         memcpy(nmsg->data, text, nmsg->data_size);
    205201
    206202        *msg = nmsg;
     
    209205
    210206/** Encode outgoing PDU */
    211 int udp_pdu_encode(inet_ep2_t *epp, udp_msg_t *msg, udp_pdu_t **pdu)
     207int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
    212208{
    213209        udp_pdu_t *npdu;
     
    219215                return ENOMEM;
    220216
    221         npdu->iplink = epp->local_link;
    222         npdu->src = epp->local.addr;
    223         npdu->dest = epp->remote.addr;
     217        npdu->iplink = sp->iplink;
     218        npdu->src = sp->local.addr;
     219        npdu->dest = sp->foreign.addr;
    224220
    225221        npdu->data_size = sizeof(udp_header_t) + msg->data_size;
     
    231227
    232228        hdr = (udp_header_t *)npdu->data;
    233         hdr->src_port = host2uint16_t_be(epp->local.port);
    234         hdr->dest_port = host2uint16_t_be(epp->remote.port);
     229        hdr->src_port = host2uint16_t_be(sp->local.port);
     230        hdr->dest_port = host2uint16_t_be(sp->foreign.port);
    235231        hdr->length = host2uint16_t_be(npdu->data_size);
    236232        hdr->checksum = 0;
  • uspace/srv/net/udp/pdu.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define PDU_H
    3737
    38 #include <inet/endpoint.h>
    3938#include <sys/types.h>
    4039#include "std.h"
     
    4342extern udp_pdu_t *udp_pdu_new(void);
    4443extern void udp_pdu_delete(udp_pdu_t *);
    45 extern int udp_pdu_decode(udp_pdu_t *, inet_ep2_t *, udp_msg_t **);
    46 extern int udp_pdu_encode(inet_ep2_t *, udp_msg_t *, udp_pdu_t **);
     44extern int udp_pdu_decode(udp_pdu_t *, udp_sockpair_t *, udp_msg_t **);
     45extern int udp_pdu_encode(udp_sockpair_t *, udp_msg_t *, udp_pdu_t **);
    4746
    4847#endif
  • uspace/srv/net/udp/udp.c

    ree1c2d9 r0453261  
    4141#include <task.h>
    4242
    43 #include "assoc.h"
    44 #include "service.h"
    4543#include "udp_inet.h"
     44#include "sock.h"
    4645
    4746#define NAME       "udp"
     
    5352        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_init()");
    5453
    55         rc = udp_assocs_init();
    56         if (rc != EOK) {
    57                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing associations.");
    58                 return ENOMEM;
    59         }
    60 
    6154        rc = udp_inet_init();
    6255        if (rc != EOK) {
     
    6558        }
    6659
    67         rc = udp_service_init();
     60        rc = udp_sock_init();
    6861        if (rc != EOK) {
    69                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing UDP service.");
     62                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
    7063                return ENOENT;
    7164        }
  • uspace/srv/net/udp/udp_inet.c

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    6464
    6565        pdu = udp_pdu_new();
    66         pdu->iplink = dgram->iplink;
    6766        pdu->data = dgram->data;
    6867        pdu->data_size = dgram->size;
     
    106105{
    107106        udp_msg_t *dmsg;
    108         inet_ep2_t rident;
     107        udp_sockpair_t rident;
    109108
    110109        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_received_pdu()");
  • uspace/srv/net/udp/udp_type.h

    ree1c2d9 r0453261  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define UDP_TYPE_H
    3737
    38 #include <async.h>
    3938#include <fibril.h>
    4039#include <fibril_synch.h>
    41 #include <inet/endpoint.h>
    4240#include <ipc/loc.h>
     41#include <socket_core.h>
    4342#include <sys/types.h>
    4443#include <inet/addr.h>
     
    5049        /* Insufficient resources */
    5150        UDP_ENORES,
    52         /* Remote endpoint unspecified */
     51        /* Foreign socket unspecified */
    5352        UDP_EUNSPEC,
    5453        /* No route to destination */
     
    6160        XF_DUMMY = 0x1
    6261} xflags_t;
     62
     63enum udp_port {
     64        UDP_PORT_ANY = 0
     65};
     66
     67typedef struct {
     68        inet_addr_t addr;
     69        uint16_t port;
     70} udp_sock_t;
     71
     72typedef struct {
     73        service_id_t iplink;
     74        udp_sock_t local;
     75        udp_sock_t foreign;
     76} udp_sockpair_t;
    6377
    6478/** Unencoded UDP message (datagram) */
     
    8599
    86100typedef struct {
    87         void (*recv_msg)(void *, inet_ep2_t *, udp_msg_t *);
    88 } udp_assoc_cb_t;
     101        async_sess_t *sess;
     102        socket_cores_t sockets;
     103} udp_client_t;
    89104
    90105/** UDP association
     
    92107 * This is a rough equivalent of a TCP connection endpoint. It allows
    93108 * sending and receiving UDP datagrams and it is uniquely identified
    94  * by an endpoint pair.
     109 * by a socket pair.
    95110 */
    96111typedef struct {
     
    98113        link_t link;
    99114
    100         /** Association identification (endpoint pair) */
    101         inet_ep2_t ident;
     115        /** Association identification (local and foreign socket) */
     116        udp_sockpair_t ident;
    102117
    103118        /** True if association was reset by user */
     
    116131        /** Receive queue CV. Broadcast when new datagram is inserted */
    117132        fibril_condvar_t rcv_queue_cv;
    118 
    119         udp_assoc_cb_t *cb;
    120         void *cb_arg;
    121133} udp_assoc_t;
    122134
     
    124136} udp_assoc_status_t;
    125137
    126 typedef struct {
    127         /** Link to receive queue */
    128         link_t link;
    129         /** Endpoint pair */
    130         inet_ep2_t epp;
    131         /** Message */
    132         udp_msg_t *msg;
    133 } udp_rcv_queue_entry_t;
    134 
    135 typedef struct udp_cassoc {
    136         /** Association */
     138typedef struct udp_sockdata {
     139        /** Lock */
     140        fibril_mutex_t lock;
     141        /** Socket core */
     142        socket_core_t *sock_core;
     143        /** Client */
     144        udp_client_t *client;
     145        /** Connection */
    137146        udp_assoc_t *assoc;
    138         /** Association ID for the client */
    139         sysarg_t id;
    140         /** Client */
    141         struct udp_client *client;
    142         link_t lclient;
    143 } udp_cassoc_t;
     147        /** User-configured IP link */
     148        service_id_t iplink;
     149        /** Receiving fibril */
     150        fid_t recv_fibril;
     151        uint8_t recv_buffer[UDP_FRAGMENT_SIZE];
     152        size_t recv_buffer_used;
     153        udp_sock_t recv_fsock;
     154        fibril_mutex_t recv_buffer_lock;
     155        fibril_condvar_t recv_buffer_cv;
     156        udp_error_t recv_error;
     157} udp_sockdata_t;
    144158
    145159typedef struct {
    146160        /** Link to receive queue */
    147161        link_t link;
    148         /** Endpoint pair */
    149         inet_ep2_t epp;
     162        /** Socket pair */
     163        udp_sockpair_t sp;
    150164        /** Message */
    151165        udp_msg_t *msg;
    152         /** Client association */
    153         udp_cassoc_t *cassoc;
    154 } udp_crcv_queue_entry_t;
    155 
    156 typedef struct udp_client {
    157         /** Client callback session */
    158         async_sess_t *sess;
    159         /** Client assocations */
    160         list_t cassoc; /* of udp_cassoc_t */
    161         /** Client receive queue */
    162         list_t crcv_queue;
    163 } udp_client_t;
     166} udp_rcv_queue_entry_t;
    164167
    165168#endif
Note: See TracChangeset for help on using the changeset viewer.