| 793 | */ |
| 794 | |
| 795 | const char * /* O - FQDN for connection or system */ |
| 796 | httpGetHostname(http_t *http, /* I - HTTP connection or NULL */ |
| 797 | char *s, /* I - String buffer for name */ |
| 798 | int slen) /* I - Size of buffer */ |
| 799 | { |
| 800 | if (http) |
| 801 | { |
| 802 | if (!s || slen <= 1) |
| 803 | { |
| 804 | if (http->hostname[0] == '/') |
| 805 | return ("localhost"); |
| 806 | else |
| 807 | return (http->hostname); |
| 808 | } |
| 809 | else if (http->hostname[0] == '/') |
| 810 | strlcpy(s, "localhost", (size_t)slen); |
| 811 | else |
| 812 | strlcpy(s, http->hostname, (size_t)slen); |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | /* |
| 817 | * Get the hostname... |
| 818 | */ |
| 819 | |
| 820 | if (!s || slen <= 1) |
| 821 | return (NULL); |
| 822 | |
| 823 | if (gethostname(s, (size_t)slen) < 0) |
| 824 | strlcpy(s, "localhost", (size_t)slen); |
| 825 | |
| 826 | if (!strchr(s, '.')) |
| 827 | { |
| 828 | #ifdef HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME |
| 829 | /* |
| 830 | * The hostname is not a FQDN, so use the local hostname from the |
| 831 | * SystemConfiguration framework... |
| 832 | */ |
| 833 | |
| 834 | SCDynamicStoreRef sc = SCDynamicStoreCreate(kCFAllocatorDefault, |
| 835 | CFSTR("libcups"), NULL, NULL); |
| 836 | /* System configuration data */ |
| 837 | CFStringRef local = sc ? SCDynamicStoreCopyLocalHostName(sc) : NULL; |
| 838 | /* Local host name */ |
| 839 | char localStr[1024]; /* Local host name C string */ |
| 840 | |
| 841 | if (local && CFStringGetCString(local, localStr, sizeof(localStr), |
| 842 | kCFStringEncodingUTF8)) |
| 843 | { |
| 844 | /* |
| 845 | * Append ".local." to the hostname we get... |
| 846 | */ |
| 847 | |
| 848 | snprintf(s, (size_t)slen, "%s.local.", localStr); |
| 849 | } |
| 850 | |
| 851 | if (local) |
| 852 | CFRelease(local); |