/[axel]/trunk/tcp.c
ViewVC logotype

Contents of /trunk/tcp.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Jan 11 11:57:58 2008 UTC (5 years, 4 months ago) by appaji-guest
File MIME type: text/plain
File size: 2919 byte(s)
Import 1.0a
1 /********************************************************************\
2 * Axel -- A lighter download accelerator for Linux and other Unices. *
3 * *
4 * Copyright 2001 Wilmer van der Gaast *
5 \********************************************************************/
6
7 /* TCP control file */
8
9 /*
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License with
21 the Debian GNU/Linux distribution in file /usr/doc/copyright/GPL;
22 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #include "axel.h"
27
28 /* Get a TCP connection */
29 int tcp_connect( char *hostname, int port, char *local_if )
30 {
31 struct hostent *host = NULL;
32 struct sockaddr_in addr;
33 struct sockaddr_in local;
34 int fd;
35
36 #ifdef DEBUG
37 socklen_t i = sizeof( local );
38
39 fprintf( stderr, "tcp_connect( %s, %i ) = ", hostname, port );
40 #endif
41
42 /* Why this loop? Because the call might return an empty record.
43 At least it very rarely does, on my system... */
44 for( fd = 0; fd < 5; fd ++ )
45 {
46 if( ( host = gethostbyname( hostname ) ) == NULL )
47 return( -1 );
48 if( *host->h_name ) break;
49 }
50 if( !host || !host->h_name || !*host->h_name )
51 return( -1 );
52
53 if( ( fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
54 return( -1 );
55
56 if( local_if && *local_if )
57 {
58 local.sin_family = AF_INET;
59 local.sin_port = 0;
60 local.sin_addr.s_addr = inet_addr( local_if );
61 if( bind( fd, (struct sockaddr *) &local, sizeof( struct sockaddr_in ) ) == -1 )
62 {
63 close( fd );
64 return( -1 );
65 }
66 }
67
68 addr.sin_family = AF_INET;
69 addr.sin_port = htons( port );
70 addr.sin_addr = *( (struct in_addr *) host->h_addr );
71
72 if( connect( fd, (struct sockaddr *) &addr, sizeof( struct sockaddr_in ) ) == -1 )
73 {
74 close( fd );
75 return( -1 );
76 }
77
78 #ifdef DEBUG
79 getsockname( fd, &local, &i );
80 fprintf( stderr, "%i\n", ntohs( local.sin_port ) );
81 #endif
82
83 return( fd );
84 }
85
86 int get_if_ip( char *iface, char *ip )
87 {
88 struct ifreq ifr;
89 int fd = socket( PF_INET, SOCK_DGRAM, IPPROTO_IP );
90
91 memset( &ifr, 0, sizeof( struct ifreq ) );
92
93 strcpy( ifr.ifr_name, iface );
94 ifr.ifr_addr.sa_family = AF_INET;
95 if( ioctl( fd, SIOCGIFADDR, &ifr ) == 0 )
96 {
97 struct sockaddr_in *x = (struct sockaddr_in *) &ifr.ifr_addr;
98 strcpy( ip, inet_ntoa( x->sin_addr ) );
99 return( 1 );
100 }
101 else
102 {
103 return( 0 );
104 }
105 }

  ViewVC Help
Powered by ViewVC 1.1.5