netutil.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Network utilities for the Midnight Commander Virtual File System.
  2. Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2005, 2007
  3. Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License
  6. as published by the Free Software Foundation; either version 2 of
  7. the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /**
  16. * \file
  17. * \brief Source: Virtual File System: Network utilities
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <sys/types.h>
  24. #include <pwd.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include "lib/global.h"
  28. #include "utilvfs.h"
  29. #include "netutil.h"
  30. int got_sigpipe;
  31. static void
  32. sig_pipe (int unused)
  33. {
  34. (void) unused;
  35. got_sigpipe = 1;
  36. }
  37. void
  38. tcp_init (void)
  39. {
  40. struct sigaction sa;
  41. static char _initialized = 0;
  42. if (_initialized)
  43. return;
  44. got_sigpipe = 0;
  45. sa.sa_handler = sig_pipe;
  46. sa.sa_flags = 0;
  47. sigemptyset (&sa.sa_mask);
  48. sigaction (SIGPIPE, &sa, NULL);
  49. _initialized = 1;
  50. }
  51. /** Extract the hostname and username from the path
  52. *
  53. * Format of the path is [user@]hostname:port/remote-dir, e.g.:
  54. *
  55. * ftp://sunsite.unc.edu/pub/linux
  56. * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  57. * ftp://tsx-11.mit.edu:8192/
  58. * ftp://joe@foo.edu:11321/private
  59. * ftp://joe:password@foo.se
  60. *
  61. * @param path is an input string to be parsed
  62. * @param host is an outptun g_malloc()ed hostname
  63. * @param user is an outptut g_malloc()ed username
  64. * (NULL if not specified)
  65. * @param port is an outptut integer port number
  66. * @param pass is an outptut g_malloc()ed password
  67. * @param default_port is an input default port
  68. * @param flags are parsing modifier flags (@see VFS_URL_FLAGS)
  69. *
  70. * @return g_malloc()ed host, user and pass if they are present.
  71. * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
  72. * is not set, then the current login name is supplied.
  73. * Return value is a g_malloc()ed string with the pathname relative to the
  74. * host.
  75. */
  76. char *
  77. vfs_split_url (const char *path, char **host, char **user, int *port,
  78. char **pass, int default_port, enum VFS_URL_FLAGS flags)
  79. {
  80. char *dir, *colon, *inner_colon, *at, *rest;
  81. char *retval;
  82. char * const pcopy = g_strdup (path);
  83. const char *pend = pcopy + strlen (pcopy);
  84. if (pass)
  85. *pass = NULL;
  86. *port = default_port;
  87. *user = NULL;
  88. retval = NULL;
  89. dir = pcopy;
  90. if (!(flags & URL_NOSLASH)) {
  91. /* locate path component */
  92. while (*dir != PATH_SEP && *dir)
  93. dir++;
  94. if (*dir) {
  95. retval = g_strdup (dir);
  96. *dir = 0;
  97. } else
  98. retval = g_strdup (PATH_SEP_STR);
  99. }
  100. /* search for any possible user */
  101. at = strrchr (pcopy, '@');
  102. /* We have a username */
  103. if (at) {
  104. *at = 0;
  105. inner_colon = strchr (pcopy, ':');
  106. if (inner_colon) {
  107. *inner_colon = 0;
  108. inner_colon++;
  109. if (pass)
  110. *pass = g_strdup (inner_colon);
  111. }
  112. if (*pcopy != 0)
  113. *user = g_strdup (pcopy);
  114. if (pend == at + 1)
  115. rest = at;
  116. else
  117. rest = at + 1;
  118. } else
  119. rest = pcopy;
  120. if (!*user && !(flags & URL_USE_ANONYMOUS))
  121. *user = vfs_get_local_username();
  122. /* Check if the host comes with a port spec, if so, chop it */
  123. if ('[' == *rest) {
  124. colon = strchr (++rest, ']');
  125. if (colon) {
  126. colon[0] = '\0';
  127. colon[1] = '\0';
  128. colon++;
  129. } else {
  130. g_free (pcopy);
  131. g_free (retval);
  132. *host = NULL;
  133. *port = 0;
  134. return NULL;
  135. }
  136. } else
  137. colon = strchr (rest, ':');
  138. if (colon) {
  139. *colon = 0;
  140. if (sscanf (colon + 1, "%d", port) == 1) {
  141. if (*port <= 0 || *port >= 65536)
  142. *port = default_port;
  143. } else {
  144. while (*(++colon)) {
  145. switch (*colon) {
  146. case 'C':
  147. *port = 1;
  148. break;
  149. case 'r':
  150. *port = 2;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. if (host)
  157. *host = g_strdup (rest);
  158. g_free (pcopy);
  159. return retval;
  160. }