Changeset fb4d788 in mainline for uspace/srv


Ignore:
Timestamp:
2015-07-28T11:28:14Z (10 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6accc5cf
Parents:
df2bce32 (diff), 47726b5e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from the mainline

Location:
uspace/srv
Files:
2 added
4 deleted
44 edited
2 moved

Legend:

Unmodified
Added
Removed
  • uspace/srv/bd/sata_bd/sata_bd.c

    rdf2bce32 rfb4d788  
    147147 *
    148148 */
    149 static int get_sata_disks()
     149static int get_sata_disks(void)
    150150{
    151151        devman_handle_t root_fun;
  • uspace/srv/hid/remcons/remcons.c

    rdf2bce32 rfb4d788  
    4444#include <fibril_synch.h>
    4545#include <task.h>
    46 #include <net/in.h>
    47 #include <net/inet.h>
    48 #include <net/socket.h>
     46#include <inet/addr.h>
     47#include <inet/endpoint.h>
     48#include <inet/tcp.h>
    4949#include <io/console.h>
    5050#include <inttypes.h>
     
    9999};
    100100
     101static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn);
     102
     103static tcp_listen_cb_t listen_cb = {
     104        .new_conn = remcons_new_conn
     105};
     106
     107static tcp_cb_t conn_cb = {
     108        .connected = NULL
     109};
     110
    101111static telnet_user_t *srv_to_user(con_srv_t *srv)
    102112{
     
    111121
    112122        /* Force character mode. */
    113         send(user->socket, (void *)telnet_force_character_mode_command,
    114             telnet_force_character_mode_command_count, 0);
     123        (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
     124            telnet_force_character_mode_command_count);
    115125
    116126        return EOK;
     
    272282}
    273283
    274 /** Fibril for each accepted socket.
    275  *
    276  * @param arg Corresponding @c telnet_user_t structure.
    277  */
    278 static int network_user_fibril(void *arg)
    279 {
    280         telnet_user_t *user = arg;
     284/** Handle network connection.
     285 *
     286 * @param lst  Listener
     287 * @param conn Connection
     288 */
     289static 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);
    281300
    282301        int rc = loc_service_register(user->service_name, &user->service_id);
     
    284303                telnet_user_error(user, "Unable to register %s with loc: %s.",
    285304                    user->service_name, str_error(rc));
    286                 return EOK;
     305                return;
    287306        }
    288307
    289308        telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
    290309            user->service_name, user->service_id);
    291        
     310
    292311        fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
    293312        assert(spawn_fibril);
    294313        fibril_add_ready(spawn_fibril);
    295        
     314
    296315        /* Wait for all clients to exit. */
    297316        fibril_mutex_lock(&user->guard);
    298317        while (!user_can_be_destroyed_no_lock(user)) {
    299318                if (user->task_finished) {
    300                         closesocket(user->socket);
     319                        user->conn = NULL;
    301320                        user->socket_closed = true;
    302321                        user->srvs.aborted = true;
     
    310329        }
    311330        fibril_mutex_unlock(&user->guard);
    312        
     331
    313332        rc = loc_service_unregister(user->service_id);
    314333        if (rc != EOK) {
     
    320339        telnet_user_log(user, "Destroying...");
    321340        telnet_user_destroy(user);
    322 
    323         return EOK;
    324341}
    325342
    326343int main(int argc, char *argv[])
    327344{
    328         int port = 2223;
    329        
     345        int rc;
     346        tcp_listener_t *lst;
     347        tcp_t *tcp;
     348        inet_ep_t ep;
     349
    330350        async_set_client_connection(client_connection);
    331         int rc = loc_server_register(NAME);
     351        rc = loc_server_register(NAME);
    332352        if (rc != EOK) {
    333353                fprintf(stderr, "%s: Unable to register server\n", NAME);
    334354                return rc;
    335355        }
    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;
     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;
    368371        }
    369372
    370373        printf("%s: HelenOS Remote console service\n", NAME);
    371374        task_retval(0);
    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 
     375        async_manager();
     376
     377        /* Not reached */
    400378        return 0;
    401379}
  • uspace/srv/hid/remcons/remcons.h

    rdf2bce32 rfb4d788  
    3838#define NAME       "remcons"
    3939#define NAMESPACE  "term"
    40 #define BACKLOG_SIZE  5
    4140
    4241#endif
  • uspace/srv/hid/remcons/user.c

    rdf2bce32 rfb4d788  
    4444#include <fibril_synch.h>
    4545#include <task.h>
    46 #include <net/in.h>
    47 #include <net/inet.h>
    48 #include <net/socket.h>
     46#include <inet/tcp.h>
    4947#include <io/console.h>
    5048#include <inttypes.h>
     
    5856/** Create new telnet user.
    5957 *
    60  * @param socket Socket the user communicates through.
     58 * @param conn Incoming connection.
    6159 * @return New telnet user or NULL when out of memory.
    6260 */
    63 telnet_user_t *telnet_user_create(int socket)
     61telnet_user_t *telnet_user_create(tcp_conn_t *conn)
    6462{
    6563        static int telnet_user_id_counter = 0;
     
    7876        }
    7977
    80         user->socket = socket;
     78        user->conn = conn;
    8179        user->service_id = (service_id_t) -1;
    8280        prodcons_initialize(&user->in_events);
     
    193191        /* No more buffered data? */
    194192        if (user->socket_buffer_len <= user->socket_buffer_pos) {
    195                 int recv_length = recv(user->socket, user->socket_buffer, BUFFER_SIZE, 0);
    196                 if ((recv_length == 0) || (recv_length == ENOTCONN)) {
     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) {
    197202                        user->socket_closed = true;
    198203                        user->srvs.aborted = true;
    199204                        return ENOENT;
    200205                }
    201                 if (recv_length < 0) {
    202                         return recv_length;
    203                 }
     206
    204207                user->socket_buffer_len = recv_length;
    205208                user->socket_buffer_pos = 0;
     
    359362
    360363
    361         int rc = send(user->socket, converted, converted_size, 0);
     364        int rc = tcp_conn_send(user->conn, converted, converted_size);
    362365        free(converted);
    363366
  • uspace/srv/hid/remcons/user.h

    rdf2bce32 rfb4d788  
    3838#include <adt/prodcons.h>
    3939#include <fibril_synch.h>
     40#include <inet/tcp.h>
    4041#include <inttypes.h>
    4142#include <io/con_srv.h>
     
    5152        /** Internal id, used for creating locfs entries. */
    5253        int id;
    53         /** Associated socket. */
    54         int socket;
     54        /** Associated connection. */
     55        tcp_conn_t *conn;
    5556        /** Location service id assigned to the virtual terminal. */
    5657        service_id_t service_id;
     
    8081} telnet_user_t;
    8182
    82 extern telnet_user_t *telnet_user_create(int);
     83extern telnet_user_t *telnet_user_create(tcp_conn_t *);
    8384extern void telnet_user_add(telnet_user_t *);
    8485extern void telnet_user_destroy(telnet_user_t *);
  • uspace/srv/hid/rfb/main.c

    rdf2bce32 rfb4d788  
    150150}
    151151
    152 static int socket_fibril(void *unused)
    153 {
    154         rfb_accept(&rfb);
    155        
    156         /* Not reached */
    157         return EOK;
    158 }
    159 
    160152int main(int argc, char **argv)
    161153{
     
    267259        }
    268260       
    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        
    277261        printf("%s: Accepting connections\n", NAME);
    278262        task_retval(0);
  • uspace/srv/hid/rfb/rfb.c

    rdf2bce32 rfb4d788  
    3131#include <stdlib.h>
    3232#include <fibril_synch.h>
     33#include <inet/addr.h>
     34#include <inet/endpoint.h>
     35#include <inet/tcp.h>
    3336#include <inttypes.h>
    3437#include <str.h>
     
    3841#include <io/log.h>
    3942
    40 #include <net/in.h>
    41 #include <net/inet.h>
    42 #include <net/socket.h>
    43 
    4443#include "rfb.h"
     44
     45static void rfb_new_conn(tcp_listener_t *, tcp_conn_t *);
     46
     47static tcp_listen_cb_t listen_cb = {
     48        .new_conn = rfb_new_conn
     49};
     50
     51static tcp_cb_t conn_cb = {
     52        .connected = NULL
     53};
    4554
    4655/** Buffer for receiving the request. */
     
    5362
    5463/** Receive one character (with buffering) */
    55 static int recv_char(int fd, char *c)
    56 {
     64static int recv_char(tcp_conn_t *conn, char *c)
     65{
     66        size_t nrecv;
     67        int rc;
     68
    5769        if (rbuf_out == rbuf_in) {
    5870                rbuf_out = 0;
    5971                rbuf_in = 0;
    6072               
    61                 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);
    62                 if (rc <= 0)
     73                rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv);
     74                if (rc != EOK)
    6375                        return rc;
    6476               
    65                 rbuf_in = rc;
     77                rbuf_in = nrecv;
    6678        }
    6779       
     
    7183
    7284/** Receive count characters (with buffering) */
    73 static int recv_chars(int fd, char *c, size_t count)
     85static int recv_chars(tcp_conn_t *conn, char *c, size_t count)
    7486{
    7587        for (size_t i = 0; i < count; i++) {
    76                 int rc = recv_char(fd, c);
     88                int rc = recv_char(conn, c);
    7789                if (rc != EOK)
    7890                        return rc;
     
    8294}
    8395
    84 static int recv_skip_chars(int fd, size_t count)
     96static int recv_skip_chars(tcp_conn_t *conn, size_t count)
    8597{
    8698        for (size_t i = 0; i < count; i++) {
    8799                char c;
    88                 int rc = recv_char(fd, &c);
     100                int rc = recv_char(conn, &c);
    89101                if (rc != EOK)
    90102                        return rc;
     
    204216}
    205217
    206 static int recv_message(int conn_sd, char type, void *buf, size_t size)
     218static int recv_message(tcp_conn_t *conn, char type, void *buf, size_t size)
    207219{
    208220        memcpy(buf, &type, 1);
    209         return recv_chars(conn_sd, ((char *) buf) + 1, size -1);
     221        return recv_chars(conn, ((char *) buf) + 1, size -1);
    210222}
    211223
     
    477489}
    478490
    479 static int rfb_send_framebuffer_update(rfb_t *rfb, int conn_sd, bool incremental)
     491static int rfb_send_framebuffer_update(rfb_t *rfb, tcp_conn_t *conn,
     492    bool incremental)
    480493{
    481494        fibril_mutex_lock(&rfb->lock);
     
    540553       
    541554        if (!rfb->pixel_format.true_color) {
    542                 int rc = send(conn_sd, send_palette, send_palette_size, 0);
     555                int rc = tcp_conn_send(conn, send_palette, send_palette_size);
    543556                if (rc != EOK) {
    544557                        free(buf);
     
    547560        }
    548561       
    549         int rc = send(conn_sd, buf, buf_size, 0);
     562        int rc = tcp_conn_send(conn, buf, buf_size);
    550563        free(buf);
    551564       
     
    580593}
    581594
    582 static void rfb_socket_connection(rfb_t *rfb, int conn_sd)
     595static void rfb_socket_connection(rfb_t *rfb, tcp_conn_t *conn)
    583596{
    584597        /* Version handshake */
    585         int rc = send(conn_sd, "RFB 003.008\n", 12, 0);
     598        int rc = tcp_conn_send(conn, "RFB 003.008\n", 12);
    586599        if (rc != EOK) {
    587600                log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server version %d", rc);
     
    590603       
    591604        char client_version[12];
    592         rc = recv_chars(conn_sd, client_version, 12);
     605        rc = recv_chars(conn, client_version, 12);
    593606        if (rc != EOK) {
    594607                log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client version: %d", rc);
     
    607620        sec_types[0] = 1; /* length */
    608621        sec_types[1] = RFB_SECURITY_NONE;
    609         rc = send(conn_sd, sec_types, 2, 0);
     622        rc = tcp_conn_send(conn, sec_types, 2);
    610623        if (rc != EOK) {
    611624                log_msg(LOG_DEFAULT, LVL_WARN,
     
    615628       
    616629        char selected_sec_type = 0;
    617         rc = recv_char(conn_sd, &selected_sec_type);
     630        rc = recv_char(conn, &selected_sec_type);
    618631        if (rc != EOK) {
    619632                log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving security type: %d", rc);
     
    626639        }
    627640        uint32_t security_result = RFB_SECURITY_HANDSHAKE_OK;
    628         rc = send(conn_sd, &security_result, sizeof(uint32_t), 0);
     641        rc = tcp_conn_send(conn, &security_result, sizeof(uint32_t));
    629642        if (rc != EOK) {
    630643                log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending security result: %d", rc);
     
    634647        /* Client init */
    635648        char shared_flag;
    636         rc = recv_char(conn_sd, &shared_flag);
     649        rc = recv_char(conn, &shared_flag);
    637650        if (rc != EOK) {
    638651                log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client init: %d", rc);
     
    657670        memcpy(server_init->name, rfb->name, name_length);
    658671        fibril_mutex_unlock(&rfb->lock);
    659         rc = send(conn_sd, server_init, msg_length, 0);
     672        rc = tcp_conn_send(conn, server_init, msg_length);
    660673        if (rc != EOK) {
    661674                log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server init: %d", rc);
     
    665678        while (true) {
    666679                char message_type = 0;
    667                 rc = recv_char(conn_sd, &message_type);
     680                rc = recv_char(conn, &message_type);
    668681                if (rc != EOK) {
    669682                        log_msg(LOG_DEFAULT, LVL_WARN,
     
    680693                switch (message_type) {
    681694                case RFB_CMSG_SET_PIXEL_FORMAT:
    682                         recv_message(conn_sd, message_type, &spf, sizeof(spf));
     695                        recv_message(conn, message_type, &spf, sizeof(spf));
    683696                        rfb_pixel_format_to_host(&spf.pixel_format, &spf.pixel_format);
    684697                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetPixelFormat message");
     
    690703                        break;
    691704                case RFB_CMSG_SET_ENCODINGS:
    692                         recv_message(conn_sd, message_type, &se, sizeof(se));
     705                        recv_message(conn, message_type, &se, sizeof(se));
    693706                        rfb_set_encodings_to_host(&se, &se);
    694707                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetEncodings message");
    695708                        for (uint16_t i = 0; i < se.count; i++) {
    696709                                int32_t encoding = 0;
    697                                 rc = recv_chars(conn_sd, (char *) &encoding, sizeof(int32_t));
     710                                rc = recv_chars(conn, (char *) &encoding, sizeof(int32_t));
    698711                                if (rc != EOK)
    699712                                        return;
     
    707720                        break;
    708721                case RFB_CMSG_FRAMEBUFFER_UPDATE_REQUEST:
    709                         recv_message(conn_sd, message_type, &fbur, sizeof(fbur));
     722                        recv_message(conn, message_type, &fbur, sizeof(fbur));
    710723                        rfb_framebuffer_update_request_to_host(&fbur, &fbur);
    711724                        log_msg(LOG_DEFAULT, LVL_DEBUG2,
    712725                            "Received FramebufferUpdateRequest message");
    713                         rfb_send_framebuffer_update(rfb, conn_sd, fbur.incremental);
     726                        rfb_send_framebuffer_update(rfb, conn, fbur.incremental);
    714727                        break;
    715728                case RFB_CMSG_KEY_EVENT:
    716                         recv_message(conn_sd, message_type, &ke, sizeof(ke));
     729                        recv_message(conn, message_type, &ke, sizeof(ke));
    717730                        rfb_key_event_to_host(&ke, &ke);
    718731                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received KeyEvent message");
    719732                        break;
    720733                case RFB_CMSG_POINTER_EVENT:
    721                         recv_message(conn_sd, message_type, &pe, sizeof(pe));
     734                        recv_message(conn, message_type, &pe, sizeof(pe));
    722735                        rfb_pointer_event_to_host(&pe, &pe);
    723736                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received PointerEvent message");
    724737                        break;
    725738                case RFB_CMSG_CLIENT_CUT_TEXT:
    726                         recv_message(conn_sd, message_type, &cct, sizeof(cct));
     739                        recv_message(conn, message_type, &cct, sizeof(cct));
    727740                        rfb_client_cut_text_to_host(&cct, &cct);
    728741                        log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received ClientCutText message");
    729                         recv_skip_chars(conn_sd, cct.length);
     742                        recv_skip_chars(conn, cct.length);
    730743                        break;
    731744                default:
     
    737750}
    738751
    739 int 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;
     752int 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;
    774777       
    775778        return EOK;
    776 }
    777 
    778 void 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 }
     779error:
     780        tcp_listener_destroy(lst);
     781        tcp_destroy(tcp);
     782        return rc;
     783}
     784
     785static 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}
  • uspace/srv/hid/rfb/rfb.h

    rdf2bce32 rfb4d788  
    3030#define RFB_H__
    3131
     32#include <inet/tcp.h>
    3233#include <io/pixelmap.h>
    3334#include <fibril_synch.h>
     
    151152        rfb_pixel_format_t pixel_format;
    152153        const char *name;
    153         int listen_sd;
     154        tcp_t *tcp;
     155        tcp_listener_t *lst;
    154156        pixelmap_t framebuffer;
    155157        rfb_rectangle_t damage_rect;
     
    165167extern int rfb_set_size(rfb_t *, uint16_t, uint16_t);
    166168extern int rfb_listen(rfb_t *, uint16_t);
    167 extern void rfb_accept(rfb_t *);
    168169
    169170#endif
  • uspace/srv/klog/klog.c

    rdf2bce32 rfb4d788  
    5151#include <io/logctl.h>
    5252
    53 #define NAME       "klog"
     53#define NAME  "klog"
    5454
    5555typedef size_t __attribute__ ((aligned(1))) unaligned_size_t;
     
    9898 *
    9999 */
    100 static void producer()
     100static void producer(void)
    101101{
    102102        int read = klog_read(buffer, BUFFER_SIZE);
  • uspace/srv/net/dhcp/dhcp.c

    rdf2bce32 rfb4d788  
    3737#include <adt/list.h>
    3838#include <bitops.h>
     39#include <byteorder.h>
    3940#include <errno.h>
    4041#include <fibril_synch.h>
  • uspace/srv/net/dhcp/transport.c

    rdf2bce32 rfb4d788  
    3636
    3737#include <bitops.h>
     38#include <errno.h>
    3839#include <inet/addr.h>
    3940#include <inet/dnsr.h>
     
    4142#include <io/log.h>
    4243#include <loc.h>
    43 #include <net/in.h>
    44 #include <net/inet.h>
    45 #include <net/socket.h>
    4644#include <stdio.h>
    4745#include <stdlib.h>
     
    6765} dhcp_offer_t;
    6866
    69 static int dhcp_recv_fibril(void *);
     67static void dhcp_recv_msg(udp_assoc_t *, udp_rmsg_t *);
     68static void dhcp_recv_err(udp_assoc_t *, udp_rerr_t *);
     69static void dhcp_link_state(udp_assoc_t *, udp_link_state_t);
     70
     71static 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};
    7076
    7177int dhcp_send(dhcp_transport_t *dt, void *msg, size_t size)
    7278{
    73         struct sockaddr_in addr;
     79        inet_ep_t ep;
    7480        int rc;
    7581
    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);
     82        inet_ep_init(&ep);
     83        ep.port = dhcp_server_port;
     84        inet_addr_set(addr32_broadcast_all_hosts, &ep.addr);
    7985
    80         rc = sendto(dt->fd, msg, size, 0,
    81             (struct sockaddr *)&addr, sizeof(addr));
     86        rc = udp_assoc_send_msg(dt->assoc, &ep, msg, size);
    8287        if (rc != EOK) {
    83                 log_msg(LOG_DEFAULT, LVL_ERROR, "Sending failed");
     88                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed sending message");
    8489                return rc;
    8590        }
     
    8893}
    8994
    90 static int dhcp_recv_msg(dhcp_transport_t *dt, void **rmsg, size_t *rsize)
     95static void dhcp_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
    9196{
    92         struct sockaddr_in src_addr;
    93         socklen_t src_addr_size;
    94         size_t recv_size;
     97        dhcp_transport_t *dt;
     98        size_t s;
    9599        int rc;
    96100
    97         src_addr_size = sizeof(src_addr);
    98         rc = recvfrom(dt->fd, msgbuf, MAX_MSG_SIZE, 0,
    99             (struct sockaddr *)&src_addr, &src_addr_size);
    100         if (rc < 0) {
    101                 log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom failed (%d)", rc);
    102                 return rc;
     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;
    103112        }
    104113
    105         recv_size = (size_t)rc;
    106         *rmsg = msgbuf;
    107         *rsize = recv_size;
     114        log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg() - call recv_cb");
     115        dt->recv_cb(dt->cb_arg, msgbuf, s);
     116}
    108117
    109         return EOK;
     118static void dhcp_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr)
     119{
     120        log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error");
     121}
     122
     123static void dhcp_link_state(udp_assoc_t *assoc, udp_link_state_t ls)
     124{
     125        log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change");
    110126}
    111127
     
    113129    dhcp_recv_cb_t recv_cb, void *arg)
    114130{
    115         int fd;
    116         struct sockaddr_in laddr;
    117         int fid;
     131        udp_t *udp = NULL;
     132        udp_assoc_t *assoc = NULL;
     133        inet_ep2_t epp;
    118134        int rc;
    119135
    120         log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcptransport_init()");
     136        log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_transport_init()");
    121137
    122         laddr.sin_family = AF_INET;
    123         laddr.sin_port = htons(dhcp_client_port);
    124         laddr.sin_addr.s_addr = INADDR_ANY;
     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;
    125142
    126         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    127         if (fd < 0) {
    128                 rc = EIO;
    129                 goto error;
    130         }
    131 
    132         log_msg(LOG_DEFAULT, LVL_DEBUG, "Bind socket.");
    133         rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
     143        rc = udp_create(&udp);
    134144        if (rc != EOK) {
    135145                rc = EIO;
     
    137147        }
    138148
    139         log_msg(LOG_DEFAULT, LVL_DEBUG, "Set socket options");
    140         rc = setsockopt(fd, SOL_SOCKET, SO_IPLINK, &link_id, sizeof(link_id));
     149        rc = udp_assoc_create(udp, &epp, &dhcp_transport_cb, dt, &assoc);
    141150        if (rc != EOK) {
    142151                rc = EIO;
     
    144153        }
    145154
    146         dt->fd = fd;
     155        dt->udp = udp;
     156        dt->assoc = assoc;
    147157        dt->recv_cb = recv_cb;
    148158        dt->cb_arg = arg;
    149159
    150         fid = fibril_create(dhcp_recv_fibril, dt);
    151         if (fid == 0) {
    152                 rc = ENOMEM;
    153                 goto error;
    154         }
    155 
    156         dt->recv_fid = fid;
    157         fibril_add_ready(fid);
    158 
    159160        return EOK;
    160161error:
    161         closesocket(fd);
     162        udp_assoc_destroy(assoc);
     163        udp_destroy(udp);
    162164        return rc;
    163165}
     
    165167void dhcp_transport_fini(dhcp_transport_t *dt)
    166168{
    167         closesocket(dt->fd);
    168 }
    169 
    170 static int dhcp_recv_fibril(void *arg)
    171 {
    172         dhcp_transport_t *dt = (dhcp_transport_t *)arg;
    173         void *msg;
    174         size_t size = (size_t) -1;
    175         int rc;
    176 
    177         while (true) {
    178                 rc = dhcp_recv_msg(dt, &msg, &size);
    179                 if (rc != EOK)
    180                         break;
    181 
    182                 assert(size != (size_t) -1);
    183 
    184                 dt->recv_cb(dt->cb_arg, msg, size);
    185         }
    186 
    187         return EOK;
     169        udp_assoc_destroy(dt->assoc);
     170        udp_destroy(dt->udp);
    188171}
    189172
  • uspace/srv/net/dhcp/transport.h

    rdf2bce32 rfb4d788  
    3838#define TRANSPORT_H
    3939
     40#include <inet/udp.h>
    4041#include <ipc/loc.h>
    4142#include <sys/types.h>
     
    4748
    4849struct dhcp_transport {
    49         /** Transport socket */
    50         int fd;
     50        /** UDP */
     51        udp_t *udp;
     52        /** UDP association */
     53        udp_assoc_t *assoc;
    5154        /** Receive callback */
    5255        dhcp_recv_cb_t recv_cb;
    5356        /** Callback argument */
    5457        void *cb_arg;
    55         /** Receive fibril ID */
    56         int recv_fid;
    5758};
    5859
  • uspace/srv/net/dnsrsrv/transport.c

    rdf2bce32 rfb4d788  
    3737#include <errno.h>
    3838#include <fibril_synch.h>
     39#include <inet/addr.h>
     40#include <inet/endpoint.h>
     41#include <inet/udp.h>
    3942#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 fid_t recv_fid;
    75 static int transport_fd = -1;
     74static udp_t *transport_udp;
     75static udp_assoc_t *transport_assoc;
    7676
    7777/** Outstanding requests */
     
    7979static FIBRIL_MUTEX_INITIALIZE(treq_lock);
    8080
    81 static int transport_recv_fibril(void *arg);
     81static void transport_recv_msg(udp_assoc_t *, udp_rmsg_t *);
     82static void transport_recv_err(udp_assoc_t *, udp_rerr_t *);
     83static void transport_link_state(udp_assoc_t *, udp_link_state_t);
     84
     85static udp_cb_t transport_cb = {
     86        .recv_msg = transport_recv_msg,
     87        .recv_err = transport_recv_err,
     88        .link_state = transport_link_state
     89};
    8290
    8391int transport_init(void)
    8492{
    85         struct sockaddr_in laddr;
    86         int fd;
    87         fid_t fid;
     93        inet_ep2_t epp;
    8894        int rc;
    8995
    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) {
     96        inet_ep2_init(&epp);
     97
     98        rc = udp_create(&transport_udp);
     99        if (rc != EOK) {
    98100                rc = EIO;
    99101                goto error;
    100102        }
    101103
    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;
     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
    114111        return EOK;
    115112error:
    116         log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network socket.");
    117         if (fd >= 0)
    118                 closesocket(fd);
     113        log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network.");
     114        udp_assoc_destroy(transport_assoc);
     115        udp_destroy(transport_udp);
    119116        return rc;
    120117}
     
    122119void transport_fini(void)
    123120{
    124         if (transport_fd >= 0)
    125                 closesocket(transport_fd);
     121        udp_assoc_destroy(transport_assoc);
     122        udp_destroy(transport_udp);
    126123}
    127124
     
    182179{
    183180        trans_req_t *treq = NULL;
    184         struct sockaddr *saddr = NULL;
    185         socklen_t saddrlen;
    186        
     181        inet_ep_t ep;
     182
    187183        void *req_data;
    188184        size_t req_size;
     
    190186        if (rc != EOK)
    191187                goto error;
    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        
     188
     189        inet_ep_init(&ep);
     190        ep.addr = dns_server_addr;
     191        ep.port = DNS_SERVER_PORT;
     192
    200193        size_t ntry = 0;
    201        
     194
    202195        while (ntry < REQ_RETRY_MAX) {
    203                 rc = sendto(transport_fd, req_data, req_size, 0,
    204                     saddr, saddrlen);
     196                rc = udp_assoc_send_msg(transport_assoc, &ep, req_data,
     197                    req_size);
    205198                if (rc != EOK)
    206199                        goto error;
    207                
     200
    208201                treq = treq_create(req);
    209202                if (treq == NULL) {
     
    211204                        goto error;
    212205                }
    213                
     206
    214207                fibril_mutex_lock(&treq->done_lock);
    215208                while (treq->done != true) {
     
    221214                        }
    222215                }
    223                
     216
    224217                fibril_mutex_unlock(&treq->done_lock);
    225                
     218
    226219                if (rc != ETIMEOUT)
    227220                        break;
    228221        }
    229        
     222
    230223        if (ntry >= REQ_RETRY_MAX) {
    231224                rc = EIO;
    232225                goto error;
    233226        }
    234        
     227
    235228        if (treq->status != EOK) {
    236229                rc = treq->status;
    237230                goto error;
    238231        }
    239        
     232
    240233        *rresp = treq->resp;
    241234        treq_destroy(treq);
    242235        free(req_data);
    243         free(saddr);
    244236        return EOK;
    245        
     237
    246238error:
    247239        if (treq != NULL)
    248240                treq_destroy(treq);
    249        
     241
    250242        free(req_data);
    251         free(saddr);
    252243        return rc;
    253244}
    254245
    255 static 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 
    282 error:
    283         return rc;
    284 }
    285 
    286 static int transport_recv_fibril(void *arg)
     246static void transport_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
    287247{
    288248        dns_message_t *resp = NULL;
    289249        trans_req_t *treq;
     250        size_t size;
     251        inet_ep_t remote_ep;
    290252        int rc;
    291253
    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);
     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) {
    307278                fibril_mutex_unlock(&treq_lock);
    308 
    309                 treq_complete(treq, resp);
    310         }
    311 
    312         return 0;
    313 }
     279                return;
     280        }
     281
     282        list_remove(&treq->lreq);
     283        fibril_mutex_unlock(&treq_lock);
     284
     285        treq_complete(treq, resp);
     286}
     287
     288static 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
     293static 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
    314298
    315299/** @}
  • uspace/srv/net/inetsrv/inet_link.c

    rdf2bce32 rfb4d788  
    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        
     90        inet_link_t *ilink;
     91
     92        ilink = (inet_link_t *)iplink_get_userptr(iplink);
     93
    9194        switch (ver) {
    9295        case ip_v4:
    93                 rc = inet_pdu_decode(sdu->data, sdu->size, &packet);
     96                rc = inet_pdu_decode(sdu->data, sdu->size, ilink->svc_id,
     97                    &packet);
    9498                break;
    9599        case ip_v6:
    96                 rc = inet_pdu_decode6(sdu->data, sdu->size, &packet);
     100                rc = inet_pdu_decode6(sdu->data, sdu->size, ilink->svc_id,
     101                    &packet);
    97102                break;
    98103        default:
     
    100105                return EINVAL;
    101106        }
    102        
     107
    103108        if (rc != EOK) {
    104109                log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU");
    105110                return rc;
    106111        }
    107        
     112
     113        log_msg(LOG_DEFAULT, LVL_NOTE, "inet_iplink_recv: link_id=%zu", packet.link_id);
    108114        log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet()");
    109115        rc = inet_recv_packet(&packet);
    110116        log_msg(LOG_DEFAULT, LVL_DEBUG, "call inet_recv_packet -> %d", rc);
    111117        free(packet.data);
    112        
     118
    113119        return rc;
    114120}
     
    177183        }
    178184
    179         rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, &ilink->iplink);
     185        rc = iplink_open(ilink->sess, &inet_iplink_ev_ops, ilink, &ilink->iplink);
    180186        if (rc != EOK) {
    181187                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed opening IP link '%s'",
  • uspace/srv/net/inetsrv/inetsrv.c

    rdf2bce32 rfb4d788  
    469469{
    470470        async_exch_t *exch = async_exchange_begin(client->sess);
    471        
     471
    472472        ipc_call_t answer;
    473         aid_t req = async_send_1(exch, INET_EV_RECV, dgram->tos, &answer);
    474        
     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
    475480        int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t));
    476481        if (rc != EOK) {
     
    479484                return rc;
    480485        }
    481        
     486
    482487        rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t));
    483488        if (rc != EOK) {
     
    486491                return rc;
    487492        }
    488        
     493
    489494        rc = async_data_write_start(exch, dgram->data, dgram->size);
    490        
     495
    491496        async_exchange_end(exch);
    492        
     497
    493498        if (rc != EOK) {
    494499                async_forget(req);
    495500                return rc;
    496501        }
    497        
     502
    498503        sysarg_t retval;
    499504        async_wait_for(req, &retval);
    500        
     505
    501506        return (int) retval;
    502507}
     
    511516        if (proto == IP_PROTO_ICMP)
    512517                return icmp_recv(dgram);
    513        
     518
    514519        if (proto == IP_PROTO_ICMPV6)
    515520                return icmpv6_recv(dgram);
     
    540545                if (packet->offs == 0 && !packet->mf) {
    541546                        /* It is complete deliver it immediately */
     547                        dgram.iplink = packet->link_id;
    542548                        dgram.src = packet->src;
    543549                        dgram.dest = packet->dest;
  • uspace/srv/net/inetsrv/inetsrv.h

    rdf2bce32 rfb4d788  
    7575
    7676typedef struct {
     77        /** Local link ID */
     78        service_id_t link_id;
    7779        /** Source address */
    7880        inet_addr_t src;
  • uspace/srv/net/inetsrv/pdu.c

    rdf2bce32 rfb4d788  
    298298/** Decode IPv4 datagram
    299299 *
    300  * @param data   Serialized IPv4 datagram
    301  * @param size   Length of serialized IPv4 datagram
    302  * @param packet IP datagram structure to be filled
     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
    303304 *
    304305 * @return EOK on success
     
    307308 *
    308309 */
    309 int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet)
     310int inet_pdu_decode(void *data, size_t size, service_id_t link_id,
     311    inet_packet_t *packet)
    310312{
    311313        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode()");
     
    366368       
    367369        memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
     370        packet->link_id = link_id;
    368371       
    369372        return EOK;
     
    372375/** Decode IPv6 datagram
    373376 *
    374  * @param data   Serialized IPv6 datagram
    375  * @param size   Length of serialized IPv6 datagram
    376  * @param packet IP datagram structure to be filled
     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
    377381 *
    378382 * @return EOK on success
     
    381385 *
    382386 */
    383 int inet_pdu_decode6(void *data, size_t size, inet_packet_t *packet)
     387int inet_pdu_decode6(void *data, size_t size, service_id_t link_id,
     388    inet_packet_t *packet)
    384389{
    385390        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode6()");
     
    457462       
    458463        memcpy(packet->data, (uint8_t *) data + data_offs, packet->size);
    459        
     464        packet->link_id = link_id;
    460465        return EOK;
    461466}
  • uspace/srv/net/inetsrv/pdu.h

    rdf2bce32 rfb4d788  
    3838#define INET_PDU_H_
    3939
     40#include <loc.h>
    4041#include <sys/types.h>
    4142#include "inetsrv.h"
     
    5051extern int inet_pdu_encode6(inet_packet_t *, addr128_t, addr128_t, size_t,
    5152    size_t, void **, size_t *, size_t *);
    52 extern int inet_pdu_decode(void *, size_t, inet_packet_t *);
    53 extern int inet_pdu_decode6(void *, size_t, inet_packet_t *);
     53extern int inet_pdu_decode(void *, size_t, service_id_t, inet_packet_t *);
     54extern int inet_pdu_decode6(void *, size_t, service_id_t, inet_packet_t *);
    5455
    5556extern int ndp_pdu_decode(inet_dgram_t *, ndp_packet_t *);
  • uspace/srv/net/inetsrv/reass.c

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

    rdf2bce32 rfb4d788  
    2828
    2929USPACE_PREFIX = ../../..
    30 LIBS = $(LIBNET_PREFIX)/libnet.a
    31 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include
     30
     31LIBS = \
     32        $(LIBNETTL_PREFIX)/libnettl.a
     33
     34EXTRA_CFLAGS += \
     35        -I$(LIBNETTL_PREFIX)/include
     36
    3237BINARY = tcp
    3338
     
    3944        rqueue.c \
    4045        segment.c \
     46        service.c \
    4147        seq_no.c \
    42         sock.c \
    4348        tcp.c \
    4449        test.c \
  • uspace/srv/net/tcp/conn.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636
    3737#include <adt/list.h>
    38 #include <stdbool.h>
    3938#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>
    4244#include <stdlib.h>
    4345#include "conn.h"
     
    5557#define TIME_WAIT_TIMEOUT       (2*MAX_SEGMENT_LIFETIME)
    5658
    57 LIST_INITIALIZE(conn_list);
    58 FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
     59static LIST_INITIALIZE(conn_list);
     60/** Taken after tcp_conn_t lock */
     61static FIBRIL_MUTEX_INITIALIZE(conn_list_lock);
     62static amap_t *amap;
    5963
    6064static void tcp_conn_seg_process(tcp_conn_t *conn, tcp_segment_t *seg);
     
    6266static void tcp_conn_tw_timer_clear(tcp_conn_t *conn);
    6367
     68/** Initialize connections. */
     69int 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
    6482/** Create new connection structure.
    6583 *
    66  * @param lsock         Local socket (will be deeply copied)
    67  * @param fsock         Foreign socket (will be deeply copied)
     84 * @param epp           Endpoint pair (will be deeply copied)
    6885 * @return              New connection or NULL
    6986 */
    70 tcp_conn_t *tcp_conn_new(tcp_sock_t *lsock, tcp_sock_t *fsock)
     87tcp_conn_t *tcp_conn_new(inet_ep2_t *epp)
    7188{
    7289        tcp_conn_t *conn = NULL;
     
    121138        fibril_condvar_initialize(&conn->cstate_cv);
    122139
    123         conn->cstate_cb = NULL;
     140        conn->cb = NULL;
    124141
    125142        conn->cstate = st_listen;
     
    128145        conn->ap = ap_passive;
    129146        conn->fin_is_acked = false;
    130         conn->ident.local = *lsock;
    131         if (fsock != NULL)
    132                 conn->ident.foreign = *fsock;
     147        if (epp != NULL)
     148                conn->ident = *epp;
    133149
    134150        return conn;
     
    184200void tcp_conn_addref(tcp_conn_t *conn)
    185201{
    186         log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p)", conn->name, conn);
     202        log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_addref(%p) before=%zu",
     203            conn->name, conn, atomic_get(&conn->refcnt));
    187204        atomic_inc(&conn->refcnt);
    188205}
     
    196213void tcp_conn_delref(tcp_conn_t *conn)
    197214{
    198         log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p)", conn->name, conn);
     215        log_msg(LOG_DEFAULT, LVL_DEBUG2, "%s: tcp_conn_delref(%p) before=%zu",
     216            conn->name, conn, atomic_get(&conn->refcnt));
    199217
    200218        if (atomic_predec(&conn->refcnt) == 0)
     
    237255
    238256        assert(conn->deleted == false);
     257        conn->deleted = true;
     258        conn->cb = NULL;
     259        conn->cb_arg = NULL;
    239260        tcp_conn_delref(conn);
    240261}
     
    244265 * Add connection to the connection map.
    245266 */
    246 void tcp_conn_add(tcp_conn_t *conn)
    247 {
     267int tcp_conn_add(tcp_conn_t *conn)
     268{
     269        inet_ep2_t aepp;
     270        int rc;
     271
    248272        tcp_conn_addref(conn);
    249273        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;
    250285        list_append(&conn->link, &conn_list);
    251286        fibril_mutex_unlock(&conn_list_lock);
     287
     288        return EOK;
    252289}
    253290
     
    259296{
    260297        fibril_mutex_lock(&conn_list_lock);
     298        amap_remove(amap, &conn->ident);
    261299        list_remove(&conn->link);
    262300        fibril_mutex_unlock(&conn_list_lock);
     
    275313
    276314        /* Run user callback function */
    277         if (conn->cstate_cb != NULL) {
     315        if (conn->cb != NULL && conn->cb->cstate_change != NULL) {
    278316                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - run user CB");
    279                 conn->cstate_cb(conn, conn->cstate_cb_arg);
     317                conn->cb->cstate_change(conn, conn->cb_arg, old_state);
    280318        } else {
    281319                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_state_set() - no user CB");
     
    284322        assert(old_state != st_closed);
    285323        if (nstate == st_closed) {
     324                tcp_conn_remove(conn);
    286325                /* Drop one reference for now being in closed state */
    287326                tcp_conn_delref(conn);
     
    332371}
    333372
    334 /** Match socket with pattern. */
    335 static 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. */
    354 static 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.
     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.
    371377 * The connection reference count is bumped by one.
    372378 *
    373  * @param sp    Socket pair
     379 * @param epp   Endpoint pair
    374380 * @return      Connection structure or NULL if not found.
    375381 */
    376 tcp_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        
     382tcp_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
    383390        fibril_mutex_lock(&conn_list_lock);
    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        
     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
    398402        fibril_mutex_unlock(&conn_list_lock);
    399         return NULL;
     403        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref: got conn=%p",
     404            conn);
     405        return conn;
    400406}
    401407
     
    407413{
    408414        log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_reset()", conn->name);
     415        conn->reset = true;
    409416        tcp_conn_state_set(conn, st_closed);
    410         conn->reset = true;
    411417
    412418        tcp_conn_tw_timer_clear(conn);
     
    877883        if (conn->fin_is_acked) {
    878884                log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: FIN acked -> Closed", conn->name);
    879                 tcp_conn_remove(conn);
    880885                tcp_conn_state_set(conn, st_closed);
    881886                return cp_done;
     
    10061011
    10071012        /* Signal to the receive function that new data has arrived */
    1008         fibril_condvar_broadcast(&conn->rcv_buf_cv);
     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        }
    10091018
    10101019        log_msg(LOG_DEFAULT, LVL_DEBUG, "Received %zu bytes of data.", xfer_size);
     
    10981107                conn->rcv_buf_fin = true;
    10991108                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);
    11001111
    11011112                tcp_segment_delete(seg);
     
    11681179 *
    11691180 * @param conn          Connection
    1170  * @param seg           Segment
    1171  */
    1172 void tcp_conn_segment_arrived(tcp_conn_t *conn, tcp_segment_t *seg)
    1173 {
     1181 * @param epp           Endpoint pair on which segment was received
     1182 * @param seg           Segment
     1183 */
     1184void 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
    11741191        log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_conn_segment_arrived(%p)",
    11751192            conn->name, seg);
    11761193
     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
    11771239        switch (conn->cstate) {
    11781240        case st_listen:
    1179                 tcp_conn_sa_listen(conn, seg); break;
     1241                tcp_conn_sa_listen(conn, seg);
     1242                break;
    11801243        case st_syn_sent:
    1181                 tcp_conn_sa_syn_sent(conn, seg); break;
     1244                tcp_conn_sa_syn_sent(conn, seg);
     1245                break;
    11821246        case st_syn_received:
    11831247        case st_established:
     
    11891253        case st_time_wait:
    11901254                /* Process segments in order of sequence number */
    1191                 tcp_conn_sa_queue(conn, seg); break;
     1255                tcp_conn_sa_queue(conn, seg);
     1256                break;
    11921257        case st_closed:
    11931258                log_msg(LOG_DEFAULT, LVL_DEBUG, "state=%d", (int) conn->cstate);
    11941259                assert(false);
    11951260        }
     1261
     1262        tcp_conn_unlock(conn);
    11961263}
    11971264
     
    12161283
    12171284        log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: TW Timeout -> Closed", conn->name);
    1218         tcp_conn_remove(conn);
    12191285        tcp_conn_state_set(conn, st_closed);
    12201286
     
    12631329}
    12641330
    1265 /** Handle unexpected segment received on a socket pair.
     1331/** Handle unexpected segment received on an endpoint pair.
    12661332 *
    12671333 * We reply with an RST unless the received segment has RST.
    12681334 *
    1269  * @param sp            Socket pair which received the segment
     1335 * @param sp            Endpoint pair which received the segment
    12701336 * @param seg           Unexpected segment
    12711337 */
    1272 void 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);
     1338void 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);
    12751342
    12761343        if ((seg->ctrl & CTL_RST) == 0)
    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  */
    1287 void tcp_sockpair_flipped(tcp_sockpair_t *sp, tcp_sockpair_t *fsp)
    1288 {
    1289         fsp->local = sp->foreign;
    1290         fsp->foreign = sp->local;
     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 */
     1354void tcp_ep2_flipped(inet_ep2_t *epp, inet_ep2_t *fepp)
     1355{
     1356        fepp->local = epp->remote;
     1357        fepp->remote = epp->local;
    12911358}
    12921359
    12931360/** Send RST in response to an incoming segment.
    12941361 *
    1295  * @param sp            Socket pair which received the segment
     1362 * @param epp           Endpoint pair which received the segment
    12961363 * @param seg           Incoming segment
    12971364 */
    1298 void tcp_reply_rst(tcp_sockpair_t *sp, tcp_segment_t *seg)
     1365void tcp_reply_rst(inet_ep2_t *epp, tcp_segment_t *seg)
    12991366{
    13001367        tcp_segment_t *rseg;
    13011368
    1302         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", sp, seg);
     1369        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_reply_rst(%p, %p)", epp, seg);
    13031370
    13041371        rseg = tcp_segment_make_rst(seg);
    1305         tcp_transmit_segment(sp, rseg);
     1372        tcp_transmit_segment(epp, rseg);
    13061373}
    13071374
  • uspace/srv/net/tcp/conn.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define CONN_H
    3737
     38#include <inet/endpoint.h>
    3839#include <stdbool.h>
    3940#include "tcp_type.h"
    4041
    41 extern tcp_conn_t *tcp_conn_new(tcp_sock_t *, tcp_sock_t *);
     42extern int tcp_conns_init(void);
     43extern tcp_conn_t *tcp_conn_new(inet_ep2_t *);
    4244extern void tcp_conn_delete(tcp_conn_t *);
    43 extern void tcp_conn_add(tcp_conn_t *);
     45extern int tcp_conn_add(tcp_conn_t *);
    4446extern void tcp_conn_remove(tcp_conn_t *);
    4547extern void tcp_conn_reset(tcp_conn_t *conn);
     
    4749extern void tcp_conn_fin_sent(tcp_conn_t *);
    4850extern void tcp_conn_ack_of_fin_rcvd(tcp_conn_t *);
    49 extern tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *);
     51extern tcp_conn_t *tcp_conn_find_ref(inet_ep2_t *);
    5052extern void tcp_conn_addref(tcp_conn_t *);
    5153extern void tcp_conn_delref(tcp_conn_t *);
     
    5355extern void tcp_conn_unlock(tcp_conn_t *);
    5456extern bool tcp_conn_got_syn(tcp_conn_t *);
    55 extern void tcp_conn_segment_arrived(tcp_conn_t *, tcp_segment_t *);
     57extern void tcp_conn_segment_arrived(tcp_conn_t *, inet_ep2_t *,
     58    tcp_segment_t *);
    5659extern void tcp_conn_trim_seg_to_wnd(tcp_conn_t *, tcp_segment_t *);
    57 extern void tcp_unexpected_segment(tcp_sockpair_t *, tcp_segment_t *);
    58 extern void tcp_sockpair_flipped(tcp_sockpair_t *, tcp_sockpair_t *);
    59 extern void tcp_reply_rst(tcp_sockpair_t *, tcp_segment_t *);
     60extern void tcp_unexpected_segment(inet_ep2_t *, tcp_segment_t *);
     61extern void tcp_ep2_flipped(inet_ep2_t *, inet_ep2_t *);
     62extern void tcp_reply_rst(inet_ep2_t *, tcp_segment_t *);
    6063
    6164#endif
  • uspace/srv/net/tcp/ncsim.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4242#include <async.h>
    4343#include <errno.h>
     44#include <inet/endpoint.h>
    4445#include <io/log.h>
    4546#include <stdlib.h>
     
    6566/** Bounce segment through simulator into receive queue.
    6667 *
    67  * @param sp    Socket pair, oriented for transmission
     68 * @param epp   Endpoint pair, oriented for transmission
    6869 * @param seg   Segment
    6970 */
    70 void tcp_ncsim_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
     71void tcp_ncsim_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
    7172{
    7273        tcp_squeue_entry_t *sqe;
     
    7576
    7677        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_bounce_seg()");
    77         tcp_rqueue_bounce_seg(sp, seg);
     78        tcp_rqueue_bounce_seg(epp, seg);
    7879        return;
    7980
     
    9293
    9394        sqe->delay = random() % (1000 * 1000);
    94         sqe->sp = *sp;
     95        sqe->epp = *epp;
    9596        sqe->seg = seg;
    9697
     
    147148
    148149                log_msg(LOG_DEFAULT, LVL_DEBUG, "NCSim - End Sleep");
    149                 tcp_rqueue_bounce_seg(&sqe->sp, sqe->seg);
     150                tcp_rqueue_bounce_seg(&sqe->epp, sqe->seg);
    150151                free(sqe);
    151152        }
  • uspace/srv/net/tcp/ncsim.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define NCSIM_H
    3737
     38#include <inet/endpoint.h>
    3839#include "tcp_type.h"
    3940
    4041extern void tcp_ncsim_init(void);
    41 extern void tcp_ncsim_bounce_seg(tcp_sockpair_t *, tcp_segment_t *);
     42extern void tcp_ncsim_bounce_seg(inet_ep2_t *, tcp_segment_t *);
    4243extern void tcp_ncsim_fibril_start(void);
    4344
  • uspace/srv/net/tcp/pdu.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3838#include <byteorder.h>
    3939#include <errno.h>
     40#include <inet/endpoint.h>
    4041#include <mem.h>
    4142#include <stdlib.h>
     
    125126}
    126127
    127 static void tcp_header_setup(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_header_t *hdr)
     128static void tcp_header_setup(inet_ep2_t *epp, tcp_segment_t *seg, tcp_header_t *hdr)
    128129{
    129130        uint16_t doff_flags;
    130131        uint16_t doff;
    131132
    132         hdr->src_port = host2uint16_t_be(sp->local.port);
    133         hdr->dest_port = host2uint16_t_be(sp->foreign.port);
     133        hdr->src_port = host2uint16_t_be(epp->local.port);
     134        hdr->dest_port = host2uint16_t_be(epp->remote.port);
    134135        hdr->seq = host2uint32_t_be(seg->seq);
    135136        hdr->ack = host2uint32_t_be(seg->ack);
     
    190191}
    191192
    192 static int tcp_header_encode(tcp_sockpair_t *sp, tcp_segment_t *seg,
     193static int tcp_header_encode(inet_ep2_t *epp, tcp_segment_t *seg,
    193194    void **header, size_t *size)
    194195{
     
    199200                return ENOMEM;
    200201
    201         tcp_header_setup(sp, seg, hdr);
     202        tcp_header_setup(epp, seg, hdr);
    202203        *header = hdr;
    203204        *size = sizeof(tcp_header_t);
     
    293294
    294295/** Decode incoming PDU */
    295 int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg)
     296int tcp_pdu_decode(tcp_pdu_t *pdu, inet_ep2_t *epp, tcp_segment_t **seg)
    296297{
    297298        tcp_segment_t *nseg;
     
    307308        hdr = (tcp_header_t *)pdu->header;
    308309
    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;
     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;
    313314
    314315        *seg = nseg;
     
    317318
    318319/** Encode outgoing PDU */
    319 int tcp_pdu_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu)
     320int tcp_pdu_encode(inet_ep2_t *epp, tcp_segment_t *seg, tcp_pdu_t **pdu)
    320321{
    321322        tcp_pdu_t *npdu;
     
    327328                return ENOMEM;
    328329
    329         npdu->src = sp->local.addr;
    330         npdu->dest = sp->foreign.addr;
    331         tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size);
     330        npdu->src = epp->local.addr;
     331        npdu->dest = epp->remote.addr;
     332        tcp_header_encode(epp, seg, &npdu->header, &npdu->header_size);
    332333
    333334        text_size = tcp_segment_text_size(seg);
  • uspace/srv/net/tcp/pdu.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define PDU_H
    3737
     38#include <inet/endpoint.h>
    3839#include <sys/types.h>
    3940#include "std.h"
     
    4243extern tcp_pdu_t *tcp_pdu_create(void *, size_t, void *, size_t);
    4344extern void tcp_pdu_delete(tcp_pdu_t *);
    44 extern int tcp_pdu_decode(tcp_pdu_t *, tcp_sockpair_t *, tcp_segment_t **);
    45 extern int tcp_pdu_encode(tcp_sockpair_t *, tcp_segment_t *, tcp_pdu_t **);
     45extern int tcp_pdu_decode(tcp_pdu_t *, inet_ep2_t *, tcp_segment_t **);
     46extern int tcp_pdu_encode(inet_ep2_t *, tcp_segment_t *, tcp_pdu_t **);
    4647
    4748#endif
  • uspace/srv/net/tcp/rqueue.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    6767 * This is for testing purposes only.
    6868 *
    69  * @param sp    Socket pair, oriented for transmission
     69 * @param sp    Endpoint pair, oriented for transmission
    7070 * @param seg   Segment
    7171 */
    72 void tcp_rqueue_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
     72void tcp_rqueue_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
    7373{
    74         tcp_sockpair_t rident;
     74        inet_ep2_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(sp, seg, &pdu) != EOK) {
     82        if (tcp_pdu_encode(epp, 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_sockpair_flipped(sp, &rident);
     99        tcp_ep2_flipped(epp, &rident);
    100100
    101101        /* Insert segment back into rqueue */
     
    106106/** Insert segment into receive queue.
    107107 *
    108  * @param sp    Socket pair, oriented for reception
     108 * @param epp   Endpoint pair, oriented for reception
    109109 * @param seg   Segment
    110110 */
    111 void tcp_rqueue_insert_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
     111void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
    112112{
    113113        tcp_rqueue_entry_t *rqe;
     
    122122        }
    123123
    124         rqe->sp = *sp;
     124        rqe->epp = *epp;
    125125        rqe->seg = seg;
    126126
     
    140140                rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
    141141
    142                 tcp_as_segment_arrived(&rqe->sp, rqe->seg);
     142                tcp_as_segment_arrived(&rqe->epp, rqe->seg);
    143143                free(rqe);
    144144        }
  • uspace/srv/net/tcp/rqueue.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define RQUEUE_H
    3737
     38#include <inet/endpoint.h>
    3839#include "tcp_type.h"
    3940
    4041extern void tcp_rqueue_init(void);
    41 extern void tcp_rqueue_bounce_seg(tcp_sockpair_t *, tcp_segment_t *);
    42 extern void tcp_rqueue_insert_seg(tcp_sockpair_t *, tcp_segment_t *);
     42extern void tcp_rqueue_bounce_seg(inet_ep2_t *, tcp_segment_t *);
     43extern void tcp_rqueue_insert_seg(inet_ep2_t *, tcp_segment_t *);
    4344extern void tcp_rqueue_handler(void *);
    4445extern void tcp_rqueue_fibril_start(void);
  • uspace/srv/net/tcp/service.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3030 * @{
    3131 */
    32 /** @file Socket provider
     32/** @file HelenOS service implementation
    3333 */
    3434
    35 #ifndef SOCK_H
    36 #define SOCK_H
     35#ifndef SERVICE_H
     36#define SERVICE_H
    3737
    38 #include <async.h>
    39 
    40 extern int tcp_sock_init(void);
     38extern int tcp_service_init(void);
    4139
    4240#endif
  • uspace/srv/net/tcp/tcp.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4242#include <io/log.h>
    4343#include <stdio.h>
     44#include <stdlib.h>
    4445#include <task.h>
    4546
     47#include "conn.h"
    4648#include "ncsim.h"
    4749#include "pdu.h"
    4850#include "rqueue.h"
    49 #include "sock.h"
     51#include "service.h"
    5052#include "std.h"
    5153#include "tcp.h"
     
    159161{
    160162        tcp_segment_t *dseg;
    161         tcp_sockpair_t rident;
     163        inet_ep2_t rident;
    162164
    163165        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_received_pdu()");
     
    178180        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_init()");
    179181
     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        }
     188
    180189        tcp_rqueue_init();
    181190        tcp_rqueue_fibril_start();
     
    192201        }
    193202
    194         rc = tcp_sock_init();
    195         if (rc != EOK) {
    196                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
     203        rc = tcp_service_init();
     204        if (rc != EOK) {
     205                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing service.");
    197206                return ENOENT;
    198207        }
  • uspace/srv/net/tcp/tcp_type.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4141#include <fibril.h>
    4242#include <fibril_synch.h>
    43 #include <socket_core.h>
    4443#include <sys/types.h>
    4544#include <inet/addr.h>
     45#include <inet/endpoint.h>
    4646
    4747struct tcp_conn;
     
    9090        /* Connection reset */
    9191        TCP_ERESET,
    92         /* Foreign socket unspecified */
     92        /* Remote endpoint unspecified */
    9393        TCP_EUNSPEC,
    9494        /* Insufficient resources */
     
    9797        TCP_EINVPREC,
    9898        /* Security/compartment not allowed */
    99         TCP_EINVCOMP
     99        TCP_EINVCOMP,
     100        TCP_EAGAIN
    100101} tcp_error_t;
    101102
     
    112113} tcp_control_t;
    113114
    114 typedef struct {
    115         inet_addr_t addr;
    116         uint16_t port;
    117 } tcp_sock_t;
    118 
    119 enum tcp_port {
    120         TCP_PORT_ANY = 0
    121 };
    122 
    123 typedef struct {
    124         tcp_sock_t local;
    125         tcp_sock_t foreign;
    126 } tcp_sockpair_t;
    127 
    128115/** Connection incoming segments queue */
    129116typedef struct {
     
    155142typedef void (*tcp_cstate_cb_t)(tcp_conn_t *, void *);
    156143
     144/** Connection callbacks */
     145typedef 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
    157150/** Connection */
    158151struct tcp_conn {
     
    160153        link_t link;
    161154
    162         /** Connection state change callback function */
    163         tcp_cstate_cb_t cstate_cb;
     155        /** Connection callbacks function */
     156        tcp_cb_t *cb;
    164157        /** Argument to @c cstate_cb */
    165         void *cstate_cb_arg;
    166 
    167         /** Connection identification (local and foreign socket) */
    168         tcp_sockpair_t ident;
     158        void *cb_arg;
     159
     160        /** Connection identification (local and remote endpoint) */
     161        inet_ep2_t ident;
    169162
    170163        /** Active or passive connection */
     
    274267typedef struct {
    275268        link_t link;
    276         tcp_sockpair_t sp;
     269        inet_ep2_t epp;
    277270        tcp_segment_t *seg;
    278271} tcp_rqueue_entry_t;
     
    282275        link_t link;
    283276        suseconds_t delay;
    284         tcp_sockpair_t sp;
     277        inet_ep2_t epp;
    285278        tcp_segment_t *seg;
    286279} tcp_squeue_entry_t;
     
    319312} tcp_pdu_t;
    320313
    321 typedef struct {
    322         async_sess_t *sess;
    323         socket_cores_t sockets;
    324 } tcp_client_t;
    325 
    326 #define TCP_SOCK_FRAGMENT_SIZE 1024
    327 
    328 typedef 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;
     314/** TCP client connection */
     315typedef struct tcp_cconn {
    335316        /** Connection */
    336317        tcp_conn_t *conn;
    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 
    354 typedef struct tcp_sock_lconn {
     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 */
     326typedef struct tcp_clst {
     327        /** Local endpoint */
     328        inet_ep_t elocal;
     329        /** Connection */
    355330        tcp_conn_t *conn;
    356         tcp_sockdata_t *socket;
    357         int index;
    358         link_t ready_list;
    359 } tcp_sock_lconn_t;
    360 
     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 */
     340typedef 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;
    361348
    362349#endif
  • uspace/srv/net/tcp/test.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5050{
    5151        tcp_conn_t *conn;
    52         tcp_sock_t lsock;
    53         tcp_sock_t fsock;
     52        inet_ep2_t epp;
    5453        char rcv_buf[RCV_BUF_SIZE + 1];
    5554        size_t rcvd;
     
    5756
    5857        printf("test_srv()\n");
    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        
     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
    6667        printf("S: User open...\n");
    67         tcp_uc_open(&lsock, &fsock, ap_passive, 0, &conn);
     68        tcp_uc_open(&epp, ap_passive, 0, &conn);
    6869        conn->name = (char *) "S";
    6970
     
    9394{
    9495        tcp_conn_t *conn;
    95         tcp_sock_t lsock;
    96         tcp_sock_t fsock;
     96        inet_ep2_t epp;
    9797        const char *msg = "Hello World!";
    9898
    9999        printf("test_cli()\n");
    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;
     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;
    106108
    107109        async_usleep(1000*1000*3);
    108110        printf("C: User open...\n");
    109         tcp_uc_open(&lsock, &fsock, ap_active, 0, &conn);
     111        tcp_uc_open(&epp, ap_active, 0, &conn);
    110112        conn->name = (char *) "C";
    111113
  • uspace/srv/net/tcp/tqueue.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    282282}
    283283
    284 void tcp_transmit_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
     284void tcp_transmit_segment(inet_ep2_t *epp, tcp_segment_t *seg)
    285285{
    286286        log_msg(LOG_DEFAULT, LVL_DEBUG,
    287             "tcp_transmit_segment(f:(%u),l:(%u), %p)",
    288             sp->local.port, sp->foreign.port, seg);
    289        
     287            "tcp_transmit_segment(l:(%u),f:(%u), %p)",
     288            epp->local.port, epp->remote.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(sp, seg, &pdu) != EOK) {
     303        if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
    304304                log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
    305305                return;
     
    351351
    352352        /* Reset retransmission timer */
    353         tcp_tqueue_timer_set(tqe->conn);
     353        fibril_timer_set_locked(conn->retransmit.timer, RETRANSMIT_TIMEOUT,
     354            retransmit_timeout_func, (void *) conn);
    354355
    355356        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

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define TQUEUE_H
    3737
     38#include <inet/endpoint.h>
    3839#include "std.h"
    3940#include "tcp_type.h"
     
    4849extern void tcp_prepare_transmit_segment(tcp_conn_t *, tcp_segment_t *);
    4950extern void tcp_conn_transmit_segment(tcp_conn_t *, tcp_segment_t *);
    50 extern void tcp_transmit_segment(tcp_sockpair_t *, tcp_segment_t *);
     51extern void tcp_transmit_segment(inet_ep2_t *, tcp_segment_t *);
    5152extern void tcp_header_setup(tcp_conn_t *, tcp_segment_t *, tcp_header_t *);
    5253extern void tcp_phdr_setup(tcp_conn_t *, tcp_segment_t *, tcp_phdr_t *);
  • uspace/srv/net/tcp/ucall.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3535 */
    3636
     37#include <errno.h>
    3738#include <fibril_synch.h>
    3839#include <io/log.h>
     
    5051/** OPEN user call
    5152 *
    52  * @param lsock         Local socket
    53  * @param fsock         Foreign socket
     53 * @param epp           Endpoint pair
    5454 * @param acpass        Active/passive
    5555 * @param oflags        Open flags
     
    6565 * establishment.
    6666 */
    67 tcp_error_t tcp_uc_open(tcp_sock_t *lsock, tcp_sock_t *fsock, acpass_t acpass,
     67tcp_error_t tcp_uc_open(inet_ep2_t *epp, acpass_t acpass,
    6868    tcp_open_flags_t oflags, tcp_conn_t **conn)
    6969{
    7070        tcp_conn_t *nconn;
    71 
    72         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %p, %s, %s, %p)",
    73             lsock, fsock, acpass == ap_active ? "active" : "passive",
     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",
    7475            oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
    7576
    76         nconn = tcp_conn_new(lsock, fsock);
    77         tcp_conn_add(nconn);
     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
    7884        tcp_conn_lock(nconn);
    7985
     
    188194        /* Wait for data to become available */
    189195        while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
     196                tcp_conn_unlock(conn);
     197                return TCP_EAGAIN;
    190198                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
    191199                fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
     
    250258                log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - listen/syn_sent");
    251259                tcp_conn_reset(conn);
    252                 tcp_conn_remove(conn);
    253260                tcp_conn_unlock(conn);
    254261                return TCP_EOK;
     
    294301}
    295302
    296 void 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)",
     303void 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)",
    299306            conn, cb, arg);
    300307
    301         conn->cstate_cb = cb;
    302         conn->cstate_cb_arg = arg;
     308        conn->cb = cb;
     309        conn->cb_arg = arg;
     310}
     311
     312void *tcp_uc_get_userptr(tcp_conn_t *conn)
     313{
     314        return conn->cb_arg;
    303315}
    304316
     
    308320
    309321/** Segment arrived */
    310 void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
     322void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
    311323{
    312324        tcp_conn_t *conn;
     
    314326        log_msg(LOG_DEFAULT, LVL_DEBUG,
    315327            "tcp_as_segment_arrived(f:(%u), l:(%u))",
    316             sp->foreign.port, sp->local.port);
    317 
    318         conn = tcp_conn_find_ref(sp);
     328            epp->remote.port, epp->local.port);
     329
     330        conn = tcp_conn_find_ref(epp);
    319331        if (conn == NULL) {
    320332                log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
    321                 tcp_unexpected_segment(sp, seg);
     333                tcp_unexpected_segment(epp, seg);
    322334                return;
    323335        }
    324336
    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);
     337        tcp_conn_segment_arrived(conn, epp, seg);
    347338        tcp_conn_delref(conn);
    348339}
  • uspace/srv/net/tcp/ucall.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2011 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define UCALL_H
    3737
     38#include <inet/endpoint.h>
    3839#include <sys/types.h>
    3940#include "tcp_type.h"
     
    4243 * User calls
    4344 */
    44 extern tcp_error_t tcp_uc_open(tcp_sock_t *, tcp_sock_t *, acpass_t,
     45extern tcp_error_t tcp_uc_open(inet_ep2_t *, acpass_t,
    4546    tcp_open_flags_t, tcp_conn_t **);
    4647extern tcp_error_t tcp_uc_send(tcp_conn_t *, void *, size_t, xflags_t);
     
    5051extern void tcp_uc_status(tcp_conn_t *, tcp_conn_status_t *);
    5152extern void tcp_uc_delete(tcp_conn_t *);
    52 extern void tcp_uc_set_cstate_cb(tcp_conn_t *, tcp_cstate_cb_t, void *);
     53extern void tcp_uc_set_cb(tcp_conn_t *, tcp_cb_t *, void *);
     54extern void *tcp_uc_get_userptr(tcp_conn_t *);
    5355
    5456/*
    5557 * Arriving segments
    5658 */
    57 extern void tcp_as_segment_arrived(tcp_sockpair_t *, tcp_segment_t *);
     59extern void tcp_as_segment_arrived(inet_ep2_t *, tcp_segment_t *);
    5860
    5961/*
  • uspace/srv/net/udp/Makefile

    rdf2bce32 rfb4d788  
    2828
    2929USPACE_PREFIX = ../../..
    30 LIBS = $(LIBNET_PREFIX)/libnet.a
    31 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include
     30
     31LIBS = \
     32        $(LIBNETTL_PREFIX)/libnettl.a
     33
     34EXTRA_CFLAGS += \
     35        -I$(LIBNETTL_PREFIX)/include
     36
    3237BINARY = udp
    3338
     
    3540        assoc.c \
    3641        msg.c \
    37         sock.c \
    3842        pdu.c \
    39         ucall.c \
     43        service.c \
    4044        udp.c \
    4145        udp_inet.c
  • uspace/srv/net/udp/assoc.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636
    3737#include <adt/list.h>
     38#include <errno.h>
    3839#include <stdbool.h>
    3940#include <fibril_synch.h>
     41#include <inet/endpoint.h>
    4042#include <io/log.h>
     43#include <nettl/amap.h>
    4144#include <stdlib.h>
    4245
     
    4447#include "msg.h"
    4548#include "pdu.h"
    46 #include "ucall.h"
    4749#include "udp_inet.h"
    4850#include "udp_type.h"
    4951
    50 LIST_INITIALIZE(assoc_list);
    51 FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
    52 
    53 static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *);
    54 static int udp_assoc_queue_msg(udp_assoc_t *, udp_sockpair_t *, udp_msg_t *);
    55 static bool udp_socket_match(udp_sock_t *, udp_sock_t *);
    56 static bool udp_sockpair_match(udp_sockpair_t *, udp_sockpair_t *);
     52static LIST_INITIALIZE(assoc_list);
     53static FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
     54static amap_t *amap;
     55
     56static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *);
     57static int udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *);
     58
     59/** Initialize associations. */
     60int 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}
    5772
    5873/** Create new association structure.
    5974 *
    60  * @param lsock         Local socket (will be deeply copied)
    61  * @param fsock         Foreign socket (will be deeply copied)
     75 * @param epp           Endpoint pair (will be copied)
     76 * @param cb            Callbacks
     77 * @param cb_arg        Callback argument
    6278 * @return              New association or NULL
    6379 */
    64 udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock)
     80udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg)
    6581{
    6682        udp_assoc_t *assoc = NULL;
     
    8096        fibril_condvar_initialize(&assoc->rcv_queue_cv);
    8197
    82         if (lsock != NULL)
    83                 assoc->ident.local = *lsock;
    84        
    85         if (fsock != NULL)
    86                 assoc->ident.foreign = *fsock;
    87 
     98        if (epp != NULL)
     99                assoc->ident = *epp;
     100
     101        assoc->cb = cb;
     102        assoc->cb_arg = cb_arg;
    88103        return assoc;
    89104error:
     
    166181 * Add association to the association map.
    167182 */
    168 void udp_assoc_add(udp_assoc_t *assoc)
    169 {
     183int udp_assoc_add(udp_assoc_t *assoc)
     184{
     185        inet_ep2_t aepp;
     186        int rc;
     187
    170188        udp_assoc_addref(assoc);
    171189        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;
    172199        list_append(&assoc->link, &assoc_list);
    173200        fibril_mutex_unlock(&assoc_list_lock);
     201
     202        return EOK;
    174203}
    175204
     
    181210{
    182211        fibril_mutex_lock(&assoc_list_lock);
     212        amap_remove(amap, &assoc->ident);
    183213        list_remove(&assoc->link);
    184214        fibril_mutex_unlock(&assoc_list_lock);
     
    196226            assoc, iplink);
    197227        fibril_mutex_lock(&assoc->lock);
    198         assoc->ident.iplink = iplink;
     228        assoc->ident.local_link = iplink;
    199229        fibril_mutex_unlock(&assoc->lock);
    200230}
    201231
    202 /** Set foreign socket in association.
    203  *
    204  * @param assoc         Association
    205  * @param fsock         Foreign socket (deeply copied)
    206  */
    207 void 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  */
    221 void 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  */
    235 void 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;
    240         fibril_mutex_unlock(&assoc->lock);
    241 }
    242 
    243232/** Send message to association.
    244233 *
    245234 * @param assoc         Association
    246  * @param fsock         Foreign socket or NULL not to override @a assoc
     235 * @param remote        Remote endpoint or NULL not to override @a assoc
    247236 * @param msg           Message
    248237 *
    249238 * @return              EOK on success
    250  *                      EINVAL if foreign socket is not set
     239 *                      EINVAL if remote endpoint is not set
    251240 *                      ENOMEM if out of resources
    252241 *                      EIO if no route to destination exists
    253242 */
    254 int udp_assoc_send(udp_assoc_t *assoc, udp_sock_t *fsock, udp_msg_t *msg)
     243int udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg)
    255244{
    256245        udp_pdu_t *pdu;
    257         udp_sockpair_t sp;
     246        inet_ep2_t epp;
    258247        int rc;
    259248
    260249        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
    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))
     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))
    270261                return EINVAL;
    271262
    272         rc = udp_pdu_encode(&sp, msg, &pdu);
     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);
    273271        if (rc != EOK)
    274272                return ENOMEM;
    275273
     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");
    282283        return EOK;
    283284}
     
    287288 * Pull one message from the association's receive queue.
    288289 */
    289 int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, udp_sock_t *fsock)
     290int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
    290291{
    291292        link_t *link;
     
    303304                log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset");
    304305                fibril_mutex_unlock(&assoc->lock);
    305                 return ECONNABORTED;
     306                return ENXIO;
    306307        }
    307308
     
    313314
    314315        *msg = rqe->msg;
    315         *fsock = rqe->sp.foreign;
     316        *remote = rqe->epp.remote;
    316317        free(rqe);
    317318
     
    323324 * Find the association to which the message belongs and queue it.
    324325 */
    325 void udp_assoc_received(udp_sockpair_t *rsp, udp_msg_t *msg)
     326void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
    326327{
    327328        udp_assoc_t *assoc;
    328329        int rc;
    329330
    330         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", rsp, msg);
    331 
    332         assoc = udp_assoc_find_ref(rsp);
     331        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", repp, msg);
     332
     333        assoc = udp_assoc_find_ref(repp);
    333334        if (assoc == NULL) {
    334335                log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
     
    339340        }
    340341
    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.");
     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.");
    344346                /* XXX Generate ICMP error? */
    345         }
     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);
    346353}
    347354
     
    359366}
    360367
    361 static int udp_assoc_queue_msg(udp_assoc_t *assoc, udp_sockpair_t *sp,
     368static int udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
    362369    udp_msg_t *msg)
    363370{
     
    365372
    366373        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
    367             assoc, sp, msg);
     374            assoc, epp, msg);
    368375
    369376        rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
     
    372379
    373380        link_initialize(&rqe->link);
    374         rqe->sp = *sp;
     381        rqe->epp = *epp;
    375382        rqe->msg = msg;
    376383
     
    384391}
    385392
    386 /** Match socket with pattern. */
    387 static 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. */
    406 static 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.
     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.
    425397 * The association reference count is bumped by one.
    426398 *
    427  * @param sp    Socket pair
     399 * @param epp   Endpoint pair
    428400 * @return      Association structure or NULL if not found.
    429401 */
    430 static 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        
     402static 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);
    434409        fibril_mutex_lock(&assoc_list_lock);
    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        
     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
    451421        fibril_mutex_unlock(&assoc_list_lock);
    452         return NULL;
     422        return assoc;
    453423}
    454424
  • uspace/srv/net/udp/assoc.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define ASSOC_H
    3737
     38#include <inet/endpoint.h>
    3839#include <ipc/loc.h>
    3940#include <sys/types.h>
    4041#include "udp_type.h"
    4142
    42 extern udp_assoc_t *udp_assoc_new(udp_sock_t *, udp_sock_t *);
     43extern int udp_assocs_init(void);
     44extern udp_assoc_t *udp_assoc_new(inet_ep2_t *, udp_assoc_cb_t *, void *);
    4345extern void udp_assoc_delete(udp_assoc_t *);
    44 extern void udp_assoc_add(udp_assoc_t *);
     46extern int udp_assoc_add(udp_assoc_t *);
    4547extern void udp_assoc_remove(udp_assoc_t *);
    4648extern void udp_assoc_addref(udp_assoc_t *);
    4749extern void udp_assoc_delref(udp_assoc_t *);
    4850extern void udp_assoc_set_iplink(udp_assoc_t *, service_id_t);
    49 extern void udp_assoc_set_foreign(udp_assoc_t *, udp_sock_t *);
    50 extern void udp_assoc_set_local(udp_assoc_t *, udp_sock_t *);
    51 extern void udp_assoc_set_local_port(udp_assoc_t *, uint16_t);
    52 extern int udp_assoc_send(udp_assoc_t *, udp_sock_t *, udp_msg_t *);
    53 extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, udp_sock_t *);
    54 extern void udp_assoc_received(udp_sockpair_t *, udp_msg_t *);
     51extern int udp_assoc_send(udp_assoc_t *, inet_ep_t *, udp_msg_t *);
     52extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, inet_ep_t *);
     53extern void udp_assoc_received(inet_ep2_t *, udp_msg_t *);
    5554extern void udp_assoc_reset(udp_assoc_t *);
    5655
  • uspace/srv/net/udp/msg.c

    rdf2bce32 rfb4d788  
    5050void udp_msg_delete(udp_msg_t *msg)
    5151{
     52        free(msg->data);
    5253        free(msg);
    5354}
  • uspace/srv/net/udp/pdu.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4141#include <stdlib.h>
    4242#include <inet/addr.h>
    43 #include <net/socket_codes.h>
    4443#include "msg.h"
    4544#include "pdu.h"
     
    163162
    164163/** Decode incoming PDU */
    165 int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
     164int udp_pdu_decode(udp_pdu_t *pdu, inet_ep2_t *epp, udp_msg_t **msg)
    166165{
    167166        udp_msg_t *nmsg;
     
    180179        hdr = (udp_header_t *)pdu->data;
    181180
    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;
     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;
    186186
    187187        length = uint16_t_be2host(hdr->length);
     
    197197                return ENOMEM;
    198198
    199         nmsg->data = text;
    200199        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);
    201205
    202206        *msg = nmsg;
     
    205209
    206210/** Encode outgoing PDU */
    207 int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
     211int udp_pdu_encode(inet_ep2_t *epp, udp_msg_t *msg, udp_pdu_t **pdu)
    208212{
    209213        udp_pdu_t *npdu;
     
    215219                return ENOMEM;
    216220
    217         npdu->iplink = sp->iplink;
    218         npdu->src = sp->local.addr;
    219         npdu->dest = sp->foreign.addr;
     221        npdu->iplink = epp->local_link;
     222        npdu->src = epp->local.addr;
     223        npdu->dest = epp->remote.addr;
    220224
    221225        npdu->data_size = sizeof(udp_header_t) + msg->data_size;
     
    227231
    228232        hdr = (udp_header_t *)npdu->data;
    229         hdr->src_port = host2uint16_t_be(sp->local.port);
    230         hdr->dest_port = host2uint16_t_be(sp->foreign.port);
     233        hdr->src_port = host2uint16_t_be(epp->local.port);
     234        hdr->dest_port = host2uint16_t_be(epp->remote.port);
    231235        hdr->length = host2uint16_t_be(npdu->data_size);
    232236        hdr->checksum = 0;
  • uspace/srv/net/udp/pdu.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define PDU_H
    3737
     38#include <inet/endpoint.h>
    3839#include <sys/types.h>
    3940#include "std.h"
     
    4243extern udp_pdu_t *udp_pdu_new(void);
    4344extern void udp_pdu_delete(udp_pdu_t *);
    44 extern int udp_pdu_decode(udp_pdu_t *, udp_sockpair_t *, udp_msg_t **);
    45 extern int udp_pdu_encode(udp_sockpair_t *, udp_msg_t *, udp_pdu_t **);
     45extern int udp_pdu_decode(udp_pdu_t *, inet_ep2_t *, udp_msg_t **);
     46extern int udp_pdu_encode(inet_ep2_t *, udp_msg_t *, udp_pdu_t **);
    4647
    4748#endif
  • uspace/srv/net/udp/service.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3030 * @{
    3131 */
    32 /** @file Socket provider
     32/** @file HelenOS service implementation
    3333 */
    3434
    35 #ifndef SOCK_H
    36 #define SOCK_H
     35#ifndef SERVICE_H
     36#define SERVICE_H
    3737
    38 #include <async.h>
    39 
    40 extern int udp_sock_init(void);
     38extern int udp_service_init(void);
    4139
    4240#endif
  • uspace/srv/net/udp/udp.c

    rdf2bce32 rfb4d788  
    4141#include <task.h>
    4242
     43#include "assoc.h"
     44#include "service.h"
    4345#include "udp_inet.h"
    44 #include "sock.h"
    4546
    4647#define NAME       "udp"
     
    5253        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_init()");
    5354
     55        rc = udp_assocs_init();
     56        if (rc != EOK) {
     57                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing associations.");
     58                return ENOMEM;
     59        }
     60
    5461        rc = udp_inet_init();
    5562        if (rc != EOK) {
     
    5865        }
    5966
    60         rc = udp_sock_init();
     67        rc = udp_service_init();
    6168        if (rc != EOK) {
    62                 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");
     69                log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing UDP service.");
    6370                return ENOENT;
    6471        }
  • uspace/srv/net/udp/udp_inet.c

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    6464
    6565        pdu = udp_pdu_new();
     66        pdu->iplink = dgram->iplink;
    6667        pdu->data = dgram->data;
    6768        pdu->data_size = dgram->size;
     
    105106{
    106107        udp_msg_t *dmsg;
    107         udp_sockpair_t rident;
     108        inet_ep2_t rident;
    108109
    109110        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_received_pdu()");
  • uspace/srv/net/udp/udp_type.h

    rdf2bce32 rfb4d788  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2015 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#define UDP_TYPE_H
    3737
     38#include <async.h>
    3839#include <fibril.h>
    3940#include <fibril_synch.h>
     41#include <inet/endpoint.h>
    4042#include <ipc/loc.h>
    41 #include <socket_core.h>
    4243#include <sys/types.h>
    4344#include <inet/addr.h>
     
    4950        /* Insufficient resources */
    5051        UDP_ENORES,
    51         /* Foreign socket unspecified */
     52        /* Remote endpoint unspecified */
    5253        UDP_EUNSPEC,
    5354        /* No route to destination */
     
    6061        XF_DUMMY = 0x1
    6162} xflags_t;
    62 
    63 enum udp_port {
    64         UDP_PORT_ANY = 0
    65 };
    66 
    67 typedef struct {
    68         inet_addr_t addr;
    69         uint16_t port;
    70 } udp_sock_t;
    71 
    72 typedef struct {
    73         service_id_t iplink;
    74         udp_sock_t local;
    75         udp_sock_t foreign;
    76 } udp_sockpair_t;
    7763
    7864/** Unencoded UDP message (datagram) */
     
    9985
    10086typedef struct {
    101         async_sess_t *sess;
    102         socket_cores_t sockets;
    103 } udp_client_t;
     87        void (*recv_msg)(void *, inet_ep2_t *, udp_msg_t *);
     88} udp_assoc_cb_t;
    10489
    10590/** UDP association
     
    10792 * This is a rough equivalent of a TCP connection endpoint. It allows
    10893 * sending and receiving UDP datagrams and it is uniquely identified
    109  * by a socket pair.
     94 * by an endpoint pair.
    11095 */
    11196typedef struct {
     
    11398        link_t link;
    11499
    115         /** Association identification (local and foreign socket) */
    116         udp_sockpair_t ident;
     100        /** Association identification (endpoint pair) */
     101        inet_ep2_t ident;
    117102
    118103        /** True if association was reset by user */
     
    131116        /** Receive queue CV. Broadcast when new datagram is inserted */
    132117        fibril_condvar_t rcv_queue_cv;
     118
     119        udp_assoc_cb_t *cb;
     120        void *cb_arg;
    133121} udp_assoc_t;
    134122
     
    136124} udp_assoc_status_t;
    137125
    138 typedef struct udp_sockdata {
    139         /** Lock */
    140         fibril_mutex_t lock;
    141         /** Socket core */
    142         socket_core_t *sock_core;
     126typedef 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
     135typedef struct udp_cassoc {
     136        /** Association */
     137        udp_assoc_t *assoc;
     138        /** Association ID for the client */
     139        sysarg_t id;
    143140        /** Client */
    144         udp_client_t *client;
    145         /** Connection */
    146         udp_assoc_t *assoc;
    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;
     141        struct udp_client *client;
     142        link_t lclient;
     143} udp_cassoc_t;
    158144
    159145typedef struct {
    160146        /** Link to receive queue */
    161147        link_t link;
    162         /** Socket pair */
    163         udp_sockpair_t sp;
     148        /** Endpoint pair */
     149        inet_ep2_t epp;
    164150        /** Message */
    165151        udp_msg_t *msg;
    166 } udp_rcv_queue_entry_t;
     152        /** Client association */
     153        udp_cassoc_t *cassoc;
     154} udp_crcv_queue_entry_t;
     155
     156typedef 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;
    167164
    168165#endif
Note: See TracChangeset for help on using the changeset viewer.