Skip to content

Commit

Permalink
Enable linters (#45)
Browse files Browse the repository at this point in the history
* Enable several linters

* Apply `gci` to files

* Apply `gofumpt` to files

* Enable `whitespace` linter

* Enable `predeclared`

* Remove some unnecessary conversions
  • Loading branch information
gab-arrobo authored Jun 11, 2024
1 parent 8d891fe commit 9cc4edf
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 153 deletions.
14 changes: 12 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,24 @@ linters:
- ineffassign
- typecheck
# Additional
- lll
# - lll
- godox
# - gomnd
#- maligned
#- nestif
#- goconst
- goconst
#- gocognit
- nakedret
- gci
- misspell
- gofumpt
- whitespace
# - unconvert
- predeclared
- noctx
- dogsled
- asciicheck

#disable-all: false
fast: true
issues:
Expand Down
25 changes: 13 additions & 12 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"os"
"time"

formatter "github.com/antonfisher/nested-logrus-formatter"
"github.com/omec-project/util/logger"
"github.com/omec-project/util/logger_conf"

formatter "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
)

var log *logrus.Logger
var NasLog *logrus.Entry
var NasMsgLog *logrus.Entry
var ConvertLog *logrus.Entry
var SecurityLog *logrus.Entry
var (
log *logrus.Logger
NasLog *logrus.Entry
NasMsgLog *logrus.Entry
ConvertLog *logrus.Entry
SecurityLog *logrus.Entry
)

