private void _btnRemoveBindings_Click(object sender, EventArgs e) { FormHelper.ValidateIsNotEmpty(_txtRemoveUser.Text, "user"); SendRequest(() => { var request = CreateRegisterRequest(_txtRemoveUser.Text); request.Contacts.Add(SipContactHeader.CreateWildCard()); request.Expires = HeaderFactory.CreateExpiresHeader(0); return(request); }); }
protected SipRequest CreateRequest(string method) { /*create the 'INVITE' message*/ SipAddressFactory addressFactory = _stack.CreateAddressFactory(); SipHeaderFactory headerFactory = _stack.CreateHeaderFactory(); SipMessageFactory messageFactory = _stack.CreateMessageFactory(); SipUri senderSipUri = addressFactory.CreateUri(string.Empty, new IPEndPoint(_ipAddress, 12345).ToString()); SipAddress senderSipAddress = addressFactory.CreateAddress(string.Empty, senderSipUri); SipUri receiverSipUri = addressFactory.CreateUri(string.Empty, new IPEndPoint(_ipAddress, 23456).ToString()); SipAddress receiverSipAddress = addressFactory.CreateAddress(string.Empty, senderSipUri); SipToHeader toHeader = headerFactory.CreateToHeader(receiverSipAddress); SipFromHeader fromHeader = headerFactory.CreateFromHeader(senderSipAddress, SipUtil.CreateTag()); SipCSeqHeader cseqHeader = headerFactory.CreateSCeqHeader(method, 1); SipCallIdHeader callIdheader = headerFactory.CreateCallIdHeader(SipUtil.CreateCallId()); SipViaHeader viaHeader = headerFactory.CreateViaHeader(_ipAddress, 12345, SipConstants.Udp, SipUtil.CreateBranch()); SipMaxForwardsHeader maxForwardsHeader = headerFactory.CreateMaxForwardsHeader(); SipRequest request = messageFactory.CreateRequest( receiverSipUri, method, callIdheader, cseqHeader, fromHeader, toHeader, viaHeader, maxForwardsHeader); if (method == SipMethods.Invite) { SipContactHeader contactHeader = headerFactory.CreateContactHeader(senderSipUri); request.Contacts.Add(contactHeader); } return(request); }
public void ProcessRequest(SipRequestEvent requestEvent) { var request = requestEvent.Request; var requestLine = request.RequestLine; var toUri = request.To.SipUri; var aor = toUri.FormatToString(); var newContacts = request.Contacts.ToList(); if (requestLine.Method == SipMethods.Register) { if (!string.IsNullOrWhiteSpace(requestLine.Uri.User)) { /*throw invalid exception bc the user portion for a register request must be empty*/ throw new SipException(SipResponseCodes.x400_Bad_Request, "The sip uri can not contain a user."); } /*10.3. 1. The registrar inspects the Request-URI to determine whether it * has access to bindings for the domain identified in the * Request-URI. If not, and if the server also acts as a proxy * server, the server SHOULD forward the request to the addressed * domain, following the general behavior for proxying messages * described in Section 16.*/ /*The registrar inspects the Request-URI to determine whether it * has access to bindings for the domain identified in the Request-URI. * Here we give an exception back to the client because we only supports single domain */ if (requestLine.Uri.Host != _settings.Domain) { throw new SipException(SipResponseCodes.x404_Not_Found); } if (toUri.Host != _settings.Domain) { /*If the address-of-record is not valid for the domain in the Request-URI, * the registrar MUST send a 404 (Not Found) response and skip the remaining steps.*/ throw new SipException(SipResponseCodes.x404_Not_Found); } bool hasWildCard = newContacts.Any(c => c.IsWildCard); if (hasWildCard) { if (IsInvalidWildCardRequest(request)) { throw new SipException(SipResponseCodes.x400_Bad_Request); } /*case: deregister: remove all bindings for the aor */ foreach (var binding in _addressBindingService.GetByAddressOfRecord(aor)) { if (binding.CallId != request.CallId.Value) { _addressBindingService.Remove(binding); continue; } if (request.CSeq.Sequence > binding.CSeq) { _addressBindingService.Remove(binding); } } } /*The registrar extracts the address-of-record from the To header * field of the request. If the address-of-record is not valid * for the domain in the Request-URI, the registrar MUST send a * 404 (Not Found) response and skip the remaining steps. The URI * MUST then be converted to a canonical form. To do that, all * URI parameters MUST be removed (including the user-param), and * any escaped characters MUST be converted to their unescaped * form. The result serves as an index into the list of bindings.*/ /* Each binding record records the Call-ID and CSeq values from the request.*/ foreach (var contact in newContacts) { if (!SipUtil.IsIPAddress(contact.SipUri.Host)) { throw new SipException(SipResponseCodes.x400_Bad_Request, "Contact uri must have numeric IP format."); } var host = new IPEndPoint(SipUtil.ParseIpAddress(contact.SipUri.Host), contact.SipUri.Port); int expires = _settings.DefaultExpires; if (request.Expires != null) { expires = request.Expires.Value; } if (contact.Expires.HasValue) { expires = contact.Expires.Value; } var newAddressBinding = new SipAddressBinding( aor, host, request.CSeq.Sequence, request.CallId.Value, expires); if (expires == 0) { _addressBindingService.Remove(newAddressBinding); } else if (expires < _settings.MinimumExpires) { throw new SipException(SipResponseCodes.x423_Interval_Too_Brief); } else { _addressBindingService.AddOrUpdate(newAddressBinding); } } var okResponse = request.CreateResponse(SipResponseCodes.x200_Ok); var contactsAddresses = new List <SipContactHeader>(); foreach (var binding in _addressBindingService.GetByAddressOfRecord(aor)) { var contactAddress = new SipContactHeader(); contactAddress.Expires = (int)(binding.EndTime - DateTime.Now).TotalSeconds; contactAddress.SipUri = new SipUri() { Host = binding.Host.Address.ToString(), Port = binding.Host.Port }; contactsAddresses.Add(contactAddress); } contactsAddresses.ForEach(okResponse.Contacts.Add); /*set the response*/ if (requestEvent.ServerTransaction != null) { requestEvent.ServerTransaction.SendResponse(okResponse); requestEvent.IsHandled = true; requestEvent.IsSent = true; } else { requestEvent.Response = okResponse; requestEvent.IsHandled = true; } } }