masqmail

annotate src/mservdetect.c @ 175:25b769efafe6

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