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 | #ifdef HAVE_CONFIG_H |
---|
25 | #include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <sys/types.h> |
---|
29 | #include <sys/socket.h> |
---|
30 | #include <netinet/in.h> |
---|
31 | #include <unistd.h> |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | #include <netdb.h> |
---|
35 | extern int h_errno; |
---|
36 | |
---|
37 | #include <glib.h> |
---|
38 | |
---|
39 | #include "gnome-vfs.h" |
---|
40 | #include "gnome-vfs-private.h" |
---|
41 | |
---|
42 | |
---|
43 | struct GnomeVFSInetConnection { |
---|
44 | struct sockaddr_in addr; |
---|
45 | guint sock; |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | GnomeVFSResult |
---|
50 | gnome_vfs_inet_connection_create (GnomeVFSInetConnection **connection_return, |
---|
51 | const gchar *host_name, |
---|
52 | guint host_port, |
---|
53 | GnomeVFSCancellation *cancellation) |
---|
54 | { |
---|
55 | GnomeVFSInetConnection *new; |
---|
56 | struct hostent *host_info; |
---|
57 | struct sockaddr_in addr; |
---|
58 | gint sock; |
---|
59 | |
---|
60 | g_return_val_if_fail (connection_return != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); |
---|
61 | g_return_val_if_fail (host_name != NULL, GNOME_VFS_ERROR_BAD_PARAMETERS); |
---|
62 | g_return_val_if_fail (host_port != 0, GNOME_VFS_ERROR_BAD_PARAMETERS); |
---|
63 | |
---|
64 | sock = socket (PF_INET, SOCK_STREAM, 0); |
---|
65 | if (sock < 0) |
---|
66 | return gnome_vfs_result_from_errno (); |
---|
67 | |
---|
68 | host_info = gethostbyname (host_name); |
---|
69 | if (gnome_vfs_cancellation_check (cancellation)) |
---|
70 | return GNOME_VFS_ERROR_CANCELLED; |
---|
71 | |
---|
72 | if (host_info == NULL) |
---|
73 | return gnome_vfs_result_from_h_errno (); |
---|
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 | new = g_new (GnomeVFSInetConnection, 1); |
---|
83 | memcpy (&new->addr, &addr, sizeof (addr)); |
---|
84 | new->sock = sock; |
---|
85 | |
---|
86 | *connection_return = new; |
---|
87 | |
---|
88 | return GNOME_VFS_OK; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void |
---|
93 | gnome_vfs_inet_connection_destroy (GnomeVFSInetConnection *connection, |
---|
94 | GnomeVFSCancellation *cancellation) |
---|
95 | { |
---|
96 | g_return_if_fail (connection != NULL); |
---|
97 | |
---|
98 | close (connection->sock); |
---|
99 | g_free (connection); |
---|
100 | } |
---|
101 | |
---|
102 | GnomeVFSIOBuf * |
---|
103 | gnome_vfs_inet_connection_get_iobuf (GnomeVFSInetConnection *connection) |
---|
104 | { |
---|
105 | g_return_val_if_fail (connection != NULL, NULL); |
---|
106 | |
---|
107 | return gnome_vfs_iobuf_new (connection->sock); |
---|
108 | } |
---|