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