masqmail
view src/mservdetect.c @ 421:f37384470855
Changed lockdir to /var/lock/masqmail; Create lockdir and piddir on startup.
Moved the lockdir out of the spool dir. (When /var/lock is a ramdisk
we do well to have the lock files there.) Added the new configure option
--with-lockdir to change that location. Nontheless, if we run_as_user,
then lock files are always stored in the spool dir directly.
Instead of installing the lockdir and piddir at installation time, we
create them on startup time now if they are missing. This is necessary
if lockdir or piddir are a tmpfs.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Wed, 30 May 2012 09:38:38 +0200 |
parents | 41958685480d |
children |
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
22 #include "masqmail.h"
23 #include "readsock.h"
26 gboolean
27 init_sockaddr2(struct sockaddr_in *name, gchar *addr, int port)
28 {
29 struct hostent *he;
30 struct in_addr ia;
32 if (inet_aton(addr, &ia) != 0) {
33 /* IP address */
34 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
35 } else {
36 if ((he = gethostbyname(addr)) == NULL) {
37 fprintf(stderr, "local address '%s' unknown. (deleting)\n", addr);
38 return FALSE;
39 }
40 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
41 }
42 name->sin_family = AF_INET;
43 name->sin_port = htons(port);
45 return TRUE;
46 }
49 gchar*
50 mserver_detect_online(gchar *addr, int port)
51 {
52 struct sockaddr_in saddr;
53 gchar *ret = NULL;
55 if (!init_sockaddr2(&saddr, addr, port)) {
56 return NULL;
57 }
59 int sock = socket(PF_INET, SOCK_STREAM, 0);
60 int dup_sock;
61 if (connect(sock, (struct sockaddr *) (&saddr), sizeof(saddr)) != 0) {
62 return NULL;
63 }
65 FILE *in, *out;
66 char buf[256];
68 dup_sock = dup(sock);
69 out = fdopen(sock, "w");
70 in = fdopen(dup_sock, "r");
72 if (!read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
73 return NULL;
74 }
76 /*
77 ** this is the protocol (reverse engineered):
78 **
79 ** S: READY
80 ** C: STAT
81 ** |
82 ** +----------------+-----------------+
83 ** | | |
84 ** S: DOWN S: UP foo:-1 S: UP foo:1
85 ** C: QUIT C: QUIT C: QUIT
86 **
87 ** -> offline -> offline -> online
88 ** `foo' gets printed
89 **
90 */
92 if (strncmp(buf, "READY", 5) == 0) {
93 fprintf(out, "STAT\n");
94 fflush(out);
95 if (read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
96 if (strncmp(buf, "DOWN", 4) == 0) {
97 ret = NULL;
98 } else if (strncmp(buf, "UP", 2) == 0) {
99 gchar *p = buf + 3;
100 while ((*p != ':') && *p) {
101 p++;
102 }
103 if (*p) {
104 *p = '\0';
105 p++;
106 if ((atoi(p) >= 0) && *p) {
107 /* `UP foo:N', where `N' is a non-negative number */
108 ret = g_strdup(buf + 3);
109 }
110 } else {
111 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf);
112 }
113 } else {
114 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf);
115 }
116 }
117 }
118 fprintf(out, "QUIT");
119 fflush(out);
121 close(sock);
122 close(dup_sock);
123 fclose(in);
124 fclose(out);
126 return ret;
127 }
130 int
131 main(int argc, char *argv[])
132 {
133 gchar *addr;
134 int port;
135 gchar *name;
137 if (argc != 3) {
138 fprintf(stderr, "usage: %s HOST PORT\n", argv[0]);
139 return 1;
140 }
142 addr = argv[1];
143 port = atoi(argv[2]);
145 name = mserver_detect_online(addr, port);
147 if (name) {
148 printf("%s\n", name);
149 return 0;
150 }
151 return 1;
152 }