12.06.2015 Views

The Annoyance Filter.pdf - Fourmilab

The Annoyance Filter.pdf - Fourmilab

The Annoyance Filter.pdf - Fourmilab

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

166 POP3 PROXY SERVER CLASS DEFINITION ANNOYANCE-FILTER §197<br />

197. In order to accept connections, we need to create a socket, listenSocket which is bound to the<br />

port address on which we listen. We accept connections from any IP address. <strong>The</strong> acceptConnections<br />

must be called to activate the socket before connections may be processed.<br />

〈 Class implementations 11 〉 +≡<br />

#ifdef POP3_PROXY_SERVER<br />

bool POP3Proxy ::acceptConnections (int maxBacklog )<br />

{<br />

struct sockaddr in name ;<br />

listenSocket = socket (AF_INET, SOCK_STREAM, 0);<br />

if (listenSocket < 0) {<br />

perror ("POP3Proxy␣opening␣socket␣to␣listen␣for␣connections");<br />

listenSocket = −1;<br />

return false ;<br />

} /∗ Create name with wildcards. ∗/<br />

name .sin family = AF_INET;<br />

name .sin addr .s addr = INADDR_ANY;<br />

name .sin port = htons (popProxyPort );<br />

if (bind (listenSocket , (struct sockaddr ∗) &name , sizeof name ) < 0) {<br />

close (listenSocket );<br />

perror ("POP3Proxy␣binding␣socket␣to␣listen␣for␣connections");<br />

listenSocket = −1;<br />

return false ;<br />

}<br />

if (listen (listenSocket , maxBacklog ) < 0) {<br />

close (listenSocket );<br />

perror ("POP3Proxy␣calling␣listen␣for␣connection␣socket");<br />

listenSocket = −1;<br />

return false ;<br />

}<br />

signal (SIGPIPE, absentPlumber ); /∗ Catch ”broken pipe” signals from disconnects ∗/<br />

opened = true ;<br />

return opened ;<br />

}<br />

#endif<br />

198. <strong>The</strong> serviceConnection method waits for the next client connection to the listenSocket , accepts<br />

it, and then conducts the dialogue with the client.<br />

〈 Class implementations 11 〉 +≡<br />

#ifdef POP3_PROXY_SERVER<br />

bool POP3Proxy ::serviceConnection (void)<br />

{<br />

assert(opened );<br />

int clientSocket ; /∗ Socket for talking to client ∗/<br />

struct sockaddr in from ; /∗ Client IP address ∗/<br />

socklen t fromlen ; /∗ Length of client address ∗/<br />

〈 Wait for next client connection and accept it 199 〉;<br />

〈 Conduct dialogue with client 200 〉;<br />

return true ;<br />

}<br />

#endif

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!