{"action":"created","issue":{"url":"https://api.github.com/repos/ldx/python-iptables/issues/157","labels_url":"https://api.github.com/repos/ldx/python-iptables/issues/157/labels{/name}","comments_url":"https://api.github.com/repos/ldx/python-iptables/issues/157/comments","events_url":"https://api.github.com/repos/ldx/python-iptables/issues/157/events","html_url":"https://github.com/ldx/python-iptables/issues/157","id":100233256,"number":157,"title":"scripts insert two or more rules,will crash","user":{"login":"edwin--zhao","id":8266370,"avatar_url":"https://avatars.githubusercontent.com/u/8266370?v=3","gravatar_id":"","url":"https://api.github.com/users/edwin--zhao","html_url":"https://github.com/edwin--zhao","followers_url":"https://api.github.com/users/edwin--zhao/followers","following_url":"https://api.github.com/users/edwin--zhao/following{/other_user}","gists_url":"https://api.github.com/users/edwin--zhao/gists{/gist_id}","starred_url":"https://api.github.com/users/edwin--zhao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edwin--zhao/subscriptions","organizations_url":"https://api.github.com/users/edwin--zhao/orgs","repos_url":"https://api.github.com/users/edwin--zhao/repos","events_url":"https://api.github.com/users/edwin--zhao/events{/privacy}","received_events_url":"https://api.github.com/users/edwin--zhao/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-11T05:59:55Z","updated_at":"2015-08-11T16:39:16Z","closed_at":null,"body":"```\r\n#!/usr/bin/python\r\n#config iptables rule\r\n#Author: edwin\r\n#Date: 2015-08-06\r\n\r\nimport iptc\r\nimport time\r\n\r\nOFFICE_NETWORK = \"172.16.0.0/16\"\r\n\r\ndef test(dst, port, comment):\r\n    print \"Rule\"\r\n    rule = iptc.Rule()\r\n    print \"Prerouting\"\r\n    chain = iptc.Chain(iptc.Table(iptc.Table.NAT), \"PREROUTING\")\r\n    rule.src = OFFICE_NETWORK\r\n    rule.dst = \"%s/32\" % dst\r\n    rule.protocol = \"tcp\"\r\n    print \"Multiport\"\r\n    match = iptc.Match(rule, \"multiport\")\r\n    #match = iptc.Match(rule, \"tcp\")\r\n    match.dports = str(port)\r\n    print \"Match\"\r\n    rule.add_match(match)\r\n    print \"Comment\"\r\n    comt = rule.create_match(\"comment\")\r\n    comt.comment = comment\r\n    print \"ACCEPT\"\r\n    rule.target = iptc.Target(rule, \"ACCEPT\")\r\n    print \"Insert\"\r\n    chain.insert_rule(rule)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    #main()\r\n    rules = [{\"ip\":\"10.20.1.49\",\"port\":\"47000\",\"comment\":\"_0000_ test1\"},\r\n\t{\"ip\":\"10.20.1.49\",\"port\":\"48000\",\"comment\":\"_0001_ test2\"},\r\n\t{\"ip\":\"10.20.1.49\",\"port\":\"49000\",\"comment\":\"_0003_ test3\"},\r\n\t{\"ip\":\"10.20.1.49\",\"port\":\"46000\",\"comment\":\"_0004_ test4\"},\r\n\t]\r\n#    for i in rules:\r\n#        dst = i[\"ip\"]\r\n#        port = i[\"port\"]\r\n#        comment = i[\"comment\"]\r\n#    \t test(dst, port, comment)\r\n    #pdb.set_trace()\r\n    print 0\r\n    dst = rules[0][\"ip\"]\r\n    port = rules[0][\"port\"]\r\n    comment = rules[0][\"comment\"]\r\n    test(dst, port, comment)\r\n    print 1\r\n    dst = rules[1][\"ip\"]\r\n    port = rules[1][\"port\"]\r\n    comment = rules[1][\"comment\"]\r\n    test(dst, port, comment)\r\n    print 2\r\n    dst = rules[2][\"ip\"]\r\n    port = rules[2][\"port\"]\r\n    comment = rules[2][\"comment\"]\r\n    test(dst, port, comment)\r\n    print 3\r\n    dst = rules[3][\"ip\"]\r\n    port = rules[3][\"port\"]\r\n    comment = rules[3][\"comment\"]\r\n    test(dst, port, comment)\r\n```\r\n\r\nrun scripts get this:\r\n\r\n```\r\n0\r\nRule\r\nPrerouting\r\nMultiport\r\nMatch\r\nComment\r\nACCEPT\r\nInsert\r\n1\r\nRule\r\nPrerouting\r\nMultiport\r\nMatch\r\nComment\r\nACCEPT\r\nInsert\r\n2\r\nRule\r\nPrerouting\r\nMultiport\r\nMatch\r\nComment\r\nACCEPT\r\nInsert\r\n3\r\nRule\r\nPrerouting\r\nMultiport\r\nMatch\r\nComment\r\nACCEPT\r\nInsert\r\n*** glibc detected *** python: free(): invalid pointer: 0x00007f4ae839c860 ***\r\n======= Backtrace: =========\r\n/lib64/libc.so.6(+0x75e66)[0x7f4af2878e66]\r\n/usr/lib64/libffi.so.5(ffi_call_unix64+0x4c)[0x7f4aeb926dac]\r\n/usr/lib64/libffi.so.5(ffi_call+0x214)[0x7f4aeb926b34]\r\n/usr/lib64/python2.6/lib-dynload/_ctypes.so(_CallProc+0x394)[0x7f4aebb3a074]\r\n/usr/lib64/python2.6/lib-dynload/_ctypes.so(+0xa3a2)[0x7f4aebb333a2]\r\n/usr/lib64/libpython2.6.so.1.0(PyObject_Call+0x53)[0x3b01e43c63]\r\n/usr/lib64/libpython2.6.so.1.0(PyEval_EvalFrameEx+0x47d4)[0x3b01ed4f74]\r\n/usr/lib64/libpython2.6.so.1.0(PyEval_EvalCodeEx+0x927)[0x3b01ed7657]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e6acb0]\r\n/usr/lib64/libpython2.6.so.1.0(PyObject_Call+0x53)[0x3b01e43c63]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e566af]\r\n/usr/lib64/libpython2.6.so.1.0(PyObject_Call+0x53)[0x3b01e43c63]\r\n/usr/lib64/libpython2.6.so.1.0(PyEval_CallObjectWithKeywords+0x43)[0x3b01ecfc93]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e95ffe]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e9a57f]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e79e4b]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e9a75c]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e70aff]\r\n/usr/lib64/libpython2.6.so.1.0(PyDict_Clear+0x10b)[0x3b01e7b9cb]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01e7ba39]\r\n/usr/lib64/libpython2.6.so.1.0[0x3b01f0071e]\r\n/usr/lib64/libpython2.6.so.1.0(PyGC_Collect+0x24)[0x3b01f00cc4]\r\n/usr/lib64/libpython2.6.so.1.0(Py_Finalize+0x116)[0x3b01ef28a6]\r\n/usr/lib64/libpython2.6.so.1.0(Py_Main+0x596)[0x3b01eff2d6]\r\n/lib64/libc.so.6(__libc_start_main+0xfd)[0x7f4af2821d5d]\r\npython[0x400649]\r\n======= Memory map: ========\r\n00400000-00401000 r-xp 00000000 08:01 271163                             /usr/bin/python\r\n00600000-00602000 rw-p 00000000 08:01 271163                             /usr/bin/python\r\n025ef000-02c5b000 rw-p 00000000 00:00 0                                  [heap]\r\n3b01600000-3b01615000 r-xp 00000000 08:01 130997                         /lib64/libz.so.1.2.3\r\n3b01615000-3b01814000 ---p 00015000 08:01 130997                         /lib64/libz.so.1.2.3\r\n3b01814000-3b01815000 r--p 00014000 08:01 130997                         /lib64/libz.so.1.2.3\r\n3b01815000-3b01816000 rw-p 00015000 08:01 130997                         /lib64/libz.so.1.2.3\r\n3b01e00000-3b01f5d000 r-xp 00000000 08:01 274170                         /usr/lib64/libpython2.6.so.1.0\r\n3b01f5d000-3b0215c000 ---p 0015d000 08:01 274170                         /usr/lib64/libpython2.6.so.1.0\r\n3b0215c000-3b02198000 rw-p 0015c000 08:01 274170                         /usr/lib64/libpython2.6.so.1.0\r\n3b02198000-3b021a6000 rw-p 00000000 00:00 0\r\n3b02200000-3b0221d000 r-xp 00000000 08:01 130999                         /lib64/libselinux.so.1\r\n3b0221d000-3b0241c000 ---p 0001d000 08:01 130999                         /lib64/libselinux.so.1\r\n3b0241c000-3b0241d000 r--p 0001c000 08:01 130999                         /lib64/libselinux.so.1\r\n3b0241d000-3b0241e000 rw-p 0001d000 08:01 130999                         /lib64/libselinux.so.1\r\n3b0241e000-3b0241f000 rw-p 00000000 00:00 0\r\n3b02e00000-3b02e16000 r-xp 00000000 08:01 131001                         /lib64/libgcc_s-4.4.7-20120601.so.1\r\n3b02e16000-3b03015000 ---p 00016000 08:01 131001                         /lib64/libgcc_s-4.4.7-20120601.so.1\r\n3b03015000-3b03016000 rw-p 00015000 08:01 131001                         /lib64/libgcc_s-4.4.7-20120601.so.1\r\n3b03200000-3b03203000 r-xp 00000000 08:01 131014                         /lib64/libcom_err.so.2.1\r\n3b03203000-3b03402000 ---p 00003000 08:01 131014                         /lib64/libcom_err.so.2.1\r\n3b03402000-3b03403000 r--p 00002000 08:01 131014                         /lib64/libcom_err.so.2.1\r\n3b03403000-3b03404000 rw-p 00003000 08:01 131014                         /lib64/libcom_err.so.2.1\r\n3b03e00000-3b03e04000 r-xp 00000000 08:01 131002                         /lib64/libuuid.so.1.3.0\r\n3b03e04000-3b04003000 ---p 00004000 08:01 131002                         /lib64/libuuid.so.1.3.0\r\n3b04003000-3b04004000 rw-p 00003000 08:01 131002                         /lib64/libuuid.so.1.3.0\r\n3b04a00000-3b04bb9000 r-xp 00000000 08:01 281227                         /usr/lib64/libcrypto.so.1.0.1e\r\n3b04bb9000-3b04db8000 ---p 001b9000 08:01 281227                         /usr/lib64/libcrypto.so.1.0.1e\r\n3b04db8000-3b04dd3000 r--p 001b8000 08:01 281227                         /usr/lib64/libcrypto.so.1.0.1e\r\n3b04dd3000-3b04ddf000 rw-p 001d3000 08:01 281227                         /usr/lib64/libcrypto.so.1.0.1e\r\n3b04ddf000-3b04de3000 rw-p 00000000 00:00 0\r\n3b06a00000-3b06a02000 r-xp 00000000 08:01 131011                         /lib64/libkeyutils.so.1.3\r\n3b06a02000-3b06c01000 ---p 00002000 08:01 131011                         /lib64/libkeyutils.so.1.3\r\n3b06c01000-3b06c02000 r--p 00001000 08:01 131011                         /lib64/libkeyutils.so.1.3\r\n3b06c02000-3b06c03000 rw-p 00002000 08:01 131011                         /lib64/libkeyutils.so.1.3\r\n3d38000000-3d38029000 r-xp 00000000 08:01 130570                         /lib64/libk5crypto.so.3.1\r\n3d38029000-3d38229000 ---p 00029000 08:01 130570                         /lib64/libk5crypto.so.3.1\r\n3d38229000-3d3822a000 r--p 00029000 08:01 130570                         /lib64/libk5crypto.so.3.1\r\n3d3822a000-3d3822b000 rw-p 0002a000 08:01 130570                         /lib64/libk5crypto.so.3.1\r\n3d3822b000-3d3822c000 rw-p 00000000 00:00 0\r\n3d38400000-3d38441000 r-xp 00000000 08:01 130583                         /lib64/libgssapi_krb5.so.2.2\r\n3d38441000-3d38641000 ---p 00041000 08:01 130583                         /lib64/libgssapi_krb5.so.2.2\r\n3d38641000-3d38642000 r--p 00041000 08:01 130583                         /lib64/libgssapi_krb5.so.2.2\r\n3d38642000-3d38644000 rw-p 00042000 08:01 130583                         /lib64/libgssapi_krb5.so.2.2\r\n3d38800000-3d388db000 r-xp 00000000 08:01 130581                         /lib64/libkrb5.so.3.3\r\n3d388db000-3d38ada000 ---p 000db000 08:01 130581                         /lib64/libkrb5.so.3.3\r\n3d38ada000-3d38ae4000 r--p 000da000 08:01 130581                         /lib64/libkrb5.so.3.3\r\n3d38ae4000-3d38ae6000 rw-p 000e4000 08:01 130581                         /lib64/libkrb5.so.3.3\r\n3d38c00000-3d38c0a000 r-xp 00000000 08:01 130567                         /lib64/libkrb5support.so.0.1\r\n3d38c0a000-3d38e09000 ---p 0000a000 08:01 130567                         /lib64/libkrb5support.so.0.1\r\n3d38e09000-3d38e0a000 r--p 00009000 08:01 130567                         /lib64/libkrb5support.so.0.1\r\n3d38e0a000-3d38e0b000 rw-p 0000a000 08:01 130567                         /lib64/libkrb5support.so.0.1Aborted\r\n```\r\n\r\nand iptables rule has insert success:\r\n\r\n```\r\n-A PREROUTING -s 172.16.0.0/16 -d 10.20.1.49/32 -p tcp -m multiport --dports 46000 -m comment --comment \"_0004_ test4\" -j ACCEPT\r\n-A PREROUTING -s 172.16.0.0/16 -d 10.20.1.49/32 -p tcp -m multiport --dports 49000 -m comment --comment \"_0003_ test3\" -j ACCEPT\r\n-A PREROUTING -s 172.16.0.0/16 -d 10.20.1.49/32 -p tcp -m multiport --dports 48000 -m comment --comment \"_0001_ test2\" -j ACCEPT\r\n-A PREROUTING -s 172.16.0.0/16 -d 10.20.1.49/32 -p tcp -m multiport --dports 47000 -m comment --comment \"_0000_ test1\" -j ACCEPT\r\n```\r\n\r\nif use two function test() test1()，test() run finish ,scripts crash"},"comment":{"url":"https://api.github.com/repos/ldx/python-iptables/issues/comments/129961461","html_url":"https://github.com/ldx/python-iptables/issues/157#issuecomment-129961461","issue_url":"https://api.github.com/repos/ldx/python-iptables/issues/157","id":129961461,"user":{"login":"edwin--zhao","id":8266370,"avatar_url":"https://avatars.githubusercontent.com/u/8266370?v=3","gravatar_id":"","url":"https://api.github.com/users/edwin--zhao","html_url":"https://github.com/edwin--zhao","followers_url":"https://api.github.com/users/edwin--zhao/followers","following_url":"https://api.github.com/users/edwin--zhao/following{/other_user}","gists_url":"https://api.github.com/users/edwin--zhao/gists{/gist_id}","starred_url":"https://api.github.com/users/edwin--zhao/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edwin--zhao/subscriptions","organizations_url":"https://api.github.com/users/edwin--zhao/orgs","repos_url":"https://api.github.com/users/edwin--zhao/repos","events_url":"https://api.github.com/users/edwin--zhao/events{/privacy}","received_events_url":"https://api.github.com/users/edwin--zhao/received_events","type":"User","site_admin":false},"created_at":"2015-08-11T16:39:15Z","updated_at":"2015-08-11T16:39:15Z","body":"CentOS release 6.6 (Final)\r\n\r\niptables v1.4.7\r\n\r\n>>> print(iptc.version.__version__)\r\n0.10.0-dev\r\n\r\nPython 2.6.6\r\n"}}