]> jfr.im git - irc/ircd-hybrid/libopm.git/commitdiff
Added custom protocol support
authorstrtok <redacted>
Tue, 15 Oct 2002 19:55:19 +0000 (19:55 +0000)
committerstrtok <redacted>
Tue, 15 Oct 2002 19:55:19 +0000 (19:55 +0000)
src/libopm.c
src/libopm.h
src/opm.h
src/proxy.c
src/proxy.h
src/test.c

index e66aa03a7ab2bea7ad5b8b4280066130af16bb7c..1d26a4372e8aa85a9cbb6873abe422ffb00ba4d8 100644 (file)
@@ -50,6 +50,9 @@ RCSID("$Id$");
 static OPM_PROTOCOL_CONFIG_T *libopm_protocol_config_create();
 static void libopm_protocol_config_free(OPM_PROTOCOL_CONFIG_T *);
 
+static OPM_PROTOCOL_T *libopm_protocol_create();
+static void libopm_protocol_free(OPM_PROTOCOL_T *);
+
 static OPM_SCAN_T *libopm_scan_create(OPM_T *, OPM_REMOTE_T *);
 static void libopm_scan_free(OPM_SCAN_T *);
 
@@ -263,6 +266,10 @@ void opm_free(OPM_T *scanner)
    LIST_FOREACH_SAFE(p, next, scanner->protocols->head)
    {
       ppc = (OPM_PROTOCOL_CONFIG_T *) p->data;
+
+      if(ppc->type->type == OPM_TYPE_CUSTOM)
+         libopm_protocol_free(ppc->type);     
+
       libopm_protocol_config_free(ppc);
       libopm_list_remove(scanner->protocols, p);
       libopm_node_free(p);
@@ -330,23 +337,23 @@ OPM_ERR_T opm_config(OPM_T *scanner, int key, void *value)
  *    (write in future error codes)
  */
 
-OPM_ERR_T opm_addtype(OPM_T *scanner, int type, int port)
+OPM_ERR_T opm_addtype(OPM_T *scanner, int type, unsigned short int port)
 {
    int i;
 
    OPM_NODE_T *node;
-   OPM_PROTOCOL_CONFIG_T *protocol;
+   OPM_PROTOCOL_CONFIG_T *protocol_config;
 
    for(i = 0; i < sizeof(OPM_PROTOCOLS) / sizeof(OPM_PROTOCOL_T); i++)
    {
       if(type == OPM_PROTOCOLS[i].type)
       {
-         protocol = libopm_protocol_config_create();
+         protocol_config = libopm_protocol_config_create();
 
-         protocol->type = &OPM_PROTOCOLS[i];
-         protocol->port = port;
+         protocol_config->type = &OPM_PROTOCOLS[i];
+         protocol_config->port = port;
   
-         node = libopm_node_create(protocol);
+         node = libopm_node_create(protocol_config);
          libopm_list_add(scanner->protocols, node);
 
          return OPM_SUCCESS;
@@ -359,7 +366,96 @@ OPM_ERR_T opm_addtype(OPM_T *scanner, int type, int port)
 
 
 
-/* protocol_config_create
+/* opm_addcustom
+
+     Add custom protocol format and port.
+
+   Parameters:
+     scanner: Scanner to add custom protocol to
+     id: a unique ID to describe this protocol (ex: HTTP, SOCKS4)
+     format: Format string of custom protocol
+     port: Port custom protocol scans on
+
+   Return:
+     OPM_ERR_T
+*/
+
+OPM_ERR_T opm_addcustom(OPM_T *scanner, char *id, char *format, unsigned short int port)
+{
+
+   OPM_NODE_T *node;
+   OPM_PROTOCOL_CONFIG_T *protocol_config;
+   OPM_PROTOCOL_T *protocol;
+
+   protocol_config = libopm_protocol_config_create();
+   protocol = libopm_protocol_create();
+
+   protocol_config->type = protocol;
+   protocol_config->port = port;
+
+   protocol->id = (char *) strdup(id);
+   protocol->format = (char *) strdup(format);
+   protocol->type = OPM_TYPE_CUSTOM;
+
+   node = libopm_node_create(protocol_config);
+   libopm_list_add(scanner->protocols, node);
+
+   return OPM_SUCCESS;
+}
+
+
+
+/* libopm_protocol_create
+ *
+ *    Create OPM_PROTOCOL_T struct.
+ *
+ * Parameters:
+ *    None
+ * Return:
+ *    Pointer to new struct
+ */
+
+static OPM_PROTOCOL_T *libopm_protocol_create()
+{
+   OPM_PROTOCOL_T *ret;
+   ret = MyMalloc(sizeof(OPM_PROTOCOL_T));
+
+   ret->type           = 0;
+   ret->format         = NULL;
+   ret->write_function = NULL;
+   ret->read_function  = NULL;
+   
+   return ret;
+}
+
+
+
+/* libopm_protocol_free
+ *
+ *    Free an OPM_PROTOCOL_T struct. Assume that if 
+ *    format is not NULL, it is pointed to dynamically 
+ *    allocated memory and free it.
+ * 
+ * Parameters:
+ *    protocol: struct to free
+ * 
+ * Return:
+ *    None
+ */
+
+static void libopm_protocol_free(OPM_PROTOCOL_T *protocol)
+{
+   if(protocol->format != NULL)
+      MyFree(protocol->format);
+   if(protocol->id != NULL)
+      MyFree(protocol->id);
+   MyFree(protocol);
+}
+
+
+
+
+/* libopm_protocol_config_create
  *
  *    Allocate and return address of a new OPM_PROTOCOL_CONFIG_T
  *
@@ -370,7 +466,7 @@ OPM_ERR_T opm_addtype(OPM_T *scanner, int type, int port)
  *    Address of new OPM_PROTOCOL_CONFIG_T
  */
 
-OPM_PROTOCOL_CONFIG_T *libopm_protocol_config_create()
+static OPM_PROTOCOL_CONFIG_T *libopm_protocol_config_create()
 {
    OPM_PROTOCOL_CONFIG_T *ret;
    ret = MyMalloc(sizeof(OPM_PROTOCOL_CONFIG_T));
@@ -1072,7 +1168,10 @@ static void libopm_do_writeready(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTIO
    protocol = conn->protocol;
 
    /* Call write function for specific protocol */
-   protocol->write_function(scanner, scan, conn);
+   if(protocol->type == OPM_TYPE_CUSTOM)
+      libopm_proxy_custom(scanner, scan, conn);
+   else 
+      protocol->write_function(scanner, scan, conn);
 
    /* Flag as NEGSENT so we don't have to send data again*/
    conn->state = OPM_STATE_NEGSENT;  
@@ -1080,6 +1179,7 @@ static void libopm_do_writeready(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTIO
 
 
 
+
 /* do_hup
  *
  *    Connection ended prematurely
index ba8b18c7ccd4e8a89926501448aff7bb1d11f575..4fb92b1b483132f85b5ac3c408a14c7e342902bb 100644 (file)
@@ -50,6 +50,7 @@ struct _OPM_PROTOCOL
    OPM_PROXYWRITE_T *write_function;    /* Write function handler for this protocol */
    OPM_PROXYREAD_T  *read_function;     /* Read function handler for this protocol */
 
+   char *id;
    char *format;                        /* Custom formatting if this is a custom protocol 
                                            this is only valid if type == OPM_TYPE_CUSTOM */
 };
index 4ec8484346dd2fa761f4c454fc0de1044a34ae64..eca6119dff74c6381f4f98e54c43b712f18d547e 100644 (file)
--- a/src/opm.h
+++ b/src/opm.h
@@ -51,7 +51,9 @@ void opm_remote_free(OPM_REMOTE_T *);
 
 OPM_ERR_T opm_config(OPM_T *, int, void *);
 OPM_ERR_T opm_scan(OPM_T *, OPM_REMOTE_T *);
-OPM_ERR_T opm_addtype(OPM_T *, int, int);
+
+OPM_ERR_T opm_addtype(OPM_T *, int, unsigned short int);
+OPM_ERR_T opm_addcustom(OPM_T *, char *, char *, unsigned short int);
 
 OPM_ERR_T opm_remote_callback(OPM_REMOTE_T *, int, OPM_CALLBACK_T *);
 OPM_ERR_T opm_callback(OPM_T *, int, OPM_CALLBACK_T *);
index 9b06746d6804addf0e46e95946222806fbc2273f..06798080161ffcc62c09661a5ab9c106d3fcdf14 100644 (file)
@@ -37,6 +37,69 @@ RCSID("$Id$");
 
 static char SENDBUFF[512];
 
+
+/* proxy_custom
+ *
+ * Handle writing of custom protocol
+ *
+ * Parameters:
+ *    scanner: scanner object
+ *    scan: scan object
+ *    conn: specific connection to handle scan on
+ *
+ * Return:
+ *    None
+ */
+
+int libopm_proxy_custom(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
+{
+   OPM_PROTOCOL_T *protocol;
+   int sblen, fmpos;
+   char c, tmp[32];
+
+   char *scan_ip                =  (char *) libopm_config(scanner->config, OPM_CONFIG_SCAN_IP);
+   unsigned short int scan_port =  *(int *) libopm_config(scanner->config, OPM_CONFIG_SCAN_PORT);
+  
+   protocol = conn->protocol;
+   sblen = fmpos = 0;
+   SENDBUFF[0] = '\0';
+
+   while((c = protocol->format[fmpos++]) != '\0')
+   {
+      if(c == '%')
+      {
+         switch(protocol->format[fmpos])
+         {
+            case '\0':
+               continue;
+            case '%':
+               break;
+            case 'i':
+               strncat(SENDBUFF, scan_ip, (511 - sblen));
+               break;
+            case 'p':
+               snprintf(tmp, 31, "%d", scan_port);
+               strncat(SENDBUFF, tmp, (511 - sblen));
+               break;
+         }
+
+         sblen = strlen(SENDBUFF);
+         fmpos++;
+      }
+      else
+      {
+         SENDBUFF[sblen] = c;
+         SENDBUFF[++sblen] = '\0';
+      }
+   }
+
+
+   printf("SENDBUFF: [%s]\n", SENDBUFF);
+
+   return OPM_SUCCESS;
+}
+
+
 int libopm_proxy_http_write(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
 {
    snprintf(SENDBUFF, 128, "CONNECT %s:%d HTTP/1.0\r\n\r\n",
index dbaf105d16ab72b11a15e49ca8d3ada10541915b..c20b8c2128b3cfdfd9b30b3df87bd7dc21acb990 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "libopm.h"
 
+int libopm_proxy_custom(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
 int libopm_proxy_http_write(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
 int libopm_proxy_socks4_write(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
 int libopm_proxy_socks5_write(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
index 0b948b00206715b573526d2c173fda842abccae1..c7a09d0cd7f3ce491ba5160ba5da823243f785a5 100644 (file)
@@ -79,6 +79,8 @@ int main(int argc, char **argv)
    opm_addtype(scanner, OPM_TYPE_SOCKS4, 1080);
    opm_addtype(scanner, OPM_TYPE_SOCKS5, 1080);
 
+   opm_addcustom(scanner, "HTTP", "CONNECT %i:%p HTTP/1.1", 80);
+
    switch(opm_scan(scanner, remote))
    {
       case OPM_SUCCESS: