masqmail

view src/mservdetect.c @ 336:2d4aa516ba0e

updated ChangeLog and NEWS
author markus schnalke <meillo@marmaro.de>
date Sat, 27 Aug 2011 18:38:55 +0200
parents bd7c52a36b0c
children 41958685480d
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
21 #include "masqmail.h"
22 #include "readsock.h"
25 gboolean
26 init_sockaddr2(struct sockaddr_in * name, gchar* addr, int port)
27 {
28 struct hostent *he;
29 struct in_addr ia;
31 if (inet_aton(addr, &ia) != 0) {
32 /* IP address */
33 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
34 } else {
35 if ((he = gethostbyname(addr)) == NULL) {
36 fprintf(stderr, "local address '%s' unknown. (deleting)\n", addr);
37 return FALSE;
38 }
39 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
40 }
41 name->sin_family = AF_INET;
42 name->sin_port = htons(port);
44 return TRUE;
45 }
48 gchar*
49 mserver_detect_online(gchar* addr, int port)
50 {
51 struct sockaddr_in saddr;
52 gchar *ret = NULL;
54 if (!init_sockaddr2(&saddr, addr, port)) {
55 return NULL;
56 }
58 int sock = socket(PF_INET, SOCK_STREAM, 0);
59 int dup_sock;
60 if (connect(sock, (struct sockaddr *) (&saddr), sizeof(saddr)) != 0) {
61 return NULL;
62 }
64 FILE *in, *out;
65 char buf[256];
67 dup_sock = dup(sock);
68 out = fdopen(sock, "w");
69 in = fdopen(dup_sock, "r");
71 if (!read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
72 return NULL;
73 }
75 /* this is the protocol (reverse engineered):
77 S: READY
78 C: STAT
79 |
80 +----------------+-----------------+
81 | | |
82 S: DOWN S: UP foo:-1 S: UP foo:1
83 C: QUIT C: QUIT C: QUIT
85 -> offline -> offline -> online
86 `foo' gets printed
88 */
90 if (strncmp(buf, "READY", 5) == 0) {
91 fprintf(out, "STAT\n");
92 fflush(out);
93 if (read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
94 if (strncmp(buf, "DOWN", 4) == 0) {
95 ret = NULL;
96 } else if (strncmp(buf, "UP", 2) == 0) {
97 gchar *p = buf + 3;
98 while ((*p != ':') && *p) {
99 p++;
100 }
101 if (*p) {
102 *p = '\0';
103 p++;
104 if ((atoi(p) >= 0) && *p) {
105 /* `UP foo:N', where `N' is a non-negative number */
106 ret = g_strdup(buf + 3);
107 }
108 } else {
109 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf);
110 }
111 } else {
112 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf);
113 }
114 }
115 }
116 fprintf(out, "QUIT");
117 fflush(out);
119 close(sock);
120 close(dup_sock);
121 fclose(in);
122 fclose(out);
124 return ret;
125 }
128 int
129 main(int argc, char *argv[])
130 {
131 gchar* addr;
132 int port;
133 gchar *name;
135 if (argc != 3) {
136 fprintf(stderr, "usage: %s HOST PORT\n", argv[0]);
137 return 1;
138 }
140 addr = argv[1];
141 port = atoi(argv[2]);
143 name = mserver_detect_online(addr, port);
145 if (name) {
146 printf("%s\n", name);
147 return 0;
148 }
149 return 1;
150 }