Skip to content

Commit

Permalink
Add support for concurrent authentication sent packets
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikPelli committed Nov 1, 2024
1 parent bd43950 commit 18af5a6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
35 changes: 21 additions & 14 deletions auth/pap/pap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,45 @@ const (

// PAP is the PAP protocol implementation
type PAP struct {
logger *zerolog.Logger
sendChan chan []byte
recvChan chan []byte
requestID uint8
logger *zerolog.Logger
sendChan chan []byte
recvChan chan []byte
requestID uint8
concurrentRetries int
}

// NewPAP creates a new PAP instance with uname, Password;
// uses pppProtol as the underlying PPP protocol;
func NewPAP(pppProto *ppp.PPP, requestID uint8) *PAP {
func NewPAP(pppProto *ppp.PPP, requestID uint8, concurrentRetries int) *PAP {
r := new(PAP)
r.sendChan, r.recvChan = pppProto.Register(ppp.ProtoPAP)
logger := pppProto.Logger.With().Str("Name", "PAP").Logger()
r.logger = &logger
r.requestID = requestID
r.concurrentRetries = concurrentRetries
return r
}

func (pap *PAP) getResponse(ctx context.Context, req Packet) (resp Packet, err error) {
for i := 0; i < _defaultRetryNumber; i++ {
// Increase request ID counter
pap.requestID--
req.ID = pap.requestID
for i := 0; i < pap.concurrentRetries; i++ {
// Increase request ID counter
pap.requestID--
req.ID = pap.requestID

// Send request
pppData, err := ppp.NewPacket(&req, ppp.ProtoPAP).Serialize()
if err != nil {
return resp, err
// Send request
pppData, err := ppp.NewPacket(&req, ppp.ProtoPAP).Serialize()
if err != nil {
return resp, err
}

pap.sendChan <- pppData
pap.logger.Debug().Any("req", req).Msg("sent PAP auth request")
}
pap.sendChan <- pppData
pap.logger.Debug().Any("req", req).Msg("sent PAP auth request")

// Parse response
// Here I define a function to call properly the
// `defer cancel()` inside a loop
resp, err = func(resp Packet) (Packet, error) {
ctx, cancel := context.WithTimeout(ctx, _defaultTimeout)
defer cancel()
Expand Down
44 changes: 26 additions & 18 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Setup struct {
AuthProto ppp.ProtocolNumber
// InitialAuthIdentifier is the starting value for the incremental authentication Identifier
InitialAuthIdentifier uint8
// ConcurrentAuthRetries specifies how many packets should be sent for a single authentication
ConcurrentAuthRetries int
// UserName for PAP/CHAP auth
UserName string
// Password for PAP/CHAP auth
Expand Down Expand Up @@ -113,29 +115,35 @@ func (setup *Setup) Validate() error {
setup.MacStep = 1
}

if setup.ConcurrentAuthRetries == 0 {
setup.ConcurrentAuthRetries = 1
}

return nil
}

func (setup *Setup) Clone(index int) *Setup {
return &Setup{
Logger: setup.Logger,
LogLevel: setup.LogLevel,
Apply: setup.Apply,
Timeout: setup.Timeout,
NumOfClients: setup.NumOfClients,
StartMAC: setup.StartMAC,
MacStep: setup.MacStep,
InterfaceName: setup.InterfaceName,
PPPInterfaceName: genStrFunc(setup.PPPInterfaceName, index),
RID: genStrFunc(setup.RID, index),
CID: genStrFunc(setup.CID, index),
AuthProto: setup.AuthProto,
UserName: genStrFunc(setup.UserName, index),
Password: genStrFunc(setup.Password, index),
IPv4: setup.IPv4,
IPv6: setup.IPv6,
DHCPv6IANA: setup.DHCPv6IANA,
DHCPv6IAPD: setup.DHCPv6IAPD,
Logger: setup.Logger,
LogLevel: setup.LogLevel,
Apply: setup.Apply,
Timeout: setup.Timeout,
NumOfClients: setup.NumOfClients,
StartMAC: setup.StartMAC,
MacStep: setup.MacStep,
InterfaceName: setup.InterfaceName,
PPPInterfaceName: genStrFunc(setup.PPPInterfaceName, index),
RID: genStrFunc(setup.RID, index),
CID: genStrFunc(setup.CID, index),
AuthProto: setup.AuthProto,
InitialAuthIdentifier: setup.InitialAuthIdentifier,
ConcurrentAuthRetries: setup.ConcurrentAuthRetries,
UserName: genStrFunc(setup.UserName, index),
Password: genStrFunc(setup.Password, index),
IPv4: setup.IPv4,
IPv6: setup.IPv6,
DHCPv6IANA: setup.DHCPv6IANA,
DHCPv6IAPD: setup.DHCPv6IAPD,
}
}

Expand Down
8 changes: 3 additions & 5 deletions client/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,14 @@ func (s *session) lcpEvtHandler(evt lcp.LayerNotifyEvent) {
switch authProto {
case ppp.ProtoCHAP:
chapProto := chap.NewCHAP(s.pppProto)
err := chapProto.AuthSelf(ctx, s.cfg.UserName, s.cfg.Password)
if err != nil {
if err := chapProto.AuthSelf(ctx, s.cfg.UserName, s.cfg.Password); err != nil {
s.cfg.Logger.Error().Err(err).Msg("auth failed")
_ = s.Close()
return
}
case ppp.ProtoPAP:
papProto := pap.NewPAP(s.pppProto, s.cfg.InitialAuthIdentifier)
err := papProto.AuthSelf(ctx, s.cfg.UserName, s.cfg.Password)
if err != nil {
papProto := pap.NewPAP(s.pppProto, s.cfg.InitialAuthIdentifier, s.cfg.ConcurrentAuthRetries)
if err := papProto.AuthSelf(ctx, s.cfg.UserName, s.cfg.Password); err != nil {
s.cfg.Logger.Error().Err(err).Msg("auth failed")
_ = s.Close()
return
Expand Down

0 comments on commit 18af5a6

Please sign in to comment.