示例#1
0
// Copy a SrvRecord list into another one
void SrvRecord::copy(ObjList& dest, const ObjList& src)
{
    dest.clear();
    for (ObjList* o = src.skipNull(); o; o = o->skipNext()) {
	SrvRecord* rec = static_cast<SrvRecord*>(o->get());
	dest.append(new SrvRecord(rec->order(),rec->pref(),rec->address(),rec->port()));
    }
}
示例#2
0
// Parse a received buffer according to RFC 3435
// See Appendix A for the grammar
bool MGCPMessage::parse(MGCPEngine* engine, ObjList& dest,
	const unsigned char* buffer, unsigned int len, const char* sdpType)
{
    if (!buffer)
	return false;

#ifdef PARSER_DEBUG
    String t((const char*)buffer,len);
    Debug(engine,DebugAll,"Parse received buffer\r\n%s",t.c_str());
#endif

    int errorCode = 510;              // Protocol error
    unsigned int trans = 0;
    String error;
    unsigned int crt = 0;

    while (crt < len && !error) {
	unsigned int count = 0;
	const char* line = 0;

	// Skip empty lines before a message line and skip trailing blanks on the message line
	while (crt < len) {
	    line = getLine(buffer,len,crt,count);
	    if (!line) {
		error = "Invalid end-of-line";
		break;
	    }
	    // Exit loop if the line is not empty
	    if (count)
		break;
	}
	if (!count || error)
	    break;

	// *** Decode the message line
	MGCPMessage* msg = decodeMessage(line,count,trans,error,engine);
	if (!msg)
	    break;
	dest.append(msg);

#ifdef PARSER_DEBUG
	String m((const char*)line,count);
	Debug(engine,DebugAll,"Decoded message: %s",m.c_str());
#endif

	// *** Decode parameters
	if (decodeParams(buffer,len,crt,msg,error,engine))
	    continue;
	if (error) {
	    if (msg->isCommand())
		trans = msg->transactionId();
	    break;
	}
	if (crt >= len)
	    break;

	// *** Decode SDP
	// Decode SDPs until the end of buffer or
        //  a line containing a dot (message separator in a piggybacked block)
	// SDPs are separated by an empty line
	int empty = 0;
	while (empty < 2) {
	    // Skip until an empty line, a line containing a dot or end of buffer
	    unsigned int start = crt;
	    unsigned int sdpLen = 0;
	    while (true) {
		line = getLine(buffer,len,crt,count);
		if (!line) {
		    error = "Invalid end-of-line";
		    break;
		}
		if (!count || (count == 1 && (*line == '.' || !*line))) {
		    if (!count)
			empty++;
		    else
			empty = 3;
		    break;
		}
		empty = 0;
		sdpLen = crt - start;
	    }
	    if (error)
		break;
	    if (sdpLen)
		msg->sdp.append(new MimeSdpBody(sdpType,(const char*)buffer+start,sdpLen));
	}

	// Found 2 empty lines: skip until end of buffer or line containing '.' or non empty line
	if (empty == 2) {
	    unsigned int start = crt;
	    while (true) {
		line = getLine(buffer,len,crt,count);
		if (!line) {
		    error = "Invalid end-of-line";
		    break;
		}
		if (!count) {
		    if (crt == len)
			break;
		    continue;
		}
		// Fallback with current index if found non empty line which doesn't start with '.'
		if (*line && *line != '.')
		    crt = start;
		break;
	    }
	}
    }
    if (!error)
	return true;

    dest.clear();
    if (trans && trans <= 999999999)
	dest.append(new MGCPMessage(engine,0,errorCode,trans,0,0));
    Debug(engine,DebugNote,"Parser error: %s",error.c_str());
    return false;
}