-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsocket.rs
853 lines (786 loc) · 26.9 KB
/
socket.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use core::result;
pub type c_str = *libc::c_char;
#[nolink]
extern mod c {
fn socket(af: libc::c_int, typ: libc::c_int, protocol: libc::c_int) -> libc::c_int;
fn bind(s: libc::c_int, name: *sockaddr_storage, namelen: socklen_t) -> libc::c_int;
fn connect(s: libc::c_int, name: *sockaddr_storage, namelen: socklen_t) -> libc::c_int;
fn listen(s: libc::c_int, backlog: libc::c_int) -> libc::c_int;
fn accept(sockfd: libc::c_int, name: *sockaddr_storage, namelen: *socklen_t) -> libc::c_int;
fn send(sd: libc::c_int, buf: *u8, len: libc::c_int, flags: libc::c_int) -> libc::c_int;
fn recv(sd: libc::c_int, buf: *u8, len: libc::c_int, flags: libc::c_int) -> libc::c_int;
fn sendto(s: libc::c_int, msg: *u8, len: libc::c_int, flags: libc::c_int,
to: *sockaddr_storage, tolen: socklen_t) -> libc::c_int;
fn recvfrom(s: libc::c_int, msg: *u8, len: libc::c_int, flags: libc::c_int,
from: *sockaddr_storage, fromlen: *socklen_t) -> libc::c_int;
fn close(s: libc::c_int);
fn setsockopt(sockfd: libc::c_int, level: libc::c_int, optname: libc::c_int,
optval: *u8, optlen: socklen_t) -> libc::c_int;
fn getsockopt(sockfd: libc::c_int, level: libc::c_int, optname: libc::c_int,
optval: *u8, optlen: socklen_t) -> libc::c_int;
fn htons(hostshort: u16) -> u16;
fn htonl(hostlong: u32) -> u32;
fn ntohs(netshort: u16) -> u16;
fn ntohl(netlong: u32) -> u32;
fn inet_ntop(af: libc::c_int, src: *libc::c_void, dst: *u8, size: socklen_t) -> c_str;
fn inet_pton(af: libc::c_int, src: c_str, dst: *libc::c_void) -> libc::c_int;
fn gai_strerror(ecode: libc::c_int) -> c_str;
fn getaddrinfo(node: c_str, service: c_str, hints: *addrinfo, res: **addrinfo) -> libc::c_int;
fn freeaddrinfo(ai: *addrinfo);
}
pub static SOCK_STREAM: libc::c_int = 1_i32;
pub static SOCK_DGRAM: libc::c_int = 2_i32;
pub static SOCK_RAW: libc::c_int = 3_i32;
pub static SOL_SOCKET: libc::c_int = 0xffff_i32;
pub static SO_DEBUG: libc::c_int = 0x0001_i32; // turn on debugging info recording
pub static SO_ACCEPTCONN: libc::c_int = 0x0002_i32; // socket has had listen()
pub static SO_REUSEADDR: libc::c_int = 0x0004_i32; // allow local address reuse
pub static SO_KEEPALIVE: libc::c_int = 0x0008_i32; // keep connections alive
pub static SO_DONTROUTE: libc::c_int = 0x0010_i32; // just use interface addresses
pub static SO_BROADCAST: libc::c_int = 0x0020_i32; // permit sending of broadcast msgs
pub static SO_LINGER: libc::c_int = 0x1080_i32; // linger on close if data present (in seconds)
pub static SO_OOBINLINE: libc::c_int = 0x0100_i32; // leave received OOB data in line
pub static SO_SNDBUF: libc::c_int = 0x1001_i32; // send buffer size
pub static SO_RCVBUF: libc::c_int = 0x1002_i32; // receive buffer size
pub static SO_SNDLOWAT: libc::c_int = 0x1003_i32; // send low-water mark
pub static SO_RCVLOWAT: libc::c_int = 0x1004_i32; // receive low-water mark
pub static SO_SNDTIMEO: libc::c_int = 0x1005_i32; // send timeout
pub static SO_RCVTIMEO: libc::c_int = 0x1006_i32; // receive timeout
pub static SO_ERROR: libc::c_int = 0x1007_i32; // get error status and clear
pub static SO_TYPE : libc::c_int = 0x1008_i32; // get socket type
// TODO: there are a bunch of Linux specific socket options that should be added
pub static AF_UNSPEC: libc::c_int = 0_i32;
pub static AF_UNIX: libc::c_int = 1_i32;
pub static AF_INET: libc::c_int = 2_i32;
pub static AF_INET6: libc::c_int = 30_i32;
pub static AI_PASSIVE: libc::c_int = 0x0001_i32;
pub static AI_CANONNAME: libc::c_int = 0x0002_i32;
pub static AI_NUMERICHOST: libc::c_int = 0x0004_i32;
pub static AI_NUMERICSERV: libc::c_int = 0x1000_i32;
pub static INET6_ADDRSTRLEN: u32 = 46;
// Type names are not CamelCase to match the C versions.
pub type socklen_t = u32; // 32-bit on Mac (__darwin_socklen_t in _types.h) and Ubuntu Linux (__socklen_t in types.h)
pub type x = u8;
pub struct sockaddr_basic {
sin_family: i16,
padding: (
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x)
}
pub struct sockaddr4_in {
sin_family: i16,
sin_port: u16,
sin_addr: in4_addr,
sin_zero: (
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x, x, x)
}
pub struct in4_addr {
s_addr: libc::c_uint
}
pub struct sockaddr6_in {
sin6_family: u16,
sin6_port: u16,
sin6_flowinfo: u32,
sin6_addr: in6_addr,
sin6_scope_id: u32
}
pub struct in6_addr {
s6_addr: (
x, x, x, x,
x, x, x, x,
x, x, x, x,
x, x, x, x)
}
pub enum sockaddr {
unix(sockaddr_basic),
ipv4(sockaddr4_in),
ipv6(sockaddr6_in)
}
// TODO: think something like [u8]/128 is supported now, but not sure how to initialize it.
//
// On both Linux and Mac this struct is supposed to be 128 bytes. Rather than wrestle with
// alignment we simply make contents 128 bytes which should be fine because the C API
// always uses pointers to sockaddr_storage.
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "macos")]
pub struct sockaddr_storage {
ss_len: u8,
ss_family: u8,
contents: (u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8)
}
#[cfg(target_os = "linux")]
pub struct sockaddr_storage {
ss_family: libc::c_ushort,
contents: (u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8,
u8, u8, u8, u8)
}
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "win32")]
#[cfg(target_os = "macos")]
pub struct addrinfo {
ai_flags: libc::c_int,
ai_family: libc::c_int,
ai_socktype: libc::c_int,
ai_protocol: libc::c_int,
ai_addrlen: socklen_t,
ai_canonname: *u8,
ai_addr: *sockaddr_storage,
ai_next: *u8
} //XXX ai_next should be *addrinfo
#[cfg(target_os = "linux")]
pub struct addrinfo {
ai_flags: libc::c_int,
ai_family: libc::c_int,
ai_socktype: libc::c_int,
ai_protocol: libc::c_int,
ai_addrlen: socklen_t,
ai_addr: *sockaddr_storage,
ai_canonname: *u8,
ai_next: *u8
} //XXX ai_next should be *addrinfo
pub fn sockaddr_to_string(saddr: &sockaddr) -> ~str
{
unsafe
{
match *saddr
{
unix(_basic) =>
{
~"unix" // TODO: is sockaddr_basic supposed to be a sockaddr_un?
}
ipv4(addr4) =>
{
let buffer = vec::from_elem(INET6_ADDRSTRLEN as uint + 1u, 0u8);
c::inet_ntop(
AF_INET,
cast::reinterpret_cast(&ptr::addr_of(&addr4.sin_addr)),
vec::raw::to_ptr(buffer),
INET6_ADDRSTRLEN);
str::raw::from_buf(vec::raw::to_ptr(buffer))
}
ipv6(addr6) =>
{
let buffer = vec::from_elem(INET6_ADDRSTRLEN as uint + 1u, 0u8);
c::inet_ntop(
AF_INET6,
cast::reinterpret_cast(&ptr::addr_of(&addr6.sin6_addr)),
vec::raw::to_ptr(buffer),
INET6_ADDRSTRLEN);
str::raw::from_buf(vec::raw::to_ptr(buffer))
}
}
}
}
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "macos")]
pub fn mk_default_storage() -> sockaddr_storage
{
sockaddr_storage {
ss_len: 0u8,
ss_family: 0u8,
contents: (
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0)
}
}
#[cfg(target_os = "linux")]
pub fn mk_default_storage() -> sockaddr_storage
{
sockaddr_storage {
ss_family: 0,
contents: (
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0)
}
}
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "win32")]
#[cfg(target_os = "macos")]
pub fn mk_default_addrinfo() -> addrinfo
{
addrinfo {
ai_flags: 0i32,
ai_family: 0i32,
ai_socktype: 0i32,
ai_protocol: 0i32,
ai_addrlen: 0u32,
ai_canonname: ptr::null(),
ai_addr: ptr::null(),
ai_next: ptr::null()
}
}
#[cfg(target_os = "linux")]
pub fn mk_default_addrinfo() -> addrinfo
{
addrinfo {
ai_flags: 0i32,
ai_family: 0i32,
ai_socktype: 0i32,
ai_protocol: 0i32,
ai_addrlen: 0u32,
ai_addr: ptr::null(),
ai_canonname: ptr::null(),
ai_next: ptr::null()
}
}
pub unsafe fn getaddrinfo(host: &str, port: u16, f: &fn(a: addrinfo) -> bool) -> Option<~str>
{
let mut hints: addrinfo = mk_default_addrinfo();
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
let servinfo: *addrinfo = ptr::null();
let s_port = fmt!("%u", port as uint);
let mut result: Option<~str> = None;
do str::as_c_str(host) |host| {
do str::as_c_str(s_port) |port| {
let status = c::getaddrinfo(host, port, ptr::addr_of(&hints),
ptr::addr_of(&servinfo));
if status == 0i32 {
let mut p = servinfo;
while p != ptr::null() {
if !f(*p) {
break;
}
p = cast::reinterpret_cast(&(*p).ai_next);
}
} else {
warn!("getaddrinfo returned %? (%s)", status, str::raw::from_c_str(c::gai_strerror(status)));
result = Some(~"getaddrinfo failed");
}
}
}
c::freeaddrinfo(servinfo);
result.clone()
}
pub fn inet_ntop(address: &addrinfo) -> ~str
{
unsafe {
let buffer = vec::from_elem(INET6_ADDRSTRLEN as uint + 1u, 0u8);
c::inet_ntop(address.ai_family,
if address.ai_family == AF_INET {
let addr: *sockaddr4_in = cast::reinterpret_cast(&address.ai_addr);
cast::reinterpret_cast(&ptr::addr_of(&(*addr).sin_addr))
} else {
let addr: *sockaddr6_in = cast::reinterpret_cast(&address.ai_addr);
cast::reinterpret_cast(&ptr::addr_of(&(*addr).sin6_addr))
},
vec::raw::to_ptr(buffer), INET6_ADDRSTRLEN);
str::raw::from_buf(vec::raw::to_ptr(buffer))
}
}
// TODO: there is no portable way to get errno from rust so, for now, we'll just write them to stderr
// See #2269.
pub fn log_err(mesg: &str)
{
unsafe {
do str::as_c_str(mesg) |buffer| {libc::perror(buffer)};
}
}
// TODO: Isn't socket::socket_handle redundant?
pub struct socket_handle {
sockfd: libc::c_int,
}
impl Drop for socket_handle {
fn finalize(&self)
{
unsafe { c::close(self.sockfd); }
}
}
pub fn socket_handle(x: libc::c_int) -> socket_handle
{
socket_handle {sockfd: x}
}
pub unsafe fn bind_socket(host: &str, port: u16) -> result::Result<@socket_handle, ~str>
{
let err = for getaddrinfo(host, port) |ai| {
if ai.ai_family == AF_INET || ai.ai_family == AF_INET6 // TODO: should do something to support AF_UNIX
{
let sockfd = c::socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol);
if sockfd != -1_i32 {
let val = 1;
let _ = c::setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, // this shouldn't be critical so we'll ignore errors from it
cast::reinterpret_cast(&ptr::addr_of(&val)),
sys::size_of::<int>() as socklen_t);
if c::bind(sockfd, ai.ai_addr, ai.ai_addrlen) == -1_i32 {
c::close(sockfd);
} else {
debug!(" bound to socket %?", sockfd);
return result::Ok(@socket_handle(sockfd));
}
} else {
log_err(fmt!("socket(%s) error", inet_ntop(&ai)));
}
}
};
match err
{
option::Some(mesg) => {result::Err(copy(mesg))}
option::None => {result::Err(~"bind failed to find an address")}
}
}
pub unsafe fn connect(host: &str, port: u16) -> result::Result<@socket_handle, ~str>
{
info!("connecting to %s:%?", host, port);
let err = for getaddrinfo(host, port) |ai| {
if ai.ai_family == AF_INET || ai.ai_family == AF_INET6 // TODO: should do something to support AF_UNIX
{
debug!(" trying %s", inet_ntop(&ai));
let sockfd = c::socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol);
if sockfd != -1_i32 {
if c::connect(sockfd, ai.ai_addr, ai.ai_addrlen) == -1_i32 {
c::close(sockfd);
} else {
info!(" connected to socket %?", sockfd);
return result::Ok(@socket_handle(sockfd));
}
} else {
log_err(fmt!("socket(%s, %?) error", host, port));
}
}
};
match err
{
option::Some(mesg) => {result::Err(copy(mesg))}
option::None => {result::Err(~"connect failed to find an address")}
}
}
pub fn listen(sock: @socket_handle, backlog: i32) -> result::Result<@socket_handle, ~str>
{
unsafe {
if c::listen(sock.sockfd, backlog) == -1_i32 {
log_err(~"listen error");
result::Err(~"listen failed")
} else {
result::Ok(sock)
}
}
}
// Returns a fd to allow multi-threaded servers to send the fd to a task.
pub struct accept_socket {
fd: libc::c_int,
remote_addr: ~str
}
pub unsafe fn accept(sock: @socket_handle) -> result::Result<accept_socket, ~str>
{
info!("accepting with socket %?", sock.sockfd);
let addr = mk_default_storage();
let unused: socklen_t = sys::size_of::<sockaddr>() as socklen_t;
let fd = c::accept(sock.sockfd, ptr::addr_of(&addr), ptr::addr_of(&unused));
if fd == -1_i32 {
log_err(fmt!("accept error"));
result::Err(~"accept failed")
} else {
let their_addr = if addr.ss_family as u8 == AF_INET as u8 {
ipv4(*(ptr::addr_of(&addr) as *sockaddr4_in))
} else if addr.ss_family as u8 == AF_INET6 as u8 {
ipv6(*(ptr::addr_of(&addr) as *sockaddr6_in))
} else {
unix(*(ptr::addr_of(&addr) as *sockaddr_basic))
};
info!("accepted socket %? (%s)", fd, sockaddr_to_string(&their_addr));
result::Ok(accept_socket{
fd: fd,
remote_addr: sockaddr_to_string(&their_addr)
})
}
}
pub unsafe fn send(sock: @socket_handle, buf: &[u8]) -> result::Result<uint, ~str>
{
let amt = c::send(sock.sockfd, vec::raw::to_ptr(buf),
vec::len(buf) as libc::c_int, 0i32);
if amt == -1_i32 {
log_err(fmt!("send error"));
result::Err(~"send failed")
} else {
result::Ok(amt as uint)
}
}
// Useful for sending str data (where you want to use as_buf instead of as_buffer).
pub unsafe fn send_buf(sock: @socket_handle, buf: *u8, len: uint) -> result::Result<uint, ~str>
{
let amt = c::send(sock.sockfd, buf, len as libc::c_int, 0i32);
if amt == -1_i32 {
log_err(fmt!("send error"));
result::Err(~"send_buf failed")
} else {
result::Ok(amt as uint)
}
}
pub struct recv_buffer {
buffer: ~[u8],
bytes: uint
}
pub unsafe fn recv(sock: @socket_handle, len: uint) -> result::Result<~recv_buffer, ~str>
{
let buf = vec::from_elem(len + 1u, 0u8);
let bytes = c::recv(sock.sockfd, vec::raw::to_ptr(buf), len as libc::c_int, 0i32);
if bytes == -1_i32 {
log_err(fmt!("recv error"));
result::Err(~"recv failed")
} else {
result::Ok(~recv_buffer {
buffer: buf,
bytes: bytes as uint}
)
}
}
pub unsafe fn sendto(sock: @socket_handle, buf: &[u8], to: &sockaddr)
-> result::Result<uint, ~str>
{
let (to_saddr, to_len) = match *to {
ipv4(s) => { (*(ptr::addr_of(&s) as *sockaddr_storage),
sys::size_of::<sockaddr4_in>()) }
ipv6(s) => { (*(ptr::addr_of(&s) as *sockaddr_storage),
sys::size_of::<sockaddr6_in>()) }
unix(s) => { (*(ptr::addr_of(&s) as *sockaddr_storage),
sys::size_of::<sockaddr_basic>()) }
};
let amt = c::sendto(sock.sockfd, vec::raw::to_ptr(buf), vec::len(buf) as libc::c_int, 0i32,
ptr::addr_of(&to_saddr), to_len as u32);
if amt == -1_i32 {
log_err(fmt!("sendto error"));
result::Err(~"sendto failed")
} else {
result::Ok(amt as uint)
}
}
pub unsafe fn recvfrom(sock: @socket_handle, len: uint)
-> result::Result<(~[u8], uint, sockaddr), ~str>
{
let from_saddr = mk_default_storage();
let unused: socklen_t = 0u32;
let buf = vec::from_elem(len + 1u, 0u8);
let amt = c::recvfrom(sock.sockfd, vec::raw::to_ptr(buf), vec::len(buf) as libc::c_int, 0i32,
ptr::addr_of(&from_saddr), ptr::addr_of(&unused));
if amt == -1_i32 {
log_err(fmt!("recvfrom error"));
result::Err(~"recvfrom failed")
} else {
result::Ok((buf.clone(), amt as uint,
if from_saddr.ss_family as u8 == AF_INET as u8 {
ipv4(*(ptr::addr_of(&from_saddr) as *sockaddr4_in))
} else if from_saddr.ss_family as u8 == AF_INET6 as u8 {
ipv6(*(ptr::addr_of(&from_saddr) as *sockaddr6_in))
} else {
unix(*(ptr::addr_of(&from_saddr) as *sockaddr_basic))
}))
}
}
pub unsafe fn setsockopt(sock: @socket_handle, option: int, value: int)
-> result::Result<libc::c_int, ~str>
{
let val = value;
let r = c::setsockopt(sock.sockfd, SOL_SOCKET, option as libc::c_int,
cast::reinterpret_cast(&ptr::addr_of(&val)),
sys::size_of::<int>() as socklen_t);
if r == -1_i32 {
log_err(fmt!("setsockopt error"));
result::Err(~"setsockopt failed")
} else {
result::Ok(r)
}
}
pub unsafe fn enablesockopt(sock: @socket_handle, option: int)
-> result::Result<libc::c_int, ~str>
{
setsockopt(sock, option, 1)
}
pub unsafe fn disablesockopt(sock: @socket_handle, option: int)
-> result::Result<libc::c_int, ~str>
{
setsockopt(sock, option, 0)
}
pub fn htons(hostshort: u16) -> u16
{
unsafe { c::htons(hostshort) }
}
pub fn htonl(hostlong: u32) -> u32
{
unsafe { c::htonl(hostlong) }
}
pub fn ntohs(netshort: u16) -> u16
{
unsafe { c::ntohs(netshort) }
}
pub fn ntohl(netlong: u32) -> u32
{
unsafe {c::ntohl(netlong) }
}
#[test]
fn test_server_client()
{
unsafe {
fn run_client(test_str: &str, port: u16)
{
let ts = test_str.to_owned();
unsafe {
do task::spawn {
unsafe {
match connect(~"localhost", port) {
result::Ok(handle) => {
let f: &fn(*u8, uint) -> result::Result<uint, ~str> = |
buf, _len| {
send_buf(handle, buf, ts.len())
};
let res = str::as_buf(ts, f);
assert!(result::is_ok(&res));
}
result::Err(err) => {
error!("Error %s connecting", err);
assert!(false);
}
}
}
}
}
}
unsafe fn run_server(test_str: &str, s: @socket_handle)
{
match accept(s)
{
result::Ok(args) =>
{
if !str::eq(&~"127.0.0.1", &args.remote_addr) && !str::eq(&~"::1", &args.remote_addr)
{
error!("Expected 127.0.0.1 or ::1 for remote addr but found %s", args.remote_addr);
assert!(false)
}
let c = @socket_handle(args.fd);
match recv(c, 1024u)
{
result::Ok(res) =>
{
assert!(res.bytes == str::len(test_str));
assert!(vec::slice(res.buffer, 0u, res.bytes) == str::to_bytes(test_str));
}
result::Err(err) =>
{
error!("Error %s with recv", err);
assert!(false);
}
}
}
result::Err(err) =>
{
error!("Error %s accepting", err);
assert!(false);
}
}
}
info!("---- test_server_client ------------------------");
let port = 48006u16;
let test_str = ~"testing";
match bind_socket(~"localhost", port)
{
result::Ok(s) =>
{
match listen(s, 1i32)
{
result::Ok(s) =>
{
run_client(test_str, port);
run_server(test_str, s);
}
result::Err(err) =>
{
error!("Error %s listening", err);
assert!(false);
}
}
}
result::Err(err) =>
{
error!("Error %s binding", err);
assert!(false);
}
}
}
}
#[test]
fn test_getaddrinfo_localhost()
{
info!("---- test_getaddrinfo_localhost ------------------------");
let mut hints = mk_default_addrinfo();
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
let servinfo: *addrinfo = ptr::null();
let port = 48007u16;
unsafe {
do str::as_c_str(~"localhost") |host| {
do str::as_c_str(fmt!("%u", port as uint)) |p| {
let status = c::getaddrinfo(host, p, ptr::addr_of(&hints), ptr::addr_of(&servinfo));
assert!(status == 0_i32);
unsafe {
assert!(servinfo != ptr::null());
let p = *servinfo;
let ipstr = inet_ntop(&p);
assert!(str::eq(&~"127.0.0.1", &ipstr) || str::eq(&~"::1", &ipstr));
}
c::freeaddrinfo(servinfo)
}
}
}
}
pub unsafe fn getaddrinfo2(host: &str, service: &str, f: &fn(a: addrinfo) -> bool) -> Option<~str>
{
let mut hints = mk_default_addrinfo();
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
let servinfo: *addrinfo = ptr::null();
let mut result = option::None;
do str::as_c_str(host) |host| {
do str::as_c_str(service) |port| {
let status = c::getaddrinfo(host, port, ptr::addr_of(&hints),
ptr::addr_of(&servinfo));
if status == 0i32 {
let mut p = servinfo;
while p != ptr::null() {
if !f(*p) {
break;
}
p = cast::reinterpret_cast(&(*p).ai_next);
}
} else {
warn!("getaddrinfo returned %? (%s)", status, str::raw::from_c_str(c::gai_strerror(status)));
result = option::Some(~"getaddrinfo failed");
}
}
}
c::freeaddrinfo(servinfo);
result
}
//pub fn getaddrinfo(host: &str, port: u16, f: fn(addrinfo) -> bool) -> Option<~str> unsafe {
//pub fn mk_default_addrinfo() -> addrinfo {
// {ai_flags: 0i32, ai_family: 0i32, ai_socktype: 0i32, ai_protocol: 0i32, ai_addrlen: 0u32,
// ai_canonname: ptr::null(), ai_addr: ptr::null(), ai_next: ptr::null()}
//}