masqmail
view src/mservdetect.c @ 172:cd38f10532e7
merge
author | meillo@marmaro.de |
---|---|
date | Wed, 14 Jul 2010 00:55:22 +0200 |
parents | 26e34ae9a3e3 |
children | bd7c52a36b0c |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
20 #include "config.h"
21 #include "masqmail.h"
22 #include "readsock.h"
25 gchar*
26 mserver_detect_online(interface * iface)
27 {
28 struct sockaddr_in saddr;
29 gchar *ret = NULL;
31 if (init_sockaddr(&saddr, iface)) {
32 int sock = socket(PF_INET, SOCK_STREAM, 0);
33 int dup_sock;
34 if (connect(sock, (struct sockaddr *) (&saddr), sizeof(saddr)) == 0) {
35 FILE *in, *out;
36 char buf[256];
38 dup_sock = dup(sock);
39 out = fdopen(sock, "w");
40 in = fdopen(dup_sock, "r");
42 if (read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
43 if (strncmp(buf, "READY", 5) == 0) {
44 fprintf(out, "STAT\n");
45 fflush(out);
46 if (read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
47 if (strncmp(buf, "DOWN", 4) == 0) {
48 ret = NULL;
49 } else if (strncmp(buf, "UP", 2) == 0) {
50 gchar *p = buf + 3;
51 while ((*p != ':') && *p)
52 p++;
53 if (*p) {
54 *p = 0;
55 p++;
56 if ((atoi(p) >= 0) && *p)
57 ret = g_strdup(buf + 3);
58 } else
59 logwrite(LOG_ALERT, "unexpected response from mserver after STAT cmd: %s", buf);
60 } else {
61 logwrite(LOG_ALERT, "unexpected response from mserver after STAT cmd: %s", buf);
62 }
63 }
64 }
65 fprintf(out, "QUIT");
66 fflush(out);
68 close(sock);
69 close(dup_sock);
70 fclose(in);
71 fclose(out);
72 }
73 }
74 }
75 return ret;
76 }
79 void
80 logwrite(int pri, const char *fmt, ...)
81 {
82 va_list args;
83 va_start(args, fmt);
85 vfprintf(stdout, fmt, args);
87 va_end(args);
88 }
90 void
91 debugf(const char *fmt, ...)
92 {
93 va_list args;
94 va_start(args, fmt);
96 vfprintf(stdout, fmt, args);
98 va_end(args);
99 }
101 int
102 main(int argc, char *argv[])
103 {
104 if (argc == 3) {
105 interface iface;
106 gchar *name;
108 iface.address = g_strdup(argv[1]);
109 iface.port = atoi(argv[2]);
111 name = mserver_detect_online(&iface);
113 printf("%s\n", name);
115 exit(EXIT_SUCCESS);
116 } else {
117 fprintf(stderr, "usage %s <host> <port>\n", argv[0]);
118 exit(EXIT_FAILURE);
119 }
120 }