Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remaining buffer from socket isn't available anymore after calling socket.recv the first time #66018

Closed
Sworddragon mannequin opened this issue Jun 21, 2014 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Sworddragon
Copy link
Mannequin

Sworddragon mannequin commented Jun 21, 2014

BPO 21819

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2014-06-21.10:08:56.480>
created_at = <Date 2014-06-21.05:04:51.229>
labels = ['invalid', 'type-bug', 'library']
title = "Remaining buffer from socket isn't available anymore after calling socket.recv the first time"
updated_at = <Date 2014-06-22.06:40:56.514>
user = 'https://bugs.python.org/Sworddragon'

bugs.python.org fields:

activity = <Date 2014-06-22.06:40:56.514>
actor = 'neologix'
assignee = 'none'
closed = True
closed_date = <Date 2014-06-21.10:08:56.480>
closer = 'neologix'
components = ['Library (Lib)']
creation = <Date 2014-06-21.05:04:51.229>
creator = 'Sworddragon'
dependencies = []
files = []
hgrepos = []
issue_num = 21819
keywords = []
message_count = 4.0
messages = ['221155', '221164', '221182', '221223']
nosy_count = 2.0
nosy_names = ['neologix', 'Sworddragon']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21819'
versions = ['Python 3.4']

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Jun 21, 2014

If I'm receiving data from a socket (several bytes) and making the first call to socket.recv(1) all is fine but the second call won't get any further data. But doing this again with socket.recv(2) instead will successfully get the 2 bytes. Here is a testcase:

Script:

def icmp_packet(type, code, data):
	length_data = len(data)
	if length_data % 2 == 1:
		data += b'\x00'
	checksum = code | type << 8
	i = 0
	while i < length_data:
		checksum += data[i + 1] | data[i] << 8
		checksum = (checksum & 65535) + (checksum >> 16)
		i += 2
	return bytes([type]) + bytes([code]) + (checksum ^ 65535).to_bytes(2, 'big') + data

import socket

connection = socket.socket(proto = socket.IPPROTO_ICMP, type = socket.SOCK_RAW)
connection.settimeout(1)
connection.sendto(icmp_packet(8, 0, b'\x00\x00\x00\x00'), ('8.8.8.8', 0))
print(connection.recv(2))
connection.close()
connection = socket.socket(proto = socket.IPPROTO_ICMP, type = socket.SOCK_RAW)
connection.settimeout(1)
connection.sendto(icmp_packet(8, 0, b'\x00\x00\x00\x00'), ('8.8.8.8', 0))
print(connection.recv(1))
print(connection.recv(1))
connection.close()

Here is the result:

root@ubuntu:/home/sworddragon/tmp# python3 test.py
b'E\x00'
b'E'
Traceback (most recent call last):
  File "test.py", line 24, in <module>
    print(connection.recv(1))
socket.timeout: timed out

@Sworddragon Sworddragon mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 21, 2014
@neologix
Copy link
Mannequin

neologix mannequin commented Jun 21, 2014

If I'm receiving data from a socket (several bytes) and making the
first call to socket.recv(1) all is fine but the second call won't get
any further data. But doing this again with socket.recv(2) instead will
successfully get the 2 bytes. Here is a testcase:

First, note that Python just calles the underlying recv() syscall, so it's not at fault here.

And actually, noone's at fault here, because what you're trying to do doesn't make sense: ICMP is datagram-oriented, so you should use recvfrom(): and if you try to receive less bytes than the datagram size, the rest will be discarded, like UDP.

@neologix neologix mannequin closed this as completed Jun 21, 2014
@neologix neologix mannequin added the invalid label Jun 21, 2014
@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Jun 21, 2014

and if you try to receive less bytes than the datagram size, the rest will be discarded, like UDP.

I'm wondering how would it be possible then to fetch packets of an unknown size without using an extremely big buffer.

@neologix
Copy link
Mannequin

neologix mannequin commented Jun 22, 2014

I'm wondering how would it be possible then to fetch packets of an unknown size without using an extremely big buffer.

IP packets are limited to 64K, so just pass a 64K buffer, that's not
"extremely big".
If you really wanted to avoid this, you could try the FIONREAD ioctl,
but I wouldn't advise it.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

0 participants