Title: socket.connect - support custom family value · Issue #92658 · python/cpython · GitHub
Open Graph Title: socket.connect - support custom family value · Issue #92658 · python/cpython
X Title: socket.connect - support custom family value · Issue #92658 · python/cpython
Description: Feature or enhancement Currently socket.connect() accepts a wide range of values when connecting to a socket with the code in socketmodule.c transforming it based on the family the socket was created with. Unfortunately there is a check ...
Open Graph Description: Feature or enhancement Currently socket.connect() accepts a wide range of values when connecting to a socket with the code in socketmodule.c transforming it based on the family the socket was creat...
X Description: Feature or enhancement Currently socket.connect() accepts a wide range of values when connecting to a socket with the code in socketmodule.c transforming it based on the family the socket was creat...
Opengraph URL: https://github.com/python/cpython/issues/92658
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"socket.connect - support custom family value","articleBody":"**Feature or enhancement**\r\n\r\nCurrently `socket.connect()` accepts a wide range of values when connecting to a socket with the code in `socketmodule.c` transforming it based on the family the socket was created with. Unfortunately there is a check that fails the connection if the family is unrecognized https://github.com/python/cpython/blob/9d85aba9e245c1a0f6d1879f8bc6c260cb4eb721/Modules/socketmodule.c#L2530-L2534.\r\n\r\nMy proposal is to allow a caller to bypass this check if passing in a raw byte value to be used as the addr info on the native call.\r\n\r\n**Pitch**\r\n\r\nThe reason why I am hoping for this feature is to support clients connecting to a Hyper-V socket. This uses the `AF_HYPERV` family which isn't known to Python so any attempts to connect to it won't work.\r\n\r\n Currently I am using the following to work around this restriction by using ctypes to call the C API directly and using the fileno:\r\n\r\n```python\r\nimport ctypes\r\nimport socket\r\nimport uuid\r\n\r\nHV_GUID_VM_SESSION_SERVICE_ID = uuid.UUID(\"999e53d4-3d5c-4c3e-8779-bed06ec056e1\")\r\nHV_GUID_VM_SESSION_SERVICE_ID_2 = uuid.UUID(\"a5201c21-2770-4c11-a68e-f182edb29220\")\r\nAF_HYPERV = 34\r\nHV_PROTOCOL_RAW = 1\r\n\r\n# This is the GUID of the Win VM to connect to\r\nvm_id = uuid.UUID(\"...\")\r\n\r\nwin32sock = ctypes.WinDLL(\"Ws2_32.dll\")\r\n\r\nraw_sock = win32sock.socket(AF_HYPERV, socket.SOCK_STREAM, HV_PROTOCOL_RAW)\r\nif raw_sock == -1:\r\n err = win32sock.WSAGetLastError()\r\n raise ctypes.WinError(code=err)\r\n\r\ntry:\r\n sock_addr = b\"\\x22\\x00\\x00\\x00\" + vm_id.bytes_le + HV_GUID_VM_SESSION_SERVICE_ID.bytes_le\r\n res = win32sock.connect(raw_sock, sock_addr, len(sock_addr))\r\n if res:\r\n err = win32sock.WSAGetLastError()\r\n raise ctypes.WinError(code=err)\r\n\r\n sock = socket.socket(fileno=raw_sock)\r\nexcept:\r\n win32sock.closesocket(raw_sock)\r\n raise\r\n\r\n...\r\n\r\nsock.close()\r\n```\r\n\r\nIt would be good to be able to do this instead\r\n\r\n```python\r\nimport socket\r\n\r\nsock = socket.socket(AF_HYPERV, socket.SOCK_STREAM, HV_PROTOCOL_RAW)\r\nsock_addr = b\"\\x22\\x00\\x00\\x00\" + vm_id.bytes_le + HV_GUID_VM_SESSION_SERVICE_ID.bytes_le\r\nsock.connect(sock_addr)\r\n\r\n...\r\n\r\nsock.close()\r\n```\r\n\r\nCurrently that fails due to the hardcoded check against unknown families\r\n\r\n```python\r\nTraceback (most recent call last):\r\n File \"\u003cstdin>\", line 1, in \u003cmodule>\r\nOSError: connect(): bad family\r\n```\r\n\r\nAnother option is to add support for `AF_HYPERV` on Windows and support a tuple of (vm_id, service_id) and have Python create the struct. This could be done as a separate feature request potentially.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-105267\n* gh-105398\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/jborean93","@type":"Person","name":"jborean93"},"datePublished":"2022-05-11T06:02:25Z","url":"https://github.com/python/cpython/issues/92658"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:31a9b9cf-8a6e-ffaf-8ae4-811ca9d4be78 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D310:278022:DD37F4:120D070:6AFFF9AF |
| html-safe-nonce | a72ef786c4d4f29b8562f2166809de11a92845fd559040c6f7b02ac7135e09d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzEwOjI3ODAyMjpERDM3RjQ6MTIwRDA3MDo2QUZGRjlBRiIsInZpc2l0b3JfaWQiOiI2MTI0MTA5NDQ3Mjg0MzkwMzE5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | df4e83c6fa7991c77d99c541c4a3f70397d7eea414485424c752bef53ab68108 |
| hovercard-subject-tag | issue:1232054974 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/92658/issue_layout |
| twitter:image | https://opengraph.githubassets.com/048b661604d2a1e3aeeea4cf5b3888a386726a06f14008f829eb46dcd7d0df7e/python/cpython/issues/92658 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/048b661604d2a1e3aeeea4cf5b3888a386726a06f14008f829eb46dcd7d0df7e/python/cpython/issues/92658 |
| og:image:alt | Feature or enhancement Currently socket.connect() accepts a wide range of values when connecting to a socket with the code in socketmodule.c transforming it based on the family the socket was creat... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jborean93 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width