Convert asn1 template files proto_tree_add_item() 'encoding' arg for field types...
[obnox/wireshark/wip.git] / asn1 / snmp / packet-snmp-template.c
index e6eac843e7ef1a4765cd856f370ab86efb7b993a..d4ab5e99a0cdb6cd367e45354286fc0b15ae7013 100644 (file)
@@ -449,7 +449,7 @@ dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
 
        /* then we have the  value's header */
        offset = get_ber_identifier(tvb, offset, &ber_class, &pc, &tag);
-       value_offset = offset = get_ber_length(tvb, offset, &value_len, &ind);
+       value_offset = get_ber_length(tvb, offset, &value_len, &ind);
 
        if (! (!pc) ) {
                proto_item* pi = proto_tree_add_text(tree, tvb, seq_offset, seq_len,"the value must be in primitive encoding");
@@ -467,7 +467,7 @@ dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
        pt_varbind = proto_item_add_subtree(pi_varbind,ett_varbind);
        *label = '\0';
 
-       pi_name = proto_tree_add_item(pt_varbind,hf_snmp_objectname,tvb,name_offset,name_len,FALSE);
+       pi_name = proto_tree_add_item(pt_varbind,hf_snmp_objectname,tvb,name_offset,name_len,ENC_NA);
        pt_name = proto_item_add_subtree(pi_name,ett_name);
 
        /* fetch ObjectName and its relative oid_info */
@@ -521,7 +521,7 @@ dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
                        }
                }
 
-               pi = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,FALSE);
+               pi = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,ENC_BIG_ENDIAN);
                expert_add_info_format(actx->pinfo, pi, PI_RESPONSE_CODE, PI_NOTE, "%s",note);
                g_strlcpy (label, note, ITEM_LABEL_LENGTH);
                goto set_label;
@@ -538,7 +538,7 @@ dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
                        } else if (oid_left  == 0) {
                                if (ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
                                        /* unSpecified  does not require an instance sub-id add the new value and get off the way! */
-                                       pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,FALSE);
+                                       pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,ENC_NA);
                                        goto set_label;
                                } else {
                                        proto_item* pi = proto_tree_add_text(pt_name,tvb,0,0,"A scalar should have one instance sub-id this one has none");
@@ -562,7 +562,7 @@ dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
 
                                if ( key_len == 0 && ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
                                        /* unSpecified  does not require an instance sub-id add the new value and get off the way! */
-                                       pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,FALSE);
+                                       pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,ENC_NA);
                                        goto set_label;
                                }
 
