December 4, 2022

BSD TCP/IP for Kyu - Progress report 12-4-2022

Today we crossed a finish line of sorts.

This all started around October 21, so I have been working on this for around 6 weeks (with some interruptions and distractions).

Today I got a read functionality working (as tcp_recv()), which along with connect() finishes off my first tier list of what I wanted to accomplish. It allowed me to code up this ECHO server to run within Kyu. And it works!!

#define ECHO_PORT       113

static void
run_echo ( struct socket *so )
{
        char msg[128];
        int n;

        for ( ;; ) {
            n = tcp_recv ( so, msg, 128 );
            if ( n == 0 ) {
                thr_delay ( 1 );
                continue;
            }
            tcp_send ( so, msg, n );
            if ( msg[0] == 'q' )
                break;
        }

        strcpy ( msg, "All Done\n" );
        tcp_send ( so, msg, strlen(msg) );
}

void
echo_thread ( long xxx )
{
        struct socket *so;
        struct socket *cso;
        int fip;

        so = tcp_bind ( ECHO_PORT );

        for ( ;; ) {
            cso = tcp_accept ( so );
            fip = tcp_getpeer_ip ( cso );
            printf ( "echo server got a connection from: %s\n", ip2str32_h(fip) );

            /* Really shoud run this in new thread */
            run_echo ( cso );

            (void) soclose ( cso );
        }
}

void
bsd_test_echo ( long xxx )
{
        (void) safe_thr_new ( "echo-server", echo_thread, (void *) 0, 15, 0 );
}


Have any comments? Questions? Drop me a line!

Kyu / [email protected]