-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
60 lines (51 loc) · 1.46 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![crate_type = "bin"]
extern crate nix;
extern crate libc;
extern crate core;
#[macro_use]
extern crate error_chain;
extern crate transform;
extern crate common;
use nix::errno::Errno;
use nix::sys::socket;
use std::os::unix::io::RawFd;
use std::net::ToSocketAddrs;
use std::ffi::CString;
use libc::{c_void, send};
use transform::encode;
use common::{MSGLEN, SOCK};
use common::errors::*;
quick_main!(|| -> Result<i32> {
let fd: Result<RawFd> = {
let fd = socket::socket(
socket::AddressFamily::Inet,
socket::SockType::Stream,
socket::SockFlag::empty(),
socket::SOL_TCP,
)?;
let address = SOCK.to_socket_addrs()?.next().expect(
"Cannot convert bind to socket address",
);
socket::connect(
fd,
&socket::SockAddr::new_inet(socket::InetAddr::from_std(&address)),
)?;
Ok(fd)
};
let fd = match fd {
Ok(fd) => fd,
Err(err) => {
bail!("Socket error: {}", err);
}
};
let version = encode(&CString::new("testver3").unwrap().into_bytes());
let body = encode(&CString::new("hifriend").unwrap().into_bytes());
let mut payload = [&version[..], &body[..]].concat();
let nsent = unsafe { send(fd, payload.as_mut_ptr() as *mut c_void, MSGLEN, 0) };
if nsent < 0 {
bail!("recv failed: {}", Errno::last());
} else {
println!("Sent: {} bytes...\n", nsent);
}
Ok(0)
});