source: lib/ftutil.c @ 5d550c5

Last change on this file since 5d550c5 was 5d550c5, checked in by ulim <a.sporto+bee@…>, at 2008-08-12T09:24:09Z

Added lib/ftutil.[ch].

yes, that was supposed to happen in the last commit ;)

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[5d550c5]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Utility functions for file transfer                                      *
5*                                                                           *
6*  Copyright 2008 Uli Meis <a.sporto+bee@gmail.com>                         *
7*                                                                           *
8*  This program is free software; you can redistribute it and/or modify     *
9*  it under the terms of the GNU General Public License as published by     *
10*  the Free Software Foundation; either version 2 of the License, or        *
11*  (at your option) any later version.                                      *
12*                                                                           *
13*  This program is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
16*  GNU General Public License for more details.                             *
17*                                                                           *
18*  You should have received a copy of the GNU General Public License along  *
19*  with this program; if not, write to the Free Software Foundation, Inc.,  *
20*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
21*                                                                           *
22\***************************************************************************/
23
24#define BITLBEE_CORE
25#include "bitlbee.h"
26#include <poll.h>
27#include <netinet/tcp.h>
28#include "lib/ftutil.h"
29
30/* Some ifdefs for ulibc and apparently also BSD (Thanks to Whoopie) */
31#ifndef HOST_NAME_MAX
32#include <sys/param.h>
33#ifdef MAXHOSTNAMELEN
34#define HOST_NAME_MAX MAXHOSTNAMELEN
35#else
36#define HOST_NAME_MAX 255
37#endif
38#endif
39
40#ifndef AI_NUMERICSERV
41#define AI_NUMERICSERV 0x0400   /* Don't use name resolution.  */
42#endif
43
44#define ASSERTSOCKOP(op, msg) \
45        if( (op) == -1 ) {\
46                sprintf( errmsg , msg ": %s", strerror( errno ) ); \
47                return -1; }
48
49/*
50 * Creates a listening socket and returns it in saddr_ptr.
51 */
52int ft_listen( struct sockaddr_storage *saddr_ptr, char *hostp, char *port, int for_bitlbee_client, char **errptr )
53{
54        int fd,gret,saddrlen;
55        struct addrinfo hints, *rp;
56        socklen_t ssize = sizeof( struct sockaddr_storage );
57        struct sockaddr_storage saddrs, *saddr = &saddrs;
58        static char errmsg[1024];
59        char host[ HOST_NAME_MAX + 1 ];
60        char *ftlisten = global.conf->ft_listen;
61
62        *errptr = errmsg;
63
64        sprintf( port, "0" );
65
66        /* Format is <IP-A>[:<Port-A>];<IP-B>[:<Port-B>] where
67         * A is for connections with the bitlbee client (DCC)
68         * and B is for connections with IM peers.
69         */
70        if( ftlisten ) {
71                char *scolon = strchr( ftlisten, ';' );
72                char *colon;
73
74                if( scolon ) {
75                        if( for_bitlbee_client ) {
76                                *scolon = '\0';
77                                sprintf( host, ftlisten );
78                                *scolon = ';';
79                        } else {
80                                sprintf( host, scolon + 1 );
81                        }
82                } else {
83                        sprintf( host, ftlisten );
84                }
85
86                if( ( colon = strchr( host, ':' ) ) ) {
87                        *colon = '\0';
88                        sprintf( port, colon + 1 );
89                }
90        } else {
91                ASSERTSOCKOP( gethostname( host, HOST_NAME_MAX + 1 ), "gethostname()" );
92        }
93
94        memset( &hints, 0, sizeof( struct addrinfo ) );
95        hints.ai_socktype = SOCK_STREAM;
96        hints.ai_flags = AI_NUMERICSERV;
97
98        if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 ) {
99                sprintf( errmsg, "getaddrinfo() failed: %s", gai_strerror( gret ) );
100                return -1;
101        }
102
103        saddrlen = rp->ai_addrlen;
104
105        memcpy( saddr, rp->ai_addr, saddrlen );
106
107        freeaddrinfo( rp );
108
109        ASSERTSOCKOP( fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening socket" );
110
111        ASSERTSOCKOP( bind( fd, ( struct sockaddr *)saddr, saddrlen ), "Binding socket" );
112       
113        ASSERTSOCKOP( listen( fd, 1 ), "Making socket listen" );
114
115        if ( !inet_ntop( saddr->ss_family, saddr->ss_family == AF_INET ?
116                        ( void * )&( ( struct sockaddr_in * ) saddr )->sin_addr.s_addr : ( void * )&( ( struct sockaddr_in6 * ) saddr )->sin6_addr.s6_addr
117                        , host, INET6_ADDRSTRLEN ) ) {
118                sprintf( errmsg, "inet_ntop failed on listening socket" );
119                return -1;
120        }
121
122        ASSERTSOCKOP( getsockname( fd, ( struct sockaddr *)saddr, &ssize ), "Getting socket name" );
123
124        if( saddr->ss_family == AF_INET )
125                sprintf( port, "%d", ntohs( ( ( struct sockaddr_in *) saddr )->sin_port ) );
126        else
127                sprintf( port, "%d", ntohs( ( ( struct sockaddr_in6 *) saddr )->sin6_port ) );
128
129        if( saddr_ptr )
130                memcpy( saddr_ptr, saddr, saddrlen );
131
132        strcpy( hostp, host );
133
134        return fd;
135}
Note: See TracBrowser for help on using the repository browser.