Mercurial > masqmail
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 |
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 | 19 */ |
20 | |
21 | |
22 #include "masqmail.h" | |
23 #include "readsock.h" | |
164 | 24 |
25 | |
187 | 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 | 28 { |
29 struct hostent *he; | |
30 struct in_addr ia; | |
31 | |
188
bfa7a8b566da
refactoring and simplifications in mservdetect
meillo@marmaro.de
parents:
187
diff
changeset
|
32 if (inet_aton(addr, &ia) != 0) { |
187 | 33 /* IP address */ |
34 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr)); | |
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 | 38 return FALSE; |
39 } | |
40 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr)); | |
41 } | |
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 | 44 |
45 return TRUE; | |
46 } | |
47 | |
48 | |
164 | 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 | 51 { |
52 struct sockaddr_in saddr; | |
53 gchar *ret = NULL; | |
54 | |
188
bfa7a8b566da
refactoring and simplifications in mservdetect
meillo@marmaro.de
parents:
187
diff
changeset
|
55 if (!init_sockaddr2(&saddr, addr, port)) { |
187 | 56 return NULL; |
57 } | |
58 | |
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 } | |
164 | 64 |
187 | 65 FILE *in, *out; |
66 char buf[256]; | |
67 | |
68 dup_sock = dup(sock); | |
69 out = fdopen(sock, "w"); | |
70 in = fdopen(dup_sock, "r"); | |
71 | |
72 if (!read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) { | |
73 return NULL; | |
74 } | |
164 | 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 | 90 */ |
91 | |
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); | |
164 | 109 } |
187 | 110 } else { |
111 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf); | |
164 | 112 } |
187 | 113 } else { |
114 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf); | |
164 | 115 } |
116 } | |
117 } | |
187 | 118 fprintf(out, "QUIT"); |
119 fflush(out); | |
120 | |
121 close(sock); | |
122 close(dup_sock); | |
123 fclose(in); | |
124 fclose(out); | |
125 | |
164 | 126 return ret; |
127 } | |
128 | |
0 | 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 | 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 | 135 gchar *name; |
0 | 136 |
187 | 137 if (argc != 3) { |
138 fprintf(stderr, "usage: %s HOST PORT\n", argv[0]); | |
139 return 1; | |
140 } | |
0 | 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 | 144 |
188
bfa7a8b566da
refactoring and simplifications in mservdetect
meillo@marmaro.de
parents:
187
diff
changeset
|
145 name = mserver_detect_online(addr, port); |
0 | 146 |
187 | 147 if (name) { |
148 printf("%s\n", name); | |
149 return 0; | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
150 } |
187 | 151 return 1; |
0 | 152 } |