Ignore:
File:
1 edited

Legend:

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

    r695b6ff rdb81577  
    11/*
    22 * Copyright (c) 2008 Lukas Mejdrech
    3  * Copyright (c) 2013 Jiri Svoboda
     3 * Copyright (c) 2012 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    8888static void udp_sock_notify_data(socket_core_t *sock_core)
    8989{
    90         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
     90        log_msg(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(LOG_DEFAULT, LVL_DEBUG, "udp_sock_socket()");
     105        log_msg(LVL_DEBUG, "udp_sock_socket()");
    106106        sock = calloc(1, sizeof(udp_sockdata_t));
    107107        if (sock == NULL) {
     
    159159static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    160160{
    161         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()");
    162         log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
    163        
    164         struct sockaddr_in6 *addr6 = NULL;
    165         size_t addr_len;
    166         int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len);
     161        int rc;
     162        struct sockaddr_in *addr;
     163        size_t addr_size;
     164        socket_core_t *sock_core;
     165        udp_sockdata_t *socket;
     166        udp_sock_t fsock;
     167        udp_error_t urc;
     168
     169        log_msg(LVL_DEBUG, "udp_sock_bind()");
     170        log_msg(LVL_DEBUG, " - async_data_write_accept");
     171
     172        addr = NULL;
     173
     174        rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
    167175        if (rc != EOK) {
    168176                async_answer_0(callid, rc);
    169                 return;
    170         }
    171        
    172         if ((addr_len != sizeof(struct sockaddr_in)) &&
    173             (addr_len != sizeof(struct sockaddr_in6))) {
    174                 async_answer_0(callid, EINVAL);
    175177                goto out;
    176178        }
    177        
    178         struct sockaddr_in *addr = (struct sockaddr_in *) addr6;
    179        
    180         log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
    181        
     179
     180        log_msg(LVL_DEBUG, " - call socket_bind");
    182181        rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
    183             addr6, addr_len, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
     182            addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
    184183            last_used_port);
    185184        if (rc != EOK) {
     
    187186                goto out;
    188187        }
    189        
    190         log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
    191        
    192         socket_core_t *sock_core = socket_cores_find(&client->sockets,
    193             SOCKET_GET_SOCKET_ID(call));
     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");
     195        sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
    194196        if (sock_core == NULL) {
    195197                async_answer_0(callid, ENOENT);
    196198                goto out;
    197199        }
    198        
    199         udp_sockdata_t *socket =
    200             (udp_sockdata_t *) sock_core->specific_data;
    201        
    202         udp_sock_t fsocket;
    203        
    204         fsocket.port = sock_core->port;
    205        
    206         switch (addr->sin_family) {
    207         case AF_INET:
    208                 inet_sockaddr_in_addr(addr, &fsocket.addr);
    209                 break;
    210         case AF_INET6:
    211                 inet_sockaddr_in6_addr(addr6, &fsocket.addr);
    212                 break;
    213         default:
    214                 async_answer_0(callid, EINVAL);
    215                 goto out;
    216         }
    217        
    218         udp_error_t urc = udp_uc_set_local(socket->assoc, &fsocket);
    219        
     200
     201        socket = (udp_sockdata_t *)sock_core->specific_data;
     202
     203        fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
     204        fsock.port = sock_core->port;
     205        urc = udp_uc_set_local(socket->assoc, &fsock);
     206
    220207        switch (urc) {
    221208        case UDP_EOK:
     
    234221                assert(false);
    235222        }
    236        
    237         log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
     223
     224        log_msg(LVL_DEBUG, " - success");
    238225        async_answer_0(callid, rc);
    239        
    240226out:
    241         if (addr6 != NULL)
    242                 free(addr6);
     227        if (addr != NULL)
     228                free(addr);
    243229}
    244230
    245231static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    246232{
    247         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_listen()");
     233        log_msg(LVL_DEBUG, "udp_sock_listen()");
    248234        async_answer_0(callid, ENOTSUP);
    249235}
     
    251237static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    252238{
    253         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connect()");
     239        log_msg(LVL_DEBUG, "udp_sock_connect()");
    254240        async_answer_0(callid, ENOTSUP);
    255241}
     
    257243static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    258244{
    259         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_accept()");
     245        log_msg(LVL_DEBUG, "udp_sock_accept()");
    260246        async_answer_0(callid, ENOTSUP);
    261247}
     
    263249static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    264250{
    265         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
    266        
    267         uint8_t *buffer = calloc(UDP_FRAGMENT_SIZE, 1);
    268         if (buffer == NULL) {
    269                 async_answer_0(callid, ENOMEM);
    270                 return;
    271         }
    272        
    273         struct sockaddr_in6 *addr6 = NULL;
     251        int socket_id;
     252        int fragments;
     253        int index;
    274254        struct sockaddr_in *addr;
    275         udp_sock_t fsocket;
    276         udp_sock_t *fsocket_ptr;
    277        
     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
    278270        if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
    279                 size_t addr_len;
    280                 int rc = async_data_write_accept((void **) &addr6, false,
    281                     0, 0, 0, &addr_len);
     271                rc = async_data_write_accept((void **) &addr, false,
     272                    0, 0, 0, &addr_size);
    282273                if (rc != EOK) {
    283274                        async_answer_0(callid, rc);
    284275                        goto out;
    285276                }
    286                
    287                 if ((addr_len != sizeof(struct sockaddr_in)) &&
    288                     (addr_len != sizeof(struct sockaddr_in6))) {
     277
     278                if (addr_size != sizeof(struct sockaddr_in)) {
    289279                        async_answer_0(callid, EINVAL);
    290280                        goto out;
    291281                }
    292                
    293                 addr = (struct sockaddr_in *) addr6;
    294                
    295                 switch (addr->sin_family) {
    296                 case AF_INET:
    297                         inet_sockaddr_in_addr(addr, &fsocket.addr);
    298                         break;
    299                 case AF_INET6:
    300                         inet_sockaddr_in6_addr(addr6, &fsocket.addr);
    301                         break;
    302                 default:
    303                         async_answer_0(callid, EINVAL);
    304                         goto out;
    305                 }
    306                
    307                 fsocket.port = uint16_t_be2host(addr->sin_port);
    308                 fsocket_ptr = &fsocket;
    309         } else
    310                 fsocket_ptr = NULL;
    311        
    312         int socket_id = SOCKET_GET_SOCKET_ID(call);
    313        
     282
     283                fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
     284                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);
    314292        SOCKET_GET_FLAGS(call);
    315        
    316         socket_core_t *sock_core =
    317             socket_cores_find(&client->sockets, socket_id);
     293
     294        sock_core = socket_cores_find(&client->sockets, socket_id);
    318295        if (sock_core == NULL) {
    319296                async_answer_0(callid, ENOTSOCK);
    320297                goto out;
    321298        }
    322        
    323         udp_sockdata_t *socket =
    324             (udp_sockdata_t *) sock_core->specific_data;
    325        
    326         if (sock_core->port <= 0) {
     299
     300        if (sock_core->port == 0) {
    327301                /* Implicitly bind socket to port */
    328                 int rc = socket_bind_free_port(&gsock, sock_core,
    329                     UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_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);
    330305                if (rc != EOK) {
    331306                        async_answer_0(callid, rc);
    332307                        goto out;
    333308                }
    334                
    335                 assert(sock_core->port > 0);
    336                
    337                 udp_error_t urc = udp_uc_set_local_port(socket->assoc,
    338                     sock_core->port);
    339                
    340                 if (urc != UDP_EOK) {
    341                         // TODO: better error handling
    342                         async_answer_0(callid, EINTR);
    343                         goto out;
    344                 }
    345                
    346                 last_used_port = sock_core->port;
    347         }
    348        
     309        }
     310
     311        socket = (udp_sockdata_t *)sock_core->specific_data;
    349312        fibril_mutex_lock(&socket->lock);
    350        
    351         if (inet_addr_is_any(&socket->assoc->ident.local.addr) &&
    352                 socket->assoc->ident.iplink == 0) {
     313
     314        if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {
    353315                /* Determine local IP address */
    354                 inet_addr_t loc_addr;
    355                 inet_addr_t rem_addr;
    356                
    357                 rem_addr = fsocket_ptr ? fsocket.addr :
    358                     socket->assoc->ident.foreign.addr;
    359                
    360                 int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
     316                inet_addr_t loc_addr, rem_addr;
     317
     318                rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 :
     319                    socket->assoc->ident.foreign.addr.ipv4;
     320
     321                rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
    361322                if (rc != EOK) {
    362323                        fibril_mutex_unlock(&socket->lock);
    363324                        async_answer_0(callid, rc);
    364                         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to "
     325                        log_msg(LVL_DEBUG, "udp_sock_sendto: Failed to "
    365326                            "determine local address.");
    366                         goto out;
    367                 }
    368                
    369                 socket->assoc->ident.local.addr = loc_addr;
    370         }
    371        
     327                        return;
     328                }
     329
     330                socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4;
     331                log_msg(LVL_DEBUG, "Local IP address is %x",
     332                    socket->assoc->ident.local.addr.ipv4);
     333        }
     334
     335
    372336        assert(socket->assoc != NULL);
    373        
    374         int fragments = SOCKET_GET_DATA_FRAGMENTS(call);
    375         for (int index = 0; index < fragments; index++) {
    376                 ipc_callid_t wcallid;
    377                 size_t length;
    378                
     337
     338        for (index = 0; index < fragments; index++) {
    379339                if (!async_data_write_receive(&wcallid, &length)) {
    380340                        fibril_mutex_unlock(&socket->lock);
     
    382342                        goto out;
    383343                }
    384                
     344
    385345                if (length > UDP_FRAGMENT_SIZE)
    386346                        length = UDP_FRAGMENT_SIZE;
    387                
    388                 int rc = async_data_write_finalize(wcallid, buffer, length);
     347
     348                rc = async_data_write_finalize(wcallid, buffer, length);
    389349                if (rc != EOK) {
    390350                        fibril_mutex_unlock(&socket->lock);
     
    392352                        goto out;
    393353                }
    394                
    395                 udp_error_t urc =
    396                     udp_uc_send(socket->assoc, fsocket_ptr, buffer, length, 0);
    397                
     354
     355                urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0);
     356
    398357                switch (urc) {
    399358                case UDP_EOK:
    400359                        rc = EOK;
    401360                        break;
    402                 case UDP_ENORES:
    403                         rc = ENOMEM;
    404                         break;
    405                 case UDP_EUNSPEC:
    406                         rc = EINVAL;
    407                         break;
    408                 case UDP_ENOROUTE:
    409                         rc = EIO;
    410                         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;*/
    411370                default:
    412371                        assert(false);
    413372                }
    414                
     373
    415374                if (rc != EOK) {
    416375                        fibril_mutex_unlock(&socket->lock);
     
    419378                }
    420379        }
    421        
    422         ipc_call_t answer;
    423380       
    424381        IPC_SET_ARG1(answer, 0);
     
    429386       
    430387out:
    431         if (addr6 != NULL)
    432                 free(addr6);
    433        
    434         free(buffer);
     388        if (addr != NULL)
     389                free(addr);
    435390}
    436391
    437392static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    438393{
    439         log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
    440        
    441         int socket_id = SOCKET_GET_SOCKET_ID(call);
    442        
    443         socket_core_t *sock_core =
    444             socket_cores_find(&client->sockets, socket_id);
     394        int socket_id;
     395        int flags;
     396        size_t addr_length, length;
     397        socket_core_t *sock_core;
     398        udp_sockdata_t *socket;
     399        ipc_call_t answer;
     400        ipc_callid_t rcallid;
     401        size_t data_len;
     402        udp_error_t urc;
     403        udp_sock_t rsock;
     404        struct sockaddr_in addr;
     405        int rc;
     406
     407        log_msg(LVL_DEBUG, "%p: udp_sock_recv[from]()", client);
     408
     409        socket_id = SOCKET_GET_SOCKET_ID(call);
     410        flags = SOCKET_GET_FLAGS(call);
     411
     412        sock_core = socket_cores_find(&client->sockets, socket_id);
    445413        if (sock_core == NULL) {
    446414                async_answer_0(callid, ENOTSOCK);
    447415                return;
    448416        }
    449        
    450         udp_sockdata_t *socket =
    451             (udp_sockdata_t *) sock_core->specific_data;
    452        
     417
     418        socket = (udp_sockdata_t *)sock_core->specific_data;
    453419        fibril_mutex_lock(&socket->lock);
    454        
     420
    455421        if (socket->assoc == NULL) {
    456422                fibril_mutex_unlock(&socket->lock);
     
    458424                return;
    459425        }
    460        
    461         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
    462        
     426
     427        (void)flags;
     428
     429        log_msg(LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");
    463430        fibril_mutex_lock(&socket->recv_buffer_lock);
    464        
    465         while ((socket->recv_buffer_used == 0) &&
    466             (socket->recv_error == UDP_EOK)) {
    467                 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
     431        while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) {
     432                log_msg(LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");
    468433                fibril_condvar_wait(&socket->recv_buffer_cv,
    469434                    &socket->recv_buffer_lock);
    470435        }
    471        
    472         log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
    473        
    474         size_t data_len = socket->recv_buffer_used;
    475         udp_error_t urc = socket->recv_error;
    476        
    477         log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len);
    478        
    479         int rc;
    480        
     436
     437        log_msg(LVL_DEBUG, "Got data in sock recv_buffer");
     438
     439        rsock = socket->recv_fsock;
     440        data_len = socket->recv_buffer_used;
     441        urc = socket->recv_error;
     442
     443        log_msg(LVL_DEBUG, "**** recv data_len=%zu", data_len);
     444
    481445        switch (urc) {
    482446        case UDP_EOK:
     
    493457                assert(false);
    494458        }
    495        
    496         log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
    497        
     459
     460        log_msg(LVL_DEBUG, "**** udp_uc_receive -> %d", rc);
    498461        if (rc != EOK) {
    499462                fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    502465                return;
    503466        }
    504        
    505         ipc_callid_t rcallid;
    506         size_t addr_size = 0;
    507        
     467
    508468        if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
    509                 /* Fill address */
    510                 udp_sock_t *rsock = &socket->recv_fsock;
    511                 struct sockaddr_in addr;
    512                 struct sockaddr_in6 addr6;
    513                 size_t addr_length;
    514                
    515                 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr,
    516                     &addr6);
    517                
    518                 switch (addr_af) {
    519                 case AF_INET:
    520                         addr.sin_port = host2uint16_t_be(rsock->port);
    521                        
    522                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
    523                         if (!async_data_read_receive(&rcallid, &addr_length)) {
    524                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    525                                 fibril_mutex_unlock(&socket->lock);
    526                                 async_answer_0(callid, EINVAL);
    527                                 return;
    528                         }
    529                        
    530                         if (addr_length > sizeof(addr))
    531                                 addr_length = sizeof(addr);
    532                        
    533                         addr_size = sizeof(addr);
    534                        
    535                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
    536                         rc = async_data_read_finalize(rcallid, &addr, addr_length);
    537                         if (rc != EOK) {
    538                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    539                                 fibril_mutex_unlock(&socket->lock);
    540                                 async_answer_0(callid, EINVAL);
    541                                 return;
    542                         }
    543                        
    544                         break;
    545                 case AF_INET6:
    546                         addr6.sin6_port = host2uint16_t_be(rsock->port);
    547                        
    548                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive");
    549                         if (!async_data_read_receive(&rcallid, &addr_length)) {
    550                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    551                                 fibril_mutex_unlock(&socket->lock);
    552                                 async_answer_0(callid, EINVAL);
    553                                 return;
    554                         }
    555                        
    556                         if (addr_length > sizeof(addr6))
    557                                 addr_length = sizeof(addr6);
    558                        
    559                         addr_size = sizeof(addr6);
    560                        
    561                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize");
    562                         rc = async_data_read_finalize(rcallid, &addr6, addr_length);
    563                         if (rc != EOK) {
    564                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    565                                 fibril_mutex_unlock(&socket->lock);
    566                                 async_answer_0(callid, EINVAL);
    567                                 return;
    568                         }
    569                        
    570                         break;
    571                 default:
     469                /* Fill addr */
     470                addr.sin_family = AF_INET;
     471                addr.sin_addr.s_addr = host2uint32_t_be(rsock.addr.ipv4);
     472                addr.sin_port = host2uint16_t_be(rsock.port);
     473
     474                log_msg(LVL_DEBUG, "addr read receive");
     475                if (!async_data_read_receive(&rcallid, &addr_length)) {
    572476                        fibril_mutex_unlock(&socket->recv_buffer_lock);
    573477                        fibril_mutex_unlock(&socket->lock);
     
    575479                        return;
    576480                }
    577         }
    578        
    579         log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
    580        
    581         size_t length;
     481
     482                if (addr_length > sizeof(addr))
     483                        addr_length = sizeof(addr);
     484
     485                log_msg(LVL_DEBUG, "addr read finalize");
     486                rc = async_data_read_finalize(rcallid, &addr, addr_length);
     487                if (rc != EOK) {
     488                        fibril_mutex_unlock(&socket->recv_buffer_lock);
     489                        fibril_mutex_unlock(&socket->lock);
     490                        async_answer_0(callid, EINVAL);
     491                        return;
     492                }
     493        }
     494
     495        log_msg(LVL_DEBUG, "data read receive");
    582496        if (!async_data_read_receive(&rcallid, &length)) {
    583497                fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    586500                return;
    587501        }
    588        
     502
    589503        if (length > data_len)
    590504                length = data_len;
    591        
    592         log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
    593        
     505
     506        log_msg(LVL_DEBUG, "data read finalize");
    594507        rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
    595        
    596         if ((length < data_len) && (rc == EOK))
     508
     509        if (length < data_len && rc == EOK)
    597510                rc = EOVERFLOW;
    598        
    599         log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length);
    600        
    601         ipc_call_t answer;
    602        
     511
     512        log_msg(LVL_DEBUG, "read_data_length <- %zu", length);
    603513        IPC_SET_ARG2(answer, 0);
    604514        SOCKET_SET_READ_DATA_LENGTH(answer, length);
    605         SOCKET_SET_ADDRESS_LENGTH(answer, addr_size);
     515        SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));
    606516        async_answer_3(callid, EOK, IPC_GET_ARG1(answer),
    607517            IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
    608        
     518
    609519        socket->recv_buffer_used = 0;
    610        
     520
    611521        fibril_condvar_broadcast(&socket->recv_buffer_cv);
    612522        fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    616526static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    617527{
    618         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
    619         int socket_id = SOCKET_GET_SOCKET_ID(call);
    620 
    621         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - find core");
    622         socket_core_t *sock_core =
    623             socket_cores_find(&client->sockets, socket_id);
     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);
    624537        if (sock_core == NULL) {
    625         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - core not found");
    626538                async_answer_0(callid, ENOTSOCK);
    627539                return;
    628540        }
    629541
    630         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - spec data");
    631         udp_sockdata_t *socket =
    632             (udp_sockdata_t *) sock_core->specific_data;
    633         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket");
     542        socket = (udp_sockdata_t *)sock_core->specific_data;
    634543        fibril_mutex_lock(&socket->lock);
    635544
    636         log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close() - lock socket buffer");
    637         fibril_mutex_lock(&socket->recv_buffer_lock);
    638         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - set socket->sock_core = NULL");
    639         socket->sock_core = NULL;
    640         fibril_mutex_unlock(&socket->recv_buffer_lock);
    641 
    642         udp_uc_reset(socket->assoc);
    643 
    644         int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
     545        rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
    645546            udp_free_sock_data);
    646547        if (rc != EOK) {
    647                 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - socket_destroy failed");
    648548                fibril_mutex_unlock(&socket->lock);
    649549                async_answer_0(callid, rc);
     
    651551        }
    652552
    653         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_close - broadcast recv_buffer_cv");
    654         fibril_condvar_broadcast(&socket->recv_buffer_cv);
    655 
    656553        fibril_mutex_unlock(&socket->lock);
    657554        async_answer_0(callid, EOK);
     
    660557static void udp_sock_getsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    661558{
    662         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_getsockopt()");
     559        log_msg(LVL_DEBUG, "udp_sock_getsockopt()");
    663560        async_answer_0(callid, ENOTSUP);
    664561}
     
    666563static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    667564{
    668         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt)");
    669         log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
    670        
    671         void *data = NULL;
    672         size_t data_len;
    673         int rc = async_data_write_accept(&data, false, 0, 0, 0, &data_len);
    674         if (rc != EOK) {
    675                 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed accepting data");
    676                 async_answer_0(callid, rc);
    677                 return;
    678         }
    679        
    680         sysarg_t opt_level = SOL_SOCKET;
    681         sysarg_t opt_name = SOCKET_GET_OPT_NAME(call);
    682        
    683         if (opt_level != SOL_SOCKET || opt_name != SO_IPLINK ||
    684             data_len != sizeof(service_id_t)) {
    685                 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed opt_level/name/len");
    686                 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed opt_level=%d, "
    687                     "opt_name=%d, data_len=%zu", (int)opt_level, (int)opt_name,
    688                     data_len);
    689                 async_answer_0(callid, EINVAL);
    690                 return;
    691         }
    692        
    693         log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
    694        
    695         socket_core_t *sock_core = socket_cores_find(&client->sockets,
    696             SOCKET_GET_SOCKET_ID(call));
    697         if (sock_core == NULL) {
    698                 log_msg(LOG_DEFAULT, LVL_DEBUG, " - failed getting sock_core");
    699                 async_answer_0(callid, ENOENT);
    700                 return;
    701         }
    702        
    703         udp_sockdata_t *socket =
    704             (udp_sockdata_t *) sock_core->specific_data;
    705        
    706         service_id_t iplink = *(service_id_t *)data;
    707         udp_uc_set_iplink(socket->assoc, iplink);
    708        
    709         log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
    710         async_answer_0(callid, EOK);
    711 }
    712 
     565        log_msg(LVL_DEBUG, "udp_sock_setsockopt()");
     566        async_answer_0(callid, ENOTSUP);
     567}
    713568
    714569static int udp_sock_recv_fibril(void *arg)
     
    719574        size_t rcvd;
    720575
    721         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()");
    722 
    723         fibril_mutex_lock(&sock->recv_buffer_lock);
     576        log_msg(LVL_DEBUG, "udp_sock_recv_fibril()");
    724577
    725578        while (true) {
    726                 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()");
    727                 while ((sock->recv_buffer_used != 0) && (sock->sock_core != NULL)) {
     579                log_msg(LVL_DEBUG, "[] wait for rcv buffer empty()");
     580                fibril_mutex_lock(&sock->recv_buffer_lock);
     581                while (sock->recv_buffer_used != 0) {
    728582                        fibril_condvar_wait(&sock->recv_buffer_cv,
    729583                            &sock->recv_buffer_lock);
    730584                }
    731585
    732                 fibril_mutex_unlock(&sock->recv_buffer_lock);
    733 
    734                 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
     586                log_msg(LVL_DEBUG, "[] call udp_uc_receive()");
    735587                urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
    736588                    UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
    737                 fibril_mutex_lock(&sock->recv_buffer_lock);
    738589                sock->recv_error = urc;
    739590
    740                 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] udp_uc_receive -> %d", urc);
    741 
    742                 if (sock->sock_core != NULL)
    743                         udp_sock_notify_data(sock->sock_core);
     591                udp_sock_notify_data(sock->sock_core);
    744592
    745593                if (urc != UDP_EOK) {
    746                         log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break");
    747594                        fibril_condvar_broadcast(&sock->recv_buffer_cv);
    748595                        fibril_mutex_unlock(&sock->recv_buffer_lock);
     
    750597                }
    751598
    752                 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
     599                log_msg(LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
    753600
    754601                sock->recv_buffer_used = rcvd;
     602                fibril_mutex_unlock(&sock->recv_buffer_lock);
    755603                fibril_condvar_broadcast(&sock->recv_buffer_cv);
    756604        }
    757605
    758         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() exited loop");
    759606        udp_uc_destroy(sock->assoc);
    760 
    761         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril() terminated");
    762607
    763608        return 0;
     
    773618        async_answer_0(iid, EOK);
    774619
    775         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: begin");
    776 
    777620        client.sess = async_callback_receive(EXCHANGE_SERIALIZE);
    778621        socket_cores_initialize(&client.sockets);
    779622
    780623        while (true) {
    781                 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: wait");
     624                log_msg(LVL_DEBUG, "udp_sock_connection: wait");
    782625                callid = async_get_call(&call);
    783626                if (!IPC_GET_IMETHOD(call))
    784627                        break;
    785628
    786                 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: METHOD=%d",
     629                log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d",
    787630                    (int)IPC_GET_IMETHOD(call));
    788631
     
    827670
    828671        /* Clean up */
    829         log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_connection: Clean up");
     672        log_msg(LVL_DEBUG, "udp_sock_connection: Clean up");
    830673        async_hangup(client.sess);
    831674        socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
Note: See TracChangeset for help on using the changeset viewer.