- Timestamp:
- 2010-03-17T23:23:27Z (15 years ago)
- Branches:
- master
- Children:
- c1a3c27
- Parents:
- e8c8d00
- Location:
- lib
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/ftutil.c
re8c8d00 r60e4df3 28 28 #include "lib/ftutil.h" 29 29 30 /* Some ifdefs for ulibc and apparently also BSD (Thanks to Whoopie) */31 #ifndef HOST_NAME_MAX32 #include <sys/param.h>33 #ifdef MAXHOSTNAMELEN34 #define HOST_NAME_MAX MAXHOSTNAMELEN35 #else36 #define HOST_NAME_MAX 25537 #endif38 #endif39 40 30 #define ASSERTSOCKOP(op, msg) \ 41 31 if( (op) == -1 ) {\ 42 sprintf( errmsg, msg ": %s", strerror( errno ) ); \32 g_snprintf( errmsg, sizeof( errmsg ), msg ": %s", strerror( errno ) ); \ 43 33 return -1; } 44 34 … … 46 36 * Creates a listening socket and returns it in saddr_ptr. 47 37 */ 48 int ft_listen( struct sockaddr_storage *saddr_ptr, char *host p, char *port, int for_bitlbee_client, char **errptr )38 int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr ) 49 39 { 50 int fd, gret,saddrlen;40 int fd, gret, saddrlen; 51 41 struct addrinfo hints, *rp; 52 42 socklen_t ssize = sizeof( struct sockaddr_storage ); 53 43 struct sockaddr_storage saddrs, *saddr = &saddrs; 54 44 static char errmsg[1024]; 55 char host[ HOST_NAME_MAX + 1 ];56 45 char *ftlisten = global.conf->ft_listen; 57 46 58 *errptr = errmsg; 47 if( errptr ) 48 *errptr = errmsg; 59 49 60 s printf( port, "0" );50 strcpy( port, "0" ); 61 51 62 52 /* Format is <IP-A>[:<Port-A>];<IP-B>[:<Port-B>] where … … 64 54 * and B is for connections with IM peers. 65 55 */ 66 if( ftlisten ) { 56 if( ftlisten ) 57 { 67 58 char *scolon = strchr( ftlisten, ';' ); 68 59 char *colon; 69 60 70 if( scolon ) { 71 if( for_bitlbee_client ) { 61 if( scolon ) 62 { 63 if( for_bitlbee_client ) 64 { 72 65 *scolon = '\0'; 73 s printf( host, ftlisten);66 strncpy( host, ftlisten, HOST_NAME_MAX ); 74 67 *scolon = ';'; 75 } else {76 sprintf( host, scolon + 1 );77 68 } 78 } else { 79 sprintf( host, ftlisten ); 69 else 70 { 71 strncpy( host, scolon + 1, HOST_NAME_MAX ); 72 } 73 } 74 else 75 { 76 strncpy( host, ftlisten, HOST_NAME_MAX ); 80 77 } 81 78 82 if( ( colon = strchr( host, ':' ) ) ) { 79 if( ( colon = strchr( host, ':' ) ) ) 80 { 83 81 *colon = '\0'; 84 s printf( port, colon + 1);82 strncpy( port, colon + 1, 5 ); 85 83 } 86 } else { 84 } 85 else 86 { 87 87 ASSERTSOCKOP( gethostname( host, HOST_NAME_MAX + 1 ), "gethostname()" ); 88 88 } … … 92 92 hints.ai_flags = AI_NUMERICSERV; 93 93 94 if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 ) { 94 if ( ( gret = getaddrinfo( host, port, &hints, &rp ) ) != 0 ) 95 { 95 96 sprintf( errmsg, "getaddrinfo() failed: %s", gai_strerror( gret ) ); 96 97 return -1; … … 104 105 105 106 ASSERTSOCKOP( fd = socket( saddr->ss_family, SOCK_STREAM, 0 ), "Opening socket" ); 106 107 107 ASSERTSOCKOP( bind( fd, ( struct sockaddr *)saddr, saddrlen ), "Binding socket" ); 108 109 108 ASSERTSOCKOP( listen( fd, 1 ), "Making socket listen" ); 110 109 111 110 if ( !inet_ntop( saddr->ss_family, saddr->ss_family == AF_INET ? 112 ( void * )&( ( struct sockaddr_in * ) saddr )->sin_addr.s_addr : ( void * )&( ( struct sockaddr_in6 * ) saddr )->sin6_addr.s6_addr 113 , host, INET6_ADDRSTRLEN ) ) { 114 sprintf( errmsg, "inet_ntop failed on listening socket" ); 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" ); 115 116 return -1; 116 117 } … … 119 120 120 121 if( saddr->ss_family == AF_INET ) 121 sprintf( port, "%d", ntohs( ( (struct sockaddr_in *) saddr )->sin_port ) );122 g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in *) saddr )->sin_port ) ); 122 123 else 123 sprintf( port, "%d", ntohs( ( (struct sockaddr_in6 *) saddr )->sin6_port ) );124 g_snprintf( port, 6, "%d", ntohs( ( (struct sockaddr_in6 *) saddr )->sin6_port ) ); 124 125 125 126 if( saddr_ptr ) 126 127 memcpy( saddr_ptr, saddr, saddrlen ); 127 128 128 strcpy( hostp, host ); 129 129 /* I hate static-length strings.. */ 130 host[HOST_NAME_MAX] = '\0'; 131 port[5] = '\0'; 132 130 133 return fd; 131 134 } -
lib/ftutil.h
re8c8d00 r60e4df3 26 26 #endif 27 27 28 /* Some ifdefs for ulibc and apparently also BSD (Thanks to Whoopie) */ 29 #ifndef HOST_NAME_MAX 30 #include <sys/param.h> 31 #ifdef MAXHOSTNAMELEN 32 #define HOST_NAME_MAX MAXHOSTNAMELEN 33 #else 34 #define HOST_NAME_MAX 255 35 #endif 36 #endif 37 38 /* This function should be used with care. host should be AT LEAST a 39 char[HOST_NAME_MAX+1] and port AT LEAST a char[6]. */ 28 40 int ft_listen( struct sockaddr_storage *saddr_ptr, char *host, char *port, int for_bitlbee_client, char **errptr );
Note: See TracChangeset
for help on using the changeset viewer.