Streams

TCP库有可能是基于STREAMS实现的。如:Transport Provider Interface (TPI)是一个面向传输层的接口,如果系统是基于STREAMS的,socket一般就使用这个接口来实现。

STREAMS provide a full-duplex connection between a process and a driver ,driver 可以为 hardware device 或 software driver

The stream head consists of the kernel routines that are invoked when the application makes a system call for a STREAMS descriptor (e.g., read, putmsg, ioctl, and the like).

A process can dynamically add and remove intermediate processing modules between the stream head and the driver. A module performs some type of filtering on the messages going up and down a stream

A special type of pseudo-device driver is a multiplexor, which accepts data from multiple sources

  1. When a socket is created, the module sockmod is pushed onto the stream by the sockets library. It is the combination of the sockets library and the sockmod STREAMS module that provides the sockets API to the process
  2. Various service interfaces define the format of the networking messages exchanged up and down a stream. We describe the three most common. TPI  defines the interface provided by a transport-layer provider (e.g., TCP and UDP) to the modules above it. The Network Provider Interface (NPI)  defines the interface provided by a network-layer provider (e.g., IP). DLPI is the Data Link Provider Interface .

Each component in a stream the stream head, all processing modules, and the driver contains at least one pair of queues: a write queue and a read queue

STREAMS messages can be categorized as high priority, priority band, or normal. There are 256 different priority bands, between 0 and 255, with normal messages in band 0. The priority of a STREAMS message is used for both queueing and flow control. By convention, high-priority messages are unaffected by flow control

Although the STREAMS system supports 256 different priority bands, networking protocols often use band 1 for expedited data and band 0 for normal data.

TCP's out-of-band data is not considered true expedited data by TPI. Indeed, TCP uses band 0 for both normal data and its out-of-band data. The use of band 1 for expedited data is for protocols in which the expedited data (not just the urgent pointer, as in TCP) is sent ahead of normal data

The term "normal message" should always refer to a message with a band of 0

we are interested in only three different types of messages: M_DATA, M_PROTO, and M_PCPROTO (PC stands for "priority control" and implies a high-priority message)

#include <stropts.h>
int getmsg(int fd, struct strbuf *ctlptr, struct strbuf *dataptr, int *flagsp) ;
int putmsg(int fd, const struct strbuf *ctlptr, const struct strbuf *dataptr, int flags) ;
Both return: non-negative value if OK , -1 on error

getmsg returns 0 only if the entire message was returned to the caller. If the control buffer is too small, the return value is MORECTL. if the data buffer is too small, MOREDATA can be returned. If both are too small, the logical OR of these two flags is returned

Both the control and data portions of the message are described by the following strbuf structure:

struct strbuf 
{
    int maxlen; /* maximum size of buf */
    int len; /* actual amount of data in buf */
    char *buf; /* data */
};

To indicate the absence of control information, we can either specify ctlptr as a null pointer or set ctlptr->len to -1. The same technique is used to indicate no data.

If there is no control information, an M_DATA message is generated by putmsg, otherwise, either an M_PROTO or an M_PCPROTO message is generated, depending on the
flags. The flags argument to putmsg is 0 for a normal message or RS_HIPRI for a high-priority message

#include <stropts.h>
int getpmsg(int fd, struct strbuf *ctlptr, struct strbuf *dataptr, int *bandp, int *flagsp) ;
int putpmsg(int fd, const struct strbuf *ctlptr, const struct strbuf *dataptr, int band, int flags) ;
Both return: non-negative value if OK, -1 on error

The band argument to putpmsg must be between 0 and 255, inclusive If the flags argument is MSG_BAND, then a message is generated in the specified priority band .Setting
flags to MSG_BAND and specifying a band of 0 is equivalent to calling putmsg. If flags is MSG_HIPRI, band must be 0, and a high-priority message is generated. (Note that this flag is named differently from the RS_HIPRI flag for putmsg.)

The two integers pointed to by bandp and flagsp are value-result arguments for getpmsg. The integer pointed to by flagsp for getpmsg can be MSG_HIPRI (to read a high-priority message), MSG_BAND (to read a message whose priority band is at least equal to the integer pointed to by bandp), or MSG_ANY (to read any message). On return, the integer pointed to by bandp contains the band of the message that was read and the integer pointed to by flagsp contains MSG_HIPRI (if a high-priority message was read) or MSG_BAND (if some other message was read).

Transport Provider Interface (TPI)

TPI is the service interface into the transport layer.  sockets use this interface in a STREAMS environment. it is a combination of the sockets library and sockmod. sockmod exchange TPI messages with TCP and UDP

TPI is a message-based interface. It defines the messages that are exchanged up and down a stream between the application (e.g., the sockets library) and the transport layer: the format of these messages and what operation each message performs.

In many instances, the application sends a request to the provider (such as "bind this local address") and the provider sends back a response ("OK" or "error"). Some events occur asynchronously at the provider (the arrival of a connection request for a server), causing a message or a signal to be sent up the stream.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值