Added command line argument to extract payload

This commit is contained in:
Christopher Ramey 2019-10-06 10:51:41 -08:00 committed by Christopher Ramey
parent 0cb656e659
commit fe886ef655

View File

@ -5,69 +5,94 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"flag"
) )
func main() { func main() {
if len(os.Args) < 2 { flags := flag.NewFlagSet("sdb", flag.ExitOnError)
fmt.Fprintf(os.Stderr, flags.SetOutput(os.Stdout)
"required argument missing\n") flags.Usage = func() {
fmt.Fprintf(flags.Output(),
"Usage: %s [OPTION] <files> ...\nOptions:\n",
os.Args[0])
flags.PrintDefaults()
}
plonly := flags.Bool("payload", false, "only return the payload")
flags.Parse(os.Args[1:])
if flags.NArg() < 1 {
fmt.Fprintf(os.Stderr, "files parameter cannot be empty\n")
os.Exit(1) os.Exit(1)
} }
raw, err := ioutil.ReadFile(os.Args[1]) for _, fn := range flags.Args() {
if err != nil { raw, err := ioutil.ReadFile(fn)
fmt.Fprintf(os.Stderr, if err != nil {
"error reading \"%s\": %s\n", fmt.Fprintf(os.Stderr,
os.Args[1], err.Error()) "error reading \"%s\": %s\n",
} fn, err.Error())
continue
data, err := sdb.ParseSDB(raw)
if err != nil {
fmt.Fprintf(os.Stderr,
"%s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Protocol Revision: %d\n",
data.ProtocolRevision)
fmt.Printf("Message Length: %d\n",
data.Length)
for c, i := range data.Elements {
if c > 0 {
fmt.Println()
} }
switch v := i.(type) {
case sdb.MOHeader:
fmt.Printf("\tMO Header IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Header Length: %d\n",
v.Length)
fmt.Printf("\tMO Header CDR Reference: %d\n",
v.CDRReference)
fmt.Printf("\tMO Header IMEI: %s\n",
v.IMEI)
fmt.Printf("\tMO Header Status: %s (%d)\n",
v.StatusString(), v.Status)
fmt.Printf("\tMO Header MOMSN: %d\n",
v.MOMSN)
fmt.Printf("\tMO Header MTMSN: %d\n",
v.MTMSN)
fmt.Printf("\tMO Header Session Time: %s\n",
v.SessionTime().String())
case sdb.MOPayload: data, err := sdb.ParseSDB(raw)
fmt.Printf("\tMO Payload IEI: %d\n", if err != nil {
v.IEI) fmt.Fprintf(os.Stderr,
fmt.Printf("\tMO Payload Length: %d\n", "%s\n", err.Error())
v.Length) continue
}
case sdb.MOConfirmation: if *plonly {
fmt.Printf("\tMO Confirmation IEI: %d\n", for _, i := range data.Elements {
v.IEI) switch v := i.(type) {
fmt.Printf("\tMO Confirmation Length: %d\n", case sdb.MOPayload:
v.Length) os.Stdout.Write(v.Payload)
fmt.Printf("\tMO Confirmation Status: %d\n", }
v.Status) }
} else {
fmt.Printf("Protocol Revision: %d\n",
data.ProtocolRevision)
fmt.Printf("Message Length: %d\n",
data.Length)
for c, i := range data.Elements {
if c > 0 {
fmt.Println()
}
switch v := i.(type) {
case sdb.MOHeader:
fmt.Printf("\tMO Header IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Header Length: %d\n",
v.Length)
fmt.Printf("\tMO Header CDR Reference: %d\n",
v.CDRReference)
fmt.Printf("\tMO Header IMEI: %s\n",
v.IMEI)
fmt.Printf("\tMO Header Status: %s (%d)\n",
v.StatusString(), v.Status)
fmt.Printf("\tMO Header MOMSN: %d\n",
v.MOMSN)
fmt.Printf("\tMO Header MTMSN: %d\n",
v.MTMSN)
fmt.Printf("\tMO Header Session Time: %s\n",
v.SessionTime().String())
case sdb.MOPayload:
fmt.Printf("\tMO Payload IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Payload Length: %d\n",
v.Length)
case sdb.MOConfirmation:
fmt.Printf("\tMO Confirmation IEI: %d\n",
v.IEI)
fmt.Printf("\tMO Confirmation Length: %d\n",
v.Length)
fmt.Printf("\tMO Confirmation Status: %d\n",
v.Status)
}
}
} }
} }
} }