masqmail
diff src/mservdetect.c @ 188:bfa7a8b566da
refactoring and simplifications in mservdetect
author | meillo@marmaro.de |
---|---|
date | Thu, 15 Jul 2010 00:33:09 +0200 |
parents | bd7c52a36b0c |
children | 41958685480d |
line diff
1.1 --- a/src/mservdetect.c Thu Jul 15 00:14:26 2010 +0200 1.2 +++ b/src/mservdetect.c Thu Jul 15 00:33:09 2010 +0200 1.3 @@ -18,74 +18,40 @@ 1.4 */ 1.5 1.6 1.7 -#include "config.h" 1.8 #include "masqmail.h" 1.9 #include "readsock.h" 1.10 1.11 1.12 - 1.13 gboolean 1.14 -init_sockaddr(struct sockaddr_in * name, interface * iface) 1.15 +init_sockaddr2(struct sockaddr_in * name, gchar* addr, int port) 1.16 { 1.17 struct hostent *he; 1.18 struct in_addr ia; 1.19 1.20 - if (inet_aton(iface->address, &ia) != 0) { 1.21 + if (inet_aton(addr, &ia) != 0) { 1.22 /* IP address */ 1.23 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr)); 1.24 } else { 1.25 - if ((he = gethostbyname(iface->address)) == NULL) { 1.26 - logwrite(LOG_ALERT, "local address '%s' unknown. (deleting)\n", iface->address); 1.27 + if ((he = gethostbyname(addr)) == NULL) { 1.28 + fprintf(stderr, "local address '%s' unknown. (deleting)\n", addr); 1.29 return FALSE; 1.30 } 1.31 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr)); 1.32 } 1.33 name->sin_family = AF_INET; 1.34 - name->sin_port = htons(iface->port); 1.35 + name->sin_port = htons(port); 1.36 1.37 return TRUE; 1.38 } 1.39 1.40 1.41 -int 1.42 -make_server_socket(interface * iface) 1.43 -{ 1.44 - int sock = -1; 1.45 - struct sockaddr_in server; 1.46 - 1.47 - memset(&server, 0, sizeof(struct sockaddr_in)); 1.48 - 1.49 - /* Create the socket. */ 1.50 - sock = socket(PF_INET, SOCK_STREAM, 0); 1.51 - if (sock < 0) { 1.52 - logwrite(LOG_ALERT, "socket: %s\n", strerror(errno)); 1.53 - return -1; 1.54 - } 1.55 - 1.56 - if (init_sockaddr(&server, iface)) { 1.57 - /* bind the socket */ 1.58 - if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) { 1.59 - logwrite(LOG_ALERT, "bind: %s\n", strerror(errno)); 1.60 - return -1; 1.61 - } 1.62 - } else { 1.63 - close(sock); 1.64 - return -1; 1.65 - } 1.66 - 1.67 - return sock; 1.68 -} 1.69 - 1.70 - 1.71 - 1.72 - 1.73 gchar* 1.74 -mserver_detect_online(interface * iface) 1.75 +mserver_detect_online(gchar* addr, int port) 1.76 { 1.77 struct sockaddr_in saddr; 1.78 gchar *ret = NULL; 1.79 1.80 - if (!init_sockaddr(&saddr, iface)) { 1.81 + if (!init_sockaddr2(&saddr, addr, port)) { 1.82 return NULL; 1.83 } 1.84 1.85 @@ -107,23 +73,18 @@ 1.86 } 1.87 1.88 /* this is the protocol (reverse engineered): 1.89 - S: READY 1.90 - C: STAT 1.91 - S: DOWN 1.92 - C: QUIT 1.93 - -> offline 1.94 - 1.95 - S: READY 1.96 - C: STAT 1.97 - S: UP foo:-1 1.98 - C: QUIT 1.99 - -> offline 1.100 - 1.101 - S: READY 1.102 - C: STAT 1.103 - S: UP foo:1 1.104 - C: QUIT 1.105 - -> online, `foo' gets printed 1.106 + 1.107 + S: READY 1.108 + C: STAT 1.109 + | 1.110 + +----------------+-----------------+ 1.111 + | | | 1.112 + S: DOWN S: UP foo:-1 S: UP foo:1 1.113 + C: QUIT C: QUIT C: QUIT 1.114 + 1.115 + -> offline -> offline -> online 1.116 + `foo' gets printed 1.117 + 1.118 */ 1.119 1.120 if (strncmp(buf, "READY", 5) == 0) { 1.121 @@ -164,22 +125,11 @@ 1.122 } 1.123 1.124 1.125 -void 1.126 -logwrite(int pri, const char *fmt, ...) 1.127 -{ 1.128 - va_list args; 1.129 - va_start(args, fmt); 1.130 - 1.131 - vfprintf(stderr, fmt, args); 1.132 - 1.133 - va_end(args); 1.134 -} 1.135 - 1.136 - 1.137 int 1.138 main(int argc, char *argv[]) 1.139 { 1.140 - interface iface; 1.141 + gchar* addr; 1.142 + int port; 1.143 gchar *name; 1.144 1.145 if (argc != 3) { 1.146 @@ -187,10 +137,10 @@ 1.147 return 1; 1.148 } 1.149 1.150 - iface.address = g_strdup(argv[1]); 1.151 - iface.port = atoi(argv[2]); 1.152 + addr = argv[1]; 1.153 + port = atoi(argv[2]); 1.154 1.155 - name = mserver_detect_online(&iface); 1.156 + name = mserver_detect_online(addr, port); 1.157 1.158 if (name) { 1.159 printf("%s\n", name);