当前位置: 首页 > news >正文

Linux网络TCP sticky分析工具

1 TCP粘包 - TCP_NODELAY
TCP粘包(sticky)的表现是TCP nagle算法将应用层发送的多个包进行合并后,再发送,很容易出现burst导致bcm89230丢包。由于OABR不支持流控,所以需要使用Linux tc对对应的TCP port进行流量整形。

2 check sticky
import operator
import shutil
import struct
import sys,os
import time

# https://wiki.wireshark.org/Development/LibpcapFileFormat

def read_pkt_from_binary_file(f, pkt_len, list_data):
    for i in range(0, pkt_len):
        b = f.read(1)
        list_data.append('0x%.2x' % ord(b))

def my_cmp(list1, list2):
    if sys.version_info[0] >= 3:  # Python 3
        return operator.eq(list1, list2)
    else:  # Python 2
        if (0 == cmp(list1, list2)):
            return True
        else:
            return False

def _search_hex(list_to_be_found, last_list,
    cur_list, pkt_cnt, hdr):
    list_len = len(list_to_be_found)
    last_list_len = len(last_list)

    ts_sec, ts_usec, incl_len, orig_len = \
        struct.unpack('IIII', hdr)

    ### transfer to UTC from timestamp
    utc = time.asctime(time.localtime(int(ts_sec)))
    index = utc.rfind(' ')
    utc = utc[0:index] # skip the year YYYY

    for i in range(0, incl_len - list_len + 1):
        if (True == my_cmp(list_to_be_found,
            cur_list[i:(i + list_len)])):
            print(str(pkt_cnt) + ' ' + utc + '.' +
                str(ts_usec) + ' ' +
                str(incl_len) + ' ' +
                str(orig_len) + ' offset = '
                + str(hex(i)))

    new_list = last_list + cur_list
    for i in range(0, len(new_list) - list_len + 1):
        if ((True == my_cmp(list_to_be_found,
            new_list[i:(i + list_len)])) and \
            (i < last_list_len) and \
            ((i + list_len) > last_list_len)):  # across border

            print(str(pkt_cnt) + ' ' +
                utc + '.' + str(ts_usec) + ' '  +
                str(incl_len) + ' ' +
                str(orig_len) + ' offset = ' +
                str(hex(i)) +
                '---TCP sticky with last line')

def search_hex(file_name, list_to_be_found):
    last_list = []
    pkt_cnt = 1;
    ### skip the global header
    offset = 24

    f = open(file_name, 'rb')
    while f:
        f.seek(offset, os.SEEK_SET)
        ### packet header
        hdr = f.read(16)
        if len(hdr) == 0:
            f.close()
            break
        offset += 16
        ts_sec, ts_usec, incl_len, orig_len = \
            struct.unpack('IIII', hdr)

        ### packet data
        f.seek(offset, os.SEEK_SET)
        cur_list = []
        read_pkt_from_binary_file(f, incl_len, cur_list)
        _search_hex(list_to_be_found,
            last_list, cur_list, pkt_cnt, hdr)

        # save current packet as last for next comparing
        last_list = cur_list
        pkt_cnt += 1
        offset += incl_len

def main():
    arg0_proc_name = sys.argv[0]
    if sys.argv[0].rfind(os.path.sep) > 0 :
        index = sys.argv[0].rfind(os.path.sep)
        arg0_proc_name = sys.argv[0][index+1:]

    if len(sys.argv) < 3:
        print('\nUsage: python ' + arg0_proc_name +
            ' <file_name> <hex...>\n')
        print('python ' + arg0_proc_name + \
            ' <file_name.pcap> ' +
            '0x01 0x01 0x90 0x02\n')
        sys.exit(0)

    list_to_be_found = []
    for i in range(2, len(sys.argv)):
        list_to_be_found.append(sys.argv[i])
    search_hex(sys.argv[1], list_to_be_found)

if __name__ == '__main__':
    main()

# ServiceID: 0x0101
# MethodID: 0x9002

http://www.lryc.cn/news/19967.html

相关文章:

  • 华为OD机试题,用 Java 解【DNA 序列】问题
  • python的所有知识点+代码+注释,不看就亏死了
  • 读懂分布式事务
  • 多目标粒子群算法求解帕累托前沿Pareto,Pareto的原理,测试函数100种求解之21
  • 数组:二分查找、移除数组等经典数组题
  • 负责任动物纤维标准RAF
  • storybook使用info插件报错
  • 【每日一题Day129】LC1247交换字符使得字符串相同 | 贪心
  • 性能优化之node中间件耗时
  • 3-1 图文并茂说明raid0,raid1, raid10, raid01, raid5等原理
  • 西北工业大学大学物理(I)下2019-2020选填考题解析
  • 自动化测试selenium
  • 熟悉GC常用算法,熟悉常见垃圾收集器,具有实际JVM调优实战经验
  • 常量和变量——“Python”
  • 《蓝桥杯每日一题》KMP算法·AcWing 141. 周期
  • URL介绍
  • 学习 Python 之 Pygame 开发魂斗罗(一)
  • ARM uboot 源码分析8 - uboot的环境变量
  • 【蓝牙mesh】Network协议层介绍
  • 基于遗传算法的配电网故障定位(Matlab代码实现)
  • Leetcode.1247 交换字符使得字符串相同
  • python语音识别whisper
  • Prometheus -- 浅谈Exporter
  • 如何确定RocketMQ中消费者的线程大小
  • OpenAPI SDK组件之Spring Aop源码拓展
  • 蓝桥杯C/C++VIP试题每日一练之龟兔赛跑预测
  • 为你的Vue2.x老项目安装Vite发动机吧
  • ZCMU--5012: 铺设道路(差分思路)
  • 算法模板总结(自用)
  • 【架构师】零基础到精通——架构发展