1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* gnome-vfs-inet-connection.c - Functions for creating and destroying Internet |
---|
3 | connections. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
7 | The Gnome Library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The Gnome Library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Ettore Perazzoli <ettore@gnu.org> */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | #include "gnome-vfs-inet-connection.h" |
---|
26 | |
---|
27 | #include "gnome-vfs-private.h" |
---|
28 | #include "gnome-vfs.h" |
---|
29 | #include <errno.h> |
---|
30 | #include <glib.h> |
---|
31 | #include <netdb.h> |
---|
32 | #include <netinet/in.h> |
---|
33 | #include <string.h> |
---|
34 | #include <sys/socket.h> |
---|
35 | #include <sys/types.h> |
---|
36 | #include <unistd.h> |
---|
37 | |
---|
38 | extern int h_errno; |
---|
39 | |
---|
40 | |
---|
41 | struct GnomeVFSInetConnection { |
---|
42 | struct sockaddr_in addr; |
---|
43 | guint sock; |
---|
44 | }; |
---|
45 | |
---|
46 | |
---|
47 | GnomeVFSResult |
---|
48 | gnome_vfs_inet_connection_create (GnomeVFSInetConnection **connection_return, |
---|
49 | const gchar *host_name, |
---|
50 | guint host_port, |
---|
51 | GnomeVFSCancellation *cancellation) |
---|
52 | { |
---|
53 | GnomeVFSInetConnection *new; |
---|
54 | struct hostent *host_info; |
---|
55 | struct sockaddr_in addr; |
---|
56 | gint sock; |
---|
57 | |
---|
58 | g_return_val_if_fail (connection_return != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); |
---|
59 | g_return_val_if_fail (host_name != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); |
---|
60 | g_return_val_if_fail (host_port != 0, GNOME_VFS_ERROR_BAD_PARAMETERS); |
---|
61 | |
---|
62 | sock = socket (PF_INET, SOCK_STREAM, 0); |
---|
63 | if (sock < 0) |
---|
64 | return gnome_vfs_result_from_errno (); |
---|
65 | |
---|
66 | host_info = gethostbyname (host_name); |
---|
67 | if (gnome_vfs_cancellation_check (cancellation)) { |
---|
68 | return GNOME_VFS_ERROR_CANCELLED; |
---|
69 | } |
---|
70 | |
---|
71 | if (host_info == NULL) { |
---|
72 | return gnome_vfs_result_from_h_errno (); |
---|
73 | } |
---|
74 | |
---|
75 | addr.sin_family = host_info->h_addrtype; |
---|
76 | addr.sin_addr = * (struct in_addr *) host_info->h_addr; |
---|
77 | addr.sin_port = htons (host_port); |
---|
78 | |
---|
79 | if (connect (sock, (struct sockaddr *) &addr, sizeof (addr)) < 0) { |
---|
80 | return gnome_vfs_result_from_errno (); |
---|
81 | } |
---|
82 | |
---|
83 | new = g_new (GnomeVFSInetConnection, 1); |
---|
84 | memcpy (&new->addr, &addr, sizeof (addr)); |
---|
85 | new->sock = sock; |
---|
86 | |
---|
87 | *connection_return = new; |
---|
88 | |
---|
89 | return GNOME_VFS_OK; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | void |
---|
94 | gnome_vfs_inet_connection_destroy (GnomeVFSInetConnection *connection, |
---|
95 | GnomeVFSCancellation *cancellation) |
---|
96 | { |
---|
97 | g_return_if_fail (connection != NULL); |
---|
98 | |
---|
99 | close (connection->sock); |
---|
100 | g_free (connection); |
---|
101 | } |
---|
102 | |
---|
103 | static void |
---|
104 | gnome_vfs_inet_connection_close (GnomeVFSInetConnection *connection) |
---|
105 | { |
---|
106 | gnome_vfs_inet_connection_destroy (connection, NULL); |
---|
107 | } |
---|
108 | |
---|
109 | GnomeVFSIOBuf * |
---|
110 | gnome_vfs_inet_connection_get_iobuf (GnomeVFSInetConnection *connection) |
---|
111 | { |
---|
112 | g_return_val_if_fail (connection != NULL, NULL); |
---|
113 | |
---|
114 | return gnome_vfs_iobuf_new (connection->sock); |
---|
115 | } |
---|
116 | |
---|
117 | gint |
---|
118 | gnome_vfs_inet_connection_get_fd (GnomeVFSInetConnection *connection) |
---|
119 | { |
---|
120 | g_return_val_if_fail (connection != NULL, -1); |
---|
121 | return connection->sock; |
---|
122 | } |
---|
123 | |
---|
124 | /* SocketImpl for InetConnections */ |
---|
125 | |
---|
126 | static GnomeVFSResult |
---|
127 | gnome_vfs_inet_connection_read (GnomeVFSInetConnection *connection, |
---|
128 | gpointer buffer, |
---|
129 | GnomeVFSFileSize bytes, |
---|
130 | GnomeVFSFileSize *bytes_read) |
---|
131 | { |
---|
132 | gint read_val; |
---|
133 | |
---|
134 | do { |
---|
135 | read_val = read (connection->sock, buffer, bytes); |
---|
136 | } while (read_val == -1 && errno == EINTR); |
---|
137 | |
---|
138 | if (read_val == -1) { |
---|
139 | *bytes_read = 0; |
---|
140 | return gnome_vfs_result_from_errno (); |
---|
141 | } else { |
---|
142 | *bytes_read = read_val; |
---|
143 | } |
---|
144 | return GNOME_VFS_OK; |
---|
145 | } |
---|
146 | |
---|
147 | static GnomeVFSResult |
---|
148 | gnome_vfs_inet_connection_write (GnomeVFSInetConnection *connection, |
---|
149 | gconstpointer buffer, |
---|
150 | GnomeVFSFileSize bytes, |
---|
151 | GnomeVFSFileSize *bytes_written) |
---|
152 | { |
---|
153 | gint write_val; |
---|
154 | |
---|
155 | do { |
---|
156 | write_val = write (connection->sock, buffer, bytes); |
---|
157 | } while (write_val == -1 && errno == EINTR); |
---|
158 | |
---|
159 | if (write_val == -1) { |
---|
160 | *bytes_written = 0; |
---|
161 | return gnome_vfs_result_from_errno (); |
---|
162 | } else { |
---|
163 | *bytes_written = write_val; |
---|
164 | return GNOME_VFS_OK; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | static GnomeVFSSocketImpl inet_connection_socket_impl = { |
---|
169 | (GnomeVFSSocketReadFunc)gnome_vfs_inet_connection_read, |
---|
170 | (GnomeVFSSocketWriteFunc)gnome_vfs_inet_connection_write, |
---|
171 | (GnomeVFSSocketCloseFunc)gnome_vfs_inet_connection_close |
---|
172 | }; |
---|
173 | |
---|
174 | GnomeVFSSocket * |
---|
175 | gnome_vfs_inet_connection_to_socket (GnomeVFSInetConnection *connection) |
---|
176 | { |
---|
177 | return gnome_vfs_socket_new (&inet_connection_socket_impl, connection); |
---|
178 | } |
---|