Ignore:
File:
1 edited

Legend:

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

    r02a09ed r05bfce7  
    179179        }
    180180
    181         inet_addr_any(&sock->laddr);
     181        sock->laddr.ipv4 = TCP_IPV4_ANY;
    182182        sock->lconn = NULL;
    183183        sock->backlog = 0;
     
    314314        log_msg(LOG_DEFAULT, LVL_DEBUG, " - open connections");
    315315       
    316         inet_addr_any(&lsocket.addr);
     316        lsocket.addr.ipv4 = TCP_IPV4_ANY;
    317317        lsocket.port = sock_core->port;
    318        
    319         inet_addr_any(&fsocket.addr);
     318        fsocket.addr.ipv4 = TCP_IPV4_ANY;
    320319        fsocket.port = TCP_PORT_ANY;
    321320       
     
    354353}
    355354
    356 static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid,
    357     ipc_call_t call)
    358 {
     355static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
     356{
     357        int rc;
     358        struct sockaddr_in *addr;
     359        int socket_id;
     360        size_t addr_len;
     361        socket_core_t *sock_core;
     362        tcp_sockdata_t *socket;
     363        tcp_error_t trc;
     364        tcp_sock_t lsocket;
     365        tcp_sock_t fsocket;
     366
    359367        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()");
    360        
    361         struct sockaddr_in6 *addr6 = NULL;
    362         size_t addr_len;
    363         int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len);
    364         if (rc != EOK) {
     368
     369        rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len);
     370        if (rc != EOK || addr_len != sizeof(struct sockaddr_in)) {
    365371                async_answer_0(callid, rc);
    366372                return;
    367373        }
    368        
    369         if ((addr_len != sizeof(struct sockaddr_in)) &&
    370             (addr_len != sizeof(struct sockaddr_in6))) {
    371                 async_answer_0(callid, EINVAL);
    372                 goto out;
    373         }
    374        
    375         struct sockaddr_in *addr = (struct sockaddr_in *) addr6;
    376        
    377         int socket_id = SOCKET_GET_SOCKET_ID(call);
    378         socket_core_t *sock_core = socket_cores_find(&client->sockets,
    379             socket_id);
     374
     375        socket_id = SOCKET_GET_SOCKET_ID(call);
     376
     377        sock_core = socket_cores_find(&client->sockets, socket_id);
    380378        if (sock_core == NULL) {
    381379                async_answer_0(callid, ENOTSOCK);
    382                 goto out;
    383         }
    384        
    385         tcp_sockdata_t *socket =
    386             (tcp_sockdata_t *) sock_core->specific_data;
    387        
     380                return;
     381        }
     382
     383        socket = (tcp_sockdata_t *)sock_core->specific_data;
    388384        if (sock_core->port <= 0) {
    389385                rc = socket_bind_free_port(&gsock, sock_core,
     
    392388                if (rc != EOK) {
    393389                        async_answer_0(callid, rc);
    394                         goto out;
     390                        return;
    395391                }
    396392               
    397393                last_used_port = sock_core->port;
    398394        }
    399        
     395
    400396        fibril_mutex_lock(&socket->lock);
    401        
    402         if (inet_addr_is_any(&socket->laddr)) {
     397
     398        if (socket->laddr.ipv4 == TCP_IPV4_ANY) {
    403399                /* Determine local IP address */
    404                 inet_addr_t loc_addr;
    405                 inet_addr_t rem_addr;
    406                
    407                 switch (addr->sin_family) {
    408                 case AF_INET:
    409                         inet_sockaddr_in_addr(addr, &rem_addr);
    410                         break;
    411                 case AF_INET6:
    412                         inet_sockaddr_in6_addr(addr6, &rem_addr);
    413                         break;
    414                 default:
    415                         fibril_mutex_unlock(&socket->lock);
    416                         async_answer_0(callid, EINVAL);
    417                         goto out;
    418                 }
    419                
     400                inet_addr_t loc_addr, rem_addr;
     401
     402                rem_addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
    420403                rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
    421404                if (rc != EOK) {
     
    424407                        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect: Failed to "
    425408                            "determine local address.");
    426                         goto out;
    427                 }
    428                
    429                 socket->laddr = loc_addr;
    430         }
    431        
    432         tcp_sock_t lsocket;
    433         tcp_sock_t fsocket;
    434        
    435         lsocket.addr = socket->laddr;
     409                        return;
     410                }
     411
     412                socket->laddr.ipv4 = loc_addr.ipv4;
     413                log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x", socket->laddr.ipv4);
     414        }
     415
     416        lsocket.addr.ipv4 = socket->laddr.ipv4;
    436417        lsocket.port = sock_core->port;
    437        
    438         switch (addr->sin_family) {
    439         case AF_INET:
    440                 inet_sockaddr_in_addr(addr, &fsocket.addr);
    441                 break;
    442         case AF_INET6:
    443                 inet_sockaddr_in6_addr(addr6, &fsocket.addr);
    444                 break;
    445         default:
    446                 fibril_mutex_unlock(&socket->lock);
    447                 async_answer_0(callid, EINVAL);
    448                 goto out;
    449         }
    450        
     418        fsocket.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
    451419        fsocket.port = uint16_t_be2host(addr->sin_port);
    452        
    453         tcp_error_t trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0,
    454             &socket->conn);
    455        
     420
     421        trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, &socket->conn);
     422
    456423        if (socket->conn != NULL)
    457                 socket->conn->name = (char *) "C";
    458        
     424                socket->conn->name = (char *)"C";
     425
    459426        fibril_mutex_unlock(&socket->lock);
    460        
     427
    461428        switch (trc) {
    462429        case TCP_EOK:
     
    469436                assert(false);
    470437        }
    471        
     438
    472439        if (rc == EOK)
    473440                fibril_add_ready(socket->recv_fibril);
    474        
     441
    475442        async_answer_0(callid, rc);
    476        
    477 out:
    478         if (addr6 != NULL)
    479                 free(addr6);
    480443}
    481444
     
    544507        /* Replenish listening connection */
    545508
    546         inet_addr_any(&lsocket.addr);
     509        lsocket.addr.ipv4 = TCP_IPV4_ANY;
    547510        lsocket.port = sock_core->port;
    548        
    549         inet_addr_any(&fsocket.addr);
     511        fsocket.addr.ipv4 = TCP_IPV4_ANY;
    550512        fsocket.port = TCP_PORT_ANY;
    551513
     
    695657static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)
    696658{
     659        int socket_id;
     660        int flags;
     661        size_t addr_length, length;
     662        socket_core_t *sock_core;
     663        tcp_sockdata_t *socket;
     664        ipc_call_t answer;
     665        ipc_callid_t rcallid;
     666        size_t data_len;
     667        struct sockaddr_in addr;
     668        tcp_sock_t *rsock;
     669        int rc;
     670
    697671        log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: tcp_sock_recv[from]()", client);
    698        
    699         int socket_id = SOCKET_GET_SOCKET_ID(call);
    700        
    701         socket_core_t *sock_core =
    702             socket_cores_find(&client->sockets, socket_id);
     672
     673        socket_id = SOCKET_GET_SOCKET_ID(call);
     674        flags = SOCKET_GET_FLAGS(call);
     675
     676        sock_core = socket_cores_find(&client->sockets, socket_id);
    703677        if (sock_core == NULL) {
    704678                async_answer_0(callid, ENOTSOCK);
    705679                return;
    706680        }
    707        
    708         tcp_sockdata_t *socket =
    709             (tcp_sockdata_t *) sock_core->specific_data;
    710        
     681
     682        socket = (tcp_sockdata_t *)sock_core->specific_data;
    711683        fibril_mutex_lock(&socket->lock);
    712        
     684
    713685        if (socket->conn == NULL) {
    714686                fibril_mutex_unlock(&socket->lock);
     
    716688                return;
    717689        }
    718        
     690
     691        (void)flags;
     692
    719693        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom(): lock recv_buffer_lock");
    720        
    721694        fibril_mutex_lock(&socket->recv_buffer_lock);
    722         while ((socket->recv_buffer_used == 0) &&
    723             (socket->recv_error == TCP_EOK)) {
     695        while (socket->recv_buffer_used == 0 && socket->recv_error == TCP_EOK) {
    724696                log_msg(LOG_DEFAULT, LVL_DEBUG, "wait for recv_buffer_cv + recv_buffer_used != 0");
    725697                fibril_condvar_wait(&socket->recv_buffer_cv,
    726698                    &socket->recv_buffer_lock);
    727699        }
    728        
     700
    729701        log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");
    730        
    731         size_t data_len = socket->recv_buffer_used;
    732         tcp_error_t trc = socket->recv_error;
    733         int rc;
    734        
    735         switch (trc) {
     702
     703        data_len = socket->recv_buffer_used;
     704        rc = socket->recv_error;
     705
     706        switch (socket->recv_error) {
    736707        case TCP_EOK:
    737708                rc = EOK;
     
    747718                assert(false);
    748719        }
    749        
     720
    750721        log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv result -> %d", rc);
    751        
    752722        if (rc != EOK) {
    753723                fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    756726                return;
    757727        }
    758        
    759         ipc_callid_t rcallid;
    760        
     728
    761729        if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) {
    762                 /* Fill address */
    763                 tcp_sock_t *rsock = &socket->conn->ident.foreign;
    764                 struct sockaddr_in addr;
    765                 struct sockaddr_in6 addr6;
    766                 size_t addr_length;
    767                
    768                 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr,
    769                     &addr6);
    770                
    771                 switch (addr_af) {
    772                 case AF_INET:
    773                         addr.sin_port = host2uint16_t_be(rsock->port);
    774                        
    775                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
    776                         if (!async_data_read_receive(&rcallid, &addr_length)) {
    777                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    778                                 fibril_mutex_unlock(&socket->lock);
    779                                 async_answer_0(callid, EINVAL);
    780                                 return;
    781                         }
    782                        
    783                         if (addr_length > sizeof(addr))
    784                                 addr_length = sizeof(addr);
    785                        
    786                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
    787                         rc = async_data_read_finalize(rcallid, &addr, addr_length);
    788                         if (rc != EOK) {
    789                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    790                                 fibril_mutex_unlock(&socket->lock);
    791                                 async_answer_0(callid, EINVAL);
    792                                 return;
    793                         }
    794                        
    795                         break;
    796                 case AF_INET6:
    797                         addr6.sin6_port = host2uint16_t_be(rsock->port);
    798                        
    799                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive");
    800                         if (!async_data_read_receive(&rcallid, &addr_length)) {
    801                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    802                                 fibril_mutex_unlock(&socket->lock);
    803                                 async_answer_0(callid, EINVAL);
    804                                 return;
    805                         }
    806                        
    807                         if (addr_length > sizeof(addr6))
    808                                 addr_length = sizeof(addr6);
    809                        
    810                         log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize");
    811                         rc = async_data_read_finalize(rcallid, &addr6, addr_length);
    812                         if (rc != EOK) {
    813                                 fibril_mutex_unlock(&socket->recv_buffer_lock);
    814                                 fibril_mutex_unlock(&socket->lock);
    815                                 async_answer_0(callid, EINVAL);
    816                                 return;
    817                         }
    818                        
    819                         break;
    820                 default:
     730                /* Fill addr */
     731                rsock = &socket->conn->ident.foreign;
     732                addr.sin_family = AF_INET;
     733                addr.sin_addr.s_addr = host2uint32_t_be(rsock->addr.ipv4);
     734                addr.sin_port = host2uint16_t_be(rsock->port);
     735
     736                log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive");
     737                if (!async_data_read_receive(&rcallid, &addr_length)) {
    821738                        fibril_mutex_unlock(&socket->recv_buffer_lock);
    822739                        fibril_mutex_unlock(&socket->lock);
     
    824741                        return;
    825742                }
    826         }
    827        
     743
     744                if (addr_length > sizeof(addr))
     745                        addr_length = sizeof(addr);
     746
     747                log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");
     748                rc = async_data_read_finalize(rcallid, &addr, addr_length);
     749                if (rc != EOK) {
     750                        fibril_mutex_unlock(&socket->recv_buffer_lock);
     751                        fibril_mutex_unlock(&socket->lock);
     752                        async_answer_0(callid, EINVAL);
     753                        return;
     754                }
     755        }
     756
    828757        log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive");
    829        
    830         size_t length;
    831758        if (!async_data_read_receive(&rcallid, &length)) {
    832759                fibril_mutex_unlock(&socket->recv_buffer_lock);
     
    835762                return;
    836763        }
    837        
     764
    838765        if (length > data_len)
    839766                length = data_len;
    840        
     767
    841768        log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize");
    842        
    843769        rc = async_data_read_finalize(rcallid, socket->recv_buffer, length);
    844        
     770
    845771        socket->recv_buffer_used -= length;
    846        
    847772        log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom: %zu left in buffer",
    848773            socket->recv_buffer_used);
    849        
    850774        if (socket->recv_buffer_used > 0) {
    851775                memmove(socket->recv_buffer, socket->recv_buffer + length,
     
    853777                tcp_sock_notify_data(socket->sock_core);
    854778        }
    855        
     779
    856780        fibril_condvar_broadcast(&socket->recv_buffer_cv);
    857        
    858         if ((length < data_len) && (rc == EOK))
     781
     782        if (length < data_len && rc == EOK)
    859783                rc = EOVERFLOW;
    860        
    861         ipc_call_t answer;
    862        
     784
    863785        SOCKET_SET_READ_DATA_LENGTH(answer, length);
    864786        async_answer_1(callid, EOK, IPC_GET_ARG1(answer));
    865        
     787
    866788        fibril_mutex_unlock(&socket->recv_buffer_lock);
    867789        fibril_mutex_unlock(&socket->lock);
Note: See TracChangeset for help on using the changeset viewer.