@@ -588,7 +588,7 @@ dissect_snmp_VarBind(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset,
                                                                if (IS_FT_INT(k->ft_type)) {
                                                                        proto_tree_add_int(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
                                                                } else { /* if it's not an unsigned int let proto_tree_add_uint throw a warning */
-                                                                       proto_tree_add_uint(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
+                                                                       proto_tree_add_uint64(pt_name,k->hfid,tvb,name_offset,name_len,(guint)subids[key_start]);
                                                                }
                                                                key_start++;
                                                                key_len--;
@@ -692,11 +692,11 @@ show_oid_index:
                                                                        case OID_KEY_TYPE_IPADDR: {
                                                                                guint32* ipv4_p = (void*)buf;
                                                                                proto_tree_add_ipv4(pt_name,k->hfid,tvb,name_offset,buf_len, *ipv4_p);
+                                                                               }
                                                                                break;
                                                                        default:
                                                                                DISSECTOR_ASSERT_NOT_REACHED();
                                                                                break;
-                                                                       }
                                                                }
 
                                                                key_start += buf_len;
@@ -729,31 +729,34 @@ indexing_done:
 
        if (oid_info_is_ok && oid_info->value_type) {
                if (ber_class == BER_CLASS_UNI && tag == BER_UNI_TAG_NULL) {
-                       pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,FALSE);
-               }  else {
+                       pi_value = proto_tree_add_item(pt_varbind,hf_snmp_unSpecified,tvb,value_offset,value_len,ENC_NA);
+               } else {
+                       /* Provide a tree_item to attach errors to, if needed. */
+                       pi_value = pi_name;
+
                        if ((oid_info->value_type->ber_class != BER_CLASS_ANY) &&
                                (ber_class != oid_info->value_type->ber_class))
                                format_error = BER_WRONG_TAG;
-
-                       if ((oid_info->value_type->ber_tag != BER_TAG_ANY) &&
+                       else if ((oid_info->value_type->ber_tag != BER_TAG_ANY) &&
                                (tag != oid_info->value_type->ber_tag))
                                format_error = BER_WRONG_TAG;
+                       else {
+                               max_len = oid_info->value_type->max_len == -1 ? 0xffffff : oid_info->value_type->max_len;
+                               min_len  = oid_info->value_type->min_len;
 
-                       max_len = oid_info->value_type->max_len == -1 ? 0xffffff : oid_info->value_type->max_len;
-                       min_len  = oid_info->value_type->min_len;
-
-                       if ((int)value_len < min_len || (int)value_len > max_len) {
-                               format_error = BER_WRONG_LENGTH;
-                       } else {
-                               pi_value = proto_tree_add_item(pt_varbind,oid_info->value_hfid,tvb,value_offset,value_len,FALSE);
+                               if ((int)value_len < min_len || (int)value_len > max_len)
+                                       format_error = BER_WRONG_LENGTH;
                        }
+
+                       if (format_error == BER_NO_ERROR)
+                               pi_value = proto_tree_add_item(pt_varbind,oid_info->value_hfid,tvb,value_offset,value_len,ENC_BIG_ENDIAN);
                }
        } else {
                switch(ber_class|(tag<<4)) {
                        case BER_CLASS_UNI|(BER_UNI_TAG_INTEGER<<4):
                        {
                                gint64 val=0;
-                               unsigned offset = value_offset;
+                               unsigned int_val_offset = value_offset;
                                unsigned i;
 
                                max_len = 5; min_len = 1;
@@ -764,12 +767,12 @@ indexing_done:
 
                                if(value_len > 0) {
                                        /* extend sign bit */
-                                       if(tvb_get_guint8(tvb, offset)&0x80) {
+                                       if(tvb_get_guint8(tvb, int_val_offset)&0x80) {
                                                val=-1;
                                        }
                                        for(i=0;i<value_len;i++) {
-                                               val=(val<<8)|tvb_get_guint8(tvb, offset);
-                                               offset++;
+                                               val=(val<<8)|tvb_get_guint8(tvb, int_val_offset);
+                                               int_val_offset++;
                                        }
                                }
                                proto_tree_add_int64(pt_varbind, hf_snmp_integer32_value, tvb,value_offset,value_len, val);
@@ -822,7 +825,7 @@ indexing_done:
                                break;
                }
 
-               pi_value = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,FALSE);
+               pi_value = proto_tree_add_item(pt_varbind,hfid,tvb,value_offset,value_len,ENC_BIG_ENDIAN);
                if (format_error != BER_NO_ERROR) {
                        expert_add_info_format(actx->pinfo, pi_value, PI_UNDECODED, PI_NOTE, "Unresolved value, Missing MIB");
                }
@@ -946,7 +949,7 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
     /* first bit: engine id conformance */
     if (len_remain<4) return offset;
     conformance = ((tvb_get_guint8(tvb, offset)>>7) & 0x01);
-    proto_tree_add_item(tree, hf_snmp_engineid_conform, tvb, offset, 1, FALSE);
+    proto_tree_add_item(tree, hf_snmp_engineid_conform, tvb, offset, 1, ENC_BIG_ENDIAN);
 
     /* 4-byte enterprise number/name */
     if (len_remain<4) return offset;
@@ -986,7 +989,7 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
       case SNMP_ENGINEID_FORMAT_IPV4:
        /* 4-byte IPv4 address */
        if (len_remain==4) {
-         proto_tree_add_item(tree, hf_snmp_engineid_ipv4, tvb, offset, 4, FALSE);
+         proto_tree_add_item(tree, hf_snmp_engineid_ipv4, tvb, offset, 4, ENC_BIG_ENDIAN);
          offset+=4;
          len_remain=0;
        }
@@ -994,7 +997,7 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
       case SNMP_ENGINEID_FORMAT_IPV6:
        /* 16-byte IPv6 address */
        if (len_remain==16) {
-         proto_tree_add_item(tree, hf_snmp_engineid_ipv6, tvb, offset, 16, FALSE);
+         proto_tree_add_item(tree, hf_snmp_engineid_ipv6, tvb, offset, 16, ENC_NA);
          offset+=16;
          len_remain=0;
        }
@@ -1002,13 +1005,13 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
       case SNMP_ENGINEID_FORMAT_MACADDRESS:
        /* See: https://supportforums.cisco.com/message/3010617#3010617 for details. */
        if ((enterpriseid==9)&&(len_remain==7)) {
-         proto_tree_add_item(tree, hf_snmp_engineid_cisco_type, tvb, offset, 1, FALSE);
+         proto_tree_add_item(tree, hf_snmp_engineid_cisco_type, tvb, offset, 1, ENC_BIG_ENDIAN);
          offset++;
          len_remain--;
        }
        /* 6-byte MAC address */
        if (len_remain==6) {
-         proto_tree_add_item(tree, hf_snmp_engineid_mac, tvb, offset, 6, FALSE);
+         proto_tree_add_item(tree, hf_snmp_engineid_mac, tvb, offset, 6, ENC_BIG_ENDIAN);
          offset+=6;
          len_remain=0;
        }
@@ -1016,7 +1019,7 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
       case SNMP_ENGINEID_FORMAT_TEXT:
        /* max. 27-byte string, administratively assigned */
        if (len_remain<=27) {
-         proto_tree_add_item(tree, hf_snmp_engineid_text, tvb, offset, len_remain, FALSE);
+         proto_tree_add_item(tree, hf_snmp_engineid_text, tvb, offset, len_remain, ENC_ASCII|ENC_NA);
          offset+=len_remain;
          len_remain=0;
        }
@@ -1027,7 +1030,7 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
          proto_item_append_text(item, (enterpriseid==2021) ? ": UCD-SNMP Random" : ": Net-SNMP Random");
          /* demystify: 4B random, 4B epoch seconds */
          if (len_remain==8) {
-           proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, 4, FALSE);
+           proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, 4, ENC_NA);
            seconds = tvb_get_letohl(tvb, offset+4);
            ts.secs = seconds;
            ts.nsecs = 0;
@@ -1043,7 +1046,7 @@ dissect_snmp_engineid(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
       default:
        /* max. 27 bytes, administratively assigned or unknown format */
        if (len_remain<=27) {
-         proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, len_remain, FALSE);
+         proto_tree_add_item(tree, hf_snmp_engineid_data, tvb, offset, len_remain, ENC_NA);
          offset+=len_remain;
          len_remain=0;
        }
@@ -1171,9 +1174,9 @@ get_user_assoc(tvbuff_t* engine_tvb, tvbuff_t* user_tvb)
 
        if (! ( user_tvb && engine_tvb ) ) return NULL;
 
-       given_username_len = tvb_length_remaining(user_tvb,0);
+       given_username_len = tvb_ensure_length_remaining(user_tvb,0);
        given_username = ep_tvb_memdup(user_tvb,0,-1);
-       given_engine_len = tvb_length_remaining(engine_tvb,0);
+       given_engine_len = tvb_ensure_length_remaining(engine_tvb,0);
        given_engine = ep_tvb_memdup(engine_tvb,0,-1);
 
        for (a = localized_ues; a; a = a->next) {
@@ -1558,8 +1561,16 @@ dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
                 * BER).
                 */
                if (length_remaining < 6) {
+                       /*
+                        * Yes.  Tell the TCP dissector where the data
+                        * for this message starts in the data it handed
+                        * us and that we need "some more data."  Don't tell
+                        * it exactly how many bytes we need because if/when
+                        * we ask for even more (after the header) that will
+                        * break reassembly.
+                        */
                        pinfo->desegment_offset = offset;
-                       pinfo->desegment_len = 6 - length_remaining;
+                       pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
 
                        /*
                         * Return 0, which means "I didn't dissect anything
@@ -1619,7 +1630,7 @@ dissect_snmp_pdu(tvbuff_t *tvb, int offset, packet_info *pinfo,
 
        if (tree) {
                item = proto_tree_add_item(tree, proto, tvb, start_offset,
-                                          message_length, FALSE);
+                                          message_length, ENC_BIG_ENDIAN);
                snmp_tree = proto_item_add_subtree(item, ett);
        }
 
@@ -1700,7 +1711,7 @@ dissect_snmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
                }
        }
        /* then comes an INTEGER (version)*/
-       offset = get_ber_identifier(tvb, offset, &tmp_class, &tmp_pc, &tmp_tag);
+       get_ber_identifier(tvb, offset, &tmp_class, &tmp_pc, &tmp_tag);
        if((tmp_class!=BER_CLASS_UNI)||(tmp_tag!=BER_UNI_TAG_INTEGER)) {
                return 0;
        }
@@ -1770,11 +1781,11 @@ dissect_smux(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
        col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMUX");
 
        if (tree) {
-               item = proto_tree_add_item(tree, proto_smux, tvb, 0, -1, FALSE);
+               item = proto_tree_add_item(tree, proto_smux, tvb, 0, -1, ENC_BIG_ENDIAN);
                smux_tree = proto_item_add_subtree(item, ett_smux);
        }
 
-       dissect_SMUX_PDUs_PDU(tvb, pinfo, tree);
+       dissect_SMUX_PDUs_PDU(tvb, pinfo, smux_tree);
 }
 
 
@@ -1951,7 +1962,7 @@ snmp_users_update_cb(void* p _U_, const char** err)
                snmp_ue_assoc_t* u = &(ueas[i]);
 
                /* RFC 3411 section 5 */
-               if (u->engine.len < 5 || u->engine.len > 32) {
+               if ((u->engine.len > 0) && (u->engine.len < 5 || u->engine.len > 32)) {
                        g_string_append_printf(es, "Invalid engineId length (%u). Must be between 5 and 32 (10 and 64 hex digits)\n", u->engine.len);
                }