--- l2_sockmon.c 2001/09/05 16:15:48 1.1
+++ l2_sockmon.c 2001/09/06 14:31:33 1.2
@@ -36,15 +36,34 @@
#define BACKLOG 1024 /* max # pending connections */
#define BUFFSIZ 256 /* max string size */
-void do_work(int sock)
+char *strcasedup(const char *pszIn)
{
- char buf[BUFFSIZ];
+ int i = 0;
+ char *pszTemp = NULL;
+
+/* ASSERT(pszIn);*/
+/* ASSERT(*pszIn);*/
+
+ if ((pszTemp = (char *)strdup(pszIn)) == NULL)
+ return NULL;
+ else
+ {
+ for (i = 0; *(pszTemp + i); i++)
+ if (isupper(*(pszTemp + i)))
+ *(pszTemp + i) = (char)tolower(*(pszTemp + i));
+ return pszTemp;
+ }
+}
+
+void dowork(int iSock)
+{
+ char pszBuf[BUFFSIZ];
int cc;
-/* ASSERT(sock); */
+/* ASSERT(iSock);*/
while (1) { /* Exits when read finishes */
- cc = read(sock, buf, sizeof (buf));
+ cc = read(iSock, pszBuf, sizeof (pszBuf));
if (cc == -1) {
perror("read");
exit(1);
@@ -52,78 +71,95 @@
if (cc == 0) {
/* EOF */
- (void) close(sock);
+ (void) close(iSock);
(void) printf("Connection closed\n");
return;
}
- buf[cc + 1] = '\0';
- (void) printf("Read: %s", buf);
+ pszBuf[cc + 1] = '\0';
+ (void) printf("Read: %s", pszBuf);
- if (write(sock, buf, cc) == -1) {
+ if (write(iSock, pszBuf, cc) == -1) {
perror("write");
exit(1);
}
}
}
-int myserver(int port)
+int myserver(int iFamily, int iProtocol, int iPort)
{
- struct sockaddr_in laddr, faddr ;
- int sock, new_sock, sock_opt;
- socklen_t faddrlen ;
+ struct sockaddr_in laddr, faddr;
+ int iSock, iNewsock, iSockopt, iProtofam;
+ socklen_t faddrlen;
-/* ASSERT(port); */
+/* ASSERT(iPort);*/
/* Set up an IPv4 TCP socket to listen on for connections. */
- laddr.sin_family = AF_INET;
- laddr.sin_port = port;
+ laddr.sin_family = iFamily;
+ laddr.sin_port = iPort;
laddr.sin_addr.s_addr = INADDR_ANY;
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock == -1) {
+ iProtofam = (iFamily == AF_INET) ? PF_INET : PF_INET6;
+ iSock = socket(iProtofam, SOCK_STREAM, iProtocol);
+
+ if (iSock == -1) {
perror("socket");
return (-1);
}
/* Tell the system to allow local addresses to be reused. */
- sock_opt = 1;
- if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&sock_opt, sizeof (sock_opt)) == -1) {
+ iSockopt = 1;
+ if (setsockopt(iSock, SOL_SOCKET, SO_REUSEADDR, (void *)&iSockopt,\
+ sizeof (iSockopt)) == -1) {
perror("setsockopt(SO_REUSEADDR)");
- (void) close(sock);
+ (void) close(iSock);
return (-1);
}
- if (bind(sock, (struct sockaddr *)&laddr, sizeof (laddr)) == -1) {
+ if (bind(iSock, (struct sockaddr *)&laddr, sizeof (laddr)) == -1) {
perror("bind"); /* Bad bind */
- (void) close(sock);
+ (void) close(iSock);
return (-1);
}
- if (listen(sock, BACKLOG) == -1) {
+fprintf(stderr, "Listening on %d or 0x%x\n", iPort, iPort);
+ if (listen(iSock, BACKLOG) == -1) {
perror("listen"); /* Listen just blew up */
- (void) close(sock);
+ (void) close(iSock);
return (-1);
}
/* Wait for a connection request. */
for (;;) {
faddrlen = sizeof (faddr);
- new_sock = accept(sock, (struct sockaddr *)&faddr, &faddrlen);
- if (new_sock == -1) {
+ iNewsock = accept(iSock, (struct sockaddr *)&faddr, &faddrlen);
+ if (iNewsock == -1) {
if (errno != EINTR && errno != ECONNABORTED) {
perror("accept");
}
continue;
}
(void) printf("Connection from %s/%d\n", inet_ntoa(faddr.sin_addr), ntohs(faddr.sin_port));
- do_work(new_sock); /* do some sockwork */
+ dowork(iNewsock); /* do some sockwork */
}
/*NOTREACHED*/
}
int main(int argc, char *argv[])
{
- if (argc != 2) {
- (void) fprintf(stderr, "Usage: %s <port>\n", argv[0]);
+ char *pszArgv = NULL;
+ int iFamily = -1;
+ int iNetproto = -1;
+ int iNetport = 0;
+
+ if (argc < 2) {
+ (void) fprintf(stderr, "Usage: %s [IPv6] [UDP] <Port>\n", argv[0]);
exit(1); /* Dink donk if the user doesn't know how to use this */
}
- (void) myserver(htons(atoi(argv[1])));
- return 0;
+
+ if ((pszArgv = strcasedup(*argv)) == NULL)
+ return -1;
+
+ iFamily = (strstr(pszArgv, "ipv6")) ? AF_INET6 : AF_INET;
+ iNetproto = (strstr(pszArgv, "udp")) ? IPPROTO_UDP : IPPROTO_TCP;
+ iNetport = htons(atoi(argv[argc - 1]));
+
+ free (pszArgv);
+ return myserver(iFamily, iNetproto, iNetport);
}
|