Date: Fri, 20 Feb 1998 16:18:02 -0800 (PST) From: Paul Traina <pst@Shockwave.COM> To: msmith@FreeBSD.ORG Cc: questions@FreeBSD.ORG Subject: more on refusing tcp connections before accepting them Message-ID: <199802210018.QAA08429@precipice.shockwave.com>
next in thread | raw e-mail | index | archive | help
client: telnet localhost 13999 Trying 127.0.0.1... Connected to localhost.shockwave.com. Escape character is '^]'. Connection closed by foreign host. server: pst@precipice$ ./foo foo: recvmsg: Socket is not connected Received connection from 127.0.0.1:1453 I don't want the client to even see the connection establised, I want it refused by TCP, but I need to see his source address first. /* * Test jig to see if we can grab connection data before accepting a * remote TCP connection. */ #include <sys/types.h> #include <sys/param.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <err.h> #include <stdio.h> #include <string.h> #include <unistd.h> void dump_control (struct msghdr *m) { u_int i; struct cmsghdr *cmsg; u_int len = m->msg_controllen; for (cmsg = CMSG_FIRSTHDR(m); len > 0; cmsg = CMSG_NXTHDR(m, cmsg)) { len -= cmsg->cmsg_len; printf("Message: [%d,%d,%d] =", cmsg->cmsg_len, cmsg->cmsg_level, cmsg->cmsg_type); for (i = sizeof(struct cmsghdr); i < cmsg->cmsg_len; i++) printf(" 0x%x", CMSG_DATA(cmsg)[i]); printf("\n"); } } void check_recvmsg (int s) { struct msghdr msg; char ctrlbuf[BUFSIZ]; memset(&msg, 0, sizeof(msg)); msg.msg_control = ctrlbuf; msg.msg_controllen = sizeof(ctrlbuf); memset(ctrlbuf, 0, sizeof(ctrlbuf)); if (recvmsg(s, &msg, MSG_PEEK) < 0) warn("recvmsg"); else dump_control(&msg); } int main (int argc, char **argv) { int s, s2; struct sockaddr_in saddr, iaddr; int iaddrlen; if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket"); memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_port = htons(13999); if (bind(s, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) err(1, "bind"); if (listen(s, 5) < 0) err(1, "listen"); check_recvmsg(s); /* try it for grins on the listen socket */ iaddrlen = sizeof(iaddr); if ((s2 = accept(s, (struct sockaddr *) &iaddr, &iaddrlen)) < 0) err(1, "accept"); printf("Received connection from %s:%d\n", inet_ntoa(iaddr.sin_addr), htons(iaddr.sin_port)); check_recvmsg(s2); /* nope, see if anything on accept socket */ close(s2); close(s); exit(0); } To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199802210018.QAA08429>