⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udp-slp.cc

📁 the User Agent and Service Agent functionality of the Service Location Protocol, based on RFC 2608:
💻 CC
字号:
#include "slp/udp-slp.h"int hdr_slp::offset_;// SLP Header Classstatic class SLPHeaderClass : public PacketHeaderClass {public:        SLPHeaderClass() : PacketHeaderClass("PacketHeader/SLP",sizeof(hdr_slp)) {		bind_offset(&hdr_slp::offset_);        }} class_slphdr;// UdpSlpAgent OTcl linkage classstatic class UdpSLPAgentClass : public TclClass {public:	UdpSLPAgentClass() : TclClass("Agent/UDP/UDPslp") {}	TclObject* create(int, const char*const*) { 		return (new UdpSLPAgent());	}} class_udpslp_agent;// Constructor (with no arg)UdpSLPAgent::UdpSLPAgent() : UdpAgent(){	support_slp_ = 0;	type_ = PT_SLP;}UdpSLPAgent::UdpSLPAgent(packet_t type) : UdpAgent(type){	support_slp_ = 0;	type_ = PT_SLP;}// Add Support of SLP Application to UdpAgent::sendtovoid UdpSLPAgent::sendto(int nbytes, const char* flags, ns_addr_t dst){	Packet *p;	int n, remain;	if (size_) {		n = (nbytes/size_ + (nbytes%size_ ? 1 : 0));		remain = nbytes%size_;	}	else		printf("Error: UDPslp size = 0\n");	if (nbytes == -1) {		printf("Error:  sendmsg() for UDPslp should not be -1\n");		return;	}	double local_time =Scheduler::instance().clock();	while (n-- > 0) {		p = allocpkt();		if(n==0 && remain>0) 			HDR_CMN(p)->size() = remain;		else 		{			//hdr_cmn::access(p)->size() = size_;			printf("Error: SLP message (%d bytes) could not fit UDP datagram (%d bytes)\n",				nbytes,size_);			return;		}		hdr_rtp* rh = HDR_RTP(p);		rh->flags() = 0;		rh->seqno() = ++seqno_;		HDR_CMN(p)->timestamp() = (u_int32_t)(SAMPLERATE*local_time);		// to eliminate recv to use SLP fields for non SLP packets		hdr_slp* sh = HDR_SLP(p);		/*		sh->function_id = 0;		sh->length = 0;		sh->length_contd = 0;		sh->reserved = 0;		sh->next_ext_offset = 0;		sh->next_ext_offset_contd = 0;		sh->xid = -1;		sh->language_tag_length = 0;		sh->language_tag = 0;		*/		// slp udp packets are distinguished by setting the ip		// priority bit to 14.		if(support_slp_) {			hdr_ip* ih = HDR_IP(p);			ih->prio_ = 14;			if(flags) // SLP Seq Num is passed as flags			{                                if (((hdr_slp *)flags)->function_id == SLPTYPE_SRVRQST) {					hdr_slp_srvrqst* srvrqst = HDR_SLP_SRVRQST(p);					ih->dst()=dst;					memcpy(srvrqst, flags, sizeof(hdr_slp_srvrqst));				}				else if (((hdr_slp *)flags)->function_id == SLPTYPE_SRVRPLY) {					ih->dst()=dst;					hdr_slp_srvrply* srvrply = HDR_SLP_SRVRPLY(p);					memcpy(srvrply, flags, sizeof(hdr_slp_srvrply));				}				else if (((hdr_slp *)flags)->function_id == SLPTYPE_SAADVERT) {					ih->dst()=dst;					hdr_slp_saadvert* saadvert = HDR_SLP_SAADVERT(p);					memcpy(saadvert, flags, sizeof(hdr_slp_saadvert));				}				else { 					printf("Error: UdpSlpAgent::sendmsg ... slp message (%d) not supported\n",						 ((hdr_slp *)flags)->function_id);					return;				}#ifdef DEBUGUDP printf("n%d: ---->UDP agent sending %d bytes message to %x:%d\n",addr(), nbytes, ih->daddr(),ih->dport());#endif			 			}								}		// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)		if (flags && (0 ==strcmp(flags, "NEW_BURST")))			rh->flags() |= RTP_M;		target_->recv(p);	}	idle();}// Support SLP Applicationvoid UdpSLPAgent::recv(Packet* p, Handler*){	hdr_ip* ih = HDR_IP(p);	int bytes_to_deliver = HDR_CMN(p)->size();#ifdef DEBUGUDP  printf ("n%d: <----UDP agent receving message from %x:%d\n",addr(), ih->saddr(),ih->sport());#endif	// if it is a SLP packet (data or ack)	if(ih->prio_ == 14) { 		if(app_) {  // if SLP Application exists			hdr_slp* sh = HDR_SLP(p);			if (sh->function_id == SLPTYPE_SRVRQST) {				hdr_slp_srvrqst* srvrqst = HDR_SLP_SRVRQST(p);				hdr_slp_srvrqst srvrqst_buf;				memcpy(&srvrqst_buf, srvrqst, sizeof(hdr_slp_srvrqst));				app_->recv_slpmsg((ns_addr_t)ih->src(), srvrqst_buf.size(), (char*) &srvrqst_buf);			}			else if (sh->function_id == SLPTYPE_SRVRPLY) {  //printf ("n%d: <----UDP agent receving message from %x:%d to %d\n",addr(), ih->saddr(),ih->sport(), ih->daddr());				hdr_slp_srvrply* srvrply = HDR_SLP_SRVRPLY(p);								hdr_slp_srvrply srvrply_buf;				memcpy(&srvrply_buf, srvrply, sizeof(hdr_slp_srvrply));				app_->recv_slpmsg((ns_addr_t)ih->src(), srvrply_buf.size(), (char*) &srvrply_buf);			}			else if (sh->function_id == SLPTYPE_SAADVERT) {				hdr_slp_saadvert* saadvert = HDR_SLP_SAADVERT(p);								hdr_slp_saadvert saadvert_buf;				memcpy(&saadvert_buf, saadvert, sizeof(hdr_slp_saadvert));				app_->recv_slpmsg((ns_addr_t)ih->src(), saadvert_buf.size(), (char*) &saadvert_buf);			}			else { 				printf("Error: UdpSlpAgent::recv ... slp message not supported\n");				return;			}				}		Packet::free(p);	}	// if it is a normal data packet (not SLP data or ack packet)	else { 		if (app_) app_->recv(bytes_to_deliver);		Packet::free(p);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -