1/**
2 * \file net_openport_test.c
3 * \brief A test program to check the working of port management.
4 * This programs open specified number of ports.
5 */
6
7/*
8 * Copyright (c) 2007-2011, ETH Zurich.
9 * All rights reserved.
10 *
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
14 */
15
16#include <stdio.h>
17#include <barrelfish/barrelfish.h>
18#include <barrelfish/waitset.h>
19#include <lwip/init.h>
20#include <lwip/tcp.h>
21
22
23static void event_polling_loop(void)
24{
25    err_t err;
26    printf("Starting event polling loop\n");
27    struct waitset *ws = get_default_waitset();
28    while (1) {
29        err = event_dispatch(ws);
30        if (err_is_fail(err)) {
31            DEBUG_ERR(err, "in event_dispatch");
32            break;
33        }
34    }
35}
36
37int main(int argc, char *argv[])
38{
39    if(argc != 3) {
40        printf("USAGE: %s <start_port_no> <no. of ports>\n", argv[0]);
41        printf("EXAMPLE: %s 50 100\n", argv[0]);
42        exit(1);
43    }
44
45    uint16_t start_port_range = atoi(argv[1]);
46    int ports_to_bind = atoi(argv[2]);
47    uint16_t port;
48    int port_count = 0;
49    err_t err;
50
51    // Connect to the network driver driver
52    assert(lwip_init_auto() == true);
53
54    printf("openport_test: setup done\n");
55
56
57    // Stopping the application here
58    event_polling_loop();
59    return 0;
60
61    printf("openport_test: binding %d tcp ports starting from %u\n",
62            ports_to_bind, start_port_range);
63
64    port_count = 0;
65    port = start_port_range;
66    for (port_count = 0; port_count < ports_to_bind; ++port_count){
67
68        printf("openport_test: opening port %u\n", port);
69        struct tcp_pcb *pcb = tcp_new();
70        if (pcb == NULL) {
71            printf("openport_test: tcp_new failed in opening port %u\n", port);
72            return ERR_MEM;
73        }
74
75        do {
76            err = tcp_bind(pcb, IP_ADDR_ANY, port);
77            if(err != ERR_OK) {
78                if (err == ERR_USE){
79                    printf("port %u is already in use\n", port);
80                }
81                else{
82                    printf("some prob in opening port, exiting!!\n");
83                    return(err);
84                }
85            }
86            printf("openport_test: port %u opened\n", port);
87            ++port;
88        } while(err != ERR_OK);
89    } /* end for: each port */
90
91    printf("openport_test: total %d ports opened\n", port_count);
92
93    event_polling_loop();
94    return 0;
95
96} /* end function: main */
97
98