lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] [lwip] tcp_write in the connect handling function


From: Rajaraman Krishnan
Subject: [lwip-users] [lwip] tcp_write in the connect handling function
Date: Wed, 08 Jan 2003 22:27:32 -0000

This is a multi-part message in MIME format.

------=_NextPart_000_00F2_01C1B3C1.D938E890
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

When I try to do a tcp_write from the function that handle the connection
(i.e. which was passed to tcp_connect), I get the following message and it
aborts.

 Assertion "tcp_enqueue: valid queue length" failed at line 178 in
/home/rajaram/utils/lwip-0.5.0/src/core/tcp_output.c

I noticed that this was because the function passed to tcp_connect is called
even before the initial window packet is sent. The server side doesnt even
receive the connect. When I remove the tcp_write from this function It
connects fine.

So should I wait for some other call back to do the tcp_write? I thought I
would register a tcp_sent call back. But that doesnt get called in this
case. So what am I to do if the first thing I want to do after setting up a
connection is write to it?

Rajaram


------=_NextPart_000_00F2_01C1B3C1.D938E890
Content-Type: application/octet-stream;
        name="lwip_client.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
        filename="lwip_client.c"

#include "lwip/debug.h"

#include <unistd.h>


#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/sys.h"

#include "lwip/stats.h"


#include "lwip/tcpip.h"

#include "netif/tapif.h"
#include "netif/tunif.h"

#include "netif/unixif.h"
#include "netif/dropif.h"

#include "netif/loopif.h"

#include "netif/tcpdump.h"

#include "netif/sioslipif.h"


#include "lwip/ip_addr.h"

#include <stdio.h>

#define MAX_STRING 100

typedef struct
{
    int sent;
    char msg[MAX_STRING];
    int len;
} ReceiveDetails;

struct ip_addr host, gw;
int port;

// Read the message from socket
err_t ReadSocket(void * arg, struct tcp_pcb *dsock, struct pbuf *p, =
err_t err)
{
    struct pbuf* np;
    ReceiveDetails *recv =3D (ReceiveDetails *)arg;

    printf("Data to be read in connection.\n");

    if (err !=3D 0)
    {
        printf("Error in HandleNewConn.\n");
        exit(1);
    }

    if (p =3D=3D NULL)
    {
        printf("Server closed connection.\n");

        /* Close the Dsock. */
        tcp_close(dsock);

        exit(1);
    }

    np =3D p;
    while (np !=3D NULL)
    {
        printf("Processing packet at %p.\n", np);
        printf("Processing packet of length: %d of total %d at %d.\n",
               np->len, np->tot_len, recv->len);
        memcpy(recv->msg+recv->len, np->payload, np->len);
        recv->len +=3D np->len;
        np =3D np->next;
    }

    printf("Received all bufs. Total len =3D %d.\n", recv->len);
    tcp_recved(dsock, p->tot_len);

    /* Check if a full string has been received. */
    if (recv->msg[recv->len-1] =3D=3D '\0')
    {
        printf("Message Received: %s. \nSending Reply: Hi2U2.\n", recv->msg);
        tcp_close(dsock);
        exit(0);
    }

    return 0;
}


err_t HandleConnect(void *arg, struct tcp_pcb *dsock, err_t err)
{
    ReceiveDetails *recv;

    recv =3D (ReceiveDetails *) malloc(sizeof(ReceiveDetails));

    if (err !=3D 0)
    {
        printf("Error in HandleNewConn.\n");
        exit(1);
    }

    printf("Connected.\n");

    printf("Sending message to Server: Hello.\n");
    tcp_write(dsock, "Hello", 6, 1);

    recv->len =3D 0;
    tcp_arg(dsock, recv);
    tcp_recv(dsock, ReadSocket);

    return 0;
}


