PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 22, 2022

[FIXED] How to connect to remote address with UdpSocket?

 October 22, 2022     https, rust, sockets, udp     No comments   

Issue

I'd like to send an HTTPS request over UDP to a server on the internet (where I don't know if the server supports HTTPS over UDP). As a toy example, right now, I'm trying to send a GET request to http://httpbin.org/get. How do I do this?

Right now, I'm doing this in Rust, and my code looks like this:

use std::net::ToSocketAddrs;

fn main() {
    let socket = std::net::UdpSocket::bind("127.0.0.1:34254").unwrap();
    let remote_addr: std::net::SocketAddr = "httpbin.org:80".to_socket_addrs().unwrap().next().unwrap();
    socket.connect(remote_addr).unwrap();

    let msg = "GET /get HTTP/1.1\r\nHost: httpbin.org\r\naccept: application/json\r\n\r\n\r\n";
    socket.send(msg.as_bytes()).unwrap();
    let mut buf = [0; 5000];
    let num_bytes = socket.recv(&mut buf).unwrap();
    println!("recieved {} bytes: {:?}", num_bytes, std::str::from_utf8(&buf[..num_bytes]));
}

However, when I run the code I get the following output (this happens on socket.connect(...)):

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" }', ...

Is what I'm doing even possible? Or am I doing it wrong?


Solution

As Finomnis pointed out, there are several questions here so I will choose to answer only one of them - namely how to fix the error you are experiencing.

Your issue comes from binding the UDP socket on 127.0.0.1. Since this is a local address, the socket will only bind to the local interface and you will be able to communicate with local hosts only. As you want to connect to a remote server, you should instead bind to 0.0.0.0, which is a shorthand for all network interfaces:

let socket = std::net::UdpSocket::bind("0.0.0.0:34254").unwrap();

With this change the code no longer panics, but instead hangs on socket.recv(). Since it's unlikely that httpbin.org is ready to respond to this request, you will probably never get a response.

PS: When saying HTTPS over UDP, you might be thinking of HTTP/3 which is indeed based on UDP, but is a much more complicated protocol that HTTP/1.



Answered By - apilat
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing