annotate src/mservdetect.c @ 434:f2a7271746d1 default tip

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