Java: Hopefully working HubListeners.
[doldaconnect.git] / daemon / fnet-adc.c
index 1357257..5c609f8 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Dolda Connect - Modular multiuser Direct Connect-style client
- *  Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
+ *  Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -38,7 +38,7 @@
 #include "transfer.h"
 #include "sysevents.h"
 #include "net.h"
-#include "tiger.h"
+#include <tiger.h>
 
 /* Protocol states */
 #define ADC_PROTOCOL 0
@@ -59,6 +59,7 @@ struct qcmd {
 };
 
 struct adchub {
+    struct socket *sk;
     char *inbuf;
     size_t inbufdata, inbufsize;
     wchar_t *sid;
@@ -67,6 +68,7 @@ struct adchub {
     wchar_t **sup;
     iconv_t ich;
     int state;
+    struct wcspair *hubinf;
     struct qcmd *queue;
 };
 
@@ -191,7 +193,14 @@ static void freeqcmd(struct qcmd *qcmd)
 }
 
 #define ADC_CMDFN(name) static void name(struct fnetnode *fn, wchar_t *command, wchar_t *sender, int argc, wchar_t **argv)
-#define ADC_CMDCOM struct socket *sk = fn->sk; struct adchub *hub = fn->data;
+#ifdef __GNUC__
+#define UNUSED __attribute__ ((unused))
+#else
+#define UNUSED
+#endif
+#define ADC_CMDCOM \
+       struct adchub *hub UNUSED = fn->data; \
+       struct socket *sk UNUSED = hub->sk;
 
 ADC_CMDFN(cmd_sup)
 {
@@ -215,6 +224,7 @@ ADC_CMDFN(cmd_sup)
        } else if(!wcsncmp(argv[i], L"RM", 2)) {
            if(!f)
                continue;
+           free(hub->sup[o]);
            memmove(hub->sup[o], hub->sup[o + 1], parrlen(hub->sup) - o);
        }
     }
@@ -232,9 +242,19 @@ ADC_CMDFN(cmd_sid)
     }
 }
 
+ADC_CMDFN(cmd_inf)
+{
+    ADC_CMDCOM;
+    
+    if(sender == NULL) {
+       
+    }
+}
+
 static struct command hubcmds[] = {
     {L"SUP", 1, 0, cmd_sup},
     {L"SID", 2, 0, cmd_sid},
+    {L"INF", 0, 0, cmd_inf},
     {NULL, 0, 0, NULL}
 };
 
@@ -332,32 +352,31 @@ static void huberr(struct socket *sk, int err, struct fnetnode *fn)
     killfnetnode(fn);
 }
 
-static void hubconnect(struct fnetnode *fn)
+static void hubconnect(struct fnetnode *fn, struct socket *sk)
 {
     struct adchub *hub;
     
-    fn->sk->readcb = (void (*)(struct socket *, void *))hubread;
-    fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr;
-    fn->sk->data = fn;
-    getfnetnode(fn);
+    sk->readcb = (void (*)(struct socket *, void *))hubread;
+    sk->errcb = (void (*)(struct socket *, int, void *))huberr;
+    sk->data = fn;
     
     hub = smalloc(sizeof(*hub));
     memset(hub, 0, sizeof(*hub));
+    getsock(hub->sk = sk);
     if((hub->ich = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) {
        flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
        killfnetnode(fn);
        return;
     }
     fn->data = hub;
-    sendadc(fn->sk, 0, L"HSUP", L"ADBASE", eoc, NULL);
+    sendadc(sk, 0, L"HSUP", L"ADBASE", eoc, NULL);
 }
 
 static void hubdestroy(struct fnetnode *fn)
 {
     struct adchub *hub;
     
-    if((hub = fn->data) == NULL)
-       return;
+    hub = fn->data;
     iconv_close(hub->ich);
     if(hub->inbuf != NULL)
        free(hub->inbuf);
@@ -366,6 +385,14 @@ static void hubdestroy(struct fnetnode *fn)
     free(hub);
 }
 
+static void hubkill(struct fnetnode *fn)
+{
+    struct adchub *hub;
+    
+    hub = fn->data;
+    hub->sk->close = 1;
+}
+
 static int hubsetnick(struct fnetnode *fn, wchar_t *newnick)
 {
     return(0);
@@ -376,14 +403,17 @@ static int hubreqconn(struct fnetpeer *peer)
     return(0);
 }
 
-static struct fnet adcnet = {
+static struct fnet adcnet_store = {
     .connect = hubconnect,
     .destroy = hubdestroy,
+    .kill = hubkill,
     .setnick = hubsetnick,
     .reqconn = hubreqconn,
     .name = L"adc"
 };
 
+static struct fnet *adcnet = &adcnet_store;
+
 static int run(void)
 {
     int ret;
@@ -394,12 +424,12 @@ static int run(void)
     ret = 0;
     for(fn = fnetnodes; fn != NULL; fn = nextfn) {
        nextfn = fn->next;
-       if(fn->fnet != &adcnet)
+       if(fn->fnet != adcnet)
            continue;
        if((hub = fn->data) == NULL)
            continue;
        if((qcmd = ulqcmd(&hub->queue)) != NULL) {
-           if((fn->sk != NULL) && (fn->sk->state == SOCK_EST))
+           if((hub->sk != NULL) && (hub->sk->state == SOCK_EST))
                dispatch(qcmd, fn);
            freeqcmd(qcmd);
            ret = 1;
@@ -413,7 +443,7 @@ static void preinit(int hup)
 {
     if(hup)
        return;
-    regfnet(&adcnet);
+    regfnet(adcnet);
 }
 
 static int init(int hup)
@@ -455,7 +485,15 @@ static void terminate(void)
 }
 
 static struct configvar myvars[] = {
+    /** Specifies a specific UDP port to use for ADC search
+     * results. If left unspecified, a port is allocated
+     * dynamically. Useful for NAT routers (see also the
+     * net.visibleipv4 address for those cases). */
     {CONF_VAR_INT, "udpport", {.num = 0}},
+    /** Specifies a specific TCP port to use for ADC peer
+     * connections. If left unspecified, a port is allocated
+     * dynamically. Useful for NAT routers (see also the
+     * net.visibleipv4 address for those cases). */
     {CONF_VAR_INT, "tcpport", {.num = 0}},
     {CONF_VAR_END}
 };