]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/StartListening.cc
Transition comm_write*() from FD to Comm::Connection
[thirdparty/squid.git] / src / ipc / StartListening.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8 #include "config.h"
9 #include "base/Subscription.h"
10 #include "base/TextException.h"
11 #include "comm.h"
12 #include "comm/ConnAcceptor.h"
13 #include "comm/Connection.h"
14 #include "ipc/SharedListen.h"
15 #include "ipc/StartListening.h"
16
17
18 Ipc::StartListeningCb::StartListeningCb(): conn(NULL), errNo(0)
19 {
20 }
21
22 Ipc::StartListeningCb::~StartListeningCb()
23 {
24 }
25
26 std::ostream &Ipc::StartListeningCb::startPrint(std::ostream &os) const
27 {
28 return os << "(" << conn << ", err=" << errNo;
29 }
30
31 void
32 Ipc::StartListening(int sock_type, int proto, const Comm::ConnectionPointer &listenConn,
33 FdNoteId fdNote, AsyncCall::Pointer &callback, const Subscription::Pointer &sub)
34 {
35 if (UsingSmp()) { // if SMP is on, share
36 OpenListenerParams p;
37 p.sock_type = sock_type;
38 p.proto = proto;
39 p.addr = listenConn->local;
40 p.flags = listenConn->flags;
41 p.fdNote = fdNote;
42 p.handlerSubscription = sub;
43
44 Ipc::JoinSharedListen(p, callback);
45 return; // wait for the call back
46 }
47
48 StartListeningCb *cbd = dynamic_cast<StartListeningCb*>(callback->getDialer());
49 Must(cbd);
50 cbd->conn = listenConn;
51
52 enter_suid();
53 if (sock_type == SOCK_STREAM) {
54 // TCP: setup the subscriptions such that new connections accepted by listenConn are handled by HTTP
55 AsyncJob::Start(new Comm::ConnAcceptor(cbd->conn, FdNote(fdNote), sub));
56 } else if (sock_type == SOCK_DGRAM) {
57 // UDP: setup the listener socket, but do not set a subscriber
58 Comm::ConnectionPointer udpConn = listenConn;
59 comm_open_listener(sock_type, proto, udpConn, FdNote(fdNote));
60 } else {
61 fatalf("Invalid Socket Type (%d)",sock_type);
62 }
63 cbd->errNo = cbd->conn->isOpen() ? 0 : errno;
64 leave_suid();
65
66 debugs(54, 3, HERE << "opened listen " << cbd->conn);
67 ScheduleCallHere(callback);
68 }