2018-09-19 09:51:32 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtmp.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
rtmp.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
|
|
|
|
Derived from librtmp under the GNU Lesser General Public License 2.1
|
|
|
|
Copyright (C) 2005-2008 Team XBMC http://www.xbmc.org
|
|
|
|
Copyright (C) 2008-2009 Andrej Stepanchuk
|
|
|
|
Copyright (C) 2009-2010 Howard Chu
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
2018-09-30 12:29:01 +03:00
|
|
|
"fmt"
|
2018-10-01 02:36:41 +03:00
|
|
|
"log"
|
2018-09-30 12:29:01 +03:00
|
|
|
"net"
|
2018-10-01 04:55:16 +03:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2018-09-19 09:51:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// int RTMP_Connect(RTMP *r, RTMPPacket* cp);
|
|
|
|
// rtmp.c +1032
|
|
|
|
func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) {
|
|
|
|
if r.Link.hostname == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:29:01 +03:00
|
|
|
var hostname string
|
2018-09-19 09:51:32 +03:00
|
|
|
if r.Link.socksport != 0 {
|
2018-09-30 12:29:01 +03:00
|
|
|
hostname = fmt.Sprintf("%s:%d", r.Link.sockshost, r.Link.socksport)
|
2018-09-19 09:51:32 +03:00
|
|
|
} else {
|
2018-09-30 12:29:01 +03:00
|
|
|
hostname = fmt.Sprintf("%s:%d", r.Link.hostname, r.Link.port)
|
|
|
|
}
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp4", hostname)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
2018-09-30 12:29:01 +03:00
|
|
|
r.m_sb.conn, err = net.DialTCP("tcp4", nil, addr)
|
|
|
|
if err != nil {
|
2018-09-19 09:51:32 +03:00
|
|
|
return false
|
|
|
|
}
|
2018-10-01 02:36:41 +03:00
|
|
|
if r.Link.socksport != 0 {
|
|
|
|
if !C_SocksNegotiate(r, addr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 09:51:32 +03:00
|
|
|
|
2018-10-01 04:55:16 +03:00
|
|
|
f, err := r.m_sb.conn.File()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get fd to set timeout: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tv := setTimeval(int(r.Link.timeout))
|
|
|
|
err = unix.SetsockoptTimeval(int(f.Fd()), unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to set timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:51:32 +03:00
|
|
|
r.m_bSendCounter = true
|
|
|
|
|
|
|
|
return C_RTMP_Connect1(r, cp)
|
|
|
|
}
|
|
|
|
|
2018-10-01 02:36:41 +03:00
|
|
|
// int SocksNegotiate(RTMP* r);
|
|
|
|
// rtmp.c +1062
|
|
|
|
func C_SocksNegotiate(r *C_RTMP, addr *net.TCPAddr) (ok bool) {
|
|
|
|
ip := addr.IP.To4()
|
|
|
|
packet := []byte{
|
|
|
|
0x4, // SOCKS version
|
|
|
|
0x1, // Establish a TCP/IP stream connection
|
|
|
|
byte(r.Link.port >> 8), byte(r.Link.port),
|
|
|
|
ip[0], ip[1], ip[2], ip[3],
|
|
|
|
0x0, // Empty user ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
C_WriteN(r, packet)
|
|
|
|
|
|
|
|
if C_ReadN(r, packet[:8]) != 8 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet[0] == 0x0 && packet[1] == 0x5a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// TODO: use new logger here
|
|
|
|
log.Println("C_SocksNegotitate: SOCKS returned error code!")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:51:32 +03:00
|
|
|
// int RTMPSockBuf_Fill(RTMPSockBuf* sb);
|
|
|
|
// rtmp.c +4253
|
|
|
|
func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int {
|
|
|
|
if sb.sb_size == 0 {
|
|
|
|
sb.sb_start = 0
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:29:01 +03:00
|
|
|
n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:])
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
2018-09-30 12:29:01 +03:00
|
|
|
sb.sb_size += n
|
|
|
|
return n
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// int RTMPSockBuf_Send(RTMPSockBuf* sb, const char* buf, int len);
|
|
|
|
// rtmp.c +4297
|
|
|
|
// TODO replace send with golang net connection send
|
|
|
|
func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) int32 {
|
2018-09-30 12:29:01 +03:00
|
|
|
n, err := sb.conn.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return int32(n)
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
|
|
|
// RTMPSockBuf_Close(RTMPSockBuf *sb)
|
|
|
|
// rtmp.c +4369
|
|
|
|
func C_RTMPSockBuf_Close(sb *C_RTMPSockBuf) int32 {
|
2018-09-30 12:29:01 +03:00
|
|
|
if sb.conn != nil {
|
|
|
|
err := sb.conn.Close()
|
|
|
|
sb.conn = nil
|
|
|
|
if err == nil {
|
|
|
|
return 1
|
|
|
|
}
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|