<!-- Kamailio Project uses GitHub Issues only for bugs in the code or feature requests. Please use this template only for bug reports.
If you have questions about using Kamailio or related to its configuration file, ask on sr-users mailing list:
* http://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
If you have questions about developing extensions to Kamailio or its existing C code, ask on sr-dev mailing list:
* http://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-dev
Please try to fill this template as much as possible for any issue. It helps the developers to troubleshoot the issue.
If there is no content to be filled in a section, the entire section can be removed.
You can delete the comments from the template sections when filling.
You can delete next line and everything above before submitting (it is a comment). -->
### Description
gethostname not accurate in kubernetes
<!-- Explain what you did, what you expected to happen, and what actually happened. -->
### Troubleshooting
#### Reproduction
<!-- If the issue can be reproduced, describe how it can be done. --> on a recent test with kamailio in kubernetes, found that gethostname can return only the hostname and not the fqdn. this depends on the kind of object is used (deployment/statefulset)
### Possible Solutions
<!-- If you found a solution or workaround for the issue, describe it. Ideally, provide a pull request with a fix. -->
the below patch fixed the issues i was having
``` diff --git a/src/modules/ipops/ipops_pv.c b/src/modules/ipops/ipops_pv.c index 5f77aa9..f65b4dd 100644 --- a/src/modules/ipops/ipops_pv.c +++ b/src/modules/ipops/ipops_pv.c @@ -455,6 +455,11 @@ if (gethostname(hbuf, 512)<0) { LM_WARN("gethostname failed - host pvs will be null\n"); return -1; + } else { + struct hostent* h; + if((h = gethostbyname(hbuf)) != NULL) { + memcpy(hbuf, h->h_name, 512); + } }
hlen = strlen(hbuf);
```
### Additional Information
* **Kamailio Version** - output of `kamailio -v`
``` latest master ```
* **Operating System**:
<!-- Details about the operating system, the type: Linux (e.g.,: Debian 8.4, Ubuntu 16.04, CentOS 7.1, ...), MacOS, xBSD, Solaris, ...; Kernel details (output of `uname -a`) -->
``` buster & centos7 ```
I haven't investigated the issue, nor the fix, but I know that use if gethostbyname() is no longer recommended, being marked as obsolete:
* http://man7.org/linux/man-pages/man3/gethostbyname.3.html
``` The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete. Applications should use getaddrinfo(3), getnameinfo(3), and gai_strerror(3) instead. ```
At some point we should consider replacing these obsolete functions where they are already used in the code, but for now we should try not to add more use of them...
Could you try look this output. ```sh [safarov@safarov-dell ~]$ kubectl exec -it proxy-dc0-0 -- sh Defaulting container name to proxy. Use 'kubectl describe pod/proxy-dc0-0 -n ippbx' to see all of the containers in this pod. / # hostname -f proxy-dc0-0.proxy-dc0.ippbx.svc.cluster.local / # hostname proxy-dc0-0 ``` Here pod `proxy-dc0-0` member of `proxy-dc0` statefulset in `ippbx` namespace. Think `kamailio` may use similar API calls.
Here output of strace command ``` / # strace hostname -f execve("/bin/hostname", ["hostname", "-f"], 0x7ffd0fb7df78 /* 84 vars */) = 0 arch_prctl(ARCH_SET_FS, 0x7f36d29e7d48) = 0 set_tid_address(0x7f36d29e831c) = 34 mprotect(0x7f36d29e4000, 4096, PROT_READ) = 0 mprotect(0x564ae99b0000, 16384, PROT_READ) = 0 getuid() = 0 uname({sysname="Linux", nodename="test-st-0", ...}) = 0 open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3 fcntl(3, F_SETFD, FD_CLOEXEC) = 0 read(3, "# Kubernetes-managed hosts file."..., 1024) = 248 read(3, "", 1024) = 0 close(3) = 0 open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 3 fcntl(3, F_SETFD, FD_CLOEXEC) = 0 read(3, "# Kubernetes-managed hosts file."..., 1024) = 248 read(3, "", 1024) = 0 close(3) = 0 ioctl(1, TIOCGWINSZ, {ws_row=41, ws_col=173, ws_xpixel=0, ws_ypixel=0}) = 0 writev(1, [{iov_base="test-st-0.test-st.ippbx.svc.clus"..., iov_len=41}, {iov_base="\n", iov_len=1}], 2test-st-0.test-st.ippbx.svc.cluster.local ) = 42 exit_group(0) = ? +++ exited with 0 +++ ``` Looks as `hostname -f` take value from `/etc/hosts` file ``` / # cat /etc/hosts # Kubernetes-managed hosts file. 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet fe00::0 ip6-mcastprefix fe00::1 ip6-allnodes fe00::2 ip6-allrouters 172.21.0.141 test-st-0.test-st.ippbx.svc.cluster.local test-st-0 ``` Test made using this statefulset. ```yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: test-st spec: podManagementPolicy: Parallel serviceName: test-st replicas: 1 selector: matchLabels: app: test-st template: metadata: labels: app: test-st spec: containers: - name: proxy securityContext: privileged: true image: kamailio/kamailio-ci:master-alpine.debug imagePullPolicy: IfNotPresent command: - /bin/sh args: - "-c" - "sleep 360000000000" ```
if you prefer DNS method, then this may be used ```sh / # ip -4 addr show dev eth0 3: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 9001 qdisc noqueue state UP inet 172.21.0.141/32 brd 172.21.0.141 scope global eth0 valid_lft forever preferred_lft forever / # nslookup 172.21.0.141 nslookup: can't resolve '(null)': Name does not resolve
Name: 172.21.0.141 Address 1: 172.21.0.141 test-st-0.test-st.ippbx.svc.cluster.local ``` Generally container may have multiple NIC and IP addresses. Think need to execute until first susses result returned.
[Here](https://en.opensuse.org/openSUSE:Packaging_checks#binary-or-shlib-calls-geth...) recommended
The gethostbyname*() and gethostbyaddr*() functions are deprecated as among other things they are not IPv6 ready. Applications should use getaddrinfo(3) and getnameinfo(3) instead. Please work with upstream to port the application to the modern interface. Ulrich Drepper explains the issues in more detail.
Related https://github.com/kamailio/kamailio/issues/1714
Revising this one after quite some time, I looked at the code and the $HN(...) variable is supposed to work with `gethostname()`, being related to hostname value.
The proposed change to use `gethostbyname()` in the result from `gethostname()` is changing the purpose, because I thing there can be situations when the dns result has something else in the `h->h_name` than the value of local hostname.
@lazedo: so maybe we should look at what you actually need to get, instead of changing the behaviour of $HN().
Is it the full domain based on local IP? For what you need it (only for logs or use in sip uri, ...)?
Maybe a new var or field to this var has to be added.
@lazedo Any feedback from your side to the questions/remarks from Daniel?
@henningw we've been applying that patch in our builds and it works for us, but i'm ok in closing the issue.
Closing old feature request. If someone still has interest, the best is to make directly a PR.
Closed #2119.