Exemple #1
0
/**
 * Groups a list of avps into a data buffer
 * @param avps
 */
str AAAGroupAVPS(AAA_AVP_LIST avps)
{
	AAA_AVP *avp;
	unsigned char *p;
	str buf={0,0};

	/* count and add the avps */
	for(avp=avps.head;avp;avp=avp->next) {
		buf.len += AVP_HDR_SIZE(avp->flags)+ to_32x_len( avp->data.len );
	}

	if (!buf.len) return buf;
	/* allocate some memory */
	buf.s = (char*)shm_malloc( buf.len );
	if (!buf.s) {
		LM_ERR("hss3g_group_avps: no more free memory!\n");
		buf.len=0;
		return buf;
	}
	memset(buf.s, 0, buf.len);
	/* fill in the buffer */
	p = (unsigned char*) buf.s;
	for(avp=avps.head;avp;avp=avp->next) {
		/* AVP HEADER */
		/* avp code */
		set_4bytes(p,avp->code);
		p +=4;
		/* flags */
		(*p++) = (unsigned char)avp->flags;
		/* avp length */
		set_3bytes(p, (AVP_HDR_SIZE(avp->flags)+avp->data.len) );
		p += 3;
		/* vendor id */
		if ((avp->flags&0x80)!=0) {
			set_4bytes(p,avp->vendorId);
			p +=4;
		}
		/* data */
		memcpy( p, avp->data.s, avp->data.len);
		p += to_32x_len( avp->data.len );
	}
	if ((char*)p-buf.s!=buf.len) {
		LM_ERR("BUG:hss3g_group_avps: mismatch between len and buf!\n");
		shm_free( buf.s );
		buf.s = 0;
		buf.len = 0;
		return buf;
	}
	return buf;
}
int AAAAVPBuildBuffer(AAA_AVP *avp, unsigned char *dest)  {
  unsigned char* p = dest;

  if (!avp || !dest) {
    ERROR("trying to build msg buffer from/to NULL avp");
    return 0;
  }
  /* AVP HEADER */
  /* avp code */
  set_4bytes(p,avp->code);
  p +=4;
  /* flags */
  (*p++) = (unsigned char)avp->flags;
  /* avp length */
  set_3bytes(p, (AVP_HDR_SIZE(avp->flags)+avp->data.len) );
  p += 3;
  /* vendor id */
  if ((avp->flags&0x80)!=0) {
    set_4bytes(p,avp->vendorId);
    p +=4;
  }
  /* data */
  memcpy( p, avp->data.s, avp->data.len);
  p += to_32x_len( avp->data.len );  

  return p-dest;
}
/* from a AAAMessage structure, a buffer to be send is build
 */
