--- l2_sockmon.c 2001/09/26 09:00:21 1.10
+++ l2_sockmon.c 2001/10/12 14:48:00 1.11
@@ -38,15 +38,19 @@
#include <assert.h>
#include "l2.h"
+#include "l2_ut_sa.h"
-#define BACKLOG 1024 /* max # pending connections */
-#define BUFFSIZ 256 /* max string size */
+#define BACKLOG 1024 /* max # pending connections */
+#define BUFFSIZ 256 /* max string size */
+#define L2_SA_SOCKET_TIMEOUT 64
+#define L2_SA_SOCKET_BUFSIZE 4096
char *strlowerdup(const char *); /* prototypes */
void dowork(int);
int myserver(int, int, int);
void die(const char *);
-void closedie(int, const char *);
+void closedie(sa_t *, const char *);
+void usage(char *);
void die(const char *szMsg)
{
@@ -54,10 +58,10 @@
exit(EXIT_FAILURE);
}
-void closedie(int iSock, const char *szMsg)
+void closedie(sa_t *saSock, const char *szMsg)
{
perror(szMsg);
- close(iSock);
+ sa_destroy(saSock); /* saSock->destroy would really be nice here */
exit(EXIT_FAILURE);
}
@@ -106,60 +110,47 @@
int myserver(int iFamily, int iProtocol, int iPort)
{
+ sa_type_t saType;
+ sa_addr_t *saAddr;
+ sa_t *saListen, saAccept;
+ int iSock, iSockopt;
+
char szBuf[BUFFSIZ];
char szAddrin4[INET_ADDRSTRLEN];
char szAddrin6[INET6_ADDRSTRLEN];
- struct sockaddr_in laddr4, faddr4, caddr4;
- struct sockaddr_in6 laddr6, faddr6, caddr6;
- int iSock, iNewsock;
- int iSockopt, iProtofam;
- int iTrans, cc;
+ struct sockaddr_in faddr4, caddr4;
+ struct sockaddr_in6 faddr6, caddr6;
+ int iNewsock;
+ int cc;
socklen_t faddrlen;
- /* Set up an IPv4 TCP socket to listen on for connections. */
- if (iFamily == AF_INET6)
- {
- /* initialize address structure */
- memset(&laddr6, 0, sizeof(laddr6));
-
- laddr6.sin6_family = AF_INET6;
- laddr6.sin6_flowinfo = 0;
- laddr6.sin6_port = iPort;
- laddr6.sin6_addr = in6addr_any; /* Structure assignment */
- }
- else if (iFamily == AF_INET)
- {
- /* initialize address structure */
- memset(&laddr4, 0, sizeof(laddr4));
-
- laddr4.sin_family = AF_INET;
- laddr4.sin_port = iPort;
- laddr4.sin_addr.s_addr = INADDR_ANY;
- }
- else
- return -1; /* Not IPv4 or IPv6 */
-
- iProtofam = (iFamily == AF_INET) ? PF_INET : PF_INET6;
- iTrans = (iProtocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
- iSock = socket(iProtofam, iTrans, iProtocol);
-
- if (iSock == -1)
- die("socket()");
-
- /* Allow local addresses to be reused. */
+ /* Create socket address, the sa_addr object */
+ /* starts in a state similar to INADDR_ANY */
+ if (sa_addr_create(&saAddr) != SA_OK)
+ die("sa_addr_create()");
+
+ /* Create socket */
+ if (sa_create(&saListen) != SA_OK)
+ closedie(saListen, "sa_create()");
+
+ if (!(iProtocol == IPPROTO_UDP || iProtocol == IPPROTO_TCP))
+ closedie(saListen, "unknown protocol");
+ saType = (iProtocol == IPPROTO_UDP) ? SA_TYPE_DATAGRAM : SA_TYPE_STREAM;
+
+ /* Configure socket parameters */
+ sa_timeout(saListen, L2_SA_SOCKET_TIMEOUT, 0);
+ sa_buffers(saListen, L2_SA_SOCKET_BUFSIZE, L2_SA_SOCKET_BUFSIZE);
+ sa_type (saListen, saType);
+
+ /* Allow local addresses to be reused */
+ if (sa_getfd(saListen, &iSock) != SA_OK)
+ closedie(saListen, "sa_getfd()");
iSockopt = 1;
if (setsockopt(iSock, SOL_SOCKET, SO_REUSEADDR, (void *)&iSockopt, sizeof (iSockopt)) == -1)
- closedie(iSock, "setsockopt(SO_REUSEADDR)");
-
- if (iFamily == AF_INET6) {
- if (bind(iSock, (struct sockaddr *)&laddr6, sizeof (laddr6)) == -1)
- closedie(iSock, "bind()");
- }
+ closedie(saListen, "setsockopt()");
- else if (iFamily == AF_INET) {
- if (bind(iSock, (struct sockaddr *)&laddr4, sizeof (laddr4)) == -1)
- closedie(iSock, "bind()");
- }
+ if (sa_bind(saListen, saAddr) != SA_OK)
+ closedie(saListen, "sa_bind()");
if (iProtocol == IPPROTO_UDP) {
if (iFamily == AF_INET6) {
@@ -224,8 +215,8 @@
}
}
else if (iProtocol == IPPROTO_TCP) { /* Only try to listen if we have TCP */
- if (listen(iSock, BACKLOG) == -1)
- closedie(iSock, "listen()");
+ if (sa_listen(saListen, BACKLOG) != SA_OK)
+ closedie(saListen, "sa_listen()");
/* Wait for a connection request */
if (iFamily == AF_INET6) {
@@ -234,14 +225,12 @@
for (;;) {
faddrlen = sizeof (faddr6);
- iNewsock = accept(iSock, (struct sockaddr *)&faddr6, &faddrlen);
- if (iNewsock == -1) {
- if (errno != EINTR && errno != ECONNABORTED) {
- perror("accept()");
- }
- continue;
+ if (sa_accept(saListen, &saAddr, &saAccept) != SA_OK) {
+ sa_destroy(saAccept);
+ closedie(saListen, "sa_accept()");
}
+ if (sa_addr_a2u(saAddr, char **uri))
if (inet_ntop(AF_INET6, &faddr6.sin6_addr, szAddrin6, sizeof(szAddrin6)) != NULL) {
fprintf(stderr, "Connection from %s/%d\n", szAddrin6, ntohs(faddr6.sin6_port));
dowork(iNewsock); /* do some sockwork */
@@ -272,20 +261,35 @@
return -1; /* Failure due to non UDP/TCP request, or execution flow */
}
+void usage(char *szApp)
+{
+ fprintf(stderr, "Usage: %s <IPv4-or-IPv6> <TCP-or-UDP> <Port>\n", szApp);
+ exit(EXIT_FAILURE); /* The user doesn't know how to use this */
+}
+
int main(int argc, char *argv[])
{
int iFam = -1;
int iProto = -1;
int iPort = 0;
- if (argc != 4) {
- fprintf(stderr, "Usage: %s <IPv4-or-IPv6> <TCP-or-UDP> <Port>\n", argv[0]);
- exit(EXIT_FAILURE); /* The user doesn't know how to use this */
- }
+ if (argc != 4)
+ usage(argv[0]);
- iFam = (!strcmp(argv[1], "IPv6")) ? AF_INET6 : AF_INET;
- iProto = (!strcmp(argv[2], "UDP")) ? IPPROTO_UDP : IPPROTO_TCP;
- iPort = htons(atoi(argv[3]));
+ if (!strcmp(argv[1], "IPv6"))
+ iFam = AF_INET6;
+ else if (!strcmp(argv[1], "IPv4"))
+ iFam = AF_INET;
+ else
+ usage(argv[0]);
+
+ if (!strcmp(argv[2], "UDP"))
+ iProto = IPPROTO_UDP;
+ else if (!strcmp(argv[2], "TCP"))
+ iProto = IPPROTO_TCP;
+ else
+ usage(argv[0]);
+ iPort = htons(atoi(argv[3]));
return myserver(iFam, iProto, iPort);
}
|