Пример #1
0
bool Casan::is_ctl_msg (Msg &m)
{
    int i = 0 ;

    m.reset_next_option () ;
    for (option *o = m.next_option () ; o != NULL ; o = m.next_option ())
    {
	if (o->optcode () == option::MO_Uri_Path)
	{
	    if (i >= NTAB (casan_namespace))
		return false ;
	    if (casan_namespace [i].len != o->optlen ())
		return false ;
	    if (memcmp (casan_namespace [i].path, o->optval ((int *) 0), o->optlen ()))
		return false ;
	    i++ ;
	}
    }
    m.reset_next_option () ;
    if (i != NTAB (casan_namespace))
	return false ;

    return true ;
}
Пример #2
0
bool Casan::is_hello (Msg &m, long int &hlid)
{
    bool found = false ;

    // a hello msg is NON POST
    if (m.get_type () == COAP_TYPE_NON && m.get_code () == COAP_CODE_POST)
    {
	m.reset_next_option () ;
	for (option *o = m.next_option () ; o != NULL ; o = m.next_option ())
	{
	    if (o->optcode () == option::MO_Uri_Query)
	    {
		// we benefit from the added nul byte at the end of val
		if (sscanf ((const char *) o->optval ((int *) 0), CASAN_HELLO, &hlid) == 1)
		    found = true ;
	    }
	}
    }

    return found ;
}
Пример #3
0
bool Casan::is_assoc (Msg &m, time_t &sttl, int &mtu)
{
    bool found_ttl = false ;
    bool found_mtu = false ;

    if (m.get_type () == COAP_TYPE_CON && m.get_code () == COAP_CODE_POST)
    {
	m.reset_next_option () ;
	for (option *o = m.next_option () ; o != NULL ; o = m.next_option ())
	{
	    if (o->optcode () == option::MO_Uri_Query)
	    {
		long int n ;		// sscanf "%ld" waits for a long int

		// we benefit from the added nul byte at the end of val
		if (sscanf ((const char *) o->optval ((int *) 0), CASAN_ASSOC_TTL, &n) == 1)
		{
		    DBG1 (BLUE ("TTL recv: ")) ;
		    DBG1 (n) ;
		    DBGLN0 () ;
		    sttl = ((time_t) n) * 1000 ;
		    found_ttl = true ;
		    // continue, just in case there are other query strings
		}
		else if (sscanf ((const char *) o->optval ((int *) 0), CASAN_ASSOC_MTU, &n) == 1)
		{
		    DBG1 (BLUE ("MTU recv: ")) ;
		    DBG1 (n) ;
		    DBGLN0 () ;
		    mtu = n ;
		    found_mtu = true ;
		    // continue, just in case there are other query strings
		}
		else break ;
	    }
	}
    }

    return found_ttl && found_mtu ;
}
Пример #4
0
void Casan::process_request (Msg &in, Msg &out) 
{
    option *o ;
    bool rfound = false ;		// resource found

    in.reset_next_option () ;
    for (o = in.next_option () ; o != NULL ; o = in.next_option ())
    {
	if (o->optcode () == option::MO_Uri_Path)
	{
	    // request for all resources
	    if (o->optlen () == (int) (sizeof CASAN_RESOURCES_ALL - 1)
		&& memcmp (o->optval ((int *) 0), CASAN_RESOURCES_ALL, 
				    sizeof CASAN_RESOURCES_ALL - 1) == 0)
	    {
		rfound = true ;
		out.set_type (COAP_TYPE_ACK) ;
		out.set_id (in.get_id ()) ;
		out.set_token (in.get_token ()) ;
		out.set_code (COAP_CODE_OK) ;
		(void) get_well_known (out) ;
	    }
	    else
	    {
		Resource *res ;

		// we benefit from the added '\0' at the end of an option
		res = get_resource ((char *) o->optval ((int *) 0)) ;
		if (res != NULL)
		{
		    option *obs ;
		    uint32_t obsval ;

		    rfound = true ;

		    obs = in.search_option (option::MO_Observe) ;
		    if (obs != NULL)
			obsval = obs->optval () ;

		    if (obs != NULL && obsval == 0)
			res->observed (true, &in) ;
		    else
			res->observed (false, NULL) ;

		    out.set_type (COAP_TYPE_ACK) ;
		    out.set_id (in.get_id ()) ;
		    out.set_token (in.get_token ()) ;

		    if (obs != NULL && obsval == 0)
		    {
			option robs (option::MO_Observe, res->next_serial ()) ;
			out.push_option (robs) ;
		    }

		    request_resource (&in, &out, res) ;
		}
	    }
	    break ;
	}
    }

    if (! rfound)
    {
	out.set_type (COAP_TYPE_ACK) ;
	out.set_id (in.get_id ()) ;
	out.set_token (in.get_token ()) ;
	out.set_code (COAP_CODE_NOT_FOUND) ;
    }
}