Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/udp/sock.c

    rdb81577 r6b0b508  
    8888static void udp_sock_notify_data(socket_core_t *sock_core)
    8989{
    90         log_msg(LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
     90        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
    9191        async_exch_t *exch = async_exchange_begin(sock_core->sess);
    92         async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t)sock_core->socket_id,
     92        async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) sock_core->socket_id,
    9393            UDP_FRAGMENT_SIZE, 0, 0, 1);
    9494        async_exchange_end(exch);
     
    103103        ipc_call_t answer;
    104104
    105         log_msg(LVL_DEBUG, "udp_sock_socket()");
     105        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_socket()");
    106106        sock = calloc(1, sizeof(udp_sockdata_t));
    107107        if (sock == NULL) {
     
    167167        udp_error_t urc;
    168168
    169         log_msg(LVL_DEBUG, "udp_sock_bind()");
    170         log_msg(LVL_DEBUG, " - async_data_write_accept");
     169        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()");
     170        log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
    171171
    172172        addr = NULL;
     
    177177                goto out;
    178178        }
    179 
    180         log_msg(LVL_DEBUG, " - call socket_bind");
     179       
     180        if (addr_size != sizeof(struct sockaddr_in)) {
     181                async_answer_0(callid, EINVAL);
     182                goto out;
     183        }
     184       
     185        log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
    181186        rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
    182187            addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
     
    186191                goto out;
    187192        }
    188 
    189         if (addr_size != sizeof(struct sockaddr_in)) {
    190                 async_answer_0(callid, EINVAL);
    191                 goto out;
    192         }
    193 
    194         log_msg(LVL_DEBUG, " - call socket_cores_find");
     193       
     194        log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
    195195        sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
    196196        if (sock_core == NULL) {
     
    222222        }
    223223
    224         log_msg(LVL_DEBUG, " - success");
     224        log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
    225225        async_answer_0(callid, rc);
    226226out:
     
    231231static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    232232{
    233         log_msg(LVL_DEBUG, "udp_sock_listen()");
     233        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_listen()");
    234234        async_answer_0(callid, ENOTSUP);
    235235}
     
    237237static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    238238{
    239         log_msg(LVL_DEBUG, "udp_sock_connect()");
     239        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connect()");
    240240        async_answer_0(callid, ENOTSUP);
    241241}
     
    243243static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    244244{
    245         log_msg(LVL_DEBUG, "udp_sock_accept()");
     245        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_accept()");
    246246        async_answer_0(callid, ENOTSUP);
    247247}
     
    249249static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    250250{
    251         int socket_id;
    252         int fragments;
    253         int index;
    254         struct sockaddr_in *addr;
    255         size_t addr_size;
    256         socket_core_t *sock_core;
    257         udp_sockdata_t *socket;
    258         udp_sock_t fsock, *fsockp;
    259         ipc_call_t answer;
    260         ipc_callid_t wcallid;
    261         size_t length;
    262         uint8_t buffer[UDP_FRAGMENT_SIZE];
    263         udp_error_t urc;
    264         int rc;
    265 
    266         log_msg(LVL_DEBUG, "udp_sock_send()");
    267 
    268         addr = NULL;
    269 
     251        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
     252       
     253        struct sockaddr_in *addr = NULL;
     254        udp_sock_t fsock;
     255        udp_sock_t *fsock_ptr;
     256       
    270257        if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
    271                 rc = async_data_write_accept((void **) &addr, false,
     258                size_t addr_size;
     259                int rc = async_data_write_accept((void **) &addr, false,
    272260                    0, 0, 0, &addr_size);
    273261                if (rc != EOK) {
     
    275263                        goto out;
    276264                }
    277 
     265               
    278266                if (addr_size != sizeof(struct sockaddr_in)) {
    279267                        async_answer_0(callid, EINVAL);
    280268                        goto out;
    281269                }
    282 
     270               
    283271                fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
    284272                fsock.port = uint16_t_be2host(addr->sin_port);
    285                 fsockp = &fsock;
    286         } else {
    287                 fsockp = NULL;
    288         }
    289 
    290         socket_id = SOCKET_GET_SOCKET_ID(call);
    291         fragments = SOCKET_GET_DATA_FRAGMENTS(call);
     273                fsock_ptr = &fsock;
     274        } else
     275                fsock_ptr = NULL;
     276       
     277        int socket_id = SOCKET_GET_SOCKET_ID(call);
     278       
    292279        SOCKET_GET_FLAGS(call);
    293 
    294         sock_core = socket_cores_find(&client->sockets, socket_id);
     280       
     281        socket_core_t *sock_core =
     282            socket_cores_find(&client->sockets, socket_id);
    295283        if (sock_core == NULL) {
    296284                async_answer_0(callid, ENOTSOCK);
    297285                goto out;
    298286        }
    299 
    300         if (sock_core->port == 0) {
     287       
     288        udp_sockdata_t *socket =
     289            (udp_sockdata_t *) sock_core->specific_data;
     290       
     291        if (sock_core->port <= 0) {
    301292                /* Implicitly bind socket to port */
    302                 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
    303                     addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
    304                     last_used_port);
     293                int rc = socket_bind_free_port(&gsock, sock_core,
     294                    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port);
    305295                if (rc != EOK) {
    306296                        async_answer_0(callid, rc);
    307297                        goto out;
    308298                }
    309         }
    310 
    311         socket = (udp_sockdata_t *)sock_core->specific_data;
     299               
     300                assert(sock_core->port > 0);
     301               
     302                udp_error_t urc = udp_uc_set_local_port(socket->assoc,
     303                    sock_core->port);
     304               
     305                if (urc != UDP_EOK) {
     306                        // TODO: better error handling
     307                        async_answer_0(callid, EINTR);
     308                        goto out;
     309                }
     310               
     311                last_used_port = sock_core->port;
     312        }
     313       
    312314        fibril_mutex_lock(&socket->lock);
    313 
     315       
    314316        if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {
    315317                /* Determine local IP address */
    316318                inet_addr_t loc_addr, rem_addr;
    317 
    318                 rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 :
     319               
     320                rem_addr.ipv4 = fsock_ptr ? fsock.addr.ipv4 :
    319321                    socket->assoc->ident.foreign.addr.ipv4;
    320 
    321                 rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
     322               
     323                int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
    322324                if (rc != EOK) {
    323325                        fibril_mutex_unlock(&socket->lock);
    324326                        async_answer_0(callid, rc);
    325                         log_msg(LVL_DEBUG, "udp_sock_sendto: Failed to "
     327                        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to "
    326328                            "determine local address.");
    327329                        return;
    328330                }
    329 
     331               
    330332                socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4;
    331                 log_msg(LVL_DEBUG, "Local IP address is %x",
     333                log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x",
    332334                    socket->assoc->ident.local.addr.ipv4);
    333335        }
    334 
    335 
     336       
    336337        assert(socket->assoc != NULL);
    337 
    338         for (index = 0; index < fragments; index++) {
     338       
     339        int fragments = SOCKET_GET_DATA_FRAGMENTS(call);
     340        for (int index = 0; index < fragments; index++) {
     341                ipc_callid_t wcallid;
     342                size_t length;
     343               
    339344                if (!async_data_write_receive(&wcallid, &length)) {
    340345                        fibril_mutex_unlock(&socket->lock);
     
    342347                        goto out;
    343348                }
    344 
     349               
    345350                if (length > UDP_FRAGMENT_SIZE)
    346351                        length = UDP_FRAGMENT_SIZE;
    347 
    348                 rc = async_data_write_finalize(wcallid, buffer, length);
     352               
     353                uint8_t buffer[UDP_FRAGMENT_SIZE];
     354                int rc = async_data_write_finalize(wcallid, buffer, length);
    349355                if (rc != EOK) {
    350356                        fibril_mutex_unlock(&socket->lock);
     
    352358                        goto out;
    353359                }
    354 
    355                 urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0);
    356 
     360               
     361                udp_error_t urc =
     362                    udp_uc_send(socket->assoc, fsock_ptr, buffer, length, 0);
     363               
    357364                switch (urc) {
    358365                case UDP_EOK:
    359366                        rc = EOK;
    360367                        break;
    361 /*              case TCP_ENOTEXIST:
    362                         rc = ENOTCONN;
    363                         break;
    364                 case TCP_ECLOSING:
    365                         rc = ENOTCONN;
    366                         break;
    367                 case TCP_ERESET:
    368                         rc = ECONNABORTED;
    369                         break;*/
     368                case UDP_ENORES:
     369                        rc = ENOMEM;
     370                        break;
     371                case UDP_EUNSPEC:
     372                        rc = EINVAL;
     373                        break;
     374                case UDP_ENOROUTE:
     375                        rc = EIO;
     376                        break;
    370377                default:
    371378                        assert(false);
    372379                }
    373 
     380               
    374381                if (rc != EOK) {
    375382                        fibril_mutex_unlock(&socket->lock);
     
    378385                }
    379386        }
     387       
     388        ipc_call_t answer;
    380389       
    381390        IPC_SET_ARG1(answer, 0);
     
    405414        int rc;
    406415
    407         log_msg(LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
     416        log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
    408417
    409418        socket_id = SOCKET_GET_SOCKET_ID(call);
     
    427436        (void)flags;
    428437
    429         log_msg(LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
     438        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
    430439        fibril_mutex_lock(&socket->recv_buffer_lock);
    431440        while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) {
    432                 log_msg(LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
     441                log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
    433442                fibril_condvar_wait(&socket->recv_buffer_cv,
    434443                    &socket->recv_buffer_lock);
    435444        }
    436445
    437         log_msg(LVL_DEBUG, "Got data in sock recv_buffer");
     446        log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
    438447
    439448        rsock = socket->recv_fsock;
     
    441450        urc = socket->recv_error;
    442451
    443         log_msg(LVL_DEBUG, "**** recv data_len=%zu", data_len);
     452        log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len);
    444453
    445454        switch (urc) {
     
    458467        }
    459468
    460         log_msg(LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
     469        log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
    461470        if (rc != EOK) {
    462471                fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    472481                addr.sin_port = host2uint16_t_be(rsock.port);
    473482
    474                 log_msg(LVL_DEBUG, "addr read receive");
     483                log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
    475484                if (!async_data_read_receive(&rcallid, &addr_length)) {
    476485                        fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    483492                        addr_length = sizeof(addr);
    484493
    485                 log_msg(LVL_DEBUG, "addr read finalize");
     494                log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
    486495                rc = async_data_read_finalize(rcallid, &addr, addr_length);
    487496                if (rc != EOK) {
     
    493502        }
    494503
    495         log_msg(LVL_DEBUG, "data read receive");
     504        log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
    496505        if (!async_data_read_receive(&rcallid, &length)) {
    497506                fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    504513                length = data_len;
    505514
    506         log_msg(LVL_DEBUG, "data read finalize");
     515        log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
    507516        rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
    508517
     
    510519                rc = EOVERFLOW;
    511520
    512         log_msg(LVL_DEBUG, "read_data_length <- %zu", length);
     521        log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length);
    513522        IPC_SET_ARG2(answer, 0);
    514523        SOCKET_SET_READ_DATA_LENGTH(answer, length);
     
    526535static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    527536{
    528         int socket_id;
    529         socket_core_t *sock_core;
    530         udp_sockdata_t *socket;
    531         int rc;
    532 
    533         log_msg(LVL_DEBUG, "tcp_sock_close()");
    534         socket_id = SOCKET_GET_SOCKET_ID(call);
    535 
    536         sock_core = socket_cores_find(&client->sockets, socket_id);
     537        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
     538        int socket_id = SOCKET_GET_SOCKET_ID(call);
     539       
     540        socket_core_t *sock_core =
     541            socket_cores_find(&client->sockets, socket_id);
    537542        if (sock_core == NULL) {
    538543                async_answer_0(callid, ENOTSOCK);
    539544                return;
    540545        }
    541 
    542         socket = (udp_sockdata_t *)sock_core->specific_data;
     546       
     547        udp_sockdata_t *socket =
     548            (udp_sockdata_t *) sock_core->specific_data;
    543549        fibril_mutex_lock(&socket->lock);
    544 
    545         rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
     550       
     551        int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
    546552            udp_free_sock_data);
    547553        if (rc != EOK) {
     
    550556                return;
    551557        }
    552 
     558       
    553559        fibril_mutex_unlock(&socket->lock);
    554560        async_answer_0(callid, EOK);
     
    557563static void udp_sock_getsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    558564{
    559         log_msg(LVL_DEBUG, "udp_sock_getsockopt()");
     565        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_getsockopt()");
    560566        async_answer_0(callid, ENOTSUP);
    561567}
     
    563569static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    564570{
    565         log_msg(LVL_DEBUG, "udp_sock_setsockopt()");
     571        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt()");
    566572        async_answer_0(callid, ENOTSUP);
    567573}
     
    574580        size_t rcvd;
    575581
    576         log_msg(LVL_DEBUG, "udp_sock_recv_fibril()");
     582        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()");
    577583
    578584        while (true) {
    579                 log_msg(LVL_DEBUG, "[] wait for rcv buffer empty()");
     585                log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()");
    580586                fibril_mutex_lock(&sock->recv_buffer_lock);
    581587                while (sock->recv_buffer_used != 0) {
     
    583589                            &sock->recv_buffer_lock);
    584590                }
    585 
    586                 log_msg(LVL_DEBUG, "[] call udp_uc_receive()");
     591               
     592                log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
    587593                urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
    588594                    UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
    589595                sock->recv_error = urc;
    590 
     596               
    591597                udp_sock_notify_data(sock->sock_core);
    592 
     598               
    593599                if (urc != UDP_EOK) {
    594600                        fibril_condvar_broadcast(&sock->recv_buffer_cv);
     
    596602                        break;
    597603                }
    598 
    599                 log_msg(LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
    600 
     604               
     605                log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
     606               
    601607                sock->recv_buffer_used = rcvd;
    602608                fibril_mutex_unlock(&sock->recv_buffer_lock);
     
    622628
    623629        while (true) {
    624                 log_msg(LVL_DEBUG, "udp_sock_connection: wait");
     630                log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: wait");
    625631                callid = async_get_call(&call);
    626632                if (!IPC_GET_IMETHOD(call))
    627633                        break;
    628634
    629                 log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d",
     635                log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: METHOD=%d",
    630636                    (int)IPC_GET_IMETHOD(call));
    631637
     
    670676
    671677        /* Clean up */
    672         log_msg(LVL_DEBUG, "udp_sock_connection: Clean up");
     678        log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: Clean up");
    673679        async_hangup(client.sess);
    674680        socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
Note: See TracChangeset for help on using the changeset viewer.