// messageAddAVPsWithPath will dynamically add AVPs into the message // append: append to the message, on false overwrite if AVP is single or add to group if AVP is Grouped func messageSetAVPsWithPath(m *diam.Message, path []interface{}, avpValStr string, appnd bool, timezone string) error { if len(path) == 0 { return errors.New("Empty path as AVP filter") } dictAVPs := make([]*dict.AVP, len(path)) // for each subpath, one dictionary AVP for i, subpath := range path { if dictAVP, err := m.Dictionary().FindAVP(m.Header.ApplicationID, subpath); err != nil { return err } else if dictAVP == nil { return fmt.Errorf("Cannot find AVP with id: %s", path[len(path)-1]) } else { dictAVPs[i] = dictAVP } } if dictAVPs[len(path)-1].Data.Type == diam.GroupedAVPType { return errors.New("Last AVP in path needs not to be GroupedAVP") } var msgAVP *diam.AVP // Keep a reference here towards last AVP lastAVPIdx := len(path) - 1 for i := lastAVPIdx; i >= 0; i-- { var typeVal datatype.Type if i == lastAVPIdx { avpValByte, err := serializeAVPValueFromString(dictAVPs[i], avpValStr, timezone) if err != nil { return err } typeVal, err = datatype.Decode(dictAVPs[i].Data.Type, avpValByte) if err != nil { return err } } else { typeVal = &diam.GroupedAVP{ AVP: []*diam.AVP{msgAVP}} } newMsgAVP := diam.NewAVP(dictAVPs[i].Code, avp.Mbit, dictAVPs[i].VendorID, typeVal) // FixMe: maybe Mbit with dictionary one if i == lastAVPIdx-1 && !appnd { // last AVP needs to be appended in group avps, _ := m.FindAVPsWithPath(path[:lastAVPIdx], dict.UndefinedVendorID) if len(avps) != 0 { // Group AVP already in the message prevGrpData := avps[0].Data.(*diam.GroupedAVP) prevGrpData.AVP = append(prevGrpData.AVP, msgAVP) m.Header.MessageLength += uint32(msgAVP.Len()) return nil } } msgAVP = newMsgAVP } if !appnd { // Not group AVP, replace the previous set one with this one avps, _ := m.FindAVPsWithPath(path, dict.UndefinedVendorID) if len(avps) != 0 { // Group AVP already in the message m.Header.MessageLength -= uint32(avps[0].Len()) // decrease message length since we overwrite *avps[0] = *msgAVP m.Header.MessageLength += uint32(msgAVP.Len()) return nil } } m.AVP = append(m.AVP, msgAVP) m.Header.MessageLength += uint32(msgAVP.Len()) return nil }
// Handler for meta functions func metaHandler(m *diam.Message, tag, arg string, dur time.Duration) (string, error) { switch tag { case META_CCR_USAGE: var ok bool var reqType datatype.Enumerated var reqNr, reqUnit, usedUnit datatype.Unsigned32 if ccReqTypeAvp, err := m.FindAVP("CC-Request-Type", 0); err != nil { return "", err } else if ccReqTypeAvp == nil { return "", errors.New("CC-Request-Type not found") } else if reqType, ok = ccReqTypeAvp.Data.(datatype.Enumerated); !ok { return "", fmt.Errorf("CC-Request-Type must be Enumerated and not %v", ccReqTypeAvp.Data.Type()) } if ccReqNrAvp, err := m.FindAVP("CC-Request-Number", 0); err != nil { return "", err } else if ccReqNrAvp == nil { return "", errors.New("CC-Request-Number not found") } else if reqNr, ok = ccReqNrAvp.Data.(datatype.Unsigned32); !ok { return "", fmt.Errorf("CC-Request-Number must be Unsigned32 and not %v", ccReqNrAvp.Data.Type()) } switch reqType { case datatype.Enumerated(1), datatype.Enumerated(2): if reqUnitAVPs, err := m.FindAVPsWithPath([]interface{}{"Requested-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil { return "", err } else if len(reqUnitAVPs) == 0 { return "", errors.New("Requested-Service-Unit>CC-Time not found") } else if reqUnit, ok = reqUnitAVPs[0].Data.(datatype.Unsigned32); !ok { return "", fmt.Errorf("Requested-Service-Unit>CC-Time must be Unsigned32 and not %v", reqUnitAVPs[0].Data.Type()) } case datatype.Enumerated(3), datatype.Enumerated(4): if usedUnitAVPs, err := m.FindAVPsWithPath([]interface{}{"Used-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil { return "", err } else if len(usedUnitAVPs) != 0 { if usedUnit, ok = usedUnitAVPs[0].Data.(datatype.Unsigned32); !ok { return "", fmt.Errorf("Used-Service-Unit>CC-Time must be Unsigned32 and not %v", usedUnitAVPs[0].Data.Type()) } } } usage := usageFromCCR(int(reqType), int(reqNr), int(reqUnit), int(usedUnit), dur) return strconv.FormatFloat(usage.Seconds(), 'f', -1, 64), nil } return "", nil }
// avpsWithPath is used to find AVPs by specifying RSRField as filter func avpsWithPath(m *diam.Message, rsrFld *utils.RSRField) ([]*diam.AVP, error) { return m.FindAVPsWithPath(splitIntoInterface(rsrFld.Id, utils.HIERARCHY_SEP), dict.UndefinedVendorID) }