X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=daemon%2Ffnet-dc.c;h=3618b1262e26468ddb4eb2ab614091c782d08575;hb=refs%2Fheads%2Fjava;hp=175563b35211abe6bdba7952e07c2e79232a7eea;hpb=8b17e919cee63400e6de2c5f699c0a88d226b7e6;p=doldaconnect.git diff --git a/daemon/fnet-dc.c b/daemon/fnet-dc.c index 175563b..3618b12 100644 --- a/daemon/fnet-dc.c +++ b/daemon/fnet-dc.c @@ -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 * * 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 @@ -19,13 +19,11 @@ #include #include #include -#include #include #include #include #include #include -#include #include #include #include @@ -103,6 +101,7 @@ struct qcommand struct dchub { + struct socket *sk; char *inbuf; size_t inbufdata, inbufsize; struct qcommand *queue; @@ -110,6 +109,7 @@ struct dchub char *charset; char *nativename; char *nativenick; + char **supports; }; struct dcexppeer @@ -245,6 +245,24 @@ static int isdchash(struct hash *hash) return(1); } +/* + * Uncomment when used! + +static int hubsupports(struct dchub *hub, char *cap) +{ + char **p; + + if(hub->supports == NULL) + return(0); + for(p = hub->supports; *p != NULL; p++) + { + if(!strcasecmp(*p, cap)) + return(1); + } + return(0); +} +*/ + static int supports(struct dcpeer *peer, char *cap) { char **p; @@ -728,7 +746,7 @@ static void requestfile(struct dcpeer *peer) freedcpeer(peer); return; } - qstrf(peer->sk, "$UGetBlock %i %i %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf); + qstrf(peer->sk, "$UGetBlock %zi %zi %s|", peer->transfer->curpos, peer->transfer->size - peer->transfer->curpos, buf); } else { /* Use DCCHARSET for $Get paths until further researched... */ if((buf = icswcstombs(peer->transfer->path, DCCHARSET, NULL)) == NULL) @@ -737,7 +755,7 @@ static void requestfile(struct dcpeer *peer) freedcpeer(peer); return; } - qstrf(peer->sk, "$Get %s$%i|", buf, peer->transfer->curpos + 1); + qstrf(peer->sk, "$Get %s$%zi|", buf, peer->transfer->curpos + 1); } } @@ -1110,7 +1128,7 @@ static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char * goto out; prefix = sprintf2("$SR %s ", hub->nativenick); infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots")); - postfix = sprintf2(" (%s)\005%s|", formataddress(fn->sk->remote, fn->sk->remotelen), args + 4); + postfix = sprintf2(" (%s)\005%s|", formataddress(hub->sk->remote, hub->sk->remotelen), args + 4); dsk = sk; getsock(dsk); } else { @@ -1123,7 +1141,7 @@ static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char * addr.sin_port = htons(atoi(p2)); prefix = sprintf2("$SR %s ", hub->nativenick); infix = sprintf2(" %i/%i\005", slotsleft(), confgetint("transfer", "slots")); - postfix = sprintf2(" (%s)|", formataddress(fn->sk->remote, fn->sk->remotelen)); + postfix = sprintf2(" (%s)|", formataddress(hub->sk->remote, hub->sk->remotelen)); netdgramconn(dsk = netdupsock(udpsock), (struct sockaddr *)&addr, sizeof(addr)); } @@ -1231,10 +1249,10 @@ static void cmd_search(struct socket *sk, struct fnetnode *fn, char *cmd, char * if(node->f.b.hastth) { buf2 = base32encode(node->hashtth, 24); - qstrf(dsk, "%s%s\005%i%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix); + qstrf(dsk, "%s%s\005%zi%sTTH:%.39s%s", prefix, buf, node->size, infix, buf2, postfix); free(buf2); } else { - qstrf(dsk, "%s%s\005%i%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix); + qstrf(dsk, "%s%s\005%zi%s%s%s", prefix, buf, node->size, infix, hub->nativename, postfix); } free(buf); } @@ -1375,7 +1393,7 @@ static void cmd_to(struct socket *sk, struct fnetnode *fn, char *cmd, char *args return; *p2 = 0; p2 += 2; - hubrecvchat(fn->sk, fn, p, p2); + hubrecvchat(hub->sk, fn, p, p2); hubhandleaction(sk, fn, cmd, args); } @@ -1481,6 +1499,36 @@ static void cmd_logedin(struct socket *sk, struct fnetnode *fn, char *cmd, char hubhandleaction(sk, fn, cmd, args); } +static void cmd_hubsupports(struct socket *sk, struct fnetnode *fn, char *cmd, char *args) +{ + struct dchub *hub; + int i; + char *p, *p2; + char **arr; + size_t arrsize, arrdata; + + hub = fn->data; + if(hub->supports != NULL) + { + for(i = 0; hub->supports[i] != NULL; i++) + free(hub->supports[i]); + free(hub->supports); + } + arr = NULL; + arrsize = arrdata = 0; + p = args; + do + { + if((p2 = strchr(p, ' ')) != NULL) + *(p2++) = 0; + if(*p == 0) + continue; + addtobuf(arr, sstrdup(p)); + } while((p = p2) != NULL); + addtobuf(arr, NULL); + hub->supports = arr; +} + static void cmd_mynick(struct socket *sk, struct dcpeer *peer, char *cmd, char *args) { struct dcexppeer *expect; @@ -1861,7 +1909,7 @@ static void cmd_get(struct socket *sk, struct dcpeer *peer, char *cmd, char *arg lesk = wrapsock(fd); transferprepul(peer->transfer, sb.st_size, offset, -1, lesk); putsock(lesk); - qstrf(sk, "$FileLength %i|", peer->transfer->size); + qstrf(sk, "$FileLength %zi|", peer->transfer->size); } static void cmd_send(struct socket *sk, struct dcpeer *peer, char *cmd, char *args) @@ -2292,10 +2340,10 @@ static int hubreqconn(struct fnetpeer *peer) return(1); /* Shouldn't happen, of course, but who knows... */ if(tcpsock != NULL) { - sendctm(peer->fn->sk, mbsnick); + sendctm(hub->sk, mbsnick); expectpeer(mbsnick, peer->fn); } else { - qstrf(peer->fn->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick); + qstrf(hub->sk, "$RevConnectToMe %s %s|", hub->nativenick, mbsnick); } free(mbsnick); return(0); @@ -2335,12 +2383,12 @@ static int hubsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *st { if(*to == L'\0') { - qstrf(fn->sk, "<%s> %s|", hub->nativenick, mbsstring); + qstrf(hub->sk, "<%s> %s|", hub->nativenick, mbsstring); } else { - qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring); + qstrf(hub->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring); } } else { - qstrf(fn->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring); + qstrf(hub->sk, "$To: %s From: %s $<%s> %s|", mbsto, hub->nativenick, hub->nativenick, mbsstring); } free(mbsto); free(mbsstring); @@ -2427,8 +2475,10 @@ static struct hash *findsehash(struct sexpr *sexpr) return(h1); break; case SOP_OR: - h1 = findsehash(sexpr->l); - h2 = findsehash(sexpr->r); + if((h1 = findsehash(sexpr->l)) == NULL) + return(NULL); + if((h2 = findsehash(sexpr->r)) == NULL) + return(NULL); if(hashcmp(h1, h2)) return(h1); break; @@ -2453,7 +2503,7 @@ static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlis struct hash *hash; hub = fn->data; - if((fn->state != FNN_EST) || (fn->sk == NULL) || (fn->sk->state != SOCK_EST)) + if((fn->state != FNN_EST) || (hub->sk == NULL) || (hub->sk->state != SOCK_EST)) return(1); list = findsexprstrs(srch->sexpr); findsizelimit(srch->sexpr, &minsize, &maxsize); @@ -2531,15 +2581,15 @@ static int hubsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlis addtobuf(sstr, 0); if(tcpsock != NULL) { - if(sockgetremotename2(udpsock, fn->sk, &name, &namelen) < 0) + if(sockgetremotename2(udpsock, hub->sk, &name, &namelen) < 0) { flog(LOG_WARNING, "cannot get address of UDP socket"); } else { - qstrf(fn->sk, "$Search %s %s|", formataddress(name, namelen), sstr); + qstrf(hub->sk, "$Search %s %s|", formataddress(name, namelen), sstr); free(name); } } else { - qstrf(fn->sk, "$Search Hub:%s %s|", hub->nativenick, sstr); + qstrf(hub->sk, "$Search Hub:%s %s|", hub->nativenick, sstr); } free(sstr); freesl(&list); @@ -2572,6 +2622,7 @@ static struct command hubcmds[] = {"$UserCommand", cc(cmd_usercommand)}, {"$GetPass", cc(cmd_getpass)}, {"$LogedIn", cc(cmd_logedin)}, /* sic */ + {"$Supports", cc(cmd_hubsupports)}, {NULL, NULL} }; @@ -2870,10 +2921,13 @@ static void udpread(struct socket *sk, void *data) { for(fn = fnetnodes; fn != NULL; fn = fn->next) { - if((fn->fnet == &dcnet) && (fn->sk != NULL) && addreq(fn->sk->remote, (struct sockaddr *)&hubaddr)) + if((fn->fnet == &dcnet) && ((hub = fn->data) != NULL)) { - myfn = fn; - break; + if((hub->sk != NULL) && addreq(hub->sk->remote, (struct sockaddr *)&hubaddr)) + { + myfn = fn; + break; + } } } } @@ -3073,31 +3127,34 @@ static void freedcpeer(struct dcpeer *peer) numdcpeers--; } -static void hubconnect(struct fnetnode *fn) +static void hubconnect(struct fnetnode *fn, struct socket *sk) { - fn->sk->readcb = (void (*)(struct socket *, void *))hubread; - fn->sk->errcb = (void (*)(struct socket *, int, void *))huberr; - getfnetnode(fn); - fn->data = newdchub(fn); - fn->sk->data = fn; + struct dchub *hub; + + sk->readcb = (void (*)(struct socket *, void *))hubread; + sk->errcb = (void (*)(struct socket *, int, void *))huberr; + fn->data = hub = newdchub(fn); + sk->data = fn; + getsock(hub->sk = sk); return; } static void hubdestroy(struct fnetnode *fn) { + int i; struct dchub *hub; struct qcommand *qcmd; hub = (struct dchub *)fn->data; - if((fn->sk != NULL) && (fn->sk->data == fn)) - { - fn->sk->data = NULL; - putfnetnode(fn); - } - if(hub == NULL) - return; + putsock(hub->sk); while((qcmd = ulqcmd(&hub->queue)) != NULL) freeqcmd(qcmd); + if(hub->supports != NULL) + { + for(i = 0; hub->supports[i] != NULL; i++) + free(hub->supports[i]); + free(hub->supports); + } if(hub->nativename != NULL) free(hub->nativename); if(hub->nativenick != NULL) @@ -3109,6 +3166,14 @@ static void hubdestroy(struct fnetnode *fn) free(hub); } +static void hubkill(struct fnetnode *fn) +{ + struct dchub *hub; + + hub = (struct dchub *)fn->data; + hub->sk->close = 1; +} + static wchar_t *dcbasename(wchar_t *filename) { wchar_t *ret; @@ -3131,6 +3196,7 @@ static struct fnet dcnet = .name = L"dc", .connect = hubconnect, .destroy = hubdestroy, + .kill = hubkill, .setnick = hubsetnick, .reqconn = hubreqconn, .sendchat = hubsendchat, @@ -3243,7 +3309,7 @@ static void updatehmlist(void) if(node->f.b.type == FILE_REG) { addtobuf(buf, '|'); - sprintf(numbuf, "%i", node->size); + sprintf(numbuf, "%zi", node->size); bufcat(buf, numbuf, strlen(numbuf)); } addtobuf(buf, 13); @@ -3418,7 +3484,7 @@ static void updatexmllist(void) lev++; continue; } else { - fprintf(fs, "size); + fprintf(fs, "size); if(node->f.b.hastth) { hashbuf = base32encode(node->hashtth, 24); @@ -3528,7 +3594,7 @@ static void updatelists(int now) if(!now) { if(listwritetimer == NULL) - listwritetimer = timercallback(ntime() + 300, listtimercb, NULL); + listwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), listtimercb, NULL); return; } if(listwritetimer != NULL) @@ -3544,6 +3610,78 @@ static int shareupdate(unsigned long long uusharesize, void *data) return(0); } +static char *quotestr(char *str) +{ + unsigned char *buf; + unsigned char *p; + size_t bufsize, bufdata; + wchar_t *wbuf; + static char *enc = NULL; + size_t encsize, encdata; + + buf = NULL; + bufsize = bufdata = 0; + for(p = (unsigned char *)str; *p; p++) + { + if(*p == '\b') + bufcat(buf, "\\b", 2); + else if(*p == '\t') + bufcat(buf, "\\t", 2); + else if(*p == '\n') + bufcat(buf, "\\n", 2); + else if(*p == '\r') + bufcat(buf, "\\r", 2); + else if(*p == '\\') + bufcat(buf, "\\\\", 2); + else if(*p >= 32) + addtobuf(buf, *p); + else + bprintf(buf, "\\x%02x", *p); + } + addtobuf(buf, 0); + if(enc != NULL) + free(enc); + enc = NULL; + if((wbuf = icmbstowcs((char *)buf, DCCHARSET)) != NULL) + { + enc = icwcstombs(wbuf, NULL); + free(wbuf); + } + if(enc == NULL) + { + encsize = encdata = 0; + for(p = buf; *p; p++) { + if(*p < 128) + addtobuf(enc, *p); + else + bprintf(buf, "\\x%x", *p); + } + } + free(buf); + return(enc); +} + +static void logunimpl(char *list, char *cmd, char *args) +{ + FILE *log; + + if((log = fopen("/tmp/dc-unimpl", "a")) == NULL) + { + flog(LOG_WARNING, "could not open unimpl log: %s", strerror(errno)); + return; + } + fputs(list, log); + fputc('\t', log); + fputs(quotestr(cmd), log); + if(args != NULL) + { + fputc('\t', log); + fputs(quotestr(args), log); + } + fputc('\n', log); + fclose(log); +} + static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, struct socket *sk, void *data) { char *p; @@ -3557,11 +3695,16 @@ static void dispatchcommand(struct qcommand *qcmd, struct command *cmdlist, stru break; } if(cmd->handler != NULL) + { cmd->handler(sk, data, qcmd->string, p); -/* - else - flog(LOG_DEBUG, "Unimplemented DC command: %s \"%s\"", qcmd->string, p?p:"noargs"); -*/ + } else if(confgetint("dc", "logunimpl")) { + if(cmdlist == hubcmds) + logunimpl("hub", qcmd->string, p); + else if(cmdlist == peercmds) + logunimpl("peer", qcmd->string, p); + else + logunimpl("other?!", qcmd->string, p); + } } static int run(void) @@ -3585,10 +3728,10 @@ static int run(void) { if(*qcmd->string == '$') { - if((fn->sk != NULL) && (fn->sk->state == SOCK_EST)) - dispatchcommand(qcmd, hubcmds, fn->sk, fn); + if((hub->sk != NULL) && (hub->sk->state == SOCK_EST)) + dispatchcommand(qcmd, hubcmds, hub->sk, fn); } else if(*qcmd->string != 0) { - hubrecvchat(fn->sk, fn, NULL, qcmd->string); + hubrecvchat(hub->sk, fn, NULL, qcmd->string); } freeqcmd(qcmd); ret = 1; @@ -3707,12 +3850,36 @@ static void terminate(void) static struct configvar myvars[] = { + /** Specifies the share description reported to other DC users. */ {CONF_VAR_STRING, "desc", {.str = L""}}, + /** Specifies the speed reported to other DC users. Normal values + * are 28.8Kbps, 33.6Kbps, 56Kbps, Satellite, ISDN, DSL, Cable, + * LAN(T1) or LAN(T3)*/ {CONF_VAR_STRING, "speedstring", {.str = L"LAN(T1)"}}, + /** The e-mail address to report to other DC users. */ {CONF_VAR_STRING, "email", {.str = L"spam@spam.org"}}, + /** Specifies a specific UDP port to use for DC 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 DC 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}}, + /** If set to true, doldacond will do its best to emulate DC++ + * (currently v0.674). This should be left off if at all possible, + * since turning it on will violate the rules of most hubs and + * thus give hub owners an actual reason to kick you if it is + * detected. It might be needed for some of the more bone-headed + * hub owners, though. Note that DC++ emulation can also be turned + * on or off for individual hubs, overriding this setting. */ {CONF_VAR_BOOL, "dcppemu", {.num = 0}}, + /** Use for debugging. If set to true, doldacond will log all + * unknown commands it receives, and their arguments, to + * /tmp/dc-unimpl. */ + {CONF_VAR_BOOL, "logunimpl", {.num = 0}}, {CONF_VAR_END} };