[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 | #define ASSERTSOCKOP(op, msg) \ |
---|
| 31 | if( (op) == -1 ) {\ |
---|
[60e4df3] | 32 | g_snprintf( errmsg, sizeof( errmsg ), msg ": %s", strerror( errno ) ); \ |
---|
[5d550c5] | 33 | return -1; } |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | * Creates a listening socket and returns it in saddr_ptr. |
---|
| 37 | */ |
---|
[60e4df3] | 38 | int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr ) |
---|
[5d550c5] | 39 | { |
---|
[60e4df3] | 40 | int fd, gret, saddrlen; |
---|
[5d550c5] | 41 | struct addrinfo hints, *rp; |
---|
| 42 | socklen_t ssize = sizeof( struct sockaddr_storage ); |
---|
| 43 | struct sockaddr_storage saddrs, *saddr = &saddrs; |
---|
| 44 | static char errmsg[1024]; |
---|
| 45 | char *ftlisten = global.conf->ft_listen; |
---|
| 46 | |
---|
[60e4df3] | 47 | if( errptr ) |
---|
| 48 | *errptr = errmsg; |
---|
[5d550c5] | 49 | |
---|
[60e4df3] | 50 | strcpy( port, "0" ); |
---|
[5d550c5] | 51 | |
---|
| 52 | /* Format is <IP-A>[:<Port-A>];<IP-B>[:<Port-B>] where |
---|
| 53 | * A is for connections with the bitlbee client (DCC) |
---|
| 54 | * and B is for connections with IM peers. |
---|
| 55 | */ |
---|
[60e4df3] | 56 | if( ftlisten ) |
---|
| 57 | { |
---|
[5d550c5] | 58 | char *scolon = strchr( ftlisten, ';' ); |
---|
| 59 | char *colon; |
---|
| 60 | |
---|
[60e4df3] | 61 | if( scolon ) |
---|
| 62 | { |
---|
| 63 | if( for_bitlbee_client ) |
---|
| 64 | { |
---|
[5d550c5] | 65 | *scolon = '\0'; |
---|
[60e4df3] | 66 | strncpy( host, ftlisten, HOST_NAME_MAX ); |
---|
[5d550c5] | 67 | *scolon = ';'; |
---|
| 68 | } |
---|
[60e4df3] | 69 | else |
---|
| 70 | { |
---|
| 71 | strncpy( host, scolon + 1, HOST_NAME_MAX ); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | else |
---|
| 75 | { |
---|
| 76 | strncpy( host, ftlisten, HOST_NAME_MAX ); |
---|
[5d550c5] | 77 | } |
---|
| 78 | |
---|
[60e4df3] | 79 | if( ( colon = strchr( host, ':' ) ) ) |
---|
| 80 | { |
---|
[5d550c5] | 81 | *colon = '\0'; |
---|
[60e4df3] | 82 | strncpy( port, colon + 1, 5 ); |
---|
[5d550c5] | 83 | } |
---|
[60e4df3] | 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
[5d550c5] | 87 | ASSERTSOCKOP( gethostname( host, HOST_NAME_MAX + 1 ), "gethostname()" ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | memset( &hints, 0, sizeof( struct addrinfo ) ); |
---|
| 91 | hints.ai_socktype = SOCK_STREAM; |
---|
| 92 | hints.ai_flags = AI_NUMERICSERV; |
---|
| 93 | |
---|
[60e4df3] | 94 | if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 ) |
---|
| 95 | { |
---|
[5d550c5] | 96 | sprintf( errmsg, "getaddrinfo() failed: %s", gai_strerror( gret ) ); |
---|
| 97 | return -1; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | saddrlen = rp->ai_addrlen; |
---|
| 101 | |
---|
| 102 | memcpy( saddr, rp->ai_addr, saddrlen ); |
---|
| 103 | |
---|
| 104 | freeaddrinfo( rp ); |
---|
| 105 | |
---|
| 106 | ASSERTSOCKOP( fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening socket" ); |
---|
| 107 | ASSERTSOCKOP( bind( fd, ( struct sockaddr *)saddr, saddrlen ), "Binding socket" ); |
---|
| 108 | ASSERTSOCKOP( listen( fd, 1 ), "Making socket listen" ); |
---|
| 109 | |
---|
| 110 | if ( !inet_ntop( saddr->ss_family, saddr->ss_family == AF_INET ? |
---|
[60e4df3] | 111 | ( void * )&( ( struct sockaddr_in * ) saddr )->sin_addr.s_addr : |
---|
| 112 | ( void * )&( ( struct sockaddr_in6 * ) saddr )->sin6_addr.s6_addr, |
---|
| 113 | host, HOST_NAME_MAX ) ) |
---|
| 114 | { |
---|
| 115 | strcpy( errmsg, "inet_ntop failed on listening socket" ); |
---|
[5d550c5] | 116 | return -1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | ASSERTSOCKOP( getsockname( fd, ( struct sockaddr *)saddr, &ssize ), "Getting socket name" ); |
---|
| 120 | |
---|
| 121 | if( saddr->ss_family == AF_INET ) |
---|
[60e4df3] | 122 | g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in *) saddr )->sin_port ) ); |
---|
[5d550c5] | 123 | else |
---|
[60e4df3] | 124 | g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in6 *) saddr )->sin6_port ) ); |
---|
[5d550c5] | 125 | |
---|
| 126 | if( saddr_ptr ) |
---|
| 127 | memcpy( saddr_ptr, saddr, saddrlen ); |
---|
| 128 | |
---|
[60e4df3] | 129 | /* I hate static-length strings.. */ |
---|
| 130 | host[HOST_NAME_MAX] = '\0'; |
---|
| 131 | port[5] = '\0'; |
---|
| 132 | |
---|
[5d550c5] | 133 | return fd; |
---|
| 134 | } |
---|