<!-- Kamailio Pull Request Template -->
<!-- IMPORTANT: - for detailed contributing guidelines, read: https://github.com/kamailio/kamailio/blob/master/.github/CONTRIBUTING.md - pull requests must be done to master branch, unless they are backports of fixes from master branch to a stable branch - backports to stable branches must be done with 'git cherry-pick -x ...' - code is contributed under BSD for core and main components (tm, sl, auth, tls) - code is contributed GPLv2 or a compatible license for the other components - GPL code is contributed with OpenSSL licensing exception -->
#### Pre-Submission Checklist <!-- Go over all points below, and after creating the PR, tick all the checkboxes that apply --> <!-- All points should be verified, otherwise, read the CONTRIBUTING guidelines from above--> <!-- If you're unsure about any of these, don't hesitate to ask on sr-dev mailing list --> - [x] Commit message has the format required by CONTRIBUTING guide - [x] Commits are split per component (core, individual modules, libs, utils, ...) - [x] Each component has a single commit (if not, squash them into one commit) - [x] No commits to README files for modules (changes must be done to docbook files in `doc/` subfolder, the README file is autogenerated)
#### Type Of Change - [ ] Small bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds new functionality) - [ ] Breaking change (fix or feature that would change existing functionality)
#### Checklist: <!-- Go over all points below, and after creating the PR, tick the checkboxes that apply --> - [ ] PR should be backported to stable branches - [x] Tested changes locally - [ ] Related to issue #XXXX (replace XXXX with an open issue number)
#### Description <!-- Describe your changes in detail --> new function that read alias from Contact header then write to given avp as sip uri You can view, comment on, or merge this pull request online at:
https://github.com/kamailio/kamailio/pull/2124
-- Commit Summary --
* parse_uri : added new function proto type int to str * nathelper : added new function set_alias_to_avp * nathelper : added description of set_alias_to_avp function
-- File Changes --
M src/core/parser/parse_uri.c (23) M src/core/parser/parse_uri.h (2) M src/modules/nathelper/nathelper.c (176)
-- Patch Links --
https://github.com/kamailio/kamailio/pull/2124.patch https://github.com/kamailio/kamailio/pull/2124.diff
This function , later , will be used for keepalive modules. I have new updates for keepalive module.
henningw commented on this pull request.
Thanks you Yasin for the pull request. I've did a quick review, and have added some comments and questions to the code.
@@ -1439,3 +1439,26 @@ void proto_type_to_str(unsigned short type, str *s) {
*s = s_null; } } + +void proto_type_int_to_str(int type, str *s) {
What about just using the existing function proto_type_to_str? You can easily cast the input parameter from int to unsigned short, or I am wrong?
+ contact.s = ((contact_body_t *)msg->contact->parsed)->contacts->name.s; + contact.len = ((contact_body_t *)msg->contact->parsed)->contacts->len; + + alias_to_uri(&contact,&alias_uri); + write_to_avp(msg, &alias_uri, uri_avp); + + return 1; + +} +/** +* write_to_avp function writes data to given avp +* @param data source data +* @param uri_avp destination avp name +*/ +void write_to_avp(struct sip_msg *msg, str *data, str *uri_avp){
It would be probably a good idea to also return an error code if this function fails, to later evaluate it in the set_alias_to_avp function.
+ if(!data->s){ + LM_ERR("There isnt any data to write avp\n"); + return; + } + + valx.flags = PV_VAL_STR; + valx.rs.s = data->s; + valx.rs.len = data->len; + + LM_DBG("result: %.*s\n", valx.rs.len, valx.rs.s); + pvresult->setf(msg, &pvresult->pvp, (int)EQ_T, &valx); + +} +/** +* function alias_to_uri select alias paramter from @param contact_header
spelling - parameter
+ valx.flags = PV_VAL_STR; + valx.rs.s = data->s; + valx.rs.len = data->len; + + LM_DBG("result: %.*s\n", valx.rs.len, valx.rs.s); + pvresult->setf(msg, &pvresult->pvp, (int)EQ_T, &valx); + +} +/** +* function alias_to_uri select alias paramter from @param contact_header +* then writes to @param alias_uri +* @param contact_header Source contact header +* @param alias_uri destination string +*/ +void alias_to_uri(str *contact_header, str *alias_uri){
Same as above, consider to return an error in case this function fails to properly evaluate it in the calling function.
+} +/** +* function reads from @param msg then write to given avp @param uri_avp +* as sip uri +* @result 1 succes , -1 fail +* @param msg sip message +* @param uri_avp given avp name +*/ +static int set_alias_to_avp_f(struct sip_msg *msg, str *uri_avp){ + str contact; + str alias_uri={0,0}; + + + if(parse_headers(msg,HDR_CONTACT_F,0) < 0 ) { + LM_ERR("Finding Contact Header is failed\n");
Spelling - "Could not find Contact Header"
- str contact;
+ str alias_uri={0,0}; + + + if(parse_headers(msg,HDR_CONTACT_F,0) < 0 ) { + LM_ERR("Finding Contact Header is failed\n"); + return -1; + } + + if(!msg->contact) + return -1; + + if(parse_contact(msg->contact)<0 || !msg->contact->parsed || + ((contact_body_t *)msg->contact->parsed)->contacts==NULL || + ((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL){ + LM_ERR("Parsing Contact Header is failed\n");
same
- if(!msg->contact)
+ return -1; + + if(parse_contact(msg->contact)<0 || !msg->contact->parsed || + ((contact_body_t *)msg->contact->parsed)->contacts==NULL || + ((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL){ + LM_ERR("Parsing Contact Header is failed\n"); + return -1; + } + + + + contact.s = ((contact_body_t *)msg->contact->parsed)->contacts->name.s; + contact.len = ((contact_body_t *)msg->contact->parsed)->contacts->len; + + alias_to_uri(&contact,&alias_uri);
as referenced below, return value checks for both functions
- if(memchr_pointer == NULL) {
+ LM_ERR("No alias param found for port\n"); + return ; + } else { + port.len = memchr_pointer - port.s; + i=i+port.len; + } + + //last char is proto 0,1,2,3,4..7 + proto.s= &port.s[port.len+1]; + proto_type_int_to_str(atoi(proto.s), &proto); + + LM_DBG("Host [%.*s][port: %.*s][proto: %.*s] \r\n",host.len,host.s,port.len,port.s,proto.len,proto.s); + + //sip:host:port;transport=udp + alias_uri->s =(char *) pkg_malloc(port.len+host.len+proto.len+16);
When is alias_uri->s freed?
I will solve problems then add a docs. Thanks for support Henning
ycaner06 commented on this pull request.
@@ -1439,3 +1439,26 @@ void proto_type_to_str(unsigned short type, str *s) {
*s = s_null; } } + +void proto_type_int_to_str(int type, str *s) {
i got a error for conversion .Now fixed it in next commit
ycaner06 commented on this pull request.
- if(memchr_pointer == NULL) {
+ LM_ERR("No alias param found for port\n"); + return ; + } else { + port.len = memchr_pointer - port.s; + i=i+port.len; + } + + //last char is proto 0,1,2,3,4..7 + proto.s= &port.s[port.len+1]; + proto_type_int_to_str(atoi(proto.s), &proto); + + LM_DBG("Host [%.*s][port: %.*s][proto: %.*s] \r\n",host.len,host.s,port.len,port.s,proto.len,proto.s); + + //sip:host:port;transport=udp + alias_uri->s =(char *) pkg_malloc(port.len+host.len+proto.len+16);
i looked some examples in codes for pv->setf and i dont think that it should be freed.
@ycaner06 pushed 1 commit.
2fff396c3af35216c9c3f879b373d232c686e939 nathelper : fixed memory-leak for set_alias_to_avp
Hello, if it is ok for function name , i will add doc. thanks
Looking at the resulting diff of the PR (https://patch-diff.githubusercontent.com/raw/kamailio/kamailio/pull/2124.dif...), I see that the src/core/parser/parse_uri.h has a single empty line removed. I guess that is not really needed.
The the name of the C function exported to kamailio.cfg file is named `ki_set_alias_to_avp`, but the `ki_` prefix is used for functions exported to KEMI interface. Those exported to kamailio.cfg have usually the `w_` prefix. Then the prototype for kamailio.cfg functions is with three parameters (one sip_msg* and two char*), it is safer to set it like this even if the 2nd parameter is not used, because the core interpreter gives NULL as 2nd char* param when executing the function.
Then, you should export the new function to kemi interface as well, which is more or less the `set_alias_to_avp_f()`, eventually you name this one `ki_set_alias_to_avp`.
@ycaner06 pushed 1 commit.
b602b65ec32434f3820c90cb293e83209dfad756 nathelper : new function set_alias_to_avp_f is renamed
@ycaner06 pushed 1 commit.
c00a8cd53d7cea41dedacc63afde507da1c4f9ac nathelper : added doc for set_alias_to_avp
Looking once again at the code, the function can set the value in any (pseduo-)variable, not only in AVP, right? Because it is using pv->setf() function, not the AVP specific functions.
Do you want to restrict to writing to AVP or it is fine writing to any variable?
Looking once again at the code, the function can set the value in any (pseduo-)variable, not only in AVP, right? Because it is using pv->setf() function, not the AVP specific functions.
Do you want to restrict to writing to AVP or it is fine writing to any variable?
I dont want to restrict because it will be use to add keepalive module as a destination or it can be use to setting/checking/comparing other variables like $du , $ru etc.
If you don't want to restrict it to avps, probably it would be good to rename the function (code and configuration file export) to "set_alias_to_pv" or something like this. I guess this was intention of Daniels question.
@ycaner06 pushed 1 commit.
2f863bfbf95e0f9dec590c4ec5f1839d94f8120d nathelper : set_alias_to_avp is renamed to set_alias_to_pv
Merged #2124 into master.
Thank you, merged. I will add 1-2 small formatting changes directly in git master. If there are more changes, they can be done directly in git master as well.