Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Fix limit headers and instrument stenoread.
Browse files Browse the repository at this point in the history
Change limits to int64, so we can request more than 4G of bytes.
Read limits from read headers, not (empty) write headers.
Instrument stenoread with --limit-packets and --limit-bytes flags, so you can
request limits.
  • Loading branch information
gconnell committed Feb 10, 2016
1 parent 59a4c67 commit 944dac3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
10 changes: 5 additions & 5 deletions base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func PacketsToFile(in *PacketChan, out io.Writer, limit Limit) error {
return fmt.Errorf("error writing packet: %v", err)
}
count++
if limit.ShouldStopAfter(Limit{Bytes: len(p.Data) + pcapHeaderSize, Packets: 1}) {
if limit.ShouldStopAfter(Limit{Bytes: int64(len(p.Data) + pcapHeaderSize), Packets: 1}) {
return nil
}
}
Expand Down Expand Up @@ -382,10 +382,10 @@ func Watchdog(d time.Duration, msg string) *time.Timer {
// Limit is the amount of data we want to return, or the amount taken by a
// single upload.
type Limit struct {
Bytes, Packets int
Bytes, Packets int64
}

func dec(a *int, b int) bool {
func dec(a *int64, b int64) bool {
if *a != 0 && b != 0 {
*a -= b
return *a <= 0
Expand All @@ -404,12 +404,12 @@ func (a *Limit) ShouldStopAfter(b Limit) bool {
// LimitFromHeaders returns a Limit based on HTTP headers.
func LimitFromHeaders(h http.Header) (a Limit, err error) {
if limitStr := h.Get("Steno-Limit-Bytes"); limitStr != "" {
if a.Bytes, err = strconv.Atoi(limitStr); err != nil {
if a.Bytes, err = strconv.ParseInt(limitStr, 0, 64); err != nil {
return
}
}
if limitStr := h.Get("Steno-Limit-Packets"); limitStr != "" {
if a.Packets, err = strconv.Atoi(limitStr); err != nil {
if a.Packets, err = strconv.ParseInt(limitStr, 0, 64); err != nil {
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (e *Env) handleQuery(w http.ResponseWriter, r *http.Request) {
w = httputil.Log(w, r, true)
defer log.Print(w)

limit, err := base.LimitFromHeaders(w.Header())
limit, err := base.LimitFromHeaders(r.Header)
if err != nil {
http.Error(w, "Invalid Limit Headers", http.StatusBadRequest)
return
Expand Down
31 changes: 28 additions & 3 deletions stenoread
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,37 @@ See README.md for more details on the Stenographer query language.
Set the STENOGRAPHER_CONFIG environmental variable to point to your stenographer
config if it's in a nonstandard place (defaults to /etc/stenographer/config).
$0 arguments are given before the filter. These include:
--limit-bytes X : Stop output once we've exceeded X bytes
--limit-packets X : Stop output once we've exceeded X packets
For example:
# Print first 6 packets or 2K bytes, whichever comes first,
# from source IP 1.1.1.1
$0 --limit-packets 6 --limit-bytes 2048 'host 1.1.1.1'
EOF
exit 1
fi

STENOQUERY="$1"
shift
HEADERS=""
while true; do
case "$1" in
--limit-packets)
HEADERS="$HEADERS --header Steno-Limit-Packets:$2"
shift 2
;;
--limit-bytes)
HEADERS="$HEADERS --header Steno-Limit-Bytes:$2"
shift 2
;;
*)
STENOQUERY="$1"
shift
break
;;
esac
done

TCPDUMP=$(PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin which tcpdump)
STENOCURL=$(PATH=$(dirname "$0"):$PATH which stenocurl)
Expand All @@ -46,5 +71,5 @@ echo "Running stenographer query '$STENOQUERY', piping to 'tcpdump $@'" >&2
-d "$STENOQUERY" \
--silent \
--max-time 890 \
--show-error |
--show-error $HEADERS |
"$TCPDUMP" -r /dev/stdin -s 0 "$@"
2 changes: 1 addition & 1 deletion thread/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (t *Thread) ExportDebugHandlers(mux *http.ServeMux) {
mux.HandleFunc(prefix+"/packets", func(w http.ResponseWriter, r *http.Request) {
w = httputil.Log(w, r, false)
defer log.Print(w)
limit, err := base.LimitFromHeaders(w.Header())
limit, err := base.LimitFromHeaders(r.Header)
if err != nil {
http.Error(w, "Bad limit headers", http.StatusBadRequest)
return
Expand Down

0 comments on commit 944dac3

Please sign in to comment.