AAAReturnCode AAABuildMsgBuffer( AAAMessage *msg )
{
  unsigned char *p;
  AAA_AVP       *avp;
  AAA_AVP       *mem; // group member

  /* first let's compute the length of the buffer */
  msg->buf.len = AAA_MSG_HDR_SIZE; /* AAA message header size */
  /* count and add the avps */
  for(avp=msg->avpList.head;avp;avp=avp->next) {
    msg->buf.len += AVP_HDR_SIZE(avp->flags)+ to_32x_len( avp->data.len );
  }

  //	DBG("xxxx len=%d\n",msg->buf.len);
  /* allocate some memory */
  msg->buf.s = (char*)ad_malloc( msg->buf.len );
  if (!msg->buf.s) {
    ERROR("ERROR:AAABuildMsgBuffer: no more free memory!\n");
    goto error;
  }
  memset(msg->buf.s, 0, msg->buf.len);

  /* fill in the buffer */
  p = (unsigned char*)msg->buf.s;
  /* DIAMETER HEADER */
  /* message length */
  ((unsigned int*)p)[0] =htonl(msg->buf.len);
  /* Diameter Version */
  *p = 1;
  p += VER_SIZE + MESSAGE_LENGTH_SIZE;
  /* command code */
  ((unsigned int*)p)[0] = htonl(msg->commandCode);
  /* flags */
  *p = (unsigned char)msg->flags;
  p += FLAGS_SIZE + COMMAND_CODE_SIZE;
  /* application-ID */
  ((unsigned int*)p)[0] = htonl(msg->applicationId);
  p += APPLICATION_ID_SIZE;
  /* hop by hop id */
  ((unsigned int*)p)[0] = msg->hopbyhopId;
  p += HOP_BY_HOP_IDENTIFIER_SIZE;
  /* end to end id */
  ((unsigned int*)p)[0] = msg->endtoendId;
  p += END_TO_END_IDENTIFIER_SIZE;

  /* AVPS */
  for(avp=msg->avpList.head;avp;avp=avp->next) {
    /* AVP HEADER */
    /* avp code */
    set_4bytes(p,avp->code);
    p +=4;
    /* flags */
    (*p++) = (unsigned char)avp->flags;
    /* avp length */
    set_3bytes(p, (AVP_HDR_SIZE(avp->flags)+avp->data.len) );
    p += 3;
    /* vendor id */
    if ((avp->flags&0x80)!=0) {
      set_4bytes(p,avp->vendorId);
      p +=4;
    }
    /* data */
    if (!avp->groupedHead) {
      memcpy( p, avp->data.s, avp->data.len);
      p += to_32x_len( avp->data.len );
    } else {  
      // group members
      for(mem=avp->groupedHead;mem;mem=mem->next)
	p+=AAAAVPBuildBuffer(mem, p);
    }
  }

  if ((char*)p-msg->buf.s!=msg->buf.len) {
    ERROR("BUG: build_buf_from_msg: mismatch between len and buf!\n");
    ad_free( msg->buf.s );
    msg->buf.s = 0;
    msg->buf.len = 0;
    goto error;
  }
  //	DBG("Message: %.*s\n", msg->buf.len, msg->buf.s);
  return AAA_ERR_SUCCESS;
 error:
  return -1;
}
/* This function convert message to message structure */
AAAMessage* AAATranslateMessage( unsigned char* source, unsigned int sourceLen,
				 int attach_buf)
{
  unsigned char *ptr;
  AAAMessage    *msg;
  unsigned char version;
  unsigned int  msg_len;
  AAA_AVP       *avp;
  unsigned int  avp_code;
  unsigned char avp_flags;
  unsigned int  avp_len;
  unsigned int  avp_vendorID;
  unsigned int  avp_data_len;

  /* check the params */
  if( !source || !sourceLen || sourceLen<AAA_MSG_HDR_SIZE) {
    ERROR("ERROR:AAATranslateMessage: invalid buffered received!\n");
    goto error;
  }

  /* inits */
  msg = 0;
  avp = 0;
  ptr = source;

  /* alloc a new message structure */
  msg = (AAAMessage*)ad_malloc(sizeof(AAAMessage));
  if (!msg) {
    ERROR("ERROR:AAATranslateMessage: no more free memory!!\n");
    goto error;
  }
  memset(msg,0,sizeof(AAAMessage));

  /* get the version */
  version = (unsigned char)*ptr;
  ptr += VER_SIZE;
  if (version!=1) {
    ERROR("ERROR:AAATranslateMessage: invalid version [%d]in "
	  "AAA msg\n",version);
    goto error;
  }

  /* message length */
  msg_len = get_3bytes( ptr );
  ptr += MESSAGE_LENGTH_SIZE;
  if (msg_len>sourceLen) {
    ERROR("ERROR:AAATranslateMessage: AAA message len [%d] bigger then"
	  " buffer len [%d]\n",msg_len,sourceLen);
    goto error;
  }

  /* command flags */
  msg->flags = *ptr;
  ptr += FLAGS_SIZE;

  /* command code */
  msg->commandCode = get_3bytes( ptr );
  ptr += COMMAND_CODE_SIZE;

  /* application-Id */
  msg->applicationId = get_4bytes( ptr );
  ptr += APPLICATION_ID_SIZE;

  /* Hop-by-Hop-Id */
  msg->hopbyhopId = *((unsigned int*)ptr);
  ptr += HOP_BY_HOP_IDENTIFIER_SIZE;

  /* End-to-End-Id */
  msg->endtoendId = *((unsigned int*)ptr);
  ptr += END_TO_END_IDENTIFIER_SIZE;

  /* start decoding the AVPS */
  while (ptr < source+msg_len) {
    if (ptr+AVP_HDR_SIZE(0x80)>source+msg_len){
      ERROR("ERROR:AAATranslateMessage: source buffer to short!! "
	    "Cannot read the whole AVP header!\n");
      goto error;
    }
    /* avp code */
    avp_code = get_4bytes( ptr );
    ptr += AVP_CODE_SIZE;
    /* avp flags */
    avp_flags = (unsigned char)*ptr;
    ptr += AVP_FLAGS_SIZE;
    /* avp length */
    avp_len = get_3bytes( ptr );
    ptr += AVP_LENGTH_SIZE;
    if (avp_len<1) {
      ERROR("ERROR:AAATranslateMessage: invalid AVP len [%d]\n",
	    avp_len);
      goto error;
    }
    /* avp vendor-ID */
    avp_vendorID = 0;
    if (avp_flags&AAA_AVP_FLAG_VENDOR_SPECIFIC) {
      avp_vendorID = get_4bytes( ptr );
      ptr += AVP_VENDOR_ID_SIZE;
    }
    /* data length */
    avp_data_len = avp_len-AVP_HDR_SIZE(avp_flags);
    /*check the data length */
    if ( source+msg_len<ptr+avp_data_len) {
      ERROR("ERROR:AAATranslateMessage: source buffer to short!! "
	    "Cannot read a whole data for AVP!\n");
      goto error;
    }

    /* create the AVP */
    avp = AAACreateAVP( avp_code, avp_flags, avp_vendorID, (char*)ptr,
			avp_data_len, AVP_DONT_FREE_DATA);
    if (!avp)
      goto error;

    /* link the avp into aaa message to the end */
    AAAAddAVPToMessage( msg, avp, msg->avpList.tail);

    ptr += to_32x_len( avp_data_len );
  }

  /* link the buffer to the message */
  if (attach_buf) {
    msg->buf.s = (char*)source;
    msg->buf.len = msg_len;
  }

  //AAAPrintMessage( msg );
  return  msg;
 error:
  ERROR("ERROR:AAATranslateMessage: message conversion droped!!\n");
  AAAFreeMessage(&msg);
  return 0;
}
Exemple #5
0
/**
 * Ungroup from a data buffer a list of avps
 * @param buf - payload to ungroup the list from
 * @returns the AAA_AVP_LIST or an empty one on error
 */
