Skip to content

Commit

Permalink
the host should be solved to IP before removing top route #15
Browse files Browse the repository at this point in the history
  • Loading branch information
stou committed Aug 7, 2024
1 parent f513f6d commit b0d38e4
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 81 deletions.
29 changes: 15 additions & 14 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"errors"
"fmt"
"go.uber.org/zap"
"net"
"net/url"
"strings"
"sync"
"time"

"go.uber.org/zap"
)

type BackendChangeListener interface {
Expand Down Expand Up @@ -61,7 +62,7 @@ type RoundRobinBackend struct {
backends []Backend
backendMap map[string]Backend
backendChangeListenerMgr *BackendChangeListenerMgr
dialogBasedBackend *DialogBasedBackend
//dialogBasedBackend *DialogBasedBackend
}

type UDPBackend struct {
Expand All @@ -83,7 +84,7 @@ func init() {

func CreateRoundRobinBackend(localhostport string, addresses []string) (*RoundRobinBackend, error) {
if len(addresses) <= 0 {
return nil, fmt.Errorf("No address")
return nil, fmt.Errorf("no address")
}
rrBackend := NewRoundRobinBackend()
for _, address := range addresses {
Expand Down Expand Up @@ -116,7 +117,7 @@ func CreateRoundRobinBackend(localhostport string, addresses []string) (*RoundRo
}
}
} else {
return nil, fmt.Errorf("Unsupported protocol %s", u.Scheme)
return nil, fmt.Errorf("unsupported protocol %s", u.Scheme)
}
}
return rrBackend, nil
Expand Down Expand Up @@ -164,9 +165,9 @@ func (b *UDPBackend) GetAddress() string {
func (b *UDPBackend) Close() {
err := b.udpConn.Close()
if err == nil {
zap.L().Info("Succeed to close udp backend", zap.String("address", b.backendAddr.String() ))
zap.L().Info("Succeed to close udp backend", zap.String("address", b.backendAddr.String()))
} else {
zap.L().Error("Fail to close udp backend", zap.String("address", b.backendAddr.String() ))
zap.L().Error("Fail to close udp backend", zap.String("address", b.backendAddr.String()))
}
}

Expand Down Expand Up @@ -200,7 +201,7 @@ func (t *TCPBackend) Send(msg *Message) error {
t.conn.Close()
t.conn = nil
}
return fmt.Errorf("Fail to send message to backend %s", t.backendAddr)
return fmt.Errorf("fail to send message to backend %s", t.backendAddr)
}

func (t *TCPBackend) GetAddress() string {
Expand Down Expand Up @@ -236,7 +237,7 @@ func (rb *RoundRobinBackend) GetBackend(address string) (Backend, error) {
if v, ok := rb.backendMap[address]; ok {
return v, nil
}
return nil, fmt.Errorf("Fail to find backend by %s", address)
return nil, fmt.Errorf("fail to find backend by %s", address)
}

func (rb *RoundRobinBackend) RemoveBackend(address string) {
Expand Down Expand Up @@ -272,7 +273,7 @@ func (rb *RoundRobinBackend) Send(msg *Message) error {
index, err := rb.getNextBackendIndex()
if err != nil {
zap.L().Error("Fail to send message", zap.String("error", err.Error()))
return errors.New("Fail to get next backend")
return errors.New("fail to get next backend")
}

n := rb.getBackendCount()
Expand All @@ -283,7 +284,7 @@ func (rb *RoundRobinBackend) Send(msg *Message) error {
return backend.Send(msg)
}
}
return errors.New("Fail to send msg to all the backend")
return errors.New("fail to send msg to all the backend")
}

func (rb *RoundRobinBackend) GetAddress() string {
Expand All @@ -295,7 +296,7 @@ func (rb *RoundRobinBackend) getNextBackendIndex() (int, error) {
defer rb.Unlock()
n := len(rb.backends)
if n <= 0 {
return 0, fmt.Errorf("No backend available")
return 0, fmt.Errorf("no backend available")
}
rb.index = (rb.index + 1) % n
return rb.index, nil
Expand All @@ -306,7 +307,7 @@ func (rb *RoundRobinBackend) getBackend(index int) (Backend, error) {
defer rb.Unlock()
n := len(rb.backends)
if n <= 0 {
return nil, fmt.Errorf("No backend available at %d", index)
return nil, fmt.Errorf("no backend available at %d", index)
}
return rb.backends[index%n], nil
}
Expand Down Expand Up @@ -386,7 +387,7 @@ func (dbb *DialogBasedBackend) GetBackend(dialog string) (Backend, error) {
}
delete(dbb.backends, dialog)
}
return nil, fmt.Errorf("No backend related with dialog %s", dialog)
return nil, fmt.Errorf("no backend related with dialog %s", dialog)

}

Expand Down Expand Up @@ -415,7 +416,7 @@ func (dbb *DialogBasedBackend) cleanExpiredDialog() {
}
}

for k, _ := range expiredDialogs {
for k := range expiredDialogs {
delete(dbb.backends, k)
}
}
2 changes: 1 addition & 1 deletion cseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ParseCSeq(s string) (*CSeq, error) {
}
return &CSeq{Seq: seq, Method: fields[1]}, nil
}
return nil, errors.New("Malformatted CSeq header")
return nil, errors.New("malformatted CSeq header")
}

