Added 'conditional cs_listen' feature.
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 22 Jun 1998 11:32:35 +0000 (11:32 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 22 Jun 1998 11:32:35 +0000 (11:32 +0000)
comstack/comstack.c
comstack/tcpip.c
include/comstack.h
include/tcpip.h
server/statserv.c

index d46b49e..f1892ea 100644 (file)
@@ -1,10 +1,13 @@
 /*
- * Copyright (c) 1995-1997, Index Data
+ * Copyright (c) 1995-1998, Index Data
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: comstack.c,v $
- * Revision 1.4  1997-09-29 07:16:14  adam
+ * Revision 1.5  1998-06-22 11:32:35  adam
+ * Added 'conditional cs_listen' feature.
+ *
+ * Revision 1.4  1997/09/29 07:16:14  adam
  * Array cs_errlist no longer global.
  *
  * Revision 1.3  1997/09/01 08:49:14  adam
@@ -33,7 +36,8 @@ static const char *cs_errlist[] =
     "System (lower-layer) error",
     "Operation out of state",
     "No data (operation would block)",
-    "New data while half of old buffer is on the line (flow control)"
+    "New data while half of old buffer is on the line (flow control)",
+    "Permission denied"
 };
 
 const char *cs_errmsg(int n)
index a71069e..40e3d61 100644 (file)
@@ -4,7 +4,10 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: tcpip.c,v $
- * Revision 1.21  1998-05-20 09:55:32  adam
+ * Revision 1.22  1998-06-22 11:32:36  adam
+ * Added 'conditional cs_listen' feature.
+ *
+ * Revision 1.21  1998/05/20 09:55:32  adam
  * Function tcpip_get treats EINPROGRESS error in the same way as
  * EWOULDBLOCK. EINPROGRESS shouldn't be returned - but it is on
  * Solaris in some cases.
@@ -158,7 +161,10 @@ int tcpip_connect(COMSTACK h, void *address);
 int tcpip_more(COMSTACK h);
 int tcpip_rcvconnect(COMSTACK h);
 int tcpip_bind(COMSTACK h, void *address, int mode);
-int tcpip_listen(COMSTACK h, char *addrp, int *addrlen);
+int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
+                int (*check_ip)(void *cd, const char *a, int len, int type),
+                void *cd);
+
 COMSTACK tcpip_accept(COMSTACK h);
 char *tcpip_addrstr(COMSTACK h);
 void *tcpip_straddr(COMSTACK h, const char *str);
@@ -171,6 +177,7 @@ void *tcpip_straddr(COMSTACK h, const char *str);
 /*int completeBER(unsigned char *buf, int len); */ /* from the ODR module */
 int completeWAIS(unsigned char *buf, int len); /* from waislen.c */
 
+#undef TRACE_TCPIP
 #ifdef TRACE_TCPIP
 #define TRC(x) x
 #else
@@ -410,12 +417,27 @@ int tcpip_bind(COMSTACK h, void *address, int mode)
     return 0;
 }
 
-int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)
+#if 0
+void tcpip_get_ip(COMSTACK h, char *ip_buf)
+{
+    struct tcpip_state *sp = (tcpip_state *)h->cprivate;
+    const char *ip_addr = (const char *) (&sp->addr->sin_addr.s_addr);
+    int i;
+
+    for (i = 0; i<4; i++)
+       TRC (fprintf (stderr, "%u ", ip_addr[i]));
+    TRC (fprintf (stderr, "\n"));
+}
+#endif
+
+int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
+                int (*check_ip)(void *cd, const char *a, int len, int type),
+                void *cd)
 {
     struct sockaddr_in addr;
     int len = sizeof(addr);
 
-    TRC(fprintf(stderr, "tcpip_listen\n"));
+    TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
     if (h->state != CS_IDLE)
     {
         h->cerrno = CSOUTSTATE;
@@ -434,10 +456,20 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)
             h->cerrno = CSYSERR;
         return -1;
     }
-    if (addrlen && *addrlen > sizeof(struct sockaddr_in))
+    if (addrlen && *addrlen >= sizeof(struct sockaddr_in))
         memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
     else if (addrlen)
         *addrlen = 0;
+
+#if 1
+    if (check_ip && (*check_ip)(cd, &addr.sin_addr, sizeof(addr.sin_addr),
+                               AF_INET))
+    {
+       h->cerrno = CSDENY;
+       close (h->newfd);
+       return -1;
+    }
+#endif
     h->state = CS_INCON;
     return 0;
 }
index f4d2641..449b8ce 100644 (file)
  * OF THIS SOFTWARE.
  *
  * $Log: comstack.h,v $
- * Revision 1.21  1998-05-20 09:52:39  adam
+ * Revision 1.22  1998-06-22 11:32:37  adam
+ * Added 'conditional cs_listen' feature.
+ *
+ * Revision 1.21  1998/05/20 09:52:39  adam
  * Removed 'dead' definition.
  *
  * Revision 1.20  1998/05/18 13:06:55  adam
