socket_local_client.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2006 The Android Open Source Project
  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. #include <cutils/sockets.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #ifdef HAVE_WINSOCK
  23. int socket_local_client(const char *name, int namespaceId, int type)
  24. {
  25. errno = ENOSYS;
  26. return -1;
  27. }
  28. #else /* !HAVE_WINSOCK */
  29. #include <sys/socket.h>
  30. #include <sys/un.h>
  31. #include <sys/select.h>
  32. #include <sys/types.h>
  33. #include "socket_local.h"
  34. #define LISTEN_BACKLOG 4
  35. /* Documented in header file. */
  36. int socket_make_sockaddr_un(const char *name, int namespaceId,
  37. struct sockaddr_un *p_addr, socklen_t *alen)
  38. {
  39. memset (p_addr, 0, sizeof (*p_addr));
  40. size_t namelen;
  41. switch (namespaceId) {
  42. case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
  43. #ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
  44. namelen = strlen(name);
  45. // Test with length +1 for the *initial* '\0'.
  46. if ((namelen + 1) > sizeof(p_addr->sun_path)) {
  47. goto error;
  48. }
  49. /*
  50. * Note: The path in this case is *not* supposed to be
  51. * '\0'-terminated. ("man 7 unix" for the gory details.)
  52. */
  53. p_addr->sun_path[0] = 0;
  54. memcpy(p_addr->sun_path + 1, name, namelen);
  55. #else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
  56. /* this OS doesn't have the Linux abstract namespace */
  57. namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
  58. /* unix_path_max appears to be missing on linux */
  59. if (namelen > sizeof(*p_addr)
  60. - offsetof(struct sockaddr_un, sun_path) - 1) {
  61. goto error;
  62. }
  63. strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
  64. strcat(p_addr->sun_path, name);
  65. #endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
  66. break;
  67. case ANDROID_SOCKET_NAMESPACE_RESERVED:
  68. namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
  69. /* unix_path_max appears to be missing on linux */
  70. if (namelen > sizeof(*p_addr)
  71. - offsetof(struct sockaddr_un, sun_path) - 1) {
  72. goto error;
  73. }
  74. strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
  75. strcat(p_addr->sun_path, name);
  76. break;
  77. case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
  78. namelen = strlen(name);
  79. /* unix_path_max appears to be missing on linux */
  80. if (namelen > sizeof(*p_addr)
  81. - offsetof(struct sockaddr_un, sun_path) - 1) {
  82. goto error;
  83. }
  84. strcpy(p_addr->sun_path, name);
  85. break;
  86. default:
  87. // invalid namespace id
  88. return -1;
  89. }
  90. p_addr->sun_family = AF_LOCAL;
  91. *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
  92. return 0;
  93. error:
  94. return -1;
  95. }
  96. /**
  97. * connect to peer named "name" on fd
  98. * returns same fd or -1 on error.
  99. * fd is not closed on error. that's your job.
  100. *
  101. * Used by AndroidSocketImpl
  102. */
  103. int socket_local_client_connect(int fd, const char *name, int namespaceId,
  104. int type)
  105. {
  106. struct sockaddr_un addr;
  107. socklen_t alen;
  108. size_t namelen;
  109. int err;
  110. err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
  111. if (err < 0) {
  112. goto error;
  113. }
  114. if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
  115. goto error;
  116. }
  117. return fd;
  118. error:
  119. return -1;
  120. }
  121. /**
  122. * connect to peer named "name"
  123. * returns fd or -1 on error
  124. */
  125. int socket_local_client(const char *name, int namespaceId, int type)
  126. {
  127. int s;
  128. s = socket(AF_LOCAL, type, 0);
  129. if(s < 0) return -1;
  130. if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
  131. close(s);
  132. return -1;
  133. }
  134. return s;
  135. }
  136. #endif /* !HAVE_WINSOCK */