| 1 |
// Copyright 2019 Wataru Ishida. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
// implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package sctp |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"encoding/binary" |
| 21 |
"fmt" |
| 22 |
"net" |
| 23 |
"strconv" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"sync/atomic" |
| 27 |
"syscall" |
| 28 |
"time" |
| 29 |
"unsafe" |
| 30 |
) |
| 31 |
|
| 32 |
const ( |
| 33 |
SOL_SCTP = 132 |
| 34 |
|
| 35 |
SCTP_BINDX_ADD_ADDR = 0x01 |
| 36 |
SCTP_BINDX_REM_ADDR = 0x02 |
| 37 |
|
| 38 |
MSG_NOTIFICATION = 0x8000 |
| 39 |
) |
| 40 |
|
| 41 |
const ( |
| 42 |
SCTP_RTOINFO = iota |
| 43 |
SCTP_ASSOCINFO |
| 44 |
SCTP_INITMSG |
| 45 |
SCTP_NODELAY |
| 46 |
SCTP_AUTOCLOSE |
| 47 |
SCTP_SET_PEER_PRIMARY_ADDR |
| 48 |
SCTP_PRIMARY_ADDR |
| 49 |
SCTP_ADAPTATION_LAYER |
| 50 |
SCTP_DISABLE_FRAGMENTS |
| 51 |
SCTP_PEER_ADDR_PARAMS |
| 52 |
SCTP_DEFAULT_SENT_PARAM |
| 53 |
SCTP_EVENTS |
| 54 |
SCTP_I_WANT_MAPPED_V4_ADDR |
| 55 |
SCTP_MAXSEG |
| 56 |
SCTP_STATUS |
| 57 |
SCTP_GET_PEER_ADDR_INFO |
| 58 |
SCTP_DELAYED_ACK_TIME |
| 59 |
SCTP_DELAYED_ACK = SCTP_DELAYED_ACK_TIME |
| 60 |
SCTP_DELAYED_SACK = SCTP_DELAYED_ACK_TIME |
| 61 |
|
| 62 |
SCTP_SOCKOPT_BINDX_ADD = 100 |
| 63 |
SCTP_SOCKOPT_BINDX_REM = 101 |
| 64 |
SCTP_SOCKOPT_PEELOFF = 102 |
| 65 |
SCTP_GET_PEER_ADDRS = 108 |
| 66 |
SCTP_GET_LOCAL_ADDRS = 109 |
| 67 |
SCTP_SOCKOPT_CONNECTX = 110 |
| 68 |
SCTP_SOCKOPT_CONNECTX3 = 111 |
| 69 |
) |
| 70 |
|
| 71 |
const ( |
| 72 |
SCTP_EVENT_DATA_IO = 1 << iota |
| 73 |
SCTP_EVENT_ASSOCIATION |
| 74 |
SCTP_EVENT_ADDRESS |
| 75 |
SCTP_EVENT_SEND_FAILURE |
| 76 |
SCTP_EVENT_PEER_ERROR |
| 77 |
SCTP_EVENT_SHUTDOWN |
| 78 |
SCTP_EVENT_PARTIAL_DELIVERY |
| 79 |
SCTP_EVENT_ADAPTATION_LAYER |
| 80 |
SCTP_EVENT_AUTHENTICATION |
| 81 |
SCTP_EVENT_SENDER_DRY |
| 82 |
|
| 83 |
SCTP_EVENT_ALL = SCTP_EVENT_DATA_IO | SCTP_EVENT_ASSOCIATION | SCTP_EVENT_ADDRESS | SCTP_EVENT_SEND_FAILURE | SCTP_EVENT_PEER_ERROR | SCTP_EVENT_SHUTDOWN | SCTP_EVENT_PARTIAL_DELIVERY | SCTP_EVENT_ADAPTATION_LAYER | SCTP_EVENT_AUTHENTICATION | SCTP_EVENT_SENDER_DRY |
| 84 |
) |
| 85 |
|
| 86 |
type SCTPNotificationType int |
| 87 |
|
| 88 |
const ( |
| 89 |
SCTP_SN_TYPE_BASE = SCTPNotificationType(iota + (1 << 15)) |
| 90 |
SCTP_ASSOC_CHANGE |
| 91 |
SCTP_PEER_ADDR_CHANGE |
| 92 |
SCTP_SEND_FAILED |
| 93 |
SCTP_REMOTE_ERROR |
| 94 |
SCTP_SHUTDOWN_EVENT |
| 95 |
SCTP_PARTIAL_DELIVERY_EVENT |
| 96 |
SCTP_ADAPTATION_INDICATION |
| 97 |
SCTP_AUTHENTICATION_INDICATION |
| 98 |
SCTP_SENDER_DRY_EVENT |
| 99 |
) |
| 100 |
|
| 101 |
type NotificationHandler func([]byte) error |
| 102 |
|
| 103 |
type EventSubscribe struct { |
| 104 |
DataIO uint8 |
| 105 |
Association uint8 |
| 106 |
Address uint8 |
| 107 |
SendFailure uint8 |
| 108 |
PeerError uint8 |
| 109 |
Shutdown uint8 |
| 110 |
PartialDelivery uint8 |
| 111 |
AdaptationLayer uint8 |
| 112 |
Authentication uint8 |
| 113 |
SenderDry uint8 |
| 114 |
} |
| 115 |
|
| 116 |
const ( |
| 117 |
SCTP_CMSG_INIT = iota |
| 118 |
SCTP_CMSG_SNDRCV |
| 119 |
SCTP_CMSG_SNDINFO |
| 120 |
SCTP_CMSG_RCVINFO |
| 121 |
SCTP_CMSG_NXTINFO |
| 122 |
) |
| 123 |
|
| 124 |
const ( |
| 125 |
SCTP_UNORDERED = 1 << iota |
| 126 |
SCTP_ADDR_OVER |
| 127 |
SCTP_ABORT |
| 128 |
SCTP_SACK_IMMEDIATELY |
| 129 |
SCTP_EOF |
| 130 |
) |
| 131 |
|
| 132 |
const ( |
| 133 |
SCTP_MAX_STREAM = 0xffff |
| 134 |
) |
| 135 |
|
| 136 |
type InitMsg struct { |
| 137 |
NumOstreams uint16 |
| 138 |
MaxInstreams uint16 |
| 139 |
MaxAttempts uint16 |
| 140 |
MaxInitTimeout uint16 |
| 141 |
} |
| 142 |
|
| 143 |
type SndRcvInfo struct { |
| 144 |
Stream uint16 |
| 145 |
SSN uint16 |
| 146 |
Flags uint16 |
| 147 |
_ uint16 |
| 148 |
PPID uint32 |
| 149 |
Context uint32 |
| 150 |
TTL uint32 |
| 151 |
TSN uint32 |
| 152 |
CumTSN uint32 |
| 153 |
AssocID int32 |
| 154 |
} |
| 155 |
|
| 156 |
type SndInfo struct { |
| 157 |
SID uint16 |
| 158 |
Flags uint16 |
| 159 |
PPID uint32 |
| 160 |
Context uint32 |
| 161 |
AssocID int32 |
| 162 |
} |
| 163 |
|
| 164 |
type GetAddrsOld struct { |
| 165 |
AssocID int32 |
| 166 |
AddrNum int32 |
| 167 |
Addrs uintptr |
| 168 |
} |
| 169 |
|
| 170 |
type NotificationHeader struct { |
| 171 |
Type uint16 |
| 172 |
Flags uint16 |
| 173 |
Length uint32 |
| 174 |
} |
| 175 |
|
| 176 |
type SCTPState uint16 |
| 177 |
|
| 178 |
const ( |
| 179 |
SCTP_COMM_UP = SCTPState(iota) |
| 180 |
SCTP_COMM_LOST |
| 181 |
SCTP_RESTART |
| 182 |
SCTP_SHUTDOWN_COMP |
| 183 |
SCTP_CANT_STR_ASSOC |
| 184 |
) |
| 185 |
|
| 186 |
var nativeEndian binary.ByteOrder |
| 187 |
var sndRcvInfoSize uintptr |
| 188 |
|
| 189 |
func init() { |
| 190 |
i := uint16(1) |
| 191 |
if *(*byte)(unsafe.Pointer(&i)) == 0 { |
| 192 |
nativeEndian = binary.BigEndian |
| 193 |
} else { |
| 194 |
nativeEndian = binary.LittleEndian |
| 195 |
} |
| 196 |
info := SndRcvInfo{} |
| 197 |
sndRcvInfoSize = unsafe.Sizeof(info) |
| 198 |
} |
| 199 |
|
| 200 |
func toBuf(v interface{}) []byte { |
| 201 |
var buf bytes.Buffer |
| 202 |
binary.Write(&buf, nativeEndian, v) |
| 203 |
return buf.Bytes() |
| 204 |
} |
| 205 |
|
| 206 |
func htons(h uint16) uint16 { |
| 207 |
if nativeEndian == binary.LittleEndian { |
| 208 |
return (h << 8 & 0xff00) | (h >> 8 & 0xff) |
| 209 |
} |
| 210 |
return h |
| 211 |
} |
| 212 |
|
| 213 |
var ntohs = htons |
| 214 |
|
| 215 |
// setInitOpts sets options for an SCTP association initialization |
| 216 |
// see https://tools.ietf.org/html/rfc4960#page-25 |
| 217 |
func setInitOpts(fd int, options InitMsg) error { |
| 218 |
optlen := unsafe.Sizeof(options) |
| 219 |
_, _, err := setsockopt(fd, SCTP_INITMSG, uintptr(unsafe.Pointer(&options)), uintptr(optlen)) |
| 220 |
return err |
| 221 |
} |
| 222 |
|
| 223 |
func setNumOstreams(fd, num int) error { |
| 224 |
return setInitOpts(fd, InitMsg{NumOstreams: uint16(num)}) |
| 225 |
} |
| 226 |
|
| 227 |
type SCTPAddr struct { |
| 228 |
IPAddrs []net.IPAddr |
| 229 |
Port int |
| 230 |
} |
| 231 |
|
| 232 |
func (a *SCTPAddr) ToRawSockAddrBuf() []byte { |
| 233 |
p := htons(uint16(a.Port)) |
| 234 |
if len(a.IPAddrs) == 0 { // if a.IPAddrs list is empty - fall back to IPv4 zero addr |
| 235 |
s := syscall.RawSockaddrInet4{ |
| 236 |
Family: syscall.AF_INET, |
| 237 |
Port: p, |
| 238 |
} |
| 239 |
copy(s.Addr[:], net.IPv4zero) |
| 240 |
return toBuf(s) |
| 241 |
} |
| 242 |
buf := []byte{} |
| 243 |
for _, ip := range a.IPAddrs { |
| 244 |
ipBytes := ip.IP |
| 245 |
if len(ipBytes) == 0 { |
| 246 |
ipBytes = net.IPv4zero |
| 247 |
} |
| 248 |
if ip4 := ipBytes.To4(); ip4 != nil { |
| 249 |
s := syscall.RawSockaddrInet4{ |
| 250 |
Family: syscall.AF_INET, |
| 251 |
Port: p, |
| 252 |
} |
| 253 |
copy(s.Addr[:], ip4) |
| 254 |
buf = append(buf, toBuf(s)...) |
| 255 |
} else { |
| 256 |
var scopeid uint32 |
| 257 |
ifi, err := net.InterfaceByName(ip.Zone) |
| 258 |
if err == nil { |
| 259 |
scopeid = uint32(ifi.Index) |
| 260 |
} |
| 261 |
s := syscall.RawSockaddrInet6{ |
| 262 |
Family: syscall.AF_INET6, |
| 263 |
Port: p, |
| 264 |
Scope_id: scopeid, |
| 265 |
} |
| 266 |
copy(s.Addr[:], ipBytes) |
| 267 |
buf = append(buf, toBuf(s)...) |
| 268 |
} |
| 269 |
} |
| 270 |
return buf |
| 271 |
} |
| 272 |
|
| 273 |
func (a *SCTPAddr) String() string { |
| 274 |
var b bytes.Buffer |
| 275 |
|
| 276 |
for n, i := range a.IPAddrs { |
| 277 |
if i.IP.To4() != nil { |
| 278 |
b.WriteString(i.String()) |
| 279 |
} else if i.IP.To16() != nil { |
| 280 |
b.WriteRune('[') |
| 281 |
b.WriteString(i.String()) |
| 282 |
b.WriteRune(']') |
| 283 |
} |
| 284 |
if n < len(a.IPAddrs)-1 { |
| 285 |
b.WriteRune('/') |
| 286 |
} |
| 287 |
} |
| 288 |
b.WriteRune(':') |
| 289 |
b.WriteString(strconv.Itoa(a.Port)) |
| 290 |
return b.String() |
| 291 |
} |
| 292 |
|
| 293 |
func (a *SCTPAddr) Network() string { return "sctp" } |
| 294 |
|
| 295 |
func ResolveSCTPAddr(network, addrs string) (*SCTPAddr, error) { |
| 296 |
tcpnet := "" |
| 297 |
switch network { |
| 298 |
case "", "sctp": |
| 299 |
tcpnet = "tcp" |
| 300 |
case "sctp4": |
| 301 |
tcpnet = "tcp4" |
| 302 |
case "sctp6": |
| 303 |
tcpnet = "tcp6" |
| 304 |
default: |
| 305 |
return nil, fmt.Errorf("invalid net: %s", network) |
| 306 |
} |
| 307 |
elems := strings.Split(addrs, "/") |
| 308 |
if len(elems) == 0 { |
| 309 |
return nil, fmt.Errorf("invalid input: %s", addrs) |
| 310 |
} |
| 311 |
ipaddrs := make([]net.IPAddr, 0, len(elems)) |
| 312 |
for _, e := range elems[:len(elems)-1] { |
| 313 |
tcpa, err := net.ResolveTCPAddr(tcpnet, e+":") |
| 314 |
if err != nil { |
| 315 |
return nil, err |
| 316 |
} |
| 317 |
ipaddrs = append(ipaddrs, net.IPAddr{IP: tcpa.IP, Zone: tcpa.Zone}) |
| 318 |
} |
| 319 |
tcpa, err := net.ResolveTCPAddr(tcpnet, elems[len(elems)-1]) |
| 320 |
if err != nil { |
| 321 |
return nil, err |
| 322 |
} |
| 323 |
if tcpa.IP != nil { |
| 324 |
ipaddrs = append(ipaddrs, net.IPAddr{IP: tcpa.IP, Zone: tcpa.Zone}) |
| 325 |
} else { |
| 326 |
ipaddrs = nil |
| 327 |
} |
| 328 |
return &SCTPAddr{ |
| 329 |
IPAddrs: ipaddrs, |
| 330 |
Port: tcpa.Port, |
| 331 |
}, nil |
| 332 |
} |
| 333 |
|
| 334 |
func SCTPConnect(fd int, addr *SCTPAddr) (int, error) { |
| 335 |
buf := addr.ToRawSockAddrBuf() |
| 336 |
param := GetAddrsOld{ |
| 337 |
AddrNum: int32(len(buf)), |
| 338 |
Addrs: uintptr(uintptr(unsafe.Pointer(&buf[0]))), |
| 339 |
} |
| 340 |
optlen := unsafe.Sizeof(param) |
| 341 |
_, _, err := getsockopt(fd, SCTP_SOCKOPT_CONNECTX3, uintptr(unsafe.Pointer(¶m)), uintptr(unsafe.Pointer(&optlen))) |
| 342 |
if err == nil { |
| 343 |
return int(param.AssocID), nil |
| 344 |
} else if err != syscall.ENOPROTOOPT { |
| 345 |
return 0, err |
| 346 |
} |
| 347 |
r0, _, err := setsockopt(fd, SCTP_SOCKOPT_CONNECTX, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf))) |
| 348 |
return int(r0), err |
| 349 |
} |
| 350 |
|
| 351 |
func SCTPBind(fd int, addr *SCTPAddr, flags int) error { |
| 352 |
var option uintptr |
| 353 |
switch flags { |
| 354 |
case SCTP_BINDX_ADD_ADDR: |
| 355 |
option = SCTP_SOCKOPT_BINDX_ADD |
| 356 |
case SCTP_BINDX_REM_ADDR: |
| 357 |
option = SCTP_SOCKOPT_BINDX_REM |
| 358 |
default: |
| 359 |
return syscall.EINVAL |
| 360 |
} |
| 361 |
|
| 362 |
buf := addr.ToRawSockAddrBuf() |
| 363 |
_, _, err := setsockopt(fd, option, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf))) |
| 364 |
return err |
| 365 |
} |
| 366 |
|
| 367 |
type SCTPConn struct { |
| 368 |
_fd int32 |
| 369 |
notificationHandler NotificationHandler |
| 370 |
} |
| 371 |
|
| 372 |
func (c *SCTPConn) fd() int { |
| 373 |
return int(atomic.LoadInt32(&c._fd)) |
| 374 |
} |
| 375 |
|
| 376 |
func NewSCTPConn(fd int, handler NotificationHandler) *SCTPConn { |
| 377 |
conn := &SCTPConn{ |
| 378 |
_fd: int32(fd), |
| 379 |
notificationHandler: handler, |
| 380 |
} |
| 381 |
return conn |
| 382 |
} |
| 383 |
|
| 384 |
func (c *SCTPConn) Write(b []byte) (int, error) { |
| 385 |
return c.SCTPWrite(b, nil) |
| 386 |
} |
| 387 |
|
| 388 |
func (c *SCTPConn) Read(b []byte) (int, error) { |
| 389 |
n, _, err := c.SCTPRead(b) |
| 390 |
if n < 0 { |
| 391 |
n = 0 |
| 392 |
} |
| 393 |
return n, err |
| 394 |
} |
| 395 |
|
| 396 |
func (c *SCTPConn) SetInitMsg(numOstreams, maxInstreams, maxAttempts, maxInitTimeout int) error { |
| 397 |
return setInitOpts(c.fd(), InitMsg{ |
| 398 |
NumOstreams: uint16(numOstreams), |
| 399 |
MaxInstreams: uint16(maxInstreams), |
| 400 |
MaxAttempts: uint16(maxAttempts), |
| 401 |
MaxInitTimeout: uint16(maxInitTimeout), |
| 402 |
}) |
| 403 |
} |
| 404 |
|
| 405 |
func (c *SCTPConn) SubscribeEvents(flags int) error { |
| 406 |
var d, a, ad, sf, p, sh, pa, ada, au, se uint8 |
| 407 |
if flags&SCTP_EVENT_DATA_IO > 0 { |
| 408 |
d = 1 |
| 409 |
} |
| 410 |
if flags&SCTP_EVENT_ASSOCIATION > 0 { |
| 411 |
a = 1 |
| 412 |
} |
| 413 |
if flags&SCTP_EVENT_ADDRESS > 0 { |
| 414 |
ad = 1 |
| 415 |
} |
| 416 |
if flags&SCTP_EVENT_SEND_FAILURE > 0 { |
| 417 |
sf = 1 |
| 418 |
} |
| 419 |
if flags&SCTP_EVENT_PEER_ERROR > 0 { |
| 420 |
p = 1 |
| 421 |
} |
| 422 |
if flags&SCTP_EVENT_SHUTDOWN > 0 { |
| 423 |
sh = 1 |
| 424 |
} |
| 425 |
if flags&SCTP_EVENT_PARTIAL_DELIVERY > 0 { |
| 426 |
pa = 1 |
| 427 |
} |
| 428 |
if flags&SCTP_EVENT_ADAPTATION_LAYER > 0 { |
| 429 |
ada = 1 |
| 430 |
} |
| 431 |
if flags&SCTP_EVENT_AUTHENTICATION > 0 { |
| 432 |
au = 1 |
| 433 |
} |
| 434 |
if flags&SCTP_EVENT_SENDER_DRY > 0 { |
| 435 |
se = 1 |
| 436 |
} |
| 437 |
param := EventSubscribe{ |
| 438 |
DataIO: d, |
| 439 |
Association: a, |
| 440 |
Address: ad, |
| 441 |
SendFailure: sf, |
| 442 |
PeerError: p, |
| 443 |
Shutdown: sh, |
| 444 |
PartialDelivery: pa, |
| 445 |
AdaptationLayer: ada, |
| 446 |
Authentication: au, |
| 447 |
SenderDry: se, |
| 448 |
} |
| 449 |
optlen := unsafe.Sizeof(param) |
| 450 |
_, _, err := setsockopt(c.fd(), SCTP_EVENTS, uintptr(unsafe.Pointer(¶m)), uintptr(optlen)) |
| 451 |
return err |
| 452 |
} |
| 453 |
|
| 454 |
func (c *SCTPConn) SubscribedEvents() (int, error) { |
| 455 |
param := EventSubscribe{} |
| 456 |
optlen := unsafe.Sizeof(param) |
| 457 |
_, _, err := getsockopt(c.fd(), SCTP_EVENTS, uintptr(unsafe.Pointer(¶m)), uintptr(unsafe.Pointer(&optlen))) |
| 458 |
if err != nil { |
| 459 |
return 0, err |
| 460 |
} |
| 461 |
var flags int |
| 462 |
if param.DataIO > 0 { |
| 463 |
flags |= SCTP_EVENT_DATA_IO |
| 464 |
} |
| 465 |
if param.Association > 0 { |
| 466 |
flags |= SCTP_EVENT_ASSOCIATION |
| 467 |
} |
| 468 |
if param.Address > 0 { |
| 469 |
flags |= SCTP_EVENT_ADDRESS |
| 470 |
} |
| 471 |
if param.SendFailure > 0 { |
| 472 |
flags |= SCTP_EVENT_SEND_FAILURE |
| 473 |
} |
| 474 |
if param.PeerError > 0 { |
| 475 |
flags |= SCTP_EVENT_PEER_ERROR |
| 476 |
} |
| 477 |
if param.Shutdown > 0 { |
| 478 |
flags |= SCTP_EVENT_SHUTDOWN |
| 479 |
} |
| 480 |
if param.PartialDelivery > 0 { |
| 481 |
flags |= SCTP_EVENT_PARTIAL_DELIVERY |
| 482 |
} |
| 483 |
if param.AdaptationLayer > 0 { |
| 484 |
flags |= SCTP_EVENT_ADAPTATION_LAYER |
| 485 |
} |
| 486 |
if param.Authentication > 0 { |
| 487 |
flags |= SCTP_EVENT_AUTHENTICATION |
| 488 |
} |
| 489 |
if param.SenderDry > 0 { |
| 490 |
flags |= SCTP_EVENT_SENDER_DRY |
| 491 |
} |
| 492 |
return flags, nil |
| 493 |
} |
| 494 |
|
| 495 |
func (c *SCTPConn) SetDefaultSentParam(info *SndRcvInfo) error { |
| 496 |
optlen := unsafe.Sizeof(*info) |
| 497 |
_, _, err := setsockopt(c.fd(), SCTP_DEFAULT_SENT_PARAM, uintptr(unsafe.Pointer(info)), uintptr(optlen)) |
| 498 |
return err |
| 499 |
} |
| 500 |
|
| 501 |
func (c *SCTPConn) GetDefaultSentParam() (*SndRcvInfo, error) { |
| 502 |
info := &SndRcvInfo{} |
| 503 |
optlen := unsafe.Sizeof(*info) |
| 504 |
_, _, err := getsockopt(c.fd(), SCTP_DEFAULT_SENT_PARAM, uintptr(unsafe.Pointer(info)), uintptr(unsafe.Pointer(&optlen))) |
| 505 |
return info, err |
| 506 |
} |
| 507 |
|
| 508 |
func (c *SCTPConn) Getsockopt(optname, optval, optlen uintptr) (uintptr, uintptr, error) { |
| 509 |
return getsockopt(c.fd(), optname, optval, optlen) |
| 510 |
} |
| 511 |
|
| 512 |
func (c *SCTPConn) Setsockopt(optname, optval, optlen uintptr) (uintptr, uintptr, error) { |
| 513 |
return setsockopt(c.fd(), optname, optval, optlen) |
| 514 |
} |
| 515 |
|
| 516 |
func resolveFromRawAddr(ptr unsafe.Pointer, n int) (*SCTPAddr, error) { |
| 517 |
addr := &SCTPAddr{ |
| 518 |
IPAddrs: make([]net.IPAddr, n), |
| 519 |
} |
| 520 |
|
| 521 |
switch family := (*(*syscall.RawSockaddrAny)(ptr)).Addr.Family; family { |
| 522 |
case syscall.AF_INET: |
| 523 |
addr.Port = int(ntohs(uint16((*(*syscall.RawSockaddrInet4)(ptr)).Port))) |
| 524 |
tmp := syscall.RawSockaddrInet4{} |
| 525 |
size := unsafe.Sizeof(tmp) |
| 526 |
for i := 0; i < n; i++ { |
| 527 |
a := *(*syscall.RawSockaddrInet4)(unsafe.Pointer( |
| 528 |
uintptr(ptr) + size*uintptr(i))) |
| 529 |
addr.IPAddrs[i] = net.IPAddr{IP: a.Addr[:]} |
| 530 |
} |
| 531 |
case syscall.AF_INET6: |
| 532 |
addr.Port = int(ntohs(uint16((*(*syscall.RawSockaddrInet4)(ptr)).Port))) |
| 533 |
tmp := syscall.RawSockaddrInet6{} |
| 534 |
size := unsafe.Sizeof(tmp) |
| 535 |
for i := 0; i < n; i++ { |
| 536 |
a := *(*syscall.RawSockaddrInet6)(unsafe.Pointer( |
| 537 |
uintptr(ptr) + size*uintptr(i))) |
| 538 |
var zone string |
| 539 |
ifi, err := net.InterfaceByIndex(int(a.Scope_id)) |
| 540 |
if err == nil { |
| 541 |
zone = ifi.Name |
| 542 |
} |
| 543 |
addr.IPAddrs[i] = net.IPAddr{IP: a.Addr[:], Zone: zone} |
| 544 |
} |
| 545 |
default: |
| 546 |
return nil, fmt.Errorf("unknown address family: %d", family) |
| 547 |
} |
| 548 |
return addr, nil |
| 549 |
} |
| 550 |
|
| 551 |
func sctpGetAddrs(fd, id, optname int) (*SCTPAddr, error) { |
| 552 |
|
| 553 |
type getaddrs struct { |
| 554 |
assocId int32 |
| 555 |
addrNum uint32 |
| 556 |
addrs [4096]byte |
| 557 |
} |
| 558 |
param := getaddrs{ |
| 559 |
assocId: int32(id), |
| 560 |
} |
| 561 |
optlen := unsafe.Sizeof(param) |
| 562 |
_, _, err := getsockopt(fd, uintptr(optname), uintptr(unsafe.Pointer(¶m)), uintptr(unsafe.Pointer(&optlen))) |
| 563 |
if err != nil { |
| 564 |
return nil, err |
| 565 |
} |
| 566 |
return resolveFromRawAddr(unsafe.Pointer(¶m.addrs), int(param.addrNum)) |
| 567 |
} |
| 568 |
|
| 569 |
func (c *SCTPConn) SCTPGetPrimaryPeerAddr() (*SCTPAddr, error) { |
| 570 |
|
| 571 |
type sctpGetSetPrim struct { |
| 572 |
assocId int32 |
| 573 |
addrs [128]byte |
| 574 |
} |
| 575 |
param := sctpGetSetPrim{ |
| 576 |
assocId: int32(0), |
| 577 |
} |
| 578 |
optlen := unsafe.Sizeof(param) |
| 579 |
_, _, err := getsockopt(c.fd(), SCTP_PRIMARY_ADDR, uintptr(unsafe.Pointer(¶m)), uintptr(unsafe.Pointer(&optlen))) |
| 580 |
if err != nil { |
| 581 |
return nil, err |
| 582 |
} |
| 583 |
return resolveFromRawAddr(unsafe.Pointer(¶m.addrs), 1) |
| 584 |
} |
| 585 |
|
| 586 |
func (c *SCTPConn) SCTPLocalAddr(id int) (*SCTPAddr, error) { |
| 587 |
return sctpGetAddrs(c.fd(), id, SCTP_GET_LOCAL_ADDRS) |
| 588 |
} |
| 589 |
|
| 590 |
func (c *SCTPConn) SCTPRemoteAddr(id int) (*SCTPAddr, error) { |
| 591 |
return sctpGetAddrs(c.fd(), id, SCTP_GET_PEER_ADDRS) |
| 592 |
} |
| 593 |
|
| 594 |
func (c *SCTPConn) LocalAddr() net.Addr { |
| 595 |
addr, err := sctpGetAddrs(c.fd(), 0, SCTP_GET_LOCAL_ADDRS) |
| 596 |
if err != nil { |
| 597 |
return nil |
| 598 |
} |
| 599 |
return addr |
| 600 |
} |
| 601 |
|
| 602 |
func (c *SCTPConn) RemoteAddr() net.Addr { |
| 603 |
addr, err := sctpGetAddrs(c.fd(), 0, SCTP_GET_PEER_ADDRS) |
| 604 |
if err != nil { |
| 605 |
return nil |
| 606 |
} |
| 607 |
return addr |
| 608 |
} |
| 609 |
|
| 610 |
func (c *SCTPConn) PeelOff(id int) (*SCTPConn, error) { |
| 611 |
type peeloffArg struct { |
| 612 |
assocId int32 |
| 613 |
sd int |
| 614 |
} |
| 615 |
param := peeloffArg{ |
| 616 |
assocId: int32(id), |
| 617 |
} |
| 618 |
optlen := unsafe.Sizeof(param) |
| 619 |
_, _, err := getsockopt(c.fd(), SCTP_SOCKOPT_PEELOFF, uintptr(unsafe.Pointer(¶m)), uintptr(unsafe.Pointer(&optlen))) |
| 620 |
if err != nil { |
| 621 |
return nil, err |
| 622 |
} |
| 623 |
return &SCTPConn{_fd: int32(param.sd)}, nil |
| 624 |
} |
| 625 |
|
| 626 |
func (c *SCTPConn) SetDeadline(t time.Time) error { |
| 627 |
return syscall.EOPNOTSUPP |
| 628 |
} |
| 629 |
|
| 630 |
func (c *SCTPConn) SetReadDeadline(t time.Time) error { |
| 631 |
return syscall.EOPNOTSUPP |
| 632 |
} |
| 633 |
|
| 634 |
func (c *SCTPConn) SetWriteDeadline(t time.Time) error { |
| 635 |
return syscall.EOPNOTSUPP |
| 636 |
} |
| 637 |
|
| 638 |
type SCTPListener struct { |
| 639 |
fd int |
| 640 |
m sync.Mutex |
| 641 |
} |
| 642 |
|
| 643 |
func (ln *SCTPListener) Addr() net.Addr { |
| 644 |
laddr, err := sctpGetAddrs(ln.fd, 0, SCTP_GET_LOCAL_ADDRS) |
| 645 |
if err != nil { |
| 646 |
return nil |
| 647 |
} |
| 648 |
return laddr |
| 649 |
} |
| 650 |
|
| 651 |
type SCTPSndRcvInfoWrappedConn struct { |
| 652 |
conn *SCTPConn |
| 653 |
} |
| 654 |
|
| 655 |
func NewSCTPSndRcvInfoWrappedConn(conn *SCTPConn) *SCTPSndRcvInfoWrappedConn { |
| 656 |
conn.SubscribeEvents(SCTP_EVENT_DATA_IO) |
| 657 |
return &SCTPSndRcvInfoWrappedConn{conn} |
| 658 |
} |
| 659 |
|
| 660 |
func (c *SCTPSndRcvInfoWrappedConn) Write(b []byte) (int, error) { |
| 661 |
if len(b) < int(sndRcvInfoSize) { |
| 662 |
return 0, syscall.EINVAL |
| 663 |
} |
| 664 |
info := (*SndRcvInfo)(unsafe.Pointer(&b[0])) |
| 665 |
n, err := c.conn.SCTPWrite(b[sndRcvInfoSize:], info) |
| 666 |
return n + int(sndRcvInfoSize), err |
| 667 |
} |
| 668 |
|
| 669 |
func (c *SCTPSndRcvInfoWrappedConn) Read(b []byte) (int, error) { |
| 670 |
if len(b) < int(sndRcvInfoSize) { |
| 671 |
return 0, syscall.EINVAL |
| 672 |
} |
| 673 |
n, info, err := c.conn.SCTPRead(b[sndRcvInfoSize:]) |
| 674 |
if err != nil { |
| 675 |
return n, err |
| 676 |
} |
| 677 |
copy(b, toBuf(info)) |
| 678 |
return n + int(sndRcvInfoSize), err |
| 679 |
} |
| 680 |
|
| 681 |
func (c *SCTPSndRcvInfoWrappedConn) Close() error { |
| 682 |
return c.conn.Close() |
| 683 |
} |
| 684 |
|
| 685 |
func (c *SCTPSndRcvInfoWrappedConn) LocalAddr() net.Addr { |
| 686 |
return c.conn.LocalAddr() |
| 687 |
} |
| 688 |
|
| 689 |
func (c *SCTPSndRcvInfoWrappedConn) RemoteAddr() net.Addr { |
| 690 |
return c.conn.RemoteAddr() |
| 691 |
} |
| 692 |
|
| 693 |
func (c *SCTPSndRcvInfoWrappedConn) SetDeadline(t time.Time) error { |
| 694 |
return c.conn.SetDeadline(t) |
| 695 |
} |
| 696 |
|
| 697 |
func (c *SCTPSndRcvInfoWrappedConn) SetReadDeadline(t time.Time) error { |
| 698 |
return c.conn.SetReadDeadline(t) |
| 699 |
} |
| 700 |
|
| 701 |
func (c *SCTPSndRcvInfoWrappedConn) SetWriteDeadline(t time.Time) error { |
| 702 |
return c.conn.SetWriteDeadline(t) |
| 703 |
} |
| 704 |
|
| 705 |
func (c *SCTPSndRcvInfoWrappedConn) SetWriteBuffer(bytes int) error { |
| 706 |
return c.conn.SetWriteBuffer(bytes) |
| 707 |
} |
| 708 |
|
| 709 |
func (c *SCTPSndRcvInfoWrappedConn) GetWriteBuffer() (int, error) { |
| 710 |
return c.conn.GetWriteBuffer() |
| 711 |
} |
| 712 |
|
| 713 |
func (c *SCTPSndRcvInfoWrappedConn) SetReadBuffer(bytes int) error { |
| 714 |
return c.conn.SetReadBuffer(bytes) |
| 715 |
} |
| 716 |
|
| 717 |
func (c *SCTPSndRcvInfoWrappedConn) GetReadBuffer() (int, error) { |
| 718 |
return c.conn.GetReadBuffer() |
| 719 |
} |
| 720 |
|
| 721 |
// SocketConfig contains options for the SCTP socket. |
| 722 |
type SocketConfig struct { |
| 723 |
// If Control is not nil it is called after the socket is created but before |
| 724 |
// it is bound or connected. |
| 725 |
Control func(network, address string, c syscall.RawConn) error |
| 726 |
|
| 727 |
// InitMsg is the options to send in the initial SCTP message |
| 728 |
InitMsg InitMsg |
| 729 |
} |
| 730 |
|
| 731 |
func (cfg *SocketConfig) Listen(net string, laddr *SCTPAddr) (*SCTPListener, error) { |
| 732 |
return listenSCTPExtConfig(net, laddr, cfg.InitMsg, cfg.Control) |
| 733 |
} |
| 734 |
|
| 735 |
func (cfg *SocketConfig) Dial(net string, laddr, raddr *SCTPAddr) (*SCTPConn, error) { |
| 736 |
return dialSCTPExtConfig(net, laddr, raddr, cfg.InitMsg, cfg.Control) |
| 737 |
} |