解析Dhcp Option120

    /**
    *    Code   Len   enc            Address 1               Address 2
    *  +--- +----+---+--- +---+ ---+---+----+----------
    *  | 120 |  n    |  1  | a1  | a2  | a3  | a4  | a1   |  ...
    *  +--- +----+---+--- +---+ ---+---+----+----------
    * modify ygcao
    */
    private static String decodeRfc3361(ByteBuffer buf, int byteCount) {
        Log.d(TAG, "decodeRfc3361 byteCount ="+byteCount);
        int[] unsigntbyte = new int[byteCount];
        int [] ipadresses =  new int[byteCount-1]; // remove the enc bit
        int [] dominNames = new int[byteCount-1]; // remove the enc bit
        int enc = -1;
        int ipaddresslength = -1;
        byte[] bytes = new byte[byteCount];
        buf.get(bytes);
        String sipServerIP= new String(bytes,0 ,bytes.length, StandardCharsets.US_ASCII).trim();
        ipaddresslength = bytes.length-1;
        for (int i=0 ; i< bytes.length ; i++) {
            unsigntbyte[i] =bytes[i] & 0xff;
            if (i==0) {
                enc = unsigntbyte[0];//get the rfc3361 enc bit
            } else {
                ipadresses[i-1] = unsigntbyte[i];
                dominNames[i-1] = bytes[i];
            }
        }
        StringBuffer buffer = new StringBuffer();
        Log.d(TAG, "decodeRfc3361 enc = "+enc);
        Log.d(TAG, "decodeRfc3361 ipaddresslength = "+ipaddresslength);
        if (enc ==1) { //RFC for 3361 this is user ipadtress
            for (int i= 0; i < ipadresses.length;i++) {
                if (ipaddresslength % 4 !=0) { // the same dhcp append "0x00" at the end
                   if (i == ipadresses.length-1) {
                        continue;  // remove the last "0x00"
                   }
                }
                buffer.append(ipadresses[i]);
                if ( i%4  != 3) {
                   buffer.append(".");
                } else {
                   buffer.append(" ");
                }
            }
            sipServerIP = buffer.toString();
        } else if (enc ==0) {
           // for do samethings
            int location = 0;
            while (location < dominNames.length) {
                int length = dominNames[location] & 0xff; //
                buffer.append(new String(dominNames, location + 1, length, StandardCharsets.US_ASCII));
                buffer.append(".");
                location = location + length + 1;
            }
            sipServerIP = buffer.toString().substring(0, buffer.toString().length() - 1); //remove the last "."
        }
        Log.d(TAG, "decodeRfc3361 buffer.toString ="+buffer.toString());
        Log.d(TAG, "decodeRfc3361 sipServerIP ="+sipServerIP);
        return sipServerIP;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值