Implementation of a network stack in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • svkers
    New Member
    • Jun 2007
    • 4

    Implementation of a network stack in C

    Hi all,

    I have a question concerning the implementation of an interface between two layers of a network stack (see extract of the subsequent stack). Furthermore I am thankful for some hints how to implement frames in C.

    Stack and Frames:
    ...
    ++++++++++++++
    + Layer n+1 +
    ++++++++++++++
    + Layer n +
    ++++++++++++++
    ...

    E.g. Layer n+1 defines the following sample (simple;) frame (FRAME)

    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++
    + sourceAddress | destinationAddr ess | control | payload +
    +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++

    with the following conditions:
    * 8 bit sourceAddress field,
    * 8 bit destinationAddr ess field,
    * 8 bit control field and
    * a payload field with variable length


    First of all: the implementation should be in ANSI C.
    How is it a good way to implement the frame? Should I implement it using C-structures, e.g.:

    struct FRAME {
    unsigned char sourceAddress;
    unsigned char destinationAddr ess;
    unsigned char control;
    void *payload
    };

    or maybe like this:

    struct FRAME {
    unsigned char sourceAddress;
    unsigned char destinationAddr ess;
    unsigned char control;
    };

    Is it a good idea to take the payload (pointer) also to the struct? Are there better ways to implement it? Any idea is welcome!
    The next problem, which also concerns the realization of the implementation of a frame, is as follows: How to make a good interface between two layers, e.g. layer n+1 and layer n. How should a frame be passed to next lower/higher layer? I thought about the following (but I am not sure if it is a good way...):

    struct DATA {
    unsigned int bufferSize; /* size of the buffer, i.e. header and payload (which could be variable) */
    void *buffer; /* contains the complete frame */
    };


    /* send functions */
    int sendDataDownToL owerLayer(struc t DATA *a);
    int sendDataUpToHig herLayer(struct DATA *a);


    /* receive functions */
    int receiveDataFrom LowerLayer(stru ct DATA *a);
    int receiveDataFrom HigherLayer(str uct DATA *a);

    What do you think about this approach? I never implemented a network stack so I don“t have any experience... If you have any idea, hints, ... just let me know!!!

    Thank you in advance,
    svkers
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Exactly what is a network stack? This is the first time I have heard this since I started programming in 1960.

    If it's just a regular stack, the use a LIFO linked list.

    Comment

    • svkers
      New Member
      • Jun 2007
      • 4

      #3
      I thought about a layered architecture like the ISO/OSI or TCP/IP reference model... Maybe my understanding is too theoretical... so could you please precise your answer (using a LIFO queue). What about frames? How to implement frames with variable length?

      Thanks!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A LIFO link list is one where the last element added it the first one removed.

        This is all done for you with the C++ Standard Library Stack Container:

        [code=cpp]
        stack<Frame> theStack;
        [/code]

        Read your docs on how to operate the stack.

        If the stack won't do it and oyu have to moive arounf a lot, then use The C++ Standard Library Vector Container.

        Your variable length frame may take some work. Are the frames variable in length or do they also have variable type of data members?

        Your sample Frame just has 3 chars.

        Comment

        • svkers
          New Member
          • Jun 2007
          • 4

          #5
          Both the frame fields and the frame length could be variable...
          The STL is intended to be used from C++ but I want to do a pure ANSI C implementation. .. ;) but doesn“t matter since it is no difficult task to implement my own LIFO, FIFO, ... queue in C...

          Thanks, svkers

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by svkers
            Hi all,

            I have a question concerning the implementation of an interface between two layers of a network stack (see extract of the subsequent stack). Furthermore I am thankful for some hints how to implement frames in C.

            ...
            What do you think about this approach? I never implemented a network stack so I don“t have any experience... If you have any idea, hints, ... just let me know!!!

            Thank you in advance,
            svkers
            I'm sure this has been implemented before, why are you reimplementing it?

            As for the approach, I'll leave that till I know what is your reasoning for the reimplementatio n as the best approach is to not reimplement it but to reuse something already existing (so long as it was made well and easy to interface to).

            Adrian

            Comment

            • svkers
              New Member
              • Jun 2007
              • 4

              #7
              I am also sure that something similiar has already been implemented years before... but I did not found any sources where I could take a look... Maybe you know some sources...

              I agree with you since it does not make sense to reimplement something what is already implemented. Just for your information: I am interested in wireless networking technologies and I have the plan to implement my own simple network stack for LRWPANs (low rate wireless personal area networks) which is flexible enough for using both on a pc with dongle and a small wireless node (e.g. wireless radio and microcontroller ). So that“s my intention...

              svkers

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                Originally posted by svkers
                I am also sure that something similiar has already been implemented years before... but I did not found any sources where I could take a look... Maybe you know some sources...

                svkers
                Have you tried looking at Linux?


                Adrian

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by weaknessforcats
                  Exactly what is a network stack? This is the first time I have heard this since I started programming in 1960.

                  If it's just a regular stack, the use a LIFO linked list.
                  Man wfc, you are really dating yourself. ;)


                  Adrian

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by AdrianH
                    Man wfc, you are really dating yourself. ;)
                    It gets worse. My first job (1960) was to assemble a digital autopilot for the Saturn V missile. Leading edge engineers designed it with leading edge components. It had 595 transistors and 1120 diodes. I got to build it direct from the DeMorgan equations. Weighed 8 pounds and occupied a cubic foot. It used a delay line for memory.

                    Do you recall a delay line? You know, where electricity runs along a wire at about a nanosecond a foot? By having a big coil of wire, precisily cut, you can put bits in there and go off and to soemthing else and then come back at just the right moment and snatch the bits as they come off the other end of the wire. That was how the multiplier worked with partial products.

                    I was 20.

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by weaknessforcats
                      It gets worse. My first job (1960) was to assemble a digital autopilot for the Saturn V missile. Leading edge engineers designed it with leading edge components. It had 595 transistors and 1120 diodes. I got to build it direct from the DeMorgan equations. Weighed 8 pounds and occupied a cubic foot. It used a delay line for memory.

                      Do you recall a delay line? You know, where electricity runs along a wire at about a nanosecond a foot? By having a big coil of wire, precisily cut, you can put bits in there and go off and to soemthing else and then come back at just the right moment and snatch the bits as they come off the other end of the wire. That was how the multiplier worked with partial products.

                      I was 20.
                      *old geezer voice* Yeah, I 'member when 'puters spanned buildin's and that was just to add 2+2, and on a good day ya'd get 4. ;) lol, jk. I'm not that old... yet. :D

                      You are keeping yourself quite up to date. I know this one guy who was my collogue at work. Didn't want to hear about C++ and didn't think that layering was a good thing. "Can't see what is going on", he said. His point was only partially valid on a RT system, but the system had 1 sec delays for some things (fairly soft for a RT system), so it wasn't all that valid.

                      Keep up the good work.


                      Adrian

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Originally posted by AdrianH
                        You are keeping yourself quite up to date.
                        That's a nice thing to say to an old geezer.

                        Comment

                        • AdrianH
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1251

                          #13
                          Originally posted by weaknessforcats
                          That's a nice thing to say to an old geezer.
                          heheh, I mean it in the best possible way. ;)


                          Adrian

                          Comment

                          Working...