Guest User

Untitled

a guest
May 20th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. /*
  2.  * Copyright 2012 Google Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. // Author: [email protected] (Matt Atterbury)
  18.  
  19. #include "net/instaweb/util/public/hostname_util.h"
  20.  
  21. #include <limits.h>
  22. // The following break portability.
  23.  
  24. // Windows doesn't use <unistd.h> nor does it define HOST_NAME_MAX.
  25. #if defined(WIN32)
  26. #include <windows.h>
  27. #include <winsock2.h>
  28. #define HOST_NAME_MAX (MAX_COMPUTERNAME_LENGTH+1)
  29. // MacOS does not defined HOST_NAME_MAX so fall back to the POSIX value.
  30. // We are supposed to use sysconf(_SC_HOST_NAME_MAX) but we use this value
  31. // to size an automatic array and we can't portably use variables for that.
  32. #elif !defined(HOST_NAME_MAX)
  33. #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
  34. #endif
  35.  
  36. #include "base/logging.h"
  37. #include "net/instaweb/util/public/string.h"
  38. #include "net/instaweb/util/public/string_util.h"
  39.  
  40. namespace net_instaweb {
  41.  
  42. GoogleString GetHostname() {
  43.   char hostname[HOST_NAME_MAX + 1];
  44.   hostname[sizeof(hostname) - 1] = '\0';
  45.  
  46.   // This call really shouldn't fail, so crash if it does under Debug,
  47.   // but ensure an empty (safe) value under Release.
  48.   int err = gethostname(hostname, sizeof(hostname) - 1);
  49.   if (err != 0) {
  50.     DLOG(FATAL) << "gethostname failed: " << err;
  51.     hostname[0] = '\0';
  52.   }
  53.  
  54.   return GoogleString(hostname);
  55. }
  56.  
  57. bool IsLocalhost(StringPiece host_to_test, StringPiece hostname) {
  58.   return (host_to_test == "localhost" ||
  59.           host_to_test == "127.0.0.1" ||
  60.           host_to_test == "::1" ||
  61.           host_to_test == hostname);
  62. }
  63.  
  64. }  // namespace net_instaweb
Advertisement
Add Comment
Please, Sign In to add comment