By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

Implementation of a network stack in C

P: 4
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 | destinationAddress | control | payload +
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++

with the following conditions:
* 8 bit sourceAddress field,
* 8 bit destinationAddress 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 destinationAddress;
unsigned char control;
void *payload
};

or maybe like this:

struct FRAME {
unsigned char sourceAddress;
unsigned char destinationAddress;
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 sendDataDownToLowerLayer(struct DATA *a);
int sendDataUpToHigherLayer(struct DATA *a);


/* receive functions */
int receiveDataFromLowerLayer(struct DATA *a);
int receiveDataFromHigherLayer(struct 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
Jun 6 '07 #1
Share this Question
Share on Google+
12 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Jun 6 '07 #2

P: 4
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!
Jun 6 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. stack<Frame> theStack;
  2.  
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.
Jun 6 '07 #4

P: 4
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
Jun 6 '07 #5

AdrianH
Expert 100+
P: 1,251
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 reimplementation 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
Jun 6 '07 #6

P: 4
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
Jun 6 '07 #7

AdrianH
Expert 100+
P: 1,251
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
Jun 6 '07 #8

AdrianH
Expert 100+
P: 1,251
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
Jun 6 '07 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Jun 6 '07 #10

AdrianH
Expert 100+
P: 1,251
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
Jun 7 '07 #11

weaknessforcats
Expert Mod 5K+
P: 9,197
You are keeping yourself quite up to date.
That's a nice thing to say to an old geezer.
Jun 7 '07 #12

AdrianH
Expert 100+
P: 1,251
That's a nice thing to say to an old geezer.
heheh, I mean it in the best possible way. ;)


Adrian
Jun 7 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.