在C中实现网络堆栈 [英] Implementation of a network stack in C

查看:61
本文介绍了在C中实现网络堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个关于网络堆栈两层之间接口实现的问题(参见后续堆栈的摘录)。此外,我感谢一些提示如何在C中实现框架。


堆栈和框架:

...

+ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ br $>
+ Layer n +

+++++++++++++++++++++++ />
例如图层n + 1定义以下示例(简单;)框架(框架)


+++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ destinationAddress |控制|有效载荷+

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++ br />
* 8位destinationAddress字段,

* 8位控制字段和

*可变长度的有效负载字段



首先:实现应该是ANSI C.

如何实现框架的好方法?我应该使用C结构来实现它,例如:


struct FRAME {

unsigned char sourceAddress;

unsigned char destinationAddress;

unsigned char控件;

void * payload

};


或者像这样:


struct FRAME {

unsigned char sourceAddress;

unsigned char destinationAddress;

unsigned char control ;

};


将有效负载(指针)也带到结构中是个好主意吗?有没有更好的方法来实现它?任何想法都是受欢迎的!

下一个问题,也涉及实现框架的实现,如下:如何在两个层之间建立一个良好的接口,例如:层n + 1和层n。框架应该如何传递到下一个较低/较高层?我想到了以下内容(但我不确定这是不是一个好方法......):


struct DATA {

unsigned int bufferSize; / *缓冲区的大小,即标题和有效负载(可以是变量)* /

void * buffer; / *包含完整的框架* /

};


/ *发送函数* /

int sendDataDownToLowerLayer(struct DATA * a);

int sendDataUpToHigherLayer( struct DATA * a);


/ *接收函数* /

int receiveDataFromLowerLayer(struct DATA * a);

int receiveDataFromHigherLayer(struct DATA * a);


您如何看待这种方法?我从来没有实现网络堆栈所以我没有任何经验...如果你有任何想法,提示,...只是让我知道!!!


谢谢你提前,

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.

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

推荐答案

究竟什么是网络堆栈?这是我自1960年开始编程以来第一次听到这个。


如果它只是一个常规堆栈,则使用LIFO链表。
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.


我想过像ISO / OSI或TCP / IP参考模型这样的分层架构...也许我的理解太过理论化......所以你能否准确答案(使用LIFO队列)。帧怎么样?如何实现可变长度的帧?


谢谢!
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!


LIFO链接列表是最后一个元素添加它的第一个删除的列表。


这些都是用C ++标准库堆栈容器完成的:

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:

展开 | 选择 | 换行 | 行号


这篇关于在C中实现网络堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