Ignore:
Timestamp:
2010-11-25T13:42:50Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8df8415
Parents:
a93d79a (diff), eb667613 (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 mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/net/socket_client.c

    ra93d79a r8fb1bf82  
    4343#include <stdlib.h>
    4444#include <errno.h>
    45 #include <err.h>
    4645
    4746#include <ipc/services.h>
     
    7978 */
    8079typedef struct socket socket_t;
    81 
    82 /** Type definition of the socket specific data pointer.
    83  * @see socket
    84  */
    85 typedef socket_t *socket_ref;
    8680
    8781/** Socket specific data.
     
    163157
    164158        /** Active sockets. */
    165         sockets_ref sockets;
     159        sockets_t *sockets;
    166160
    167161        /** Safety lock.
     
    184178/** Returns the active sockets.
    185179 *
    186  *  @returns            The active sockets.
    187  */
    188 static sockets_ref socket_get_sockets(void)
     180 *  @return             The active sockets.
     181 */
     182static sockets_t *socket_get_sockets(void)
    189183{
    190184        if (!socket_globals.sockets) {
    191185                socket_globals.sockets =
    192                     (sockets_ref) malloc(sizeof(sockets_t));
     186                    (sockets_t *) malloc(sizeof(sockets_t));
    193187                if (!socket_globals.sockets)
    194188                        return NULL;
     
    212206static void socket_connection(ipc_callid_t iid, ipc_call_t * icall)
    213207{
    214         ERROR_DECLARE;
    215 
    216208        ipc_callid_t callid;
    217209        ipc_call_t call;
    218         socket_ref socket;
     210        socket_t *socket;
     211        int rc;
    219212
    220213loop:
     
    231224                    SOCKET_GET_SOCKET_ID(call));
    232225                if (!socket) {
    233                         ERROR_CODE = ENOTSOCK;
     226                        rc = ENOTSOCK;
    234227                        fibril_rwlock_read_unlock(&socket_globals.lock);
    235228                        break;
     
    240233                        fibril_mutex_lock(&socket->receive_lock);
    241234                        // push the number of received packet fragments
    242                         if (!ERROR_OCCURRED(dyn_fifo_push(&socket->received,
     235                        rc = dyn_fifo_push(&socket->received,
    243236                            SOCKET_GET_DATA_FRAGMENTS(call),
    244                             SOCKET_MAX_RECEIVED_SIZE))) {
     237                            SOCKET_MAX_RECEIVED_SIZE);
     238                        if (rc == EOK) {
    245239                                // signal the received packet
    246240                                fibril_condvar_signal(&socket->receive_signal);
     
    252246                        // push the new socket identifier
    253247                        fibril_mutex_lock(&socket->accept_lock);
    254                         if (!ERROR_OCCURRED(dyn_fifo_push(&socket->accepted,
    255                             1, SOCKET_MAX_ACCEPTED_SIZE))) {
     248                        rc = dyn_fifo_push(&socket->accepted, 1,
     249                            SOCKET_MAX_ACCEPTED_SIZE);
     250                        if (rc == EOK) {
    256251                                // signal the accepted socket
    257252                                fibril_condvar_signal(&socket->accept_signal);
     
    261256
    262257                default:
    263                         ERROR_CODE = ENOTSUP;
     258                        rc = ENOTSUP;
    264259                }
    265260
     
    280275
    281276        default:
    282                 ERROR_CODE = ENOTSUP;
    283         }
    284 
    285         ipc_answer_0(callid, (ipcarg_t) ERROR_CODE);
     277                rc = ENOTSUP;
     278        }
     279
     280        ipc_answer_0(callid, (ipcarg_t) rc);
    286281        goto loop;
    287282}
     
    291286 * Connects to the TCP module if necessary.
    292287 *
    293  * @returns             The TCP module phone.
    294  * @returns             Other error codes as defined for the
     288 * @return              The TCP module phone.
     289 * @return              Other error codes as defined for the
    295290 *                      bind_service_timeout() function.
    296291 */
     
    310305 * Connects to the UDP module if necessary.
    311306 *
    312  * @returns             The UDP module phone.
    313  * @returns             Other error codes as defined for the
     307 * @return              The UDP module phone.
     308 * @return              Other error codes as defined for the
    314309 *                      bind_service_timeout() function.
    315310 */
     
    327322/** Tries to find a new free socket identifier.
    328323 *
    329  * @returns             The new socket identifier.
    330  * @returns             ELIMIT if there is no socket identifier available.
     324 * @return              The new socket identifier.
     325 * @return              ELIMIT if there is no socket identifier available.
    331326 */
    332327static int socket_generate_new_id(void)
    333328{
    334         sockets_ref sockets;
     329        sockets_t *sockets;
    335330        int socket_id = 0;
    336331        int count;
     
    372367 */
    373368static void
    374 socket_initialize(socket_ref socket, int socket_id, int phone,
     369socket_initialize(socket_t *socket, int socket_id, int phone,
    375370    services_t service)
    376371{
     
    392387 * @param[in] type      Socket type.
    393388 * @param[in] protocol  Socket protocol.
    394  * @returns             The socket identifier on success.
    395  * @returns             EPFNOTSUPPORT if the protocol family is not supported.
    396  * @returns             ESOCKNOTSUPPORT if the socket type is not supported.
    397  * @returns             EPROTONOSUPPORT if the protocol is not supported.
    398  * @returns             ENOMEM if there is not enough memory left.
    399  * @returns             ELIMIT if there was not a free socket identifier found
     389 * @return              The socket identifier on success.
     390 * @return              EPFNOTSUPPORT if the protocol family is not supported.
     391 * @return              ESOCKNOTSUPPORT if the socket type is not supported.
     392 * @return              EPROTONOSUPPORT if the protocol is not supported.
     393 * @return              ENOMEM if there is not enough memory left.
     394 * @return              ELIMIT if there was not a free socket identifier found
    400395 *                      this time.
    401  * @returns             Other error codes as defined for the NET_SOCKET message.
    402  * @returns             Other error codes as defined for the
     396 * @return              Other error codes as defined for the NET_SOCKET message.
     397 * @return              Other error codes as defined for the
    403398 *                      bind_service_timeout() function.
    404399 */
    405400int socket(int domain, int type, int protocol)
    406401{
    407         ERROR_DECLARE;
    408 
    409         socket_ref socket;
     402        socket_t *socket;
    410403        int phone;
    411404        int socket_id;
     
    413406        ipcarg_t fragment_size;
    414407        ipcarg_t header_size;
     408        int rc;
    415409
    416410        // find the appropriate service
     
    464458
    465459        // create a new socket structure
    466         socket = (socket_ref) malloc(sizeof(socket_t));
     460        socket = (socket_t *) malloc(sizeof(socket_t));
    467461        if (!socket)
    468462                return ENOMEM;
     
    479473        }
    480474
    481         if (ERROR_OCCURRED((int) async_req_3_3(phone, NET_SOCKET, socket_id, 0,
    482             service, NULL, &fragment_size, &header_size))) {
     475        rc = (int) async_req_3_3(phone, NET_SOCKET, socket_id, 0, service, NULL,
     476            &fragment_size, &header_size);
     477        if (rc != EOK) {
    483478                fibril_rwlock_write_unlock(&socket_globals.lock);
    484479                free(socket);
    485                 return ERROR_CODE;
     480                return rc;
    486481        }
    487482
     
    492487        socket_initialize(socket, socket_id, phone, service);
    493488        // store the new socket
    494         ERROR_CODE = sockets_add(socket_get_sockets(), socket_id, socket);
     489        rc = sockets_add(socket_get_sockets(), socket_id, socket);
    495490
    496491        fibril_rwlock_write_unlock(&socket_globals.lock);
    497         if (ERROR_CODE < 0) {
     492        if (rc < 0) {
    498493                dyn_fifo_destroy(&socket->received);
    499494                dyn_fifo_destroy(&socket->accepted);
     
    501496                async_msg_3(phone, NET_SOCKET_CLOSE, (ipcarg_t) socket_id, 0,
    502497                    service);
    503                 return ERROR_CODE;
     498                return rc;
    504499        }
    505500
     
    514509 * @param[in] data      The data to be sent.
    515510 * @param[in] datalength The data length.
    516  * @returns             EOK on success.
    517  * @returns             ENOTSOCK if the socket is not found.
    518  * @returns             EBADMEM if the data parameter is NULL.
    519  * @returns             NO_DATA if the datalength parameter is zero (0).
    520  * @returns             Other error codes as defined for the spcific message.
     511 * @return              EOK on success.
     512 * @return              ENOTSOCK if the socket is not found.
     513 * @return              EBADMEM if the data parameter is NULL.
     514 * @return              NO_DATA if the datalength parameter is zero (0).
     515 * @return              Other error codes as defined for the spcific message.
    521516 */
    522517static int
     
    524519    const void *data, size_t datalength)
    525520{
    526         socket_ref socket;
     521        socket_t *socket;
    527522        aid_t message_id;
    528523        ipcarg_t result;
     
    559554 * @param[in] my_addr   The port address.
    560555 * @param[in] addrlen   The address length.
    561  * @returns             EOK on success.
    562  * @returns             ENOTSOCK if the socket is not found.
    563  * @returns             EBADMEM if the my_addr parameter is NULL.
    564  * @returns             NO_DATA if the addlen parameter is zero.
    565  * @returns             Other error codes as defined for the NET_SOCKET_BIND
     556 * @return              EOK on success.
     557 * @return              ENOTSOCK if the socket is not found.
     558 * @return              EBADMEM if the my_addr parameter is NULL.
     559 * @return              NO_DATA if the addlen parameter is zero.
     560 * @return              Other error codes as defined for the NET_SOCKET_BIND
    566561 *                      message.
    567562 */
     
    580575 * @param[in] socket_id Socket identifier.
    581576 * @param[in] backlog   The maximum number of waiting sockets to be accepted.
    582  * @returns             EOK on success.
    583  * @returns             EINVAL if the backlog parameter is not positive (<=0).
    584  * @returns             ENOTSOCK if the socket is not found.
    585  * @returns             Other error codes as defined for the NET_SOCKET_LISTEN
     577 * @return              EOK on success.
     578 * @return              EINVAL if the backlog parameter is not positive (<=0).
     579 * @return              ENOTSOCK if the socket is not found.
     580 * @return              Other error codes as defined for the NET_SOCKET_LISTEN
    586581 *                      message.
    587582 */
    588583int listen(int socket_id, int backlog)
    589584{
    590         socket_ref socket;
     585        socket_t *socket;
    591586        int result;
    592587
     
    618613 * @param[out] cliaddr  The remote client address.
    619614 * @param[in] addrlen   The address length.
    620  * @returns             EOK on success.
    621  * @returns             EBADMEM if the cliaddr or addrlen parameter is NULL.
    622  * @returns             EINVAL if the backlog parameter is not positive (<=0).
    623  * @returns             ENOTSOCK if the socket is not found.
    624  * @returns             Other error codes as defined for the NET_SOCKET_ACCEPT
     615 * @return              EOK on success.
     616 * @return              EBADMEM if the cliaddr or addrlen parameter is NULL.
     617 * @return              EINVAL if the backlog parameter is not positive (<=0).
     618 * @return              ENOTSOCK if the socket is not found.
     619 * @return              Other error codes as defined for the NET_SOCKET_ACCEPT
    625620 *                      message.
    626621 */
    627622int accept(int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen)
    628623{
    629         socket_ref socket;
    630         socket_ref new_socket;
     624        socket_t *socket;
     625        socket_t *new_socket;
    631626        aid_t message_id;
    632627        ipcarg_t ipc_result;
     
    661656
    662657        // create a new scoket
    663         new_socket = (socket_ref) malloc(sizeof(socket_t));
     658        new_socket = (socket_t *) malloc(sizeof(socket_t));
    664659        if (!new_socket) {
    665660                fibril_mutex_unlock(&socket->accept_lock);
     
    721716 * @param[in] serv_addr The remote server address.
    722717 * @param[in] addrlen   The address length.
    723  * @returns             EOK on success.
    724  * @returns             EBADMEM if the serv_addr parameter is NULL.
    725  * @returns             NO_DATA if the addlen parameter is zero.
    726  * @returns             ENOTSOCK if the socket is not found.
    727  * @returns             Other error codes as defined for the NET_SOCKET_CONNECT
     718 * @return              EOK on success.
     719 * @return              EBADMEM if the serv_addr parameter is NULL.
     720 * @return              NO_DATA if the addlen parameter is zero.
     721 * @return              ENOTSOCK if the socket is not found.
     722 * @return              Other error codes as defined for the NET_SOCKET_CONNECT
    728723 *                      message.
    729724 */
     
    745740 * @param[in] socket    The socket to be destroyed.
    746741 */
    747 static void socket_destroy(socket_ref socket)
     742static void socket_destroy(socket_t *socket)
    748743{
    749744        int accepted_id;
     
    761756 *
    762757 * @param[in] socket_id Socket identifier.
    763  * @returns             EOK on success.
    764  * @returns             ENOTSOCK if the socket is not found.
    765  * @returns             EINPROGRESS if there is another blocking function in
     758 * @return              EOK on success.
     759 * @return              ENOTSOCK if the socket is not found.
     760 * @return              EINPROGRESS if there is another blocking function in
    766761 *                      progress.
    767  * @returns             Other error codes as defined for the NET_SOCKET_CLOSE
     762 * @return              Other error codes as defined for the NET_SOCKET_CLOSE
    768763 *                      message.
    769764 */
    770765int closesocket(int socket_id)
    771766{
    772         ERROR_DECLARE;
    773 
    774         socket_ref socket;
     767        socket_t *socket;
     768        int rc;
    775769
    776770        fibril_rwlock_write_lock(&socket_globals.lock);
     
    787781
    788782        // request close
    789         ERROR_PROPAGATE((int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE,
    790             (ipcarg_t) socket->socket_id, 0, socket->service));
     783        rc = (int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE,
     784            (ipcarg_t) socket->socket_id, 0, socket->service);
     785        if (rc != EOK) {
     786                fibril_rwlock_write_unlock(&socket_globals.lock);
     787                return rc;
     788        }
    791789        // free the socket structure
    792790        socket_destroy(socket);
     
    808806 *                      sockets.
    809807 * @param[in] addrlen   The address length. Used only if toaddr is not NULL.
    810  * @returns             EOK on success.
    811  * @returns             ENOTSOCK if the socket is not found.
    812  * @returns             EBADMEM if the data or toaddr parameter is NULL.
    813  * @returns             NO_DATA if the datalength or the addrlen parameter is
     808 * @return              EOK on success.
     809 * @return              ENOTSOCK if the socket is not found.
     810 * @return              EBADMEM if the data or toaddr parameter is NULL.
     811 * @return              NO_DATA if the datalength or the addrlen parameter is
    814812 *                      zero (0).
    815  * @returns             Other error codes as defined for the NET_SOCKET_SENDTO
     813 * @return              Other error codes as defined for the NET_SOCKET_SENDTO
    816814 *                      message.
    817815 */
     
    821819    socklen_t addrlen)
    822820{
    823         socket_ref socket;
     821        socket_t *socket;
    824822        aid_t message_id;
    825823        ipcarg_t result;
     
    910908 * @param[in] datalength The data length.
    911909 * @param[in] flags     Various send flags.
    912  * @returns             EOK on success.
    913  * @returns             ENOTSOCK if the socket is not found.
    914  * @returns             EBADMEM if the data parameter is NULL.
    915  * @returns             NO_DATA if the datalength parameter is zero.
    916  * @returns             Other error codes as defined for the NET_SOCKET_SEND
     910 * @return              EOK on success.
     911 * @return              ENOTSOCK if the socket is not found.
     912 * @return              EBADMEM if the data parameter is NULL.
     913 * @return              NO_DATA if the datalength parameter is zero.
     914 * @return              Other error codes as defined for the NET_SOCKET_SEND
    917915 *                      message.
    918916 */
     
    934932 * @param[in] toaddr    The destination address.
    935933 * @param[in] addrlen   The address length.
    936  * @returns             EOK on success.
    937  * @returns             ENOTSOCK if the socket is not found.
    938  * @returns             EBADMEM if the data or toaddr parameter is NULL.
    939  * @returns             NO_DATA if the datalength or the addrlen parameter is
     934 * @return              EOK on success.
     935 * @return              ENOTSOCK if the socket is not found.
     936 * @return              EBADMEM if the data or toaddr parameter is NULL.
     937 * @return              NO_DATA if the datalength or the addrlen parameter is
    940938 *                      zero.
    941  * @returns             Other error codes as defined for the NET_SOCKET_SENDTO
     939 * @return              Other error codes as defined for the NET_SOCKET_SENDTO
    942940 *                      message.
    943941 */
     
    968966 *                      read. The actual address length is set. Used only if
    969967 *                      fromaddr is not NULL.
    970  * @returns             EOK on success.
    971  * @returns             ENOTSOCK if the socket is not found.
    972  * @returns             EBADMEM if the data parameter is NULL.
    973  * @returns             NO_DATA if the datalength or addrlen parameter is zero.
    974  * @returns             Other error codes as defined for the spcific message.
     968 * @return              EOK on success.
     969 * @return              ENOTSOCK if the socket is not found.
     970 * @return              EBADMEM if the data parameter is NULL.
     971 * @return              NO_DATA if the datalength or addrlen parameter is zero.
     972 * @return              Other error codes as defined for the spcific message.
    975973 */
    976974static int
     
    978976    int flags, struct sockaddr *fromaddr, socklen_t *addrlen)
    979977{
    980         socket_ref socket;
     978        socket_t *socket;
    981979        aid_t message_id;
    982980        ipcarg_t ipc_result;
     
    10971095 * @param[in] datalength The data length.
    10981096 * @param[in] flags     Various receive flags.
    1099  * @returns             EOK on success.
    1100  * @returns             ENOTSOCK if the socket is not found.
    1101  * @returns             EBADMEM if the data parameter is NULL.
    1102  * @returns             NO_DATA if the datalength parameter is zero.
    1103  * @returns             Other error codes as defined for the NET_SOCKET_RECV
     1097 * @return              EOK on success.
     1098 * @return              ENOTSOCK if the socket is not found.
     1099 * @return              EBADMEM if the data parameter is NULL.
     1100 * @return              NO_DATA if the datalength parameter is zero.
     1101 * @return              Other error codes as defined for the NET_SOCKET_RECV
    11041102 *                      message.
    11051103 */
     
    11201118 * @param[in,out] addrlen The address length. The maximum address length is
    11211119 *                      read. The actual address length is set.
    1122  * @returns             EOK on success.
    1123  * @returns             ENOTSOCK if the socket is not found.
    1124  * @returns             EBADMEM if the data or fromaddr parameter is NULL.
    1125  * @returns             NO_DATA if the datalength or addrlen parameter is zero.
    1126  * @returns             Other error codes as defined for the NET_SOCKET_RECVFROM
     1120 * @return              EOK on success.
     1121 * @return              ENOTSOCK if the socket is not found.
     1122 * @return              EBADMEM if the data or fromaddr parameter is NULL.
     1123 * @return              NO_DATA if the datalength or addrlen parameter is zero.
     1124 * @return              Other error codes as defined for the NET_SOCKET_RECVFROM
    11271125 *                      message.
    11281126 */
     
    11501148 * @param[in,out] optlen The value buffer length. The maximum length is read.
    11511149 *                      The actual length is set.
    1152  * @returns             EOK on success.
    1153  * @returns             ENOTSOCK if the socket is not found.
    1154  * @returns             EBADMEM if the value or optlen parameter is NULL.
    1155  * @returns             NO_DATA if the optlen parameter is zero.
    1156  * @returns             Other error codes as defined for the
     1150 * @return              EOK on success.
     1151 * @return              ENOTSOCK if the socket is not found.
     1152 * @return              EBADMEM if the value or optlen parameter is NULL.
     1153 * @return              NO_DATA if the optlen parameter is zero.
     1154 * @return              Other error codes as defined for the
    11571155 *                      NET_SOCKET_GETSOCKOPT message.
    11581156 */
     
    11601158getsockopt(int socket_id, int level, int optname, void *value, size_t *optlen)
    11611159{
    1162         socket_ref socket;
     1160        socket_t *socket;
    11631161        aid_t message_id;
    11641162        ipcarg_t result;
     
    12031201 * @param[in] value     The value to be set.
    12041202 * @param[in] optlen    The value length.
    1205  * @returns             EOK on success.
    1206  * @returns             ENOTSOCK if the socket is not found.
    1207  * @returns             EBADMEM if the value parameter is NULL.
    1208  * @returns             NO_DATA if the optlen parameter is zero.
    1209  * @returns             Other error codes as defined for the
     1203 * @return              EOK on success.
     1204 * @return              ENOTSOCK if the socket is not found.
     1205 * @return              EBADMEM if the value parameter is NULL.
     1206 * @return              NO_DATA if the optlen parameter is zero.
     1207 * @return              Other error codes as defined for the
    12101208 *                      NET_SOCKET_SETSOCKOPT message.
    12111209 */
Note: See TracChangeset for help on using the changeset viewer.