func init() {
log = logrus.New()
Expand All @@ -34,12 +35,12 @@ func init() {
FieldsOrder: []string{"component", "category"},
}

free5gcLogHook, err := logger.NewFileHook(logger_conf.Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
free5gcLogHook, err := logger.NewFileHook(logger_conf.Free5gcLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
if err == nil {
log.Hooks.Add(free5gcLogHook)
}

selfLogHook, err := logger.NewFileHook(logger_conf.LibLogDir+"nas.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
selfLogHook, err := logger.NewFileHook(logger_conf.LibLogDir+"nas.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
if err == nil {
log.Hooks.Add(selfLogHook)
}
Expand All @@ -55,7 +56,7 @@ func SetLogLevel(level logrus.Level) {
log.SetLevel(level)
}

func SetReportCaller(bool bool) {
NasLog.Infoln("set report call :", bool)
log.SetReportCaller(bool)
func SetReportCaller(set bool) {
NasLog.Infoln("set report call :", set)
log.SetReportCaller(set)
}
92 changes: 46 additions & 46 deletions nas.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,35 +113,35 @@ func GetSecurityHeaderType(byteArray []byte) uint8 {

type GmmMessage struct {
GmmHeader
*nasMessage.AuthenticationRequest //8.2.1
*nasMessage.AuthenticationResponse //8.2.2
*nasMessage.AuthenticationResult //8.2.3
*nasMessage.AuthenticationFailure //8.2.4
*nasMessage.AuthenticationReject //8.2.5
*nasMessage.RegistrationRequest //8.2.6
*nasMessage.RegistrationAccept //8.2.7
*nasMessage.RegistrationComplete //8.2.8
*nasMessage.RegistrationReject //8.2.9
*nasMessage.ULNASTransport //8.2.10
*nasMessage.DLNASTransport //8.2.11
*nasMessage.DeregistrationRequestUEOriginatingDeregistration //8.2.12
*nasMessage.DeregistrationAcceptUEOriginatingDeregistration //8.2.13
*nasMessage.DeregistrationRequestUETerminatedDeregistration //8.2.14
*nasMessage.DeregistrationAcceptUETerminatedDeregistration //8.2.15
*nasMessage.ServiceRequest //8.2.16
*nasMessage.ServiceAccept //8.2.17
*nasMessage.ServiceReject //8.2.18
*nasMessage.ConfigurationUpdateCommand //8.2.19
*nasMessage.ConfigurationUpdateComplete //8.2.20
*nasMessage.IdentityRequest //8.2.21
*nasMessage.IdentityResponse //8.2.22
*nasMessage.Notification //8.2.23
*nasMessage.NotificationResponse //8.2.24
*nasMessage.SecurityModeCommand //8.2.25
*nasMessage.SecurityModeComplete //8.2.26
*nasMessage.SecurityModeReject //8.2.27
*nasMessage.SecurityProtected5GSNASMessage //8.2.28
*nasMessage.Status5GMM //8.2.29
*nasMessage.AuthenticationRequest // 8.2.1
*nasMessage.AuthenticationResponse // 8.2.2
*nasMessage.AuthenticationResult // 8.2.3
*nasMessage.AuthenticationFailure // 8.2.4
*nasMessage.AuthenticationReject // 8.2.5
*nasMessage.RegistrationRequest // 8.2.6
*nasMessage.RegistrationAccept // 8.2.7
*nasMessage.RegistrationComplete // 8.2.8
*nasMessage.RegistrationReject // 8.2.9
*nasMessage.ULNASTransport // 8.2.10
*nasMessage.DLNASTransport // 8.2.11
*nasMessage.DeregistrationRequestUEOriginatingDeregistration // 8.2.12
*nasMessage.DeregistrationAcceptUEOriginatingDeregistration // 8.2.13
*nasMessage.DeregistrationRequestUETerminatedDeregistration // 8.2.14
*nasMessage.DeregistrationAcceptUETerminatedDeregistration // 8.2.15
*nasMessage.ServiceRequest // 8.2.16
*nasMessage.ServiceAccept // 8.2.17
*nasMessage.ServiceReject // 8.2.18
*nasMessage.ConfigurationUpdateCommand // 8.2.19
*nasMessage.ConfigurationUpdateComplete // 8.2.20
*nasMessage.IdentityRequest // 8.2.21
*nasMessage.IdentityResponse // 8.2.22
*nasMessage.Notification // 8.2.23
*nasMessage.NotificationResponse // 8.2.24
*nasMessage.SecurityModeCommand // 8.2.25
*nasMessage.SecurityModeComplete // 8.2.26
*nasMessage.SecurityModeReject // 8.2.27
*nasMessage.SecurityProtected5GSNASMessage // 8.2.28
*nasMessage.Status5GMM // 8.2.29
}

const (
Expand Down Expand Up @@ -185,6 +185,7 @@ func (a *Message) PlainNasDecode(byteArray *[]byte) error {
}
return fmt.Errorf("Extended Protocol Discriminator[%d] is not allowed in Nas Message Deocde", epd)
}

func (a *Message) PlainNasEncode() ([]byte, error) {
data := new(bytes.Buffer)
if a.GmmMessage != nil {
Expand All @@ -195,7 +196,6 @@ func (a *Message) PlainNasEncode() ([]byte, error) {
return data.Bytes(), err
}
return nil, fmt.Errorf("Gmm/Gsm Message are both empty in Nas Message Encode")

}

func (a *Message) GmmMessageDecode(byteArray *[]byte) error {
Expand Down Expand Up @@ -359,22 +359,22 @@ func (a *Message) GmmMessageEncode(buffer *bytes.Buffer) error {

type GsmMessage struct {
GsmHeader
*nasMessage.PDUSessionEstablishmentRequest //8.3.1
*nasMessage.PDUSessionEstablishmentAccept //8.3.2
*nasMessage.PDUSessionEstablishmentReject //8.3.3
*nasMessage.PDUSessionAuthenticationCommand //8.3.4
*nasMessage.PDUSessionAuthenticationComplete //8.3.5
*nasMessage.PDUSessionAuthenticationResult //8.3.6
*nasMessage.PDUSessionModificationRequest //8.3.7
*nasMessage.PDUSessionModificationReject //8.3.8
*nasMessage.PDUSessionModificationCommand //8.3.9
*nasMessage.PDUSessionModificationComplete //8.3.10
*nasMessage.PDUSessionModificationCommandReject //8.3.11
*nasMessage.PDUSessionReleaseRequest //8.3.12
*nasMessage.PDUSessionReleaseReject //8.3.13
*nasMessage.PDUSessionReleaseCommand //8.3.14
*nasMessage.PDUSessionReleaseComplete //8.3.15
*nasMessage.Status5GSM //8.3.16
*nasMessage.PDUSessionEstablishmentRequest // 8.3.1
*nasMessage.PDUSessionEstablishmentAccept // 8.3.2
*nasMessage.PDUSessionEstablishmentReject // 8.3.3
*nasMessage.PDUSessionAuthenticationCommand // 8.3.4
*nasMessage.PDUSessionAuthenticationComplete // 8.3.5
*nasMessage.PDUSessionAuthenticationResult // 8.3.6
*nasMessage.PDUSessionModificationRequest // 8.3.7
*nasMessage.PDUSessionModificationReject // 8.3.8
*nasMessage.PDUSessionModificationCommand // 8.3.9
*nasMessage.PDUSessionModificationComplete // 8.3.10
*nasMessage.PDUSessionModificationCommandReject // 8.3.11
*nasMessage.PDUSessionReleaseRequest // 8.3.12
*nasMessage.PDUSessionReleaseReject // 8.3.13
*nasMessage.PDUSessionReleaseCommand // 8.3.14
*nasMessage.PDUSessionReleaseComplete // 8.3.15
*nasMessage.Status5GSM // 8.3.16
}

const (
Expand Down
2 changes: 0 additions & 2 deletions nasConvert/AmfId.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func AmfIdToNas(amfId string) (amfRegionId uint8, amfSetId uint16, amfPointer uint8) {

amfIdBytes, err := hex.DecodeString(amfId)
if err != nil {
log.Printf("amfId decode failed: %+v", err)
Expand All @@ -24,7 +23,6 @@ func AmfIdToNas(amfId string) (amfRegionId uint8, amfSetId uint16, amfPointer ui
}

func AmfIdToModels(amfRegionId uint8, amfSetId uint16, amfPointer uint8) (amfId string) {

tmpBytes := []uint8{amfRegionId, uint8(amfSetId>>2) & 0xff, uint8(amfSetId&0x03) + amfPointer&0x3f}
amfId = hex.EncodeToString(tmpBytes)
return
Expand Down
1 change: 0 additions & 1 deletion nasConvert/GPRSTimer2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
// TS 24.008 10.5.7.4, TS 24.501 9.11.2.4
// the unit of timerValue is second
func GPRSTimer2ToNas(timerValue int) (timerValueNas uint8) {

timerValueNas = 0

if timerValue <= 64 {
Expand Down
1 change: 0 additions & 1 deletion nasConvert/GPRSTimer3.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

// TS 24.008 10.5.7.4a
func GPRSTimer3ToNas(timerValue int) (timerValueNas uint8) {

if timerValue <= 2*31 {
t := uint8(timerValue / 2)
timerValueNas = (nasMessage.GPRSTimer3UnitMultiplesOf2Seconds << 5) + t
Expand Down
2 changes: 0 additions & 2 deletions nasConvert/Ladn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func LadnToModels(buf []uint8) (dnnValues []string) {

for bufOffset := 1; bufOffset < len(buf); {
lenOfDnn := int(buf[bufOffset])
dnn := string(buf[bufOffset : bufOffset+lenOfDnn])
Expand All @@ -22,7 +21,6 @@ func LadnToModels(buf []uint8) (dnnValues []string) {
}

func LadnToNas(dnn string, taiLists []models.Tai) (ladnNas []uint8) {

dnnNas := []byte(dnn)

ladnNas = append(ladnNas, uint8(len(dnnNas)))
Expand Down
9 changes: 4 additions & 5 deletions nasConvert/MobileIdentity5GS.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func GetTypeOfIdentity(buf byte) uint8 {
// "suci-0-${mcc}-${mnc}-${routingIndentifier}-${protectionScheme}-${homeNetworkPublicKeyIdentifier}-${schemeOutput}"
// suci(nai) = "nai-${naiString}"
func SuciToString(buf []byte) (suci string, plmnId string) {

var mcc, mnc, routingInd, protectionScheme, homeNetworkPublicKeyIdentifier, schemeOutput string

supiFormat := (buf[0] & 0xf0) >> 4
Expand Down Expand Up @@ -85,8 +84,10 @@ func SuciToString(buf []byte) (suci string, plmnId string) {
schemeOutput = hex.EncodeToString(buf[8:])
}

suci = strings.Join([]string{"suci", "0", mcc, mnc, routingInd, protectionScheme, homeNetworkPublicKeyIdentifier,
schemeOutput}, "-")
suci = strings.Join([]string{
"suci", "0", mcc, mnc, routingInd, protectionScheme, homeNetworkPublicKeyIdentifier,
schemeOutput,
}, "-")
return suci, plmnId
}

Expand All @@ -100,7 +101,6 @@ func NaiToString(buf []byte) (nai string) {

// nasType: TS 24.501 9.11.3.4
func GutiToString(buf []byte) (guami models.Guami, guti string) {

plmnID := PlmnIDToString(buf[1:4])
amfID := hex.EncodeToString(buf[4:7])
tmsi5G := hex.EncodeToString(buf[7:])
Expand Down Expand Up @@ -183,7 +183,6 @@ func GutiToNas(guti string) nasType.GUTI5G {

// PEI: ^(imei-[0-9]{15}|imeisv-[0-9]{16}|.+)$
func PeiToString(buf []byte) string {

var prefix string

typeOfIdentity := buf[0] & 0x07
Expand Down
2 changes: 0 additions & 2 deletions nasConvert/NetWorkName.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

// TS 24.501 9.11.3.35, TS 24.008 10.5.3.5a
func FullNetworkNameToNas(name string) (fullNetworkName nasType.FullNameForNetwork) {

asciiArray := []byte(name)
numOfSpareBits := 8 - ((7 * len(asciiArray)) % 8)

Expand Down Expand Up @@ -41,7 +40,6 @@ func FullNetworkNameToNas(name string) (fullNetworkName nasType.FullNameForNetwo
}

func ShortNetworkNameToNas(name string) (shortNetworkName nasType.ShortNameForNetwork) {

asciiArray := []byte(name)
numOfSpareBits := 8 - ((7 * len(asciiArray)) % 8)

Expand Down
1 change: 0 additions & 1 deletion nasConvert/Nssai.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func RequestedNssaiToModels(nasNssai *nasType.RequestedNSSAI) ([]models.MappingO

// TS 24.501 9.11.2.8, Length & value part of S-NSSAI IE
func snssaiToModels(lengthOfSnssaiContents uint8, buf []byte) (models.MappingOfSnssai, error) {

snssai := models.MappingOfSnssai{}

switch lengthOfSnssaiContents {
Expand Down
1 change: 0 additions & 1 deletion nasConvert/Nssai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

func TestRequestedNssaiToModels(t *testing.T) {

testCases := []struct {
name string
requestNssai nasType.RequestedNSSAI
Expand Down
1 change: 0 additions & 1 deletion nasConvert/PlmnId.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func PlmnIDToNas(plmnID models.PlmnId) []uint8 {
}

func PlmnIDToString(nasBuf []byte) string {

mccDigit1 := nasBuf[0] & 0x0f
mccDigit2 := (nasBuf[0] & 0xf0) >> 4
mccDigit3 := (nasBuf[1] & 0x0f)
Expand Down
12 changes: 2 additions & 10 deletions nasConvert/ProtocolConfigurationOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func NewProtocolOrContainerUnit() (pcu *ProtocolOrContainerUnit) {
}

func NewProtocolConfigurationOptions() (pco *ProtocolConfigurationOptions) {

pco = &ProtocolConfigurationOptions{
ProtocolOrContainerList: make([]*ProtocolOrContainerUnit, 0),
}
Expand All @@ -53,7 +52,6 @@ func NewProtocolConfigurationOptions() (pco *ProtocolConfigurationOptions) {
}

func (protocolConfigurationOptions *ProtocolConfigurationOptions) Marshal() []byte {

var metaInfo uint8
var extension uint8 = 1
var spare uint8 = 0
Expand All @@ -66,7 +64,6 @@ func (protocolConfigurationOptions *ProtocolConfigurationOptions) Marshal() []by
}

for _, containerUnit := range protocolConfigurationOptions.ProtocolOrContainerList {

if err := binary.Write(buffer, binary.BigEndian, &containerUnit.ProtocolOrContainerID); err != nil {
logger.ConvertLog.Warnf("Write protocolOrContainerID failed: %+v", err)
}
Expand Down Expand Up @@ -96,7 +93,6 @@ func (protocolConfigurationOptions *ProtocolConfigurationOptions) UnMarshal(data
var curContainer *ProtocolOrContainerUnit

for numOfBytes > 0 {

switch readingState {
case ReadingID:
curContainer = NewProtocolOrContainerUnit()
Expand Down Expand Up @@ -170,7 +166,6 @@ func (protocolConfigurationOptions *ProtocolConfigurationOptions) AddIPAddressAl
}

func (protocolConfigurationOptions *ProtocolConfigurationOptions) AddDNSServerIPv4Address(dnsIP net.IP) (err error) {

if dnsIP.To4() == nil {
err = fmt.Errorf("The DNS IP should be IPv4 in AddDNSServerIPv4Address!")
return
Expand Down Expand Up @@ -222,7 +217,6 @@ func (protocolConfigurationOptions *ProtocolConfigurationOptions) AddPCSCFIPv4Ad
}

func (protocolConfigurationOptions *ProtocolConfigurationOptions) AddDNSServerIPv6Address(dnsIP net.IP) (err error) {

if dnsIP.To16() == nil {
err = fmt.Errorf("The DNS IP should be IPv6 in AddDNSServerIPv6Address!")
return
Expand Down Expand Up @@ -251,11 +245,9 @@ func (protocolConfigurationOptions *ProtocolConfigurationOptions) AddIPv4LinkMTU
protocolOrContainerUnit.ProtocolOrContainerID = nasMessage.IPv4LinkMTUDL
protocolOrContainerUnit.LengthOfContents = 2
logger.ConvertLog.Traceln("LengthOfContents: ", protocolOrContainerUnit.LengthOfContents)
protocolOrContainerUnit.Contents =
append(protocolOrContainerUnit.Contents, []byte{uint8(mtu >> 8), uint8(mtu & 0xff)}...)
protocolOrContainerUnit.Contents = append(protocolOrContainerUnit.Contents, []byte{uint8(mtu >> 8), uint8(mtu & 0xff)}...)
logger.ConvertLog.Traceln("Contents: ", protocolOrContainerUnit.Contents)

protocolConfigurationOptions.ProtocolOrContainerList =
append(protocolConfigurationOptions.ProtocolOrContainerList, protocolOrContainerUnit)
protocolConfigurationOptions.ProtocolOrContainerList = append(protocolConfigurationOptions.ProtocolOrContainerList, protocolOrContainerUnit)
return
}
1 change: 0 additions & 1 deletion nasConvert/SessionAMBR.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

func ModelsToSessionAMBR(ambr *models.Ambr) (sessAmbr nasType.SessionAMBR) {

fmt.Println(ambr)

uplink := strings.Split(ambr.Uplink, " ")
Expand Down
Loading

0 comments on commit 9cc4edf

Please sign in to comment.