|
a/src/netcon.cpp |
|
b/src/netcon.cpp |
1 |
/* Copyright (C) 2002 J.F.Dockes
|
1 |
/* Copyright (C) 2002 J.F. Dockes
|
2 |
* This program is free software; you can redistribute it and/or modify
|
2 |
* This program is free software; you can redistribute it and/or modify
|
3 |
* it under the terms of the GNU General Public License as published by
|
3 |
* it under the terms of the GNU General Public License as published by
|
4 |
* the Free Software Foundation; either version 2 of the License, or
|
4 |
* the Free Software Foundation; either version 2 of the License, or
|
5 |
* (at your option) any later version.
|
5 |
* (at your option) any later version.
|
6 |
*
|
6 |
*
|
|
... |
|
... |
17 |
|
17 |
|
18 |
// Wrapper classes for the socket interface
|
18 |
// Wrapper classes for the socket interface
|
19 |
|
19 |
|
20 |
|
20 |
|
21 |
#ifndef TEST_NETCON
|
21 |
#ifndef TEST_NETCON
|
|
|
22 |
#ifdef BUILDING_RECOLL
|
|
|
23 |
#include "autoconfig.h"
|
|
|
24 |
#else
|
|
|
25 |
#include "config.h"
|
|
|
26 |
#endif
|
|
|
27 |
|
22 |
#include <stdio.h>
|
28 |
#include <stdio.h>
|
23 |
#include <stdlib.h>
|
29 |
#include <stdlib.h>
|
24 |
#include <string.h>
|
30 |
#include <string.h>
|
25 |
#include <errno.h>
|
31 |
#include <errno.h>
|
26 |
|
32 |
|
|
... |
|
... |
39 |
#include <netdb.h>
|
45 |
#include <netdb.h>
|
40 |
|
46 |
|
41 |
#include <map>
|
47 |
#include <map>
|
42 |
|
48 |
|
43 |
|
49 |
|
44 |
#ifdef HAVE_DEBUGLOG
|
50 |
#ifdef BUILDING_RECOLL
|
45 |
#include "debuglog.h"
|
51 |
#include "debuglog.h"
|
46 |
|
52 |
|
47 |
#else
|
53 |
#else
|
48 |
|
54 |
|
49 |
#define LOGFATAL(X)
|
55 |
#define LOGFATAL(X)
|
|
... |
|
... |
57 |
#define LOGDEB4(X)
|
63 |
#define LOGDEB4(X)
|
58 |
#endif
|
64 |
#endif
|
59 |
|
65 |
|
60 |
#include "netcon.h"
|
66 |
#include "netcon.h"
|
61 |
|
67 |
|
|
|
68 |
using namespace std;
|
|
|
69 |
|
62 |
#ifndef SOCKLEN_T
|
70 |
#ifndef SOCKLEN_T
|
63 |
#define SOCKLEN_T socklen_t
|
71 |
#define SOCKLEN_T socklen_t
|
64 |
#endif
|
72 |
#endif
|
65 |
|
73 |
|
66 |
// Size of path buffer in sockaddr_un (AF_UNIX socket
|
74 |
// Size of path buffer in sockaddr_un (AF_UNIX socket
|
|
... |
|
... |
74 |
|
82 |
|
75 |
// Need &one, &zero for setsockopt...
|
83 |
// Need &one, &zero for setsockopt...
|
76 |
static const int one = 1;
|
84 |
static const int one = 1;
|
77 |
static const int zero = 0;
|
85 |
static const int zero = 0;
|
78 |
|
86 |
|
79 |
#define LOGSYSERR(who, call, spar) \
|
87 |
#define LOGSYSERR(who, call, spar) \
|
80 |
LOGERR(("%s: %s(%s) errno %d (%s)\n", who, call, \
|
88 |
LOGERR(("%s: %s(%s) errno %d (%s)\n", who, call, \
|
81 |
spar, errno, strerror(errno)))
|
89 |
spar, errno, strerror(errno)))
|
82 |
|
90 |
|
83 |
#ifndef MIN
|
91 |
#ifndef MIN
|
84 |
#define MIN(a,b) (a<b?a:b)
|
92 |
#define MIN(a,b) (a<b?a:b)
|
85 |
#endif
|
93 |
#endif
|
|
... |
|
... |
111 |
ret = select(fd + 1, 0, &rd, 0, &tv);
|
119 |
ret = select(fd + 1, 0, &rd, 0, &tv);
|
112 |
} else {
|
120 |
} else {
|
113 |
ret = select(fd + 1, &rd, 0, 0, &tv);
|
121 |
ret = select(fd + 1, &rd, 0, 0, &tv);
|
114 |
}
|
122 |
}
|
115 |
if (!FD_ISSET(fd, &rd)) {
|
123 |
if (!FD_ISSET(fd, &rd)) {
|
116 |
LOGERR(("Netcon::select1: fd not ready after select ??\n"));
|
124 |
LOGDEB2(("Netcon::select1: fd %d timeout\n", fd));
|
117 |
return -1;
|
|
|
118 |
}
|
125 |
}
|
119 |
return ret;
|
126 |
return ret;
|
120 |
}
|
127 |
}
|
121 |
|
128 |
|
122 |
void SelectLoop::setperiodichandler(int (*handler)(void *), void *p, int ms)
|
129 |
void SelectLoop::setperiodichandler(int (*handler)(void *), void *p, int ms)
|
|
... |
|
... |
190 |
FD_ZERO(&wd);
|
197 |
FD_ZERO(&wd);
|
191 |
|
198 |
|
192 |
// Walk the netcon map and set up the read and write fd_sets
|
199 |
// Walk the netcon map and set up the read and write fd_sets
|
193 |
// for select()
|
200 |
// for select()
|
194 |
nfds = 0;
|
201 |
nfds = 0;
|
195 |
for (std::map<int, NetconP>::iterator it = m_polldata.begin();
|
202 |
for (map<int, NetconP>::iterator it = m_polldata.begin();
|
196 |
it != m_polldata.end(); it++) {
|
203 |
it != m_polldata.end(); it++) {
|
197 |
NetconP& pll = it->second;
|
204 |
NetconP& pll = it->second;
|
198 |
int fd = it->first;
|
205 |
int fd = it->first;
|
199 |
LOGDEB2(("Selectloop: fd %d flags 0x%x\n", fd, pll->m_wantedEvents));
|
206 |
LOGDEB2(("Selectloop: fd %d flags 0x%x\n", fd, pll->m_wantedEvents));
|
200 |
if (pll->m_wantedEvents & Netcon::NETCONPOLL_READ) {
|
207 |
if (pll->m_wantedEvents & Netcon::NETCONPOLL_READ) {
|
|
... |
|
... |
266 |
canwrite ? "write" : ""));
|
273 |
canwrite ? "write" : ""));
|
267 |
if (none) {
|
274 |
if (none) {
|
268 |
continue;
|
275 |
continue;
|
269 |
}
|
276 |
}
|
270 |
|
277 |
|
271 |
std::map<int, NetconP>::iterator it = m_polldata.find(fd);
|
278 |
map<int, NetconP>::iterator it = m_polldata.find(fd);
|
272 |
if (it == m_polldata.end()) {
|
279 |
if (it == m_polldata.end()) {
|
273 |
/// This should not happen actually
|
280 |
/// This should not happen actually
|
274 |
LOGDEB2(("Netcon::selectloop: fd %d not found\n", fd));
|
281 |
LOGDEB2(("Netcon::selectloop: fd %d not found\n", fd));
|
275 |
continue;
|
282 |
continue;
|
276 |
}
|
283 |
}
|
|
... |
|
... |
315 |
{
|
322 |
{
|
316 |
if (!con) {
|
323 |
if (!con) {
|
317 |
return -1;
|
324 |
return -1;
|
318 |
}
|
325 |
}
|
319 |
LOGDEB1(("Netcon::remselcon: fd %d\n", con->m_fd));
|
326 |
LOGDEB1(("Netcon::remselcon: fd %d\n", con->m_fd));
|
320 |
std::map<int, NetconP>::iterator it = m_polldata.find(con->m_fd);
|
327 |
map<int, NetconP>::iterator it = m_polldata.find(con->m_fd);
|
321 |
if (it == m_polldata.end()) {
|
328 |
if (it == m_polldata.end()) {
|
322 |
LOGDEB1(("Netcon::remselcon: con not found for fd %d\n", con->m_fd));
|
329 |
LOGDEB1(("Netcon::remselcon: con not found for fd %d\n", con->m_fd));
|
323 |
return -1;
|
330 |
return -1;
|
324 |
}
|
331 |
}
|
325 |
con->setloop(0);
|
332 |
con->setloop(0);
|
|
... |
|
... |
639 |
if ((addr = inet_addr(host)) != -1) {
|
646 |
if ((addr = inet_addr(host)) != -1) {
|
640 |
memcpy(&ip_addr.sin_addr, &addr, sizeof(addr));
|
647 |
memcpy(&ip_addr.sin_addr, &addr, sizeof(addr));
|
641 |
} else {
|
648 |
} else {
|
642 |
struct hostent *hp;
|
649 |
struct hostent *hp;
|
643 |
if ((hp = gethostbyname(host)) == 0) {
|
650 |
if ((hp = gethostbyname(host)) == 0) {
|
644 |
LOGERR(("NetconCli::openconn: gethostbyname(%s) failed\n",
|
651 |
LOGERR(("NetconCli::openconn: gethostbyname(%s) failed\n",
|
645 |
host));
|
652 |
host));
|
646 |
return -1;
|
653 |
return -1;
|
647 |
}
|
654 |
}
|
648 |
memcpy(&ip_addr.sin_addr, hp->h_addr, hp->h_length);
|
655 |
memcpy(&ip_addr.sin_addr, hp->h_addr, hp->h_length);
|
649 |
}
|
656 |
}
|
|
... |
|
... |
714 |
LOGDEB2(("Netconcli::openconn: host %s, serv %s\n", host, serv));
|
721 |
LOGDEB2(("Netconcli::openconn: host %s, serv %s\n", host, serv));
|
715 |
|
722 |
|
716 |
if (host[0] != '/') {
|
723 |
if (host[0] != '/') {
|
717 |
struct servent *sp;
|
724 |
struct servent *sp;
|
718 |
if ((sp = getservbyname(serv, "tcp")) == 0) {
|
725 |
if ((sp = getservbyname(serv, "tcp")) == 0) {
|
719 |
LOGERR(("NetconCli::openconn: getservbyname failed for %s\n",serv));
|
726 |
LOGERR(("NetconCli::openconn: getservbyname failed for %s\n", serv));
|
720 |
return -1;
|
727 |
return -1;
|
721 |
}
|
728 |
}
|
722 |
// Callee expects the port number in host byte order
|
729 |
// Callee expects the port number in host byte order
|
723 |
return openconn(host, ntohs(sp->s_port), timeo);
|
730 |
return openconn(host, ntohs(sp->s_port), timeo);
|
724 |
} else {
|
731 |
} else {
|
|
... |
|
... |
813 |
goto out;
|
820 |
goto out;
|
814 |
}
|
821 |
}
|
815 |
|
822 |
|
816 |
LOGDEB1(("NetconServLis::openservice: service opened ok\n"));
|
823 |
LOGDEB1(("NetconServLis::openservice: service opened ok\n"));
|
817 |
ret = 0;
|
824 |
ret = 0;
|
818 |
out:
|
825 |
out:
|
819 |
if (ret < 0 && m_fd >= 0) {
|
826 |
if (ret < 0 && m_fd >= 0) {
|
820 |
close(m_fd);
|
827 |
close(m_fd);
|
821 |
m_fd = -1;
|
828 |
m_fd = -1;
|
822 |
}
|
829 |
}
|
823 |
return ret;
|
830 |
return ret;
|
|
... |
|
... |
974 |
}
|
981 |
}
|
975 |
|
982 |
|
976 |
// Retrieve peer's host name. Errors are non fatal
|
983 |
// Retrieve peer's host name. Errors are non fatal
|
977 |
if (m_serv.empty() || m_serv[0] != '/') {
|
984 |
if (m_serv.empty() || m_serv[0] != '/') {
|
978 |
struct hostent *hp;
|
985 |
struct hostent *hp;
|
979 |
if ((hp = gethostbyaddr((char *) & (who.sin_addr),
|
986 |
if ((hp = gethostbyaddr((char *) & (who.sin_addr),
|
980 |
sizeof(struct in_addr), AF_INET)) == 0) {
|
987 |
sizeof(struct in_addr), AF_INET)) == 0) {
|
981 |
LOGERR(("NetconServLis::accept: gethostbyaddr failed for addr 0x%lx\n",
|
988 |
LOGERR(("NetconServLis::accept: gethostbyaddr failed for addr 0x%lx\n",
|
982 |
who.sin_addr.s_addr));
|
989 |
who.sin_addr.s_addr));
|
983 |
con->setpeer(inet_ntoa(who.sin_addr));
|
990 |
con->setpeer(inet_ntoa(who.sin_addr));
|
984 |
} else {
|
991 |
} else {
|
985 |
con->setpeer(hp->h_name);
|
992 |
con->setpeer(hp->h_name);
|
986 |
}
|
993 |
}
|
987 |
} else {
|
994 |
} else {
|
|
... |
|
... |
1200 |
if (!con) {
|
1207 |
if (!con) {
|
1201 |
fprintf(stderr, "new NetconCli failed\n");
|
1208 |
fprintf(stderr, "new NetconCli failed\n");
|
1202 |
return 1;
|
1209 |
return 1;
|
1203 |
}
|
1210 |
}
|
1204 |
int port = atoi(serv);
|
1211 |
int port = atoi(serv);
|
1205 |
int ret = port > 0 ?
|
1212 |
int ret = port > 0 ?
|
1206 |
clicon->openconn(host, port) : clicon->openconn(host, serv);
|
1213 |
clicon->openconn(host, port) : clicon->openconn(host, serv);
|
1207 |
if (ret < 0) {
|
1214 |
if (ret < 0) {
|
1208 |
fprintf(stderr, "openconn(%s, %s) failed\n", host, serv);
|
1215 |
fprintf(stderr, "openconn(%s, %s) failed\n", host, serv);
|
1209 |
return 1;
|
1216 |
return 1;
|
1210 |
}
|
1217 |
}
|
1211 |
fprintf(stderr, "openconn(%s, %s) ok\n", host, serv);
|
1218 |
fprintf(stderr, "openconn(%s, %s) ok\n", host, serv);
|
|
... |
|
... |
1226 |
} else {
|
1233 |
} else {
|
1227 |
sleep(1);
|
1234 |
sleep(1);
|
1228 |
}
|
1235 |
}
|
1229 |
}
|
1236 |
}
|
1230 |
#else
|
1237 |
#else
|
1231 |
shared_ptr<CliNetconWorker> worker = make_shared<CliNetconWorker>();
|
1238 |
STD_SHARED_PTR<NetconWorker> worker =
|
|
|
1239 |
STD_SHARED_PTR<NetconWorker>(new CliNetconWorker());
|
1232 |
clicon->setcallback(worker);
|
1240 |
clicon->setcallback(worker);
|
1233 |
SelectLoop myloop;
|
1241 |
SelectLoop myloop;
|
1234 |
myloop.addselcon(con, Netcon::NETCONPOLL_WRITE);
|
1242 |
myloop.addselcon(con, Netcon::NETCONPOLL_WRITE);
|
1235 |
fprintf(stderr, "client ready\n");
|
1243 |
fprintf(stderr, "client ready\n");
|
1236 |
ret = myloop.doLoop();
|
1244 |
ret = myloop.doLoop();
|
|
... |
|
... |
1291 |
int cando(Netcon::Event reason) {
|
1299 |
int cando(Netcon::Event reason) {
|
1292 |
NetconServCon *con = accept();
|
1300 |
NetconServCon *con = accept();
|
1293 |
if (con == 0) {
|
1301 |
if (con == 0) {
|
1294 |
return -1;
|
1302 |
return -1;
|
1295 |
}
|
1303 |
}
|
1296 |
shared_ptr<ServNetconWorker> worker =
|
1304 |
STD_SHARED_PTR<NetconWorker> worker =
|
1297 |
make_shared<ServNetconWorker>(ServNetconWorker());
|
1305 |
STD_SHARED_PTR<NetconWorker>(new ServNetconWorker());
|
1298 |
con->setcallback(worker);
|
1306 |
con->setcallback(worker);
|
1299 |
m_loop.addselcon(NetconP(con), NETCONPOLL_READ);
|
1307 |
m_loop.addselcon(NetconP(con), NETCONPOLL_READ);
|
1300 |
return 1;
|
1308 |
return 1;
|
1301 |
}
|
1309 |
}
|
1302 |
SelectLoop& m_loop;
|
1310 |
SelectLoop& m_loop;
|
|
... |
|
... |
1334 |
sigaction(SIGINT, &sa, 0);
|
1342 |
sigaction(SIGINT, &sa, 0);
|
1335 |
sigaction(SIGQUIT, &sa, 0);
|
1343 |
sigaction(SIGQUIT, &sa, 0);
|
1336 |
sigaction(SIGTERM, &sa, 0);
|
1344 |
sigaction(SIGTERM, &sa, 0);
|
1337 |
|
1345 |
|
1338 |
int port = atoi(serv);
|
1346 |
int port = atoi(serv);
|
1339 |
int ret = port > 0 ?
|
1347 |
int ret = port > 0 ?
|
1340 |
servlis->openservice(port) : servlis->openservice(serv);
|
1348 |
servlis->openservice(port) : servlis->openservice(serv);
|
1341 |
if (ret < 0) {
|
1349 |
if (ret < 0) {
|
1342 |
fprintf(stderr, "openservice(%s) failed\n", serv);
|
1350 |
fprintf(stderr, "openservice(%s) failed\n", serv);
|
1343 |
return 1;
|
1351 |
return 1;
|
1344 |
}
|
1352 |
}
|
1345 |
myloop.addselcon(lis, Netcon::NETCONPOLL_READ);
|
1353 |
myloop.addselcon(lis, Netcon::NETCONPOLL_READ);
|