annotate src/mservdetect.c @ 214:ecd8d737d78e

removed the old manual because it is really outdated now In masqmail's 0.3 branch, so much changed that the old manual causes more harm than it improves the distribution. Most content is covered by the man pages anyway. You'll still find the old manual on masqmail's homepage.
author meillo@marmaro.de
date Mon, 19 Jul 2010 14:06:08 +0200
parents bfa7a8b566da
children 41958685480d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
1 /* MasqMail
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
2 Copyright (C) 1999-2001 Oliver Kurth
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
4
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
7 the Free Software Foundation; either version 2 of the License, or
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
8 (at your option) any later version.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
9
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
13 GNU General Public License for more details.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
14
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
16 along with this program; if not, write to the Free Software
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
18 */
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
19
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
20
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
21 #include "masqmail.h"
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
22 #include "readsock.h"
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
23
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
24
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
25 gboolean
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
26 init_sockaddr2(struct sockaddr_in * name, gchar* addr, int port)
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
27 {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
28 struct hostent *he;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
29 struct in_addr ia;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
30
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
31 if (inet_aton(addr, &ia) != 0) {
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
32 /* IP address */
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
33 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr));
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
34 } else {
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
35 if ((he = gethostbyname(addr)) == NULL) {
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
36 fprintf(stderr, "local address '%s' unknown. (deleting)\n", addr);
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
37 return FALSE;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
38 }
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
39 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr));
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
40 }
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
41 name->sin_family = AF_INET;
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
42 name->sin_port = htons(port);
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
43
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
44 return TRUE;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
45 }
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
46
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
47
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
48 gchar*
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
49 mserver_detect_online(gchar* addr, int port)
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
50 {
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
51 struct sockaddr_in saddr;
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
52 gchar *ret = NULL;
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
53
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
54 if (!init_sockaddr2(&saddr, addr, port)) {
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
55 return NULL;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
56 }
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
57
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
58 int sock = socket(PF_INET, SOCK_STREAM, 0);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
59 int dup_sock;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
60 if (connect(sock, (struct sockaddr *) (&saddr), sizeof(saddr)) != 0) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
61 return NULL;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
62 }
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
63
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
64 FILE *in, *out;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
65 char buf[256];
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
66
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
67 dup_sock = dup(sock);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
68 out = fdopen(sock, "w");
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
69 in = fdopen(dup_sock, "r");
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
70
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
71 if (!read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
72 return NULL;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
73 }
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
74
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
75 /* this is the protocol (reverse engineered):
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
76
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
77 S: READY
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
78 C: STAT
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
79 |
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
80 +----------------+-----------------+
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
81 | | |
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
82 S: DOWN S: UP foo:-1 S: UP foo:1
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
83 C: QUIT C: QUIT C: QUIT
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
84
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
85 -> offline -> offline -> online
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
86 `foo' gets printed
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
87
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
88 */
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
89
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
90 if (strncmp(buf, "READY", 5) == 0) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
91 fprintf(out, "STAT\n");
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
92 fflush(out);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
93 if (read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
94 if (strncmp(buf, "DOWN", 4) == 0) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
95 ret = NULL;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
96 } else if (strncmp(buf, "UP", 2) == 0) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
97 gchar *p = buf + 3;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
98 while ((*p != ':') && *p) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
99 p++;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
100 }
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
101 if (*p) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
102 *p = '\0';
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
103 p++;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
104 if ((atoi(p) >= 0) && *p) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
105 /* `UP foo:N', where `N' is a non-negative number */
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
106 ret = g_strdup(buf + 3);
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
107 }
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
108 } else {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
109 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf);
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
110 }
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
111 } else {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
112 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf);
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
113 }
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
114 }
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
115 }
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
116 fprintf(out, "QUIT");
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
117 fflush(out);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
118
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
119 close(sock);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
120 close(dup_sock);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
121 fclose(in);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
122 fclose(out);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
123
164
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
124 return ret;
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
125 }
5b621742b2e7 removed the mserver feature
meillo@marmaro.de
parents: 10
diff changeset
126
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
127
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
128 int
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
129 main(int argc, char *argv[])
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
130 {
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
131 gchar* addr;
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
132 int port;
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
133 gchar *name;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
134
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
135 if (argc != 3) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
136 fprintf(stderr, "usage: %s HOST PORT\n", argv[0]);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
137 return 1;
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
138 }
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
139
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
140 addr = argv[1];
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
141 port = atoi(argv[2]);
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
142
188
bfa7a8b566da refactoring and simplifications in mservdetect
meillo@marmaro.de
parents: 187
diff changeset
143 name = mserver_detect_online(addr, port);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
144
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
145 if (name) {
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
146 printf("%s\n", name);
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
147 return 0;
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
148 }
187
bd7c52a36b0c improved mservdetect in various ways
meillo@marmaro.de
parents: 164
diff changeset
149 return 1;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
150 }