func (cs *CSeq) Write(writer io.Writer) (int, error) {
Expand Down
10 changes: 5 additions & 5 deletions from_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ParseFromSpec(s string) (*FromSpec, error) {
if laquot_pos != -1 {
raquot_pos = strings.Index(s, ">")
if raquot_pos == -1 || raquot_pos < laquot_pos {
return nil, fmt.Errorf("Malformatted header From: %s", s)
return nil, fmt.Errorf("malformatted header From: %s", s)
}
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func (fs *FromSpec) GetAddrSpec() (*AddrSpec, error) {
} else if fs.addrSpec != nil {
return fs.addrSpec, nil
}
return nil, errors.New("No nameSpec and addrSpec")
return nil, errors.New("no nameSpec and addrSpec")
}

func (fs *FromSpec) String() string {
Expand All @@ -96,17 +96,17 @@ func (fs *FromSpec) GetParam(name string) (string, error) {
return param.Value, nil
}
}
return "", fmt.Errorf("No such param %s", name)
return "", fmt.Errorf("no such param %s", name)
}

func (fs *FromSpec) GetTag() (string, error) {
return fs.GetParam("tag")
}

func (fs *FromSpec) SetTag(tag string) {
for _, param := range fs.params {
for i, param := range fs.params {
if param.Key == "tag" {
param.Value = tag
fs.params[i].Value = tag
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion generic_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func ParseGenericParam(s string) (KeyValue, error) {
if len(s) <= 0 {
return KeyValue{Key: "", Value: ""}, errors.New("Invalid generic-param syntax")
return KeyValue{Key: "", Value: ""}, errors.New("invalid generic-param syntax")
}
pos := strings.IndexByte(s, '=')
if pos == -1 {
Expand Down
32 changes: 17 additions & 15 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"bytes"
"errors"
"fmt"
"go.uber.org/zap"
"io"
"strconv"
"strings"

"go.uber.org/zap"
)

type RequestLine struct {
Expand Down Expand Up @@ -162,7 +163,7 @@ func parseRequestLine(line string) (*RequestLine, error) {
}
return &RequestLine{method: fields[0], requestURI: requestURI, version: fields[2]}, nil
} else {
return nil, errors.New("Not a valid sip request")
return nil, errors.New("not a valid sip request")
}
}

Expand All @@ -175,7 +176,7 @@ func parseStatusLine(line string) (*StatusLine, error) {
}
return &StatusLine{version: fields[0], statusCode: statusCode, reason: strings.Join(fields[2:], " ")}, nil
} else {
return nil, errors.New("Not a valid sip response")
return nil, errors.New("not a valid sip response")
}
}

Expand Down Expand Up @@ -211,7 +212,7 @@ func ParseMessage(reader *bufio.Reader) (*Message, error) {
} else {
pos := strings.IndexByte(line, ':')
if pos == -1 {
return nil, errors.New("Not a valid sip request")
return nil, errors.New("not a valid sip request")
}
name := line[0:pos]
value := strings.TrimSpace(line[pos+1:])
Expand All @@ -223,7 +224,7 @@ func ParseMessage(reader *bufio.Reader) (*Message, error) {
return nil, err
}
if contentLength < 0 {
return nil, errors.New("Invalid negative Content-Length field")
return nil, errors.New("invalid negative Content-Length field")
}
msg.body = make([]byte, contentLength)
if _, err = io.ReadFull(reader, msg.body); err != nil {
Expand Down Expand Up @@ -251,7 +252,7 @@ func (m *Message) GetHeader(name string) (*Header, error) {
return header, nil
}
}
return nil, fmt.Errorf("No such header %s", name)
return nil, fmt.Errorf("no such header %s", name)
}

func (m *Message) findHeaderPos(name string) (int, error) {
Expand All @@ -260,7 +261,7 @@ func (m *Message) findHeaderPos(name string) (int, error) {
return index, nil
}
}
return 0, fmt.Errorf("No such header %s", name)
return 0, fmt.Errorf("no such header %s", name)

}

Expand Down Expand Up @@ -294,7 +295,7 @@ func (m *Message) RemoveHeader(name string) (interface{}, error) {
return header.value, nil
}
}
return "", fmt.Errorf("No such header %s", name)
return "", fmt.Errorf("no such header %s", name)
}

