77 "fmt"
88 "net"
99 "os"
10+ "strings"
1011 "time"
1112
1213 "github.com/infrawatch/apputils/logging"
@@ -17,6 +18,8 @@ import (
1718
1819const (
1920 maxBufferSize = 65535
21+ udp = "udp"
22+ unix = "unix"
2023)
2124
2225var (
@@ -31,7 +34,9 @@ func rate() int64 {
3134}
3235
3336type configT struct {
34- Path string `validate:"required"`
37+ Path string `validate:"required_without=Socketaddr"`
38+ Type string
39+ Socketaddr string `validate:"required_without=Path"`
3540 DumpMessages struct {
3641 Enabled bool
3742 Path string
@@ -69,9 +74,7 @@ type Socket struct {
6974 dumpFile * os.File
7075}
7176
72- // Run implements type Transport
73- func (s * Socket ) Run (ctx context.Context , w transport.WriteFn , done chan bool ) {
74-
77+ func (s * Socket ) initUnixSocket () * net.UnixConn {
7578 var laddr net.UnixAddr
7679 laddr .Name = s .conf .Path
7780 laddr .Net = "unixgram"
@@ -80,17 +83,49 @@ func (s *Socket) Run(ctx context.Context, w transport.WriteFn, done chan bool) {
8083 pc , err := net .ListenUnixgram ("unixgram" , & laddr )
8184 if err != nil {
8285 s .logger .Errorf (err , "failed to bind unix socket %s" , laddr .Name )
83- return
86+ return nil
8487 }
8588 // create socket file if it does not exist
8689 skt , err := pc .File ()
8790 if err != nil {
8891 s .logger .Errorf (err , "failed to retrieve file handle for %s" , laddr .Name )
89- return
92+ return nil
9093 }
9194 skt .Close ()
9295
9396 s .logger .Infof ("socket listening on %s" , laddr .Name )
97+
98+ return pc
99+ }
100+
101+ func (s * Socket ) initUDPSocket () * net.UDPConn {
102+ addr , err := net .ResolveUDPAddr (udp , s .conf .Socketaddr )
103+ if err != nil {
104+ s .logger .Errorf (err , "failed to resolve udp address: %s" , s .conf .Socketaddr )
105+ return nil
106+ }
107+ pc , err := net .ListenUDP (udp , addr )
108+ if err != nil {
109+ s .logger .Errorf (err , "failed to bind udp socket to addr: %s" , s .conf .Socketaddr )
110+ return nil
111+ }
112+
113+ s .logger .Infof ("socket listening on %s" , s .conf .Socketaddr )
114+
115+ return pc
116+ }
117+
118+ // Run implements type Transport
119+ func (s * Socket ) Run (ctx context.Context , w transport.WriteFn , done chan bool ) {
120+ var pc net.Conn
121+ if s .conf .Type == udp {
122+ pc = s .initUDPSocket ()
123+ } else {
124+ pc = s .initUnixSocket ()
125+ }
126+ if pc == nil {
127+ s .logger .Errorf (nil , "Failed to initialize socket transport plugin" )
128+ }
94129 go func (maxBuffSize int64 ) {
95130 msgBuffer := make ([]byte , maxBuffSize )
96131 for {
@@ -136,14 +171,16 @@ func (s *Socket) Run(ctx context.Context, w transport.WriteFn, done chan bool) {
136171 }
137172Done:
138173 pc .Close ()
139- os .Remove (s .conf .Path )
174+ if s .conf .Type == unix {
175+ os .Remove (s .conf .Path )
176+ }
140177 s .dumpFile .Close ()
141178 s .logger .Infof ("exited" )
142179}
143180
144181// Listen ...
145182func (s * Socket ) Listen (e data.Event ) {
146- fmt .Printf ("Received event: %v\n " , e )
183+ fmt .Printf ("received event: %v\n " , e )
147184}
148185
149186// Config load configurations
@@ -155,6 +192,7 @@ func (s *Socket) Config(c []byte) error {
155192 }{
156193 Path : "/dev/stdout" ,
157194 },
195+ Type : unix ,
158196 }
159197
160198 err := config .ParseConfig (bytes .NewReader (c ), & s .conf )
@@ -171,6 +209,20 @@ func (s *Socket) Config(c []byte) error {
171209 s .dumpBuf = bufio .NewWriter (s .dumpFile )
172210 }
173211
212+ s .conf .Type = strings .ToLower (s .conf .Type )
213+ if s .conf .Type != unix && s .conf .Type != udp {
214+ return fmt .Errorf ("unable to determine socket type from configuration file. Should be either \" unix\" or \" udp\" , received: %s" ,
215+ s .conf .Type )
216+ }
217+
218+ if s .conf .Type == unix && s .conf .Path == "" {
219+ return fmt .Errorf ("the path configuration option is required when using unix socket type" )
220+ }
221+
222+ if s .conf .Type == udp && s .conf .Socketaddr == "" {
223+ return fmt .Errorf ("the socketaddr configuration option is required when using udp socket type" )
224+ }
225+
174226 return nil
175227}
176228
0 commit comments