@@ -179,7 +182,9 @@ struct comstack
     int (*f_bind)(COMSTACK handle, void *address, int mode);
 #define CS_CLIENT 0
 #define CS_SERVER 1
-    int (*f_listen)(COMSTACK handle, char *addrp, int *addrlen);
+    int (*f_listen)(COMSTACK h, char *raddr, int *addrlen,
+                  int (*check_ip)(void *cd, const char *a, int len, int type),
+                  void *cd);
     COMSTACK (*f_accept)(COMSTACK handle);
     int (*f_close)(COMSTACK handle);
     char *(*f_addrstr)(COMSTACK handle);
@@ -192,7 +197,8 @@ struct comstack
 #define cs_connect(handle, address) ((*(handle)->f_connect)(handle, address))
 #define cs_rcvconnect(handle) ((*(handle)->f_rcvconnect)(handle))
 #define cs_bind(handle, ad, mo) ((*(handle)->f_bind)(handle, ad, mo))
-#define cs_listen(handle, ap, al) ((*(handle)->f_listen)(handle, ap, al))
+#define cs_listen(handle, ap, al) ((*(handle)->f_listen)(handle, ap, al, 0, 0))
+#define cs_listen_check(handle, ap, al, cf, cd) ((*(handle)->f_listen)(handle, ap, al, cf, cd))
 #define cs_accept(handle) ((*(handle)->f_accept)(handle))
 #define cs_close(handle) ((*(handle)->f_close)(handle))
 #define cs_create(type, blocking, proto) ((*type)(-1, blocking, proto))
@@ -219,6 +225,7 @@ YAZ_EXPORT const char *cs_errmsg(int n);
 #define CSOUTSTATE 2
 #define CSNODATA   3
 #define CSWRONGBUF 4
+#define CSDENY     5
 
 /* backwards compatibility */
 #define CS_SR     PROTO_SR
index c2cf650..1c30c3f 100644 (file)
  * OF THIS SOFTWARE.
  *
  * $Log: tcpip.h,v $
- * Revision 1.10  1998-05-18 13:07:01  adam
+ * Revision 1.11  1998-06-22 11:32:38  adam
+ * Added 'conditional cs_listen' feature.
+ *
+ * Revision 1.10  1998/05/18 13:07:01  adam
  * Changed the way attribute sets are handled by the retriaval module.
  * Extended Explain conversion / schema.
  * Modified server and client to work with ASN.1 compiled protocol handlers.
@@ -81,7 +84,6 @@ extern "C" {
 #endif
 
 YAZ_EXPORT struct sockaddr_in *tcpip_strtoaddr(const char *str);
-
 YAZ_EXPORT COMSTACK tcpip_type(int s, int blocking, int protocol);
 
 #ifdef __cplusplus
index 2f9c86c..6abbe44 100644 (file)
@@ -7,7 +7,10 @@
  *   Chas Woodfield, Fretwell Downing Datasystems.
  *
  * $Log: statserv.c,v $
- * Revision 1.49  1998-02-27 14:04:55  adam
+ * Revision 1.50  1998-06-22 11:32:39  adam
+ * Added 'conditional cs_listen' feature.
+ *
+ * Revision 1.49  1998/02/27 14:04:55  adam
  * Fixed bug in statserv_remove.
  *
  * Revision 1.48  1998/02/11 11:53:36  adam
@@ -405,7 +408,7 @@ static void listener(IOCHAN h, int event)
     {
         if ((res = cs_listen(line, 0, 0)) < 0)
         {
-           logf(LOG_FATAL, "cs_listen failed.");
+           logf(LOG_FATAL, "cs_listen failed");
            return;
         }
         else if (res == 1)
@@ -495,6 +498,19 @@ void statserv_closedown()
         iochan_destroy(p);
 }
 
+static int check_ip(void *cd, const char *addr, int len, int type)
+{
+    const unsigned char *ip = (const unsigned char *) addr;
+    int i;
+    char str[64];
+
+    sprintf (str, "%u", *ip);
+    for (i = 1; i<4; i++)
+       sprintf (str + strlen(str), ".%u", ip[i]);
+    logf (LOG_DEBUG, "ip %s", str);
+    return 0;
+}
+
 static void listener(IOCHAN h, int event)
 {
     COMSTACK line = (COMSTACK) iochan_getdata(h);
@@ -562,9 +578,9 @@ static void listener(IOCHAN h, int event)
                return;
            }
        }
-       if ((res = cs_listen(line, 0, 0)) < 0)
+       if ((res = cs_listen_check(line, 0, 0, check_ip, 0)) < 0)
        {
-           logf(LOG_FATAL, "cs_listen failed.");
+           logf(LOG_WARN, "cs_listen failed");
            return;
        }
        else if (res == 1)