func (m *Message) GetFrom() (*FromSpec, error) {
Expand Down Expand Up @@ -408,7 +409,7 @@ func (m *Message) GetVia() (*Via, error) {
return v, nil

}
return nil, errors.New("The header value type is not string or Via")
return nil, errors.New("the header value type is not string or Via")
}

func (m *Message) GetCSeq() (*CSeq, error) {
Expand All @@ -429,7 +430,7 @@ func (m *Message) GetCSeq() (*CSeq, error) {
return v, nil

}
return nil, errors.New("The header value type is not string or CSeq")
return nil, errors.New("the header value type is not string or CSeq")
}

// PopVia remove first via
Expand Down Expand Up @@ -579,7 +580,7 @@ func (m *Message) GetMethod() (string, error) {

func (m *Message) GetRequestURI() (*AddrSpec, error) {
if m.request == nil {
return nil, errors.New("Not a request")
return nil, errors.New("not a request")
}
return m.request.requestURI, nil
}
Expand Down Expand Up @@ -628,8 +629,9 @@ func (m *Message) TryRemoveTopRoute(myAddr string, myPort int) error {
if err == nil && sipUri.Host == myAddr && sipUri.GetPort() == myPort {
zap.L().Info("remove top route item because the top item is my address", zap.String("route-param", routeParam.String()))
m.PopRoute()
return nil
}
return nil
return fmt.Errorf("the top route item is not my address")
}

// Get the Call-ID header
Expand Down Expand Up @@ -737,7 +739,7 @@ func (m *Message) GetTopViaSentBy() (string, error) {
return "", err
}
if via.Size() <= 0 {
return "", fmt.Errorf("No Via header is available")
return "", fmt.Errorf("no Via header is available")
}

param, err := via.GetParam(0)
Expand Down Expand Up @@ -769,13 +771,13 @@ func (m *Message) GetClientTransaction() (string, error) {

func (m *Message) GetServerTransaction() (string, error) {
if m.IsResponse() {
return "", fmt.Errorf("No server transaction for response")
return "", fmt.Errorf("no server transaction for response")
}
// the method of the request matches the one that created the
// transaction, except for ACK, where the method of the request
// that created the transaction is INVITE.
if m.request.method == "ACK" {
return "", errors.New("No server transaction for ACK")
return "", errors.New("no server transaction for ACK")
}

// Get the top Via Branch
Expand Down
2 changes: 1 addition & 1 deletion message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test`
start := time.Now()
for i := 0; i < 1000000; i++ {
msg, _ := ParseMessage(create_reader_from_string(s))
msg.String()
_ = msg.String()
}
end := time.Now()
fmt.Printf("Total time:%d\n", end.Sub(start).Milliseconds())
Expand Down
2 changes: 1 addition & 1 deletion name_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ParseNameAddr(nameAddr string) (*NameAddr, error) {
pos1 := strings.IndexByte(nameAddr, '<')
pos2 := strings.IndexByte(nameAddr, '>')
if pos1 == -1 || pos2 == -1 || pos2 < pos1 {
return nil, errors.New("Malformatted name-addr")
return nil, errors.New("malformatted name-addr")
}

addr, err := ParseAddrSpec(nameAddr[pos1+1 : pos2])
Expand Down
2 changes: 1 addition & 1 deletion preconfig_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (pcr *PreConfigRoute) FindRoute(dest string) (protocol string, host string,
return item.protocol, item.host, item.port, nil
}

return "", "", 0, fmt.Errorf("Fail to find route for %s", dest)
return "", "", 0, fmt.Errorf("fail to find route for %s", dest)
}

func (pcr *PreConfigRoute) toRegularExp(s string) string {
Expand Down
Loading

0 comments on commit b0d38e4

Please sign in to comment.