Skip to content

Commit

Permalink
Reception of unknown packet types no longer leads to a crash (#36)
Browse files Browse the repository at this point in the history
* go.mod updated

Signed-off-by: 72nd <msg@frg72.com>

* go.mod fixed

Signed-off-by: 72nd <msg@frg72.com>

* packet unmarshal now returns error on all packet types which are currently not implemented

Signed-off-by: 72nd <msg@frg72.com>

* group all not implemented opcodes in one case statement

Signed-off-by: 72nd <msg@frg72.com>

* return wrapped error when receiving not implemented/invalid opcode

Signed-off-by: 72nd <msg@frg72.com>

---------

Signed-off-by: 72nd <msg@frg72.com>
  • Loading branch information
72nd authored Feb 1, 2024
1 parent 6247bc8 commit d52b119
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions packet/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
errInvalidPacket = errors.New("invalid Art-Net packet")
errInvalidOpCode = errors.New("invalid OpCode in packet")
errInvalidStyleCode = errors.New("invalid StyleCode in packet")
errNotImplementedOpCode = errors.New("not implemented OpCode in packet")
)

// ArtNetPacket is the interface used for passing around different kinds of ArtNet packets.
Expand Down
51 changes: 28 additions & 23 deletions packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func Unmarshal(b []byte) (p ArtNetPacket, err error) {
return
}

notImplErr := fmt.Errorf("unimplemented opcode %#v found", h.OpCode)

switch h.OpCode {
case code.OpPoll:
p = &ArtPollPacket{}
Expand All @@ -33,38 +35,41 @@ func Unmarshal(b []byte) (p ArtNetPacket, err error) {
case code.OpAddress:
p = &ArtAddressPacket{}
case code.OpInput:
case code.OpTodRequest:
case code.OpTodData:
case code.OpTodControl:
case code.OpRdm:
case code.OpRdmSub:
case code.OpMedia:
case code.OpMediaPatch:
case code.OpMediaControl:
case code.OpMediaContrlReply:
return nil, notImplErr
case code.OpTimeCode:
p = &ArtTimeCodePacket{}
case code.OpTimeSync:
case code.OpTrigger:
p = &ArtTriggerPacket{}
case code.OpDirectory:
case code.OpDirectoryReply:
case code.OpVideoSetup:
case code.OpVideoPalette:
case code.OpVideoData:
case code.OpMacMaster:
case code.OpMacSlave:
case code.OpFirmwareMaster:
case code.OpFirmwareReply:
case code.OpFileTnMaster:
case code.OpFileFnMaster:
case code.OpFileFnReply:
case code.OpIPProg:
p = &ArtIPProgPacket{}
case code.OpIPProgReply:
p = &ArtIPProgReplyPacket{}
case
code.OpDirectory,
code.OpDirectoryReply,
code.OpFileFnMaster,
code.OpFileFnReply,
code.OpFileTnMaster,
code.OpFirmwareMaster,
code.OpFirmwareReply,
code.OpMacMaster,
code.OpMacSlave,
code.OpMedia,
code.OpMediaContrlReply,
code.OpMediaControl,
code.OpMediaPatch,
code.OpRdm,
code.OpRdmSub,
code.OpTimeSync,
code.OpTodControl,
code.OpTodData,
code.OpVideoData,
code.OpVideoPalette,
code.OpVideoSetup,
code.OpTodRequest:
return nil, fmt.Errorf("%w %#v", errNotImplementedOpCode, h.OpCode)
default:
return nil, fmt.Errorf("unimplemented opcode %#v found", h.OpCode)
return nil, fmt.Errorf("%w %#v", errInvalidOpCode, h.OpCode)
}

err = p.UnmarshalBinary(b)
Expand Down

0 comments on commit d52b119

Please sign in to comment.