Skip to content

Instantly share code, notes, and snippets.

@mura303
Created July 24, 2018 15:30
Show Gist options
  • Select an option

  • Save mura303/464def38cef414ece8701ea27fd3ee84 to your computer and use it in GitHub Desktop.

Select an option

Save mura303/464def38cef414ece8701ea27fd3ee84 to your computer and use it in GitHub Desktop.
#include <asio.hpp>
#include "include/stun/msg.h"
using asio::ip::udp;
class UDPClient
{
public:
UDPClient(
asio::io_service& io_service,
const std::string& host,
const std::string& port
) : io_service_(io_service), socket_(io_service, udp::endpoint(udp::v4(), 0)) {
udp::resolver resolver(io_service_);
udp::resolver::query query(udp::v4(), host, port);
udp::resolver::iterator iter = resolver.resolve(query);
endpoint_ = *iter;
}
~UDPClient()
{
socket_.close();
}
void send(const std::string& msg) {
socket_.send_to(asio::buffer(msg, msg.size()), endpoint_);
}
void sendbuf( char* buf, size_t size ){
socket_.send_to(asio::buffer(buf, size), endpoint_);
}
private:
asio::io_service& io_service_;
udp::socket socket_;
udp::endpoint endpoint_;
};
stun_msg_hdr * make_stun_request()
{
int status;
uint8_t hoge[10000];
size_t buf_len = sizeof(hoge);
uint8_t *buf = hoge;
/* Initialize the STUN message header */
stun_msg_hdr *msg_hdr = (stun_msg_hdr *)buf;
uint8_t tsx_id[12] = {
0xfd,0x95,0xe8,0x83,
0x8a,0x05,0x28,0x45,
0x6a,0x8e,0xf1,0xe2
};
stun_msg_hdr_init(msg_hdr, STUN_BINDING_REQUEST, tsx_id);
char software_name[] = "nakamud app";
/* Add a SOFTWARE attribute, which is of type variable-size */
stun_attr_varsize_add(msg_hdr, STUN_ATTR_SOFTWARE, software_name,
strlen(software_name), 0);
/* Add a PRIORITY attribute, which is of type 32-bits */
stun_attr_uint32_add(msg_hdr, STUN_ATTR_PRIORITY, 0x6e0001fful);
/* Add a ICE-CONTROLLED attribute, which is of type 64-bits */
stun_attr_uint64_add(msg_hdr, STUN_ATTR_ICE_CONTROLLED, 0x932ff9b151263b36ull);
char key[] = "hogehoge";
/* Appends a MESSAGE-INTEGRITY attribute */
stun_attr_msgint_add(msg_hdr, key, strlen(key));
/* Appends a FINGERPRINT attribute as last one */
stun_attr_fingerprint_add(msg_hdr);
/* Now, the buffer is set, send the message */
return msg_hdr;
}
int main()
{
asio::io_service io_service;
//stun.l.google.com:19302
UDPClient client(io_service, "stun.l.google.com", "19302");
stun_msg_hdr * stunmsg = make_stun_request();
client.sendbuf((char*)stunmsg, stun_msg_len(stunmsg) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment