2016-01-08 17:35:35 +00:00
|
|
|
package srslog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2016-02-03 20:40:19 +00:00
|
|
|
// dialerFunctionWrapper is a simple object that consists of a dialer function
|
|
|
|
// and its name. This is primarily for testing, so we can make sure that the
|
|
|
|
// getDialer method returns the correct dialer function. However, if you ever
|
|
|
|
// find that you need to check which dialer function you have, this would also
|
|
|
|
// be useful for you without having to use reflection.
|
|
|
|
type dialerFunctionWrapper struct {
|
|
|
|
Name string
|
|
|
|
Dialer func() (serverConn, string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the wrapped dialer function and return its return values.
|
|
|
|
func (df dialerFunctionWrapper) Call() (serverConn, string, error) {
|
|
|
|
return df.Dialer()
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDialer returns a "dialer" function that can be called to connect to a
|
|
|
|
// syslog server.
|
|
|
|
//
|
|
|
|
// Each dialer function is responsible for dialing the remote host and returns
|
|
|
|
// a serverConn, the hostname (or a default if the Writer has not specified a
|
|
|
|
// hostname), and an error in case dialing fails.
|
|
|
|
//
|
|
|
|
// The reason for separate dialers is that different network types may need
|
|
|
|
// to dial their connection differently, yet still provide a net.Conn interface
|
|
|
|
// that you can use once they have dialed. Rather than an increasingly long
|
|
|
|
// conditional, we have a map of network -> dialer function (with a sane default
|
|
|
|
// value), and adding a new network type is as easy as writing the dialer
|
|
|
|
// function and adding it to the map.
|
|
|
|
func (w *Writer) getDialer() dialerFunctionWrapper {
|
|
|
|
dialers := map[string]dialerFunctionWrapper{
|
|
|
|
"": dialerFunctionWrapper{"unixDialer", w.unixDialer},
|
|
|
|
"tcp+tls": dialerFunctionWrapper{"tlsDialer", w.tlsDialer},
|
2016-01-08 17:35:35 +00:00
|
|
|
}
|
|
|
|
dialer, ok := dialers[w.network]
|
|
|
|
if !ok {
|
2016-02-03 20:40:19 +00:00
|
|
|
dialer = dialerFunctionWrapper{"basicDialer", w.basicDialer}
|
2016-01-08 17:35:35 +00:00
|
|
|
}
|
|
|
|
return dialer
|
|
|
|
}
|
|
|
|
|
2016-02-03 20:40:19 +00:00
|
|
|
// unixDialer uses the unixSyslog method to open a connection to the syslog
|
|
|
|
// daemon running on the local machine.
|
|
|
|
func (w *Writer) unixDialer() (serverConn, string, error) {
|
2016-01-08 17:35:35 +00:00
|
|
|
sc, err := unixSyslog()
|
|
|
|
hostname := w.hostname
|
|
|
|
if hostname == "" {
|
|
|
|
hostname = "localhost"
|
|
|
|
}
|
|
|
|
return sc, hostname, err
|
|
|
|
}
|
|
|
|
|
2016-02-03 20:40:19 +00:00
|
|
|
// tlsDialer connects to TLS over TCP, and is used for the "tcp+tls" network
|
|
|
|
// type.
|
|
|
|
func (w *Writer) tlsDialer() (serverConn, string, error) {
|
2016-01-08 17:35:35 +00:00
|
|
|
c, err := tls.Dial("tcp", w.raddr, w.tlsConfig)
|
|
|
|
var sc serverConn
|
|
|
|
hostname := w.hostname
|
|
|
|
if err == nil {
|
|
|
|
sc = &netConn{conn: c}
|
|
|
|
if hostname == "" {
|
|
|
|
hostname = c.LocalAddr().String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sc, hostname, err
|
|
|
|
}
|
|
|
|
|
2016-02-03 20:40:19 +00:00
|
|
|
// basicDialer is the most common dialer for syslog, and supports both TCP and
|
|
|
|
// UDP connections.
|
|
|
|
func (w *Writer) basicDialer() (serverConn, string, error) {
|
2016-01-08 17:35:35 +00:00
|
|
|
c, err := net.Dial(w.network, w.raddr)
|
|
|
|
var sc serverConn
|
|
|
|
hostname := w.hostname
|
|
|
|
if err == nil {
|
|
|
|
sc = &netConn{conn: c}
|
|
|
|
if hostname == "" {
|
|
|
|
hostname = c.LocalAddr().String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sc, hostname, err
|
|
|
|
}
|