Mercurial > masqmail
annotate src/mservdetect.c @ 238:ec28ce798b79
minor improvements in wordings
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Mon, 25 Oct 2010 14:02:18 -0300 |
parents | bfa7a8b566da |
children | 41958685480d |
rev | line source |
---|---|
0 | 1 /* MasqMail |
2 Copyright (C) 1999-2001 Oliver Kurth | |
187 | 3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de> |
0 | 4 |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 2 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 */ | |
19 | |
20 | |
21 #include "masqmail.h" | |
22 #include "readsock.h" | |
164 | 23 |
24 | |
187 | 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 | 27 { |
28 struct hostent *he; | |
29 struct in_addr ia; | |
30 | |
188
bfa7a8b566da
refactoring and simplifications in mservdetect
meillo@marmaro.de
parents:
187
diff
changeset
|
31 if (inet_aton(addr, &ia) != 0) { |
187 | 32 /* IP address */ |
33 memcpy(&(name->sin_addr), &ia, sizeof(name->sin_addr)); | |
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 | 37 return FALSE; |
38 } | |
39 memcpy(&(name->sin_addr), he->h_addr, sizeof(name->sin_addr)); | |
40 } | |
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 | 43 |
44 return TRUE; | |
45 } | |
46 | |
47 | |
164 | 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 | 50 { |
51 struct sockaddr_in saddr; | |
52 gchar *ret = NULL; | |
53 | |
188
bfa7a8b566da
refactoring and simplifications in mservdetect
meillo@marmaro.de
parents:
187
diff
changeset
|
54 if (!init_sockaddr2(&saddr, addr, port)) { |
187 | 55 return NULL; |
56 } | |
57 | |
58 int sock = socket(PF_INET, SOCK_STREAM, 0); | |
59 int dup_sock; | |
60 if (connect(sock, (struct sockaddr *) (&saddr), sizeof(saddr)) != 0) { | |
61 return NULL; | |
62 } | |
164 | 63 |
187 | 64 FILE *in, *out; |
65 char buf[256]; | |
66 | |
67 dup_sock = dup(sock); | |
68 out = fdopen(sock, "w"); | |
69 in = fdopen(dup_sock, "r"); | |
70 | |
71 if (!read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) { | |
72 return NULL; | |
73 } | |
164 | 74 |
187 | 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 | 88 */ |
89 | |
90 if (strncmp(buf, "READY", 5) == 0) { | |
91 fprintf(out, "STAT\n"); | |
92 fflush(out); | |
93 if (read_sockline(in, buf, 256, 15, READSOCKL_CHUG)) { | |
94 if (strncmp(buf, "DOWN", 4) == 0) { | |
95 ret = NULL; | |
96 } else if (strncmp(buf, "UP", 2) == 0) { | |
97 gchar *p = buf + 3; | |
98 while ((*p != ':') && *p) { | |
99 p++; | |
100 } | |
101 if (*p) { | |
102 *p = '\0'; | |
103 p++; | |
104 if ((atoi(p) >= 0) && *p) { | |
105 /* `UP foo:N', where `N' is a non-negative number */ | |
106 ret = g_strdup(buf + 3); | |
164 | 107 } |
187 | 108 } else { |
109 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf); | |
164 | 110 } |
187 | 111 } else { |
112 fprintf(stderr, "unexpected response from mserver after STAT cmd: %s", buf); | |
164 | 113 } |
114 } | |
115 } | |
187 | 116 fprintf(out, "QUIT"); |
117 fflush(out); | |
118 | |
119 close(sock); | |
120 close(dup_sock); | |
121 fclose(in); | |
122 fclose(out); | |
123 | |
164 | 124 return ret; |
125 } | |
126 | |
0 | 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 | 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 | 133 gchar *name; |
0 | 134 |
187 | 135 if (argc != 3) { |
136 fprintf(stderr, "usage: %s HOST PORT\n", argv[0]); | |
137 return 1; | |
138 } | |
0 | 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 | 142 |
188
bfa7a8b566da
refactoring and simplifications in mservdetect
meillo@marmaro.de
parents:
187
diff
changeset
|
143 name = mserver_detect_online(addr, port); |
0 | 144 |
187 | 145 if (name) { |
146 printf("%s\n", name); | |
147 return 0; | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
148 } |
187 | 149 return 1; |
0 | 150 } |