AAA_AVP_LIST AAAUngroupAVPS(str buf)
{
	char *ptr;
	AAA_AVP       *avp;
	unsigned int  avp_code;
	unsigned char avp_flags;
	unsigned int  avp_len;
	unsigned int  avp_vendorID;
	unsigned int  avp_data_len;
	AAA_AVP_LIST	lh;

	lh.head=0;
	lh.tail=0;
	ptr = buf.s;

	/* start decoding the AVPS */
	while (ptr < buf.s+buf.len) {
		if (ptr+AVP_HDR_SIZE(0x80)>buf.s+buf.len){
			LM_ERR("hss3g_ungroup_avps: source buffer to short!! "
					"Cannot read the whole AVP header!\n");
			goto error;
		}
		/* avp code */
		avp_code = get_4bytes( ptr );
		ptr += AVP_CODE_SIZE;
		/* avp flags */
		avp_flags = (unsigned char)*ptr;
		ptr += AVP_FLAGS_SIZE;
		/* avp length */
		avp_len = get_3bytes( ptr );
		ptr += AVP_LENGTH_SIZE;
		if (avp_len<1) {
			LM_ERR("hss3g_ungroup_avps: invalid AVP len [%d]\n",
					avp_len);
			goto error;
		}
		/* avp vendor-ID */
		avp_vendorID = 0;
		if (avp_flags&AAA_AVP_FLAG_VENDOR_SPECIFIC) {
			avp_vendorID = get_4bytes( ptr );
			ptr += AVP_VENDOR_ID_SIZE;
		}
		/* data length */
		avp_data_len = avp_len - AVP_HDR_SIZE(avp_flags);
		/*check the data length */
		if ( buf.s+buf.len<ptr+avp_data_len) {
			LM_ERR("hss3g_ungroup_avps: source buffer to short!! "
					"Cannot read a whole data for AVP!\n");
			goto error;
		}

		/* create the AVP */
		avp = AAACreateAVP( avp_code, avp_flags, avp_vendorID, ptr,
				avp_data_len, AVP_DONT_FREE_DATA);
		if (!avp) {
			LM_ERR("hss3g_ungroup_avps: can't create avp for member of list\n");
			goto error;
		}

		/* link the avp into aaa message to the end */
		avp->next = 0;
		avp->prev = lh.tail;
		if (lh.tail) {
			lh.tail->next=avp;
			lh.tail=avp;
		}
		else {
			lh.tail=avp;
			lh.head=avp;
		}

		ptr += to_32x_len( avp_data_len );
	}
	return lh;

error:
	LM_CRIT("AVP:<%.*s>\n",buf.len,buf.s);
	return lh;
}