Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset f57aebc in mainline


Ignore:
Timestamp:
2010-12-25T22:40:18Z (11 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master
Children:
3750c06, 58fe0aa
Parents:
bfe6366
Message:

Address buffers cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/netecho/netecho.c

    rbfe6366 rf57aebc  
    3838 */
    3939
    40 #include <malloc.h>
     40#include <assert.h>
    4141#include <stdio.h>
    4242#include <stdlib.h>
     
    221221 * @return              EOK on success or negative error code.
    222222 */
    223 static int netecho_socket_echo_message(int listening_id)
     223static int netecho_socket_process_message(int listening_id)
    224224{
    225         socklen_t max_length = sizeof(struct sockaddr_in6);
    226         uint8_t address_data[max_length];
    227         struct sockaddr *address = (struct sockaddr *) address_data;
     225        uint8_t address_buf[sizeof(struct sockaddr_in6)];
     226
    228227        socklen_t addrlen;
    229228        int socket_id;
     
    231230        size_t length;
    232231        uint8_t *address_start;
    233         struct sockaddr_in *address_in = (struct sockaddr_in *) address;
    234         struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;
     232
    235233        char address_string[INET6_ADDRSTRLEN];
     234        struct sockaddr_in *address_in = (struct sockaddr_in *) address_buf;
     235        struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address_buf;
     236        struct sockaddr *address = (struct sockaddr *) address_buf;
     237
    236238        int rc;
    237239
    238         addrlen = (socklen_t) max_length;
    239240        if (type == SOCK_STREAM) {
    240                 /* Accept a socket if the stream socket is used */
    241                 socket_id = accept(listening_id, address, &addrlen);
     241                /* Accept a socket if a stream socket is used */
     242                addrlen = sizeof(address_buf);
     243                socket_id = accept(listening_id, (void *) address_buf, &addrlen);
    242244                if (socket_id <= 0) {
    243245                        socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
     
    246248                                printf("Socket %d accepted\n", socket_id);
    247249                }
     250
     251                assert((size_t) addrlen <= sizeof(address_buf));
    248252        } else {
    249253                socket_id = listening_id;
     
    254258
    255259                /* Receive a message to echo */
    256                 rcv_size = recvfrom(socket_id, data, size, 0, address, &addrlen);
     260                rcv_size = recvfrom(socket_id, data, size, 0, address,
     261                    &addrlen);
    257262                if (rcv_size < 0) {
    258263                        socket_print_error(stderr, rcv_size, "Socket receive: ", "\n");
     
    312317int main(int argc, char *argv[])
    313318{
    314         socklen_t max_length = sizeof(struct sockaddr_in6);
    315         uint8_t address_data[max_length];
    316         struct sockaddr *address = (struct sockaddr *) address_data;
    317         struct sockaddr_in *address_in = (struct sockaddr_in *) address;
    318         struct sockaddr_in6 *address_in6 = (struct sockaddr_in6 *) address;
     319        struct sockaddr *address;;
     320        struct sockaddr_in address_in;
     321        struct sockaddr_in6 address_in6;
    319322        socklen_t addrlen;
     323
    320324        int listening_id;
    321325        int index;
     
    351355
    352356        /* Prepare the address buffer */
    353         bzero(address_data, max_length);
    354357        switch (family) {
    355358        case PF_INET:
    356                 address_in->sin_family = AF_INET;
    357                 address_in->sin_port = htons(port);
    358                 addrlen = sizeof(struct sockaddr_in);
     359                address_in.sin_family = AF_INET;
     360                address_in.sin_port = htons(port);
     361                address = (struct sockaddr *) &address_in;
     362                addrlen = sizeof(address_in);
    359363                break;
    360364        case PF_INET6:
    361                 address_in6->sin6_family = AF_INET6;
    362                 address_in6->sin6_port = htons(port);
    363                 addrlen = sizeof(struct sockaddr_in6);
     365                address_in6.sin6_family = AF_INET6;
     366                address_in6.sin6_port = htons(port);
     367                address = (struct sockaddr *) &address_in6;
     368                addrlen = sizeof(address_in6);
    364369                break;
    365370        default:
     
    406411         */
    407412        while (count) {
    408                 rc = netecho_socket_echo_message(listening_id);
     413                rc = netecho_socket_process_message(listening_id);
    409414                if (rc != EOK)
    410415                        break;
     
    414419                        count--;
    415420                        if (verbose)
    416                                 printf("Waiting for next %d packet(s)\n", count);
     421                                printf("Waiting for next %d message(s)\n", count);
    417422                }
    418423        }
Note: See TracChangeset for help on using the changeset viewer.