/*-----------------------------------------------------------------------=
----
|  Setup socket
|------------------------------------------------------------------------=
--*/
void TcpCommunicate(int port)=20
{
    struct tcp_pcb *sock;

    /* create what looks like an ordinary TCP socket */
    if ((sock =3D tcp_new()) =3D=3D NULL)
    {
        printf("Problems with tcp_new.\n");
        exit(1);
    }

    printf("Connecting to %d.%d.%d.%d on port %d.\n",=20
           (u8_t)(ntohl(gw.addr) >> 24) & 0xff,
           (u8_t)(ntohl(gw.addr) >> 16) & 0xff,
           (u8_t)(ntohl(gw.addr) >> 8) & 0xff,
           (u8_t)ntohl(gw.addr) & 0xff, port);

    if (tcp_connect(sock, &gw, port, HandleConnect))
    {
        printf("Problems with tcp_connect.\n");
        exit(1);
    }
}

void lwip_if_init()
{
    struct ip_addr lbaddr, netmask, lbgw;

   =20
    IP4_ADDR(&netmask, 255,255,255,0);
 =20
    netif_set_default(netif_add(&host, &netmask, &gw, tapif_init,
                                tcpip_input));

    IP4_ADDR(&lbgw, 127,0,0,1);
    IP4_ADDR(&lbaddr, 127,0,0,1);
    IP4_ADDR(&netmask, 255,0,0,0);
 =20
    netif_add(&lbaddr, &netmask, &lbgw, loopif_init, tcpip_input);
}

static void
tcpip_init_done(void *arg)
{
  sys_sem_t *sem;
  sem =3D arg;
  sys_sem_signal(*sem);
}   =20

void lwip_init()
{
    sys_init();
    mem_init();
    memp_init();
    pbuf_init();
}

void main_thread(void *arg)
{
    char tmp[MAX_STRING];
    sys_sem_t sem;

    lwip_if_init();

    sem =3D sys_sem_new(0);
    tcpip_init(tcpip_init_done, &sem);
    sys_sem_wait(sem);
    sys_sem_free(sem);
    printf("TCP/IP initialized.\n"
           "Hit <RETURN> to proceed.");

    fflush(stdin);
    fgets(tmp, MAX_STRING, stdin);

    TcpCommunicate(port);

    /* Block for ever. */
    sem =3D sys_sem_new(0);
    sys_sem_wait(sem);
}


int parse_addr(char *caddr, struct ip_addr *ipaddr1, struct ip_addr =
*ipaddr2)
{
    int a, b, c, d;

    if (sscanf(caddr, "%d.%d.%d.%d", &a, &b, &c, &d) !=3D 4)
        return 0;

    if ((a < 0) || (a > 255) || (b < 0) || (b > 255) || (c < 0) || (c > =
255) ||
        (d < 0) || (d > 254))
        return 0;

    IP4_ADDR(ipaddr1, a, b, c, d);
    IP4_ADDR(ipaddr2, a, b, c, (d+1));
    return 1;
}


int main(int argc, char **argv)
{
    if (argc !=3D 3)
    {
        printf("Usage: %s <Sim IP Address> <portNo>\n",  argv[0]);
        exit(1);
    }

    if (!parse_addr(argv[1], &host, &gw))
    {
        printf("Usage: %s <Sim IP Address> <portNo>\n",  argv[0]);
        printf("\t<Sim IP Address> should be of the form A.B.C.D.\n");

        exit(1);
    }

    lwip_init();

    printf("System initialized.\n");

    if (sscanf(argv[2], "%d", &port) !=3D 1)
    {
        printf("Usage: %s <Sim IP Address> <portNo>\n",  argv[0]);
        printf("\t<Sim IP Address> should be of the form A.B.C.D.\n");

        exit(1);
    }

    sys_thread_new((void *)(main_thread), NULL);
    pause();
    return 0;
}

------=_NextPart_000_00F2_01C1B3C1.D938E890--

[This message was sent through the lwip discussion list.]




reply via email to

[Prev in Thread] Current Thread [Next in Thread]