added support for tcp and unix sockets

This commit is contained in:
Remco
2014-11-13 18:04:50 +01:00
parent 403d850150
commit 1bb7549e07
2 changed files with 31 additions and 6 deletions

View File

@@ -28,8 +28,9 @@ package clamd
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"io" "io"
"log"
"net/url"
"strings" "strings"
) )
@@ -47,13 +48,28 @@ type Stats struct {
var EICAR = []byte(`X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*`) var EICAR = []byte(`X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*`)
func (c *Clamd) newConnection() (*CLAMDConn, error) { func (c *Clamd) newConnection() (conn *CLAMDConn, err error) {
conn, err := newCLAMDUnixConn(c.address)
return conn, err var u *url.URL
if u, err = url.Parse(c.address); err != nil {
return
}
switch u.Scheme {
case "tcp":
conn, err = newCLAMDTcpConn(u.Host)
case "unix":
conn, err = newCLAMDUnixConn(u.Path)
default:
conn, err = newCLAMDUnixConn(c.address)
}
return
} }
func (c *Clamd) simpleCommand(command string) (chan string, error) { func (c *Clamd) simpleCommand(command string) (chan string, error) {
conn, err := newCLAMDUnixConn(c.address) conn, err := c.newConnection()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -110,6 +110,15 @@ func (c *CLAMDConn) readResponse() (chan string, sync.WaitGroup, error) {
return ch, wg, nil return ch, wg, nil
} }
func newCLAMDTcpConn(address string) (*CLAMDConn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, err
}
return &CLAMDConn{Conn: conn}, err
}
func newCLAMDUnixConn(address string) (*CLAMDConn, error) { func newCLAMDUnixConn(address string) (*CLAMDConn, error) {
conn, err := net.Dial("unix", address) conn, err := net.Dial("unix", address)
if err != nil { if err != nil {