diff --git a/go.mod b/go.mod index ac1f111..b924370 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module gitlab.com/ics_cinnamon/voicegateway -go 1.16 +go 1.20 require ( github.com/google/uuid v1.3.0 // indirect diff --git a/icsnet/icsudp.go b/icsnet/icsudp.go index e26dd85..dea444b 100644 --- a/icsnet/icsudp.go +++ b/icsnet/icsudp.go @@ -193,7 +193,7 @@ func (u *IcsUDPNet) WriteRTP(b []byte) (int, *icserror.IcsError) { IP: u.raddr.IPv4, Port: u.raddr.Port, } - //fmt.Println("Write addr ", raddr) + // fmt.Println("Write addr ", raddr) //wsize, err := u.conn.ICSUDPConn.WriteTo(b, raddr) wsize, err := u.conn.ICSUDPConn.WriteToUDP(b, raddr) //wsize, err := u.conn.ICSUDPConn.Write(b) diff --git a/icspacketparser/icssipparser.go b/icspacketparser/icssipparser.go index 98e0294..40addea 100644 --- a/icspacketparser/icssipparser.go +++ b/icspacketparser/icssipparser.go @@ -2,6 +2,7 @@ package icspacketparser import ( "fmt" + "regexp" "strconv" "strings" @@ -82,6 +83,7 @@ type SIP struct { ResType string XAICall string + Diversion string XAICallNumber string Media string @@ -344,6 +346,16 @@ func (s *SIP) setSipStruct(name string, value string) (icserr *icserror.IcsError return nil case "X-AICALL": s.XAICall = value + case "DIVERSION": + re := regexp.MustCompile(``) + match := re.FindStringSubmatch(strings.TrimSpace(value)) + if len(match) >= 2 { + s.Diversion = match[1] + } else { + //에러 추가 필요 디버전에 대한 + s.Diversion = "" + return icserror.ICSERRSIPHeader + } case "X-AINUMBER": s.XAICallNumber = value case "MEDIA": diff --git a/icssessionmanager/agentoperator.go b/icssessionmanager/agentoperator.go index 4482018..5036334 100644 --- a/icssessionmanager/agentoperator.go +++ b/icssessionmanager/agentoperator.go @@ -1,6 +1,7 @@ package icssessionmanager import ( + "encoding/binary" "fmt" "io/ioutil" @@ -27,6 +28,11 @@ import ( "gitlab.com/ics_cinnamon/voicegateway/sipasm" ) +const ( + REFER = 0 + RANGE = 10000 +) + func (s *IcsSession) SetRegisterStatus(status int) int { s.registerStatus = status @@ -275,15 +281,23 @@ func (s *IcsSession) RequestInvite(inviteto string) { transport := conf.SIPConfig.Transport format := conf.AgentConfig[s.ID].MediaConfig.Format + agentString := func() string { + rand.Seed(time.Now().UnixNano()) + index := rand.Intn(1) + list := []string{"07012345678"} + return list[index] + }() + fmt.Println(agentString) + //sim inviteMethod := fmt.Sprintf("sip:%s@172.31.29.47;transport=%s", s.AgentName, transport) // nlb(DNS)를 한번 갔다온 ip를 넣어줘야함. s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) to := fmt.Sprintf("", s.AgentName, ip) from := fmt.Sprintf(";tag=%s", inviteto, ip, sipasm.GenerateTag()) if conf.Representative.Value { - inviteMethod = fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", ip, transport) - s.uri = fmt.Sprintf("sip:%s@%s", "AISB", ip) - to = fmt.Sprintf("", "AISB", ip) + inviteMethod = fmt.Sprintf("sip:%s@%s;transport=%s", agentString, ip, transport) + s.uri = fmt.Sprintf("sip:%s@%s", agentString, ip) + to = fmt.Sprintf("", agentString, ip) // agentName := s.AgentName // agentSplit := agentName[len(agentName)-2:len(agentName)] // telNo := fmt.Sprintf("010243427%s", agentSplit) @@ -334,9 +348,11 @@ func (s *IcsSession) RequestInvite(inviteto string) { reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, callid) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, cseq) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) - reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAICALL, s.referto) - reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAINUMBER, s.xaiNumber) - //reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_EXPIRES, expires) + + if agentString == "AISB" { + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAICALL, s.referto) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAINUMBER, s.xaiNumber) + } reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) @@ -344,15 +360,12 @@ func (s *IcsSession) RequestInvite(inviteto string) { reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, sdpLength) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + s.inviteDuration = time.Now() wlen, werr := (*s.sipNeter).WriteSIP([]byte(reqInvite.String())) if werr != nil { l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) return } - - // s.callID = callid - // s.callID = uuid.New().String() - if conf.Representative.Value { s.SetAgentStatus(STATUS_AGENT_READY) } else { @@ -365,6 +378,7 @@ func (s *IcsSession) RequestInvite(inviteto string) { (*s.sipNeter).RemoteAddr().String(), reqInvite.String()) } + inviteFunc() } @@ -373,6 +387,12 @@ func (s *IcsSession) PassInvite(sip *icspacketparser.SIP) *icserror.IcsError { l := icslog.GetIcsLog() conf := icsconf.GetIcsConfig() + agentString := func() string { + rand.Seed(time.Now().UnixNano()) + index := rand.Intn(1) + list := []string{"07012345678"} + return list[index] + }() // TODO - send bye defer func() { if err := recover(); err != nil { @@ -391,26 +411,36 @@ func (s *IcsSession) PassInvite(sip *icspacketparser.SIP) *icserror.IcsError { transport := conf.SIPConfig.Transport format := conf.AgentConfig[s.ID].MediaConfig.Format - inviteMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", proxy, transport) + inviteMethod := fmt.Sprintf("sip:%s@%s;transport=%s", agentString, proxy, transport) reqInvite := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_INVITE, inviteMethod) via := fmt.Sprintf("SIP/2.0/UDP %s;rport;branch=%s", ip, sipasm.GenerateBranch()) maxforwards := "70" agentName := s.AgentName - agentSplit := agentName[len(agentName)-2 : len(agentName)] + agentSplit := agentName[len(agentName)-1:] + _, atoiErr := strconv.Atoi(agentSplit) + if atoiErr != nil { + agentSplit = fmt.Sprint("0", agentSplit[1:]) + } cust1 := strings.SplitN(sip.From, "@", 2) cust2 := strings.SplitN(cust1[0], ":", 2) - telNo := fmt.Sprintf("%s%s", cust2[1][0:9], agentSplit) + telNo := fmt.Sprintf("%s%s", cust2[1][0:8], agentSplit) s.CustID = telNo + + fmt.Println(s.CustID) + from := fmt.Sprintf(";tag=%s", telNo, ip, sipasm.GenerateTag()) + fmt.Println(telNo) + fmt.Println(from) + s.uri = fmt.Sprintf("sip:%s@%s", s.AgentName, ip) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) s.referto = sip.XAICall s.xaiNumber = sip.XAICallNumber - to := fmt.Sprintf("", "AISB", ip) + to := fmt.Sprintf("", agentString, ip) //to := fmt.Sprintf("", s.AgentName, ip) // postfix := fmt.Sprintf("@%s", ip) var callid string @@ -429,7 +459,7 @@ func (s *IcsSession) PassInvite(sip *icspacketparser.SIP) *icserror.IcsError { s.m.Unlock() cseq := fmt.Sprintf("%d INVITE", s.Cseq) s.Cseq++ - contact := fmt.Sprintf("", "AISB", ip, port, transport) + contact := fmt.Sprintf("", agentString, ip, port, transport) //expires := fmt.Sprintf("%d", conf.AgentConfig[s.ID].RegisterConfig.RegisterExpire) userAgent := conf.InfoConfig.Product allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" @@ -448,14 +478,19 @@ func (s *IcsSession) PassInvite(sip *icspacketparser.SIP) *icserror.IcsError { reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, callid) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, cseq) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) - reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAICALL, sip.XAICall) - reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAINUMBER, s.xaiNumber) + if agentString == "AISB" { + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAICALL, sip.XAICall) + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_XAINUMBER, s.xaiNumber) + } //reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_EXPIRES, expires) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_TYPE, "application/sdp") reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, sdpLength) + if agentString != "AISB" { + reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_DIVERSION, "07077399939") + } reqInvite.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Send INVITE! [%s] -> [%s] \r\n %+v\r\n", @@ -856,6 +891,8 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError conf := icsconf.GetIcsConfig() // s.InviteSIP = sip + inviteDuration := time.Since(s.inviteDuration) + l.Printf(icslog.LOG_LEVEL_DEBUG, s.ID, "####INVITE <-> INVITE ACK => %+v", inviteDuration) /////////////////////////////////////////// /////////////////////////////////////////// // set agentinfo @@ -916,7 +953,7 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) from := fmt.Sprintf(";tag=%s", s.CustID, ip, sipasm.GenerateTag()) - to := fmt.Sprintf("", "AISB", ip) + to := fmt.Sprintf("", "07012345678", ip) // if s.InviteSIP != nil { // from = s.InviteSIP.From // to = s.InviteSIP.To @@ -1102,6 +1139,7 @@ func (s *IcsSession) ProcACKInvite(sip *icspacketparser.SIP) *icserror.IcsError /////////////////////////////////////////// // fmt.Println(s) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent Data(%d) [%s]->[%s]>\n%s", wlen, (*s.sipNeter).LocalAddr().String(), @@ -1422,20 +1460,30 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { // seq := s.rtpSeq // voiceFilePath := "/home/leejj9612/sim/voice/9001-RX-1648533187911907029.pcm" - voiceFilePath := "/home/icsvsim/voice/Sample.pcm" + voiceFilePath := "/home/icsvsim/voice/tvoice.wav" readPcmData, ferr := ioutil.ReadFile(voiceFilePath) if ferr != nil { // fmt.Println("Read Voice File error ", ferr) l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "VoiceFile Read ERROR") return } - // l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "############ readPcmData %d", readPcmData) s.m.Lock() // pcmDataLen := len(s.tts) - pcmDataLen := len(readPcmData) + + //ljhwan + // pcmDataLen := len(readPcmData) + // pcmData := make([]byte, pcmDataLen*46) + + // for count := 0; count < 46; count++ { + // copy(pcmData[(pcmDataLen-44)*count:], readPcmData[44:]) //cut off wave header + // } + pcmDataLen := len(readPcmData) - 44 pcmData := make([]byte, pcmDataLen) copy(pcmData, readPcmData[44:]) //cut off wave header + + // pcmData := make([]byte, pcmDataLen) + //copy(pcmData, readPcmData) //cut off wave header // fmt.Printf("@@@@@ Read Data len: %d\r\n@@@@@ data: %+v\r\n@@@@@ copyData: %+v \r\n\r\n", pcmDataLen, readPcmData, pcmData) //pcmData := s.tts[44:] //cut off wave header //fmt.Println("pcmData len>>>", pcmDataLen, len(s.tts)) @@ -1458,96 +1506,122 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { } var rtp *icsrtp.RTP - if conf.CallEndInfo.Value { + + if !conf.CallEndInfo.Value { + // for { + // // fmt.Printf("@@@@@@@@@@@@@@@@@@@@@@ %d ", lc) + // select { + // case <-t.STOP: + // // s.rrData.Clear() + // l.Printf(icslog.LOG_LEVEL_DEBUG, s.ID, "######## Stoped sendrtpCB[%d] ") + // return + // case <-t.GetTick(): + // if s.TTSFlag.Load().(bool) { + // iter = 0 + // totalSentLen = 0 + // offset = 0 + // psize = icspacketparser.RTPPayloadInfo[s.payloadType].PSize + // ts = s.rtpTS + // seq = s.rtpSeq + // } else { + // offset = pcmDataLen + // } + + // if pcmDataLen < offset+psize { + // continue + // } + + // payload := pcmData[offset : offset+psize] + + // if iter == 0 { + // if offset+psize >= pcmDataLen { + // offset = 0 + // } + // rtp = icsrtp.NewRTP(1, //set mark bit + // int(s.payloadType), + // int(seq), + // int(ts), + // int(s.rtpSSRC), + // payload) + // totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + // offset += psize + // } else { + // if offset+psize >= pcmDataLen { + // offset = 0 + // } + // rtp = icsrtp.NewRTP(0, + // int(s.payloadType), + // int(seq), + // int(ts), + // int(s.rtpSSRC), + // payload) + // totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize + // offset += psize + // } + // iter++ + // ts += uint32(psize) + // seq++ + // s.txCnt++ + + // wlen, werr := s.rtpMediaNeter.WriteRTP(rtp.Byte()) + // if werr != nil { + // fmt.Println(wlen, werr) + // } + // } + // // time.Sleep(time.Second) + // } + // s.RequestBYE(s.InviteSIP) + } else { + // iter := 0 + // totalSentLen := 0 + // offset := pcmDataLen + // psize := icspacketparser.RTPPayloadInfo[s.payloadType].PSize + // ts := s.rtpTS + // seq := s.rtpSeq + // s.PreTTSFlag = true for { - // fmt.Printf("@@@@@@@@@@@@@@@@@@@@@@ %d ", lc) select { case <-t.STOP: // s.rrData.Clear() l.Printf(icslog.LOG_LEVEL_DEBUG, s.ID, "######## Stoped sendrtpCB[%d] ") return case <-t.GetTick(): - if pcmDataLen < offset+psize { - break - } - /* - uplus에서 샘플 음성으로 pcma를 사용(이미 인코딩 되있는 음성파일이라 읽어서 160씩 보내주면됨) - - 인코딩 할 필요 없는 파일이라 인코딩 제거 - - pcma여서 160으로 고정해서 전송(다른 경우는 추후 진행) - */ - payload := pcmData[offset : offset+psize] - - if iter == 0 { - if offset+psize >= pcmDataLen { + epd := s.epd + if epd == 2 { + if s.preepd == 1 { + l.Printf(icslog.LOG_LEVEL_DEBUG, s.ID, "######## SEND TTS ") + iter = 0 + totalSentLen = 0 offset = 0 + psize = icspacketparser.RTPPayloadInfo[s.payloadType].PSize + ts = s.rtpTS + seq = s.rtpSeq } - rtp = icsrtp.NewRTP(1, //set mark bit - int(s.payloadType), - int(seq), - int(ts), - int(s.rtpSSRC), - payload) - totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize - offset += psize - } else { - if offset+psize >= pcmDataLen { - offset = 0 + } else if epd == 1 { + if s.preepd == 0 { + l.Printf(icslog.LOG_LEVEL_DEBUG, s.ID, "######## STOP TTS ") + offset = pcmDataLen } - rtp = icsrtp.NewRTP(0, - int(s.payloadType), - int(seq), - int(ts), - int(s.rtpSSRC), - payload) - totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize - offset += psize } - iter++ - ts += uint32(psize) - seq++ - s.txCnt++ - wlen, werr := s.rtpMediaNeter.WriteRTP(rtp.Byte()) - if werr != nil { - fmt.Println(wlen, werr) - } - } - // time.Sleep(time.Second) - } - // s.RequestBYE(s.InviteSIP) - } else { - for { - iter := 0 - totalSentLen := 0 - offset := 0 - psize := icspacketparser.RTPPayloadInfo[s.payloadType].PSize - ts := s.rtpTS - seq := s.rtpSeq - for t1 := range t.GetTick() { - if pcmDataLen < offset+psize*2 { - break + s.preepd = epd + data := make([]byte, 0) + if pcmDataLen < offset+psize*2 || epd != 2 { + data = make([]byte, psize*2) + for index, _ := range data { + data[index] = 0x00 + } + } else { + data = pcmData[offset : offset+psize*2] } - payload, convErr := s.TxConverter.Encode(pcmData[offset : offset+psize*2]) + + payload, convErr := s.TxConverter.Encode(data) if convErr != nil { l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Encoding Error - %s", convErr.GetError()) return } - //fmt.Printf("1048@@@@ %+v\n", s.rtpMediaNeter) - /* - wflen, wferr := ttsfile1.Write(payload) - if wferr != nil { - fmt.Println("file.write error", wferr, wflen) - os.Exit(1) - } - ttsfile1.Sync() - */ - if iter == 0 { - if offset+psize >= pcmDataLen { - //if offset+psize >= alawDataLen { - offset = 0 - } rtp = icsrtp.NewRTP(1, //set mark bit int(s.payloadType), int(seq), @@ -1557,12 +1631,7 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize offset += psize * 2 - //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize } else { - if offset+psize >= pcmDataLen { - //if offset+psize >= alawDataLen { - offset = 0 - } rtp = icsrtp.NewRTP(0, int(s.payloadType), int(seq), @@ -1572,32 +1641,30 @@ func (s *IcsSession) SendRTPCB(t *icscbtimer.IcsCBTimer) { //alawData[offset:offset+icspacketparser.RTPPayloadInfo[s.payloadType].PSize]) totalSentLen += icspacketparser.RTPPayloadInfo[s.payloadType].PSize offset += psize * 2 - //offset += icspacketparser.RTPPayloadInfo[s.payloadType].PSize } iter++ ts += uint32(psize) seq++ s.txCnt++ - //fmt.Printf("1095@@@@ %+v\n", s.rtpMediaNeter) wlen, werr := s.rtpMediaNeter.WriteRTP(rtp.Byte()) if werr != nil { - fmt.Println(t1, wlen, werr) + fmt.Println(wlen, werr) } - //fmt.Printf("wlen: %d\r", totalSentLen) - //l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", wlen, s.rtpMediaNeter.RemoteAddr().String()) - } - // fmt.Printf("Sent RTP(%d) to [%s] [%s]\r\n", totalSentLen, s.rtpMediaNeter.RemoteAddr().String(), s.AgentName ) - // l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Sent RTP(%d) to %s", totalSentLen, s.rtpMediaNeter.RemoteAddr().String()) - time.Sleep(time.Second) - // s.RequestBYE(s.InviteSIP) } } } func (s *IcsSession) ReadRTP() { l := icslog.GetIcsLog() + defer func() { + if r := recover(); r != nil { + fmt.Println("ReadRTP Panic", r) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "ReadRTP Panic - %s", r) + } + }() + svc := icssvc.GetServiceStatus() conf := icsconf.GetIcsConfig() @@ -1620,15 +1687,12 @@ func (s *IcsSession) ReadRTP() { // s.ExpTimer.Start() // go s.ExpiredRTP(s.readTimer) for !svc.GetExit() || !svc.GetStop() { - //fmt.Println(">>>", s.ID, s.rtpMediaNeter.LocalAddr().String()) - if s.rtpMediaNeter == nil { l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "######### media neter nil!!!") break } data, _, rlen, rerr := s.rtpMediaNeter.ReadRTP() - //data, raddr, rlen, rerr := s.rtpMediaNeter.ReadRTP() if rerr != nil { l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "%s-%s", rerr.GetMessage(), rerr.GetError()) break @@ -1642,7 +1706,6 @@ func (s *IcsSession) ReadRTP() { continue } RPT := rtp.GetPayloadType() - if RPT == icspacketparser.PayloadType(101) { //rfc2833 /////////////// //dtmf @@ -1688,6 +1751,37 @@ func (s *IcsSession) ReadRTP() { // fmt.Println("Recev 21 TTS!!!") s.expRead = time.Now() s.expCheck = false + pcm, cerr := s.RxConverter.Decode(rtp.Payload) + if cerr != nil { + l.Print(icslog.LOG_LEVEL_ERROR, s.ID, cerr.GetMessage()) + continue + } + average, _ := calAverage(pcm) + if average > REFER+RANGE { + s.vaildcnt++ + if s.vaildcnt > 5 { + s.silencecnt = 0 + } + } else { + s.silencecnt++ + if s.silencecnt > 50 { + s.vaildcnt = 0 + } + } + + if s.silencecnt > 200 { + if s.epd == 1 { + s.epd = 2 + s.silencecnt = 0 + } else if s.epd == 2 { + s.epd = 0 + } + } else if s.vaildcnt > 10 { + if s.epd == 0 { + s.epd = 1 + } + } + } else { //rtp decoding pcm, cerr := s.RxConverter.Decode(rtp.Payload) @@ -1730,6 +1824,25 @@ func (s *IcsSession) ReadRTP() { } } +func calAverage(target []byte) (int, error) { + var sum uint32 = 0 + var err error = nil + + length := len(target) + if length != 0 { + for i := 0; i < length; i += 2 { + if i+1 < length { + data := []byte{target[i+1], target[i]} + sum = sum + uint32(binary.BigEndian.Uint16(data)) + } + } + } else { + err = fmt.Errorf("Error: integer divide by zero %+v", target) + } + + return int(int(sum) / (length / 2)), err +} + // func (s *IcsSession)ExpiredRTP(t *icscbtimer.IcsCBTimer) { func (s *IcsSession) ExpiredRTP(t time.Ticker) { s.mychannel = make(chan bool) @@ -1956,7 +2069,7 @@ func (s *IcsSession) RequestBYESIM() { // contactName := strings.SplitN(contactName1[1], "@", 2) optfunc := func() { - optMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", conf.SIPConfig.SIPProxy, transport) + optMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "07012345678", conf.SIPConfig.SIPProxy, transport) reqOpt := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_BYE, optMethod) via := fmt.Sprintf("SIP/2.0/UDP %s:%d;branch=%s", ip, port, sipasm.GenerateBranch()) @@ -2025,7 +2138,7 @@ func (s *IcsSession) RequestBYE(inviteSIP *icspacketparser.SIP) { // contactName := strings.SplitN(contactName1[1], "@", 2) optfunc := func() { - optMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "AISB", conf.SIPConfig.SIPProxy, transport) + optMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "07012345678", conf.SIPConfig.SIPProxy, transport) reqOpt := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_BYE, optMethod) via := fmt.Sprintf("SIP/2.0/UDP %s:%d;branch=%s", ip, port, sipasm.GenerateBranch()) @@ -2034,10 +2147,10 @@ func (s *IcsSession) RequestBYE(inviteSIP *icspacketparser.SIP) { // from := strings.SplitN(inviteSIP.From, ": ", 2) //from := fmt.Sprintf(";tag=%s", s.AgentName, ip, sipasm.GenerateTag()) - s.uri = fmt.Sprintf("sip:%s@%s", "AISB", ip) + s.uri = fmt.Sprintf("sip:%s@%s", "07012345678", ip) l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Set URI [%s]", s.uri) - to := fmt.Sprintf("", "AISB", ip) + to := fmt.Sprintf("", "07012345678", ip) if s.InviteSIP != nil { from = s.InviteSIP.From // to = s.InviteSIP.To @@ -2048,7 +2161,7 @@ func (s *IcsSession) RequestBYE(inviteSIP *icspacketparser.SIP) { cseq := fmt.Sprintf("%d BYE", s.Cseq) s.Cseq++ //referto := fmt.Sprintf("") - contact := fmt.Sprintf("", "AISB", ip, port, transport) + contact := fmt.Sprintf("", "07012345678", ip, port, transport) userAgent := conf.InfoConfig.Product allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" supported := "timer,path,replaces" diff --git a/icssessionmanager/csprocessor.go b/icssessionmanager/csprocessor.go index 827ea67..c8e6e5d 100644 --- a/icssessionmanager/csprocessor.go +++ b/icssessionmanager/csprocessor.go @@ -5,8 +5,8 @@ Call Signal Processor package icssessionmanager import ( - "time" "strings" + "time" "gitlab.com/ics_cinnamon/voicegateway/icsconf" "gitlab.com/ics_cinnamon/voicegateway/icserror" @@ -32,8 +32,6 @@ func (s *IcsSession) Processing(sip *parser.SIP) *icserror.IcsError { //return nil return s.PassInvite(sip) // return s.ProcInvite(sip) - - case parser.ICS_SIP_METHOD_PUBLISH: case parser.ICS_SIP_METHOD_OPTIONS: case parser.ICS_SIP_METHOD_INFO: case parser.ICS_SIP_METHOD_UPDATE: @@ -77,8 +75,8 @@ func (s *IcsSession) Processing(sip *parser.SIP) *icserror.IcsError { //////// for simul ////////////// ////////////////////////////////// // send 83 to reg200 - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Start Sim!!!") - time.Sleep(time.Second) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Start Sim!!!") + time.Sleep(time.Second * 1) s.SendRequestInvite() ////////////////////////////////// ////////////////////////////////// diff --git a/icssessionmanager/icssession.go b/icssessionmanager/icssession.go index bdba434..27f027b 100644 --- a/icssessionmanager/icssession.go +++ b/icssessionmanager/icssession.go @@ -5,6 +5,8 @@ import ( "net" "strings" "sync" + "sync/atomic" + "time" "gitlab.com/ics_cinnamon/joy4/format" @@ -139,10 +141,17 @@ type IcsSession struct { simLoopCount int xaiNumber string // ExpTimer *icscbtimer.IcsCBTimer - expRead time.Time - readTimer time.Ticker - expCheck bool - mychannel chan bool + expRead time.Time + readTimer time.Ticker + expCheck bool + mychannel chan bool + inviteDuration time.Time + TTSFlag atomic.Value + PreTTSFlag bool + silencecnt int + vaildcnt int + epd int + preepd int m *sync.Mutex } @@ -182,6 +191,7 @@ func (s *IcsSession) Init() *icserror.IcsError { //conf := icsconf.GetIcsConfig() s.RTPCBStatus = 0 + s.TTSFlag.Store(false) /* s.AgentName = s.AgentConf.Name diff --git a/icssessionmanager/icssessionmanager.go b/icssessionmanager/icssessionmanager.go index 6f9c427..039dacb 100644 --- a/icssessionmanager/icssessionmanager.go +++ b/icssessionmanager/icssessionmanager.go @@ -8,6 +8,7 @@ import ( "strings" "sync" "time" + // "strconv" "gitlab.com/ics_cinnamon/voicegateway/icscbtimer" @@ -259,6 +260,8 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { sip := icspacketparser.NewSIP() sip.SipParser(data) + + fmt.Println(sip.Diversion) if icspacketparser.ICS_SIP_METHOD_NOT_FOUND == sip.Method { //not found sip. maybe abnormal use l.Printf(icslog.LOG_LEVEL_INFO, -1, "icspacketparser.ICS_SIP_METHOD_NOT_FOUND-%s", string(data)) continue @@ -266,7 +269,7 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { // fmt.Println("메서드", sip.Method.String()) // if sip.Method == icspacketparser.ICS_SIP_METHOD_INVITE { // // conf := icsconf.GetIcsConfig() - + // // TODO - send bye // // defer func() { // // if err := recover(); err != nil { @@ -274,8 +277,7 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { // // icserror.ICSERRNETNotConnectError.GetMessage(), debug.Stack()) // // } // // }() - - + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "sip 내용 전달! %+v", sip) // // ip := strings.Split(conf.SIPConfig.SIPProxy, ":")[0] // // port, cerr := strconv.Atoi(strings.Split(conf.SIPConfig.SIPProxy, ":")[1]) @@ -283,7 +285,6 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { // // fmt.Println("cerr err") // // } // // iport := fmt.Sprintf("%s:%d", ip, port) - // // fmt.Println(ip, port) @@ -306,7 +307,7 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { // fmt.Println(err) // } // // } - + // // wlen, werr := t.WriteSIP([]byte(data)) // // if werr != nil { // // l.Print(icslog.LOG_LEVEL_FATAL, -1, werr.GetMessage()) @@ -314,204 +315,204 @@ func (sm *SessionManager) Run() (icserr *icserror.IcsError) { // // // return werr // // } // fmt.Printf("send data \r\n%s, \r\n %d\r\n", string(data), sent) - + // } else { - if SessionAvailableSipMethod(&sip) { - l.Printf(icslog.LOG_LEVEL_DEBUG, -1, "Session Processing Method-[%s, %s, %s]", - sip.Method, sip.ResType, sip.Cseq) - - s, serr := FindSession(sip) - if serr != nil { //not found session - // name1 := strings.SplitN(sip.To, "@", 2) - // name2 := strings.SplitN(name1[0], ":", 2) - // agentname := name2[1] - if conf.Representative.Value && sip.Source == "486" { - l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") - sipPort := conf.SIPConfig.Port - // sipProxy := conf.SIPConfig.SIPProxy - transport := conf.SIPConfig.Transport - // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) - // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) - // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) - ////////////////////////////////////////////// - ip := conf.InfoConfig.ServerIP - ackMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "192.168.0.222:5090", "AISB",transport) - reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) - maxforwards := "70" - // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) - // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) - contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) - userAgent := conf.InfoConfig.Product - allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" - supported := "timer,path,replaces" - - for _, viav := range sip.Via { - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) - } - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") - reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) - - // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) - wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) - if werr != nil { - l.Print(icslog.LOG_LEVEL_FATAL, -1, werr.GetMessage()) - return werr - } - l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", - wlen, - (sm.SIPNeter).LocalAddr().String(), - (sm.SIPNeter).RemoteAddr().String(), - reqAck.String()) - ////////////////////////////////////////////// - } - continue - // s, serr = AllocSession(agentname, sip.GetCallID()) - // if serr != nil { - // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "Licensed Session number[%d:%s] is EXCEEDED-%s", - // conf.GetChannelNum(), agentname, serr.GetMessage()) - // if conf.Representative.Value && sip.Source == "486" { - // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") - // sipPort := conf.SIPConfig.Port - // // sipProxy := conf.SIPConfig.SIPProxy - // transport := conf.SIPConfig.Transport - // // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) - // // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) - // // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) - // ////////////////////////////////////////////// - // ip := conf.InfoConfig.ServerIP - // ackMethod := fmt.Sprintf("sip:01025670081@%s;transport=%s", "192.168.0.222:5090", transport) - // reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) - // maxforwards := "70" - // // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) - // // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) - // contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) - // userAgent := conf.InfoConfig.Product - // allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" - // supported := "timer,path,replaces" - - // for _, viav := range sip.Via { - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) - // } - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") - // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) - - // // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) - // wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) - // if werr != nil { - // l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) - // return werr - // } - // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", - // wlen, - // (sm.SIPNeter).LocalAddr().String(), - // (sm.SIPNeter).RemoteAddr().String(), - // reqAck.String()) - // ////////////////////////////////////////////// - // } - // continue - // } - // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Session Allocated. Session ID[%d] Call ID[%s]", s.ID, sip.GetCallID()) - // s.SetSessionMethod(sip) - // //session start - // s.Start() - } else { //found session - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session found [%s][%s]", sip.GetCallID(), s.callID) - if sip.Method == icspacketparser.ICS_SIP_METHOD_INVITE { - s.SetCallID(sip.GetCallID()) - } - // 대표번호 - if sip.Method == icspacketparser.ICS_SIP_METHOD_SIP20 || strings.SplitN(sip.Cseq, " ", 2)[1] == "INVITE" { - s.SetCallID(sip.GetCallID()) - // representative - if conf.Representative.Value { - sip.SetToRep() - sip.SetTo(s.AgentName) - // if sip.Source == "486" { - // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") - // fmt.Println("##### All Agent Clling!!!!!! ") - // s.Res486Ack(&sip) - // continue - // } - } + if SessionAvailableSipMethod(&sip) { + l.Printf(icslog.LOG_LEVEL_DEBUG, -1, "Session Processing Method-[%s, %s, %s]", + sip.Method, sip.ResType, sip.Cseq) + + s, serr := FindSession(sip) + if serr != nil { //not found session + // name1 := strings.SplitN(sip.To, "@", 2) + // name2 := strings.SplitN(name1[0], ":", 2) + // agentname := name2[1] + if conf.Representative.Value && sip.Source == "486" { + l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + sipPort := conf.SIPConfig.Port + // sipProxy := conf.SIPConfig.SIPProxy + transport := conf.SIPConfig.Transport + // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) + // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) + // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) + ////////////////////////////////////////////// + ip := conf.InfoConfig.ServerIP + ackMethod := fmt.Sprintf("sip:%s@%s;transport=%s", "192.168.0.222:5090", "AISB", transport) + reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) + maxforwards := "70" + // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) + contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) + userAgent := conf.InfoConfig.Product + allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" + supported := "timer,path,replaces" + + for _, viav := range sip.Via { + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) } - - if s.CheckAutomata(&sip) { - s.SetSessionMethod(sip) - } else { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Not Allowed Method(%d)", s.MethodAutomata) - //TODO : response 400 error - continue + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") + reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + + // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) + wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) + if werr != nil { + l.Print(icslog.LOG_LEVEL_FATAL, -1, werr.GetMessage()) + return werr } - - // if conf.Representative.Value && sip.Source == "486" { - // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") - // fmt.Println("##### All Agent Clling!!!!!! ") - // s.Res486Ack(&sip) - // continue - // } - - //if cancel ACK or bye res, remove the session - if s.MethodAutomata == ICS_SIP_AUTOMATA_CANCEL && sip.Method == icspacketparser.ICS_SIP_METHOD_ACK { - s.Stop() - serr := s.RemoveSession() - if serr != nil { - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "RemoveSession Error %s", serr.GetMessage()) - - } else { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session Removed") - } - continue + l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", + wlen, + (sm.SIPNeter).LocalAddr().String(), + (sm.SIPNeter).RemoteAddr().String(), + reqAck.String()) + ////////////////////////////////////////////// + } + continue + // s, serr = AllocSession(agentname, sip.GetCallID()) + // if serr != nil { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "Licensed Session number[%d:%s] is EXCEEDED-%s", + // conf.GetChannelNum(), agentname, serr.GetMessage()) + // if conf.Representative.Value && sip.Source == "486" { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + // sipPort := conf.SIPConfig.Port + // // sipProxy := conf.SIPConfig.SIPProxy + // transport := conf.SIPConfig.Transport + // // remoteaddr := icsnet.NewNetAddrWithIPAddr(sipProxy) + // // localAddrStr := fmt.Sprintf("0.0.0.0:%d", sipPort) + // // localAddr := icsnet.NewNetAddrWithIPAddr(localAddrStr) + // ////////////////////////////////////////////// + // ip := conf.InfoConfig.ServerIP + // ackMethod := fmt.Sprintf("sip:01025670081@%s;transport=%s", "192.168.0.222:5090", transport) + // reqAck := sipasm.NewSIPMessage(sipasm.ICSSIP_METHOD_ACK, ackMethod) + // maxforwards := "70" + // // s.uri = fmt.Sprintf("sip:%s@%s", conf.Representative.Name, ip) + // // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Set URI [%s]", s.uri) + // contact := fmt.Sprintf("", conf.Representative.Name, ip, sipPort, transport) + // userAgent := conf.InfoConfig.Product + // allow := "REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE" + // supported := "timer,path,replaces" + + // for _, viav := range sip.Via { + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_VIA, viav) + // } + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_MAX_FORWARDS, maxforwards) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_FROM, sip.From) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TO, sip.To) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CALL_ID, sip.CallID) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CSEQ, sip.Cseq) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTACT, contact) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_USER_AGENT, userAgent) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_ALLOW, allow) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_SUPPORTED, supported) + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_CONTENT_LENGTH, "0") + // reqAck.AddSIPHeader(sipasm.ICSSIP_HEADER_TERMINATOR) + + // // wlen, werr := (*s.sipNeter).WriteSIPTo([]byte(reqAck.String()), s.remoteSIPAddr) + // wlen, werr := (sm.SIPNeter).WriteSIP([]byte(reqAck.String())) + // if werr != nil { + // l.Print(icslog.LOG_LEVEL_FATAL, s.ID, werr.GetMessage()) + // return werr + // } + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Sent Data(%d) [%s]->[%s]>\n%s", + // wlen, + // (sm.SIPNeter).LocalAddr().String(), + // (sm.SIPNeter).RemoteAddr().String(), + // reqAck.String()) + // ////////////////////////////////////////////// + // } + // continue + // } + // l.Printf(icslog.LOG_LEVEL_INFO, -1, "Session Allocated. Session ID[%d] Call ID[%s]", s.ID, sip.GetCallID()) + // s.SetSessionMethod(sip) + // //session start + // s.Start() + } else { //found session + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session found [%s][%s]", sip.GetCallID(), s.callID) + if sip.Method == icspacketparser.ICS_SIP_METHOD_INVITE { + s.SetCallID(sip.GetCallID()) + } + // 대표번호 + if sip.Method == icspacketparser.ICS_SIP_METHOD_SIP20 || strings.SplitN(sip.Cseq, " ", 2)[1] == "INVITE" { + s.SetCallID(sip.GetCallID()) + // representative + if conf.Representative.Value { + sip.SetToRep() + sip.SetTo(s.AgentName) + // if sip.Source == "486" { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + // fmt.Println("##### All Agent Clling!!!!!! ") + // s.Res486Ack(&sip) + // continue + // } } } - s.remoteSIPAddr = addr - - //post the sip event to session - h := icsevent.NewEventH() - evt, evtErr := h.AllocEvent(sip) - if evtErr != nil { - return evtErr - } - perr := h.PostEvent(s.ID, evt) - //perr := h.PostEvent(int(s.GetSessionID()), evt) - if perr == nil { - l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Post SIP event[%d] to Session[%03d]", evt.ID, s.ID) + if s.CheckAutomata(&sip) { + s.SetSessionMethod(sip) } else { - l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Failed to post SIP event[%d] to Session[%03d] - %s", - evt.ID, s.ID, perr.GetError()) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Not Allowed Method(%d)", s.MethodAutomata) + //TODO : response 400 error + continue } - } else { // no session - switch sip.Method { - case icspacketparser.ICS_SIP_METHOD_OPTIONS: - l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) - case icspacketparser.ICS_SIP_METHOD_SIP20: - if strings.Contains(strings.ToUpper(sip.Cseq), "OPTIONS") { - //l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) + + // if conf.Representative.Value && sip.Source == "486" { + // l.Printf(icslog.LOG_LEVEL_ERROR, -1, "All Agent Calling!!!!!") + // fmt.Println("##### All Agent Clling!!!!!! ") + // s.Res486Ack(&sip) + // continue + // } + + //if cancel ACK or bye res, remove the session + if s.MethodAutomata == ICS_SIP_AUTOMATA_CANCEL && sip.Method == icspacketparser.ICS_SIP_METHOD_ACK { + s.Stop() + serr := s.RemoveSession() + if serr != nil { + l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "RemoveSession Error %s", serr.GetMessage()) + } else { - l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Session Removed") } - default: + continue + } + } + + s.remoteSIPAddr = addr + + //post the sip event to session + h := icsevent.NewEventH() + evt, evtErr := h.AllocEvent(sip) + if evtErr != nil { + return evtErr + } + perr := h.PostEvent(s.ID, evt) + //perr := h.PostEvent(int(s.GetSessionID()), evt) + if perr == nil { + l.Printf(icslog.LOG_LEVEL_INFO, s.ID, "Post SIP event[%d] to Session[%03d]", evt.ID, s.ID) + } else { + l.Printf(icslog.LOG_LEVEL_ERROR, s.ID, "Failed to post SIP event[%d] to Session[%03d] - %s", + evt.ID, s.ID, perr.GetError()) + } + } else { // no session + switch sip.Method { + case icspacketparser.ICS_SIP_METHOD_OPTIONS: + l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) + case icspacketparser.ICS_SIP_METHOD_SIP20: + if strings.Contains(strings.ToUpper(sip.Cseq), "OPTIONS") { + //l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) + } else { l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) } + default: + l.Printf(icslog.LOG_LEVEL_INFO, -1, "%v", sip) } + } // } } diff --git a/icssessionmanager/sessionoperator.go b/icssessionmanager/sessionoperator.go index 9079554..8a812de 100644 --- a/icssessionmanager/sessionoperator.go +++ b/icssessionmanager/sessionoperator.go @@ -367,13 +367,11 @@ func (s *IcsSession) RemoveSession() *icserror.IcsError { s.rtcpMediaNeter.Close() s.rtcpMediaNeter = nil } - } e := &IcsSession{} s.m.Lock() s.eventSystem.ClearJob() - /* s.AgentConf = nil s.AgentInfo = nil @@ -408,7 +406,6 @@ func (s *IcsSession) RemoveSession() *icserror.IcsError { s.SetRegisterStatus(STATUS_REGISTER_READY) // fmt.Println("REMOVE SESSION STATUS: ", s.GetAgentStatus()) s.m.Unlock() - return nil } diff --git a/sipasm/siptemplt.go b/sipasm/siptemplt.go index 5e36772..8fbd898 100644 --- a/sipasm/siptemplt.go +++ b/sipasm/siptemplt.go @@ -86,6 +86,7 @@ const ( ICSSIP_HEADER_CONTENT_ENCODING ICSSIP_HEADER_CONTENT_LANGUAGE ICSSIP_HEADER_CONTENT_LENGTH + ICSSIP_HEADER_DIVERSION ICSSIP_HEADER_TERMINATOR ICSSIP_HEADER_NOT_DEFINED ICSSIP_HEADER_MAX @@ -222,6 +223,7 @@ func initHeaderString() { ICSSIP_HEADER_STRING[ICSSIP_HEADER_WARNING] = "Warning: %s\r\n" ICSSIP_HEADER_STRING[ICSSIP_HEADER_WWW_AUTHENTICATE] = "WWW-Authenticate: %s\r\n" ICSSIP_HEADER_STRING[ICSSIP_HEADER_TERMINATOR] = "\r\n" + ICSSIP_HEADER_STRING[ICSSIP_HEADER_DIVERSION] = "Diversion: ; reason=unknown; counter=1\r\n" ICSSIP_HEADER_STRING2TYPE = make(map[string]ICSSIP_HEADER_TYPE) ICSSIP_HEADER_STRING2TYPE["ACCEPT"] = ICSSIP_HEADER_ACCEPT @@ -272,6 +274,7 @@ func initHeaderString() { ICSSIP_HEADER_STRING2TYPE["VIA"] = ICSSIP_HEADER_VIA ICSSIP_HEADER_STRING2TYPE["WARNING"] = ICSSIP_HEADER_WARNING ICSSIP_HEADER_STRING2TYPE["WWW-AUTHENTICATE"] = ICSSIP_HEADER_WWW_AUTHENTICATE + ICSSIP_HEADER_STRING2TYPE["Diversion"] = ICSSIP_HEADER_DIVERSION } func initStatusCodeString() { diff --git a/start b/start new file mode 100755 index 0000000..37fbf97 --- /dev/null +++ b/start @@ -0,0 +1,4 @@ +#!/usr/bin/sh +BIN=$ICSVG_ROOT/bin +msg=`$BIN/loader` +echo $msg