|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package dgram |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "net" |
| 24 | + "runtime" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/elastic/beats/v7/filebeat/inputsource" |
| 28 | + "github.com/elastic/beats/v7/libbeat/logp" |
| 29 | +) |
| 30 | + |
| 31 | +// HandlerFactory returns a ConnectionHandler func |
| 32 | +type HandlerFactory func(config ListenerConfig) ConnectionHandler |
| 33 | + |
| 34 | +// ConnectionHandler is able to read from incoming connections. |
| 35 | +type ConnectionHandler func(context.Context, net.PacketConn) error |
| 36 | + |
| 37 | +// MetadataFunc defines callback executed when a line is read from the split handler. |
| 38 | +type MetadataFunc func(net.Conn) inputsource.NetworkMetadata |
| 39 | + |
| 40 | +// DatagramReaderFactory allows creation of a handler which can read packets from connections. |
| 41 | +func DatagramReaderFactory( |
| 42 | + family inputsource.Family, |
| 43 | + logger *logp.Logger, |
| 44 | + callback inputsource.NetworkFunc, |
| 45 | +) HandlerFactory { |
| 46 | + return func(config ListenerConfig) ConnectionHandler { |
| 47 | + return ConnectionHandler(func(ctx context.Context, conn net.PacketConn) error { |
| 48 | + for ctx.Err() == nil { |
| 49 | + |
| 50 | + buffer := make([]byte, config.MaxMessageSize) |
| 51 | + //conn.SetDeadline(time.Now().Add(config.Timeout)) |
| 52 | + |
| 53 | + // If you are using Windows and you are using a fixed buffer and you get a datagram which |
| 54 | + // is bigger than the specified size of the buffer, it will return an `err` and the buffer will |
| 55 | + // contains a subset of the data. |
| 56 | + // |
| 57 | + // On Unix based system, the buffer will be truncated but no error will be returned. |
| 58 | + length, addr, err := conn.ReadFrom(buffer) |
| 59 | + if err != nil { |
| 60 | + if family == inputsource.FamilyUnix { |
| 61 | + fmt.Println("connection handler error", err) |
| 62 | + } |
| 63 | + // don't log any deadline events. |
| 64 | + e, ok := err.(net.Error) |
| 65 | + if ok && e.Timeout() { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + // Closed network error string will never change in Go 1.X |
| 70 | + // https://github.com/golang/go/issues/4373 |
| 71 | + opErr, ok := err.(*net.OpError) |
| 72 | + if ok && strings.Contains(opErr.Err.Error(), "use of closed network connection") { |
| 73 | + logger.Info("Connection has been closed") |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + logger.Errorf("Error reading from the socket %s", err) |
| 78 | + |
| 79 | + // On Windows send the current buffer and mark it as truncated. |
| 80 | + // The buffer will have content but length will return 0, addr will be nil. |
| 81 | + if family == inputsource.FamilyUDP && isLargerThanBuffer(err) { |
| 82 | + callback(buffer, inputsource.NetworkMetadata{RemoteAddr: addr, Truncated: true}) |
| 83 | + continue |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if length > 0 { |
| 88 | + callback(buffer[:length], inputsource.NetworkMetadata{RemoteAddr: addr}) |
| 89 | + } |
| 90 | + } |
| 91 | + fmt.Println("end of connection handling") |
| 92 | + return nil |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func isLargerThanBuffer(err error) bool { |
| 98 | + if runtime.GOOS != "windows" { |
| 99 | + return false |
| 100 | + } |
| 101 | + return strings.Contains(err.Error(), windowErrBuffer) |
| 102 | +} |
0 commit comments