home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Musik / Misc / Amster / Source / napster.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-27  |  14.2 KB  |  758 lines

  1. /*
  2. ** Napster Protocol
  3. */
  4.  
  5. #include "include/config.h"
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include <proto/exec.h>
  12. #include <proto/socket.h>
  13. #include <netdb.h>
  14. #include <sys/time.h>
  15. #include <sys/socket.h>
  16. #include <sys/ioctl.h>
  17. #include <netinet/tcp.h>
  18. #include <bsdsocket/socketbasetags.h>
  19. #include <error.h>
  20.  
  21. #include "include/gui.h"
  22. #include "include/msg.h"
  23. #include "include/napster.h"
  24. #include "include/navigator.h"
  25. #include "include/protos.h"
  26. #include "include/prefs.h"
  27. #include "include/share.h"
  28. #include "include/transfer.h"
  29. #include "include/download.h"
  30. #include "include/upload.h"
  31. #include "amster_Cat.h"
  32.  
  33. #define NAP_SWAPIP(x) ( ((x&0xFF)<<24) | ((x&0xFF00)<<8) | ((x&0xFF0000)>>8) | (x>>24) )
  34.  
  35. struct Library *SocketBase=NULL;
  36. u_long nap_sigmask;
  37. long nap_sock= -1;
  38. long loc_sock= -1;
  39.  
  40. char nap_host[512];
  41. char nap_server[512+6];
  42.  
  43. int nap_state= -1;
  44. #define NAPS_OFF -1
  45. #define NAPS_MAINCON 0
  46. #define NAPS_MAINON 1
  47. #define NAPS_CON 2
  48. #define NAPS_ON 3
  49.  
  50. char nap_buffer[AMSTER_NAPBUFSIZE];
  51. char *nap_buf = &nap_buffer[4];
  52.  
  53. char *nap_linktype[] = {
  54.     (char*)_MSG_LINE_UNKNOWN,
  55.     (char*)_MSG_LINE_14400,
  56.     (char*)_MSG_LINE_28800,
  57.     (char*)_MSG_LINE_33600,
  58.     (char*)_MSG_LINE_57600,
  59.     (char*)_MSG_LINE_64000,
  60.     (char*)_MSG_LINE_128000,
  61.     (char*)_MSG_LINE_CABLE,
  62.     (char*)_MSG_LINE_DSL,
  63.     (char*)_MSG_LINE_T1,
  64.     (char*)_MSG_LINE_T3,
  65.     NULL
  66. };
  67.  
  68. BOOL nap_logininit(void);
  69. int nap_connect(char *server);
  70. int nap_recv(u_char *buf);
  71. void nap_interpret(u_int com, char *data);
  72. void nap_parseresult(int type, char *data);
  73. char *nap_token(char **s);
  74. u_long nap_ltoken(char **str);
  75. int nap_itoken(char **str);
  76. int createlistener(void);
  77. char *getneterror(int type);
  78.  
  79.  
  80. void nap_login_fromlist(char *server)
  81. {
  82.     if (!nap_logininit()) return;
  83.  
  84.     nap_state = NAPS_CON;
  85.     gui_stat((char*)MSG_STATUS2_CONNECTINGTO, server);
  86.     if (!nap_connect(server)) {
  87.         nap_logout();
  88.         return;
  89.     }
  90. }
  91.  
  92.  
  93. void nap_login(void)
  94. {
  95.     if (!nap_logininit()) return;
  96.  
  97.     if (prf->server) {
  98.         nap_state = NAPS_CON;
  99.         gui_stat((char*)MSG_STATUS2_CONNECTINGTO, prf->server);
  100.         if (!nap_connect(prf->server)) {
  101.             nap_logout();
  102.             return;
  103.         }
  104.     }
  105.     else {
  106.         nap_state = NAPS_MAINCON;
  107.         gui_stat((char*)MSG_STATUS2_FINDINGOPTIMAL);
  108.         if (!nap_connect(prf->mainserver)) {
  109.             nap_logout();
  110.             return;
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116. BOOL nap_logininit(void)
  117. {
  118.     gui_state(0);
  119.     SocketBase = OpenLibrary("bsdsocket.library", 0);
  120.     if (!SocketBase) {
  121.         gui_stat((char *)MSG_NO_TCPIP);
  122.         nap_logout();
  123.         return FALSE;
  124.     }
  125.     SocketBaseTags(SBTM_SETVAL(SBTC_SIGIOMASK), (char *)nap_sigmask, TAG_DONE);
  126.  
  127.     return TRUE;
  128. }
  129.  
  130.  
  131. void nap_logout(void)
  132. {
  133.     nap_state = NAPS_OFF;
  134.     if(nap_sock>=0) CloseSocket(nap_sock);
  135.     nap_sock = -1;
  136.     if(loc_sock>=0) CloseSocket(loc_sock);
  137.     loc_sock = -1;
  138.     if(SocketBase) CloseLibrary(SocketBase);
  139.     SocketBase = NULL;
  140.     gui_state(-1);
  141. }
  142.  
  143.  
  144. void nap_listen(void)
  145. {
  146.     static u_char buf[AMSTER_NAPBUFSIZE];
  147.     static struct fd_set fds;
  148.     static struct timeval tv;
  149.  
  150.     if (nap_state == NAPS_OFF) return;
  151.  
  152.     FD_ZERO(&fds);
  153.     FD_SET(nap_sock,&fds);
  154.     tv.tv_sec = 0;
  155.     tv.tv_usec = 0;
  156.  
  157.     switch (nap_state) {
  158.         case NAPS_MAINCON:
  159.             if (WaitSelect(nap_sock+1,NULL,&fds,NULL,&tv,0) != 1) break;
  160.             {
  161.                 long tmp=0;
  162.                 IoctlSocket(nap_sock,FIONBIO,(char*)&tmp);
  163.                 nap_state = NAPS_MAINON;
  164.             }
  165.  
  166.         case NAPS_MAINON:
  167.             if (WaitSelect(nap_sock+1,&fds,NULL,NULL,&tv,0) != 1) break;
  168.             {
  169.                 int r;
  170.                 char *col;
  171.                 r = recv(nap_sock, buf, 255,0);
  172.                 CloseSocket(nap_sock);
  173.                 nap_sock = -1;
  174.                 if (r <= 0) {
  175.                     gui_stat((char *)MSG_STATUS2_CONNECTFAILED_TMP, nap_server, getneterror(Errno()));
  176.                     nap_logout();
  177.                     return;
  178.                 }
  179.                 buf[r] = 0;
  180.                 col = strchr(buf, '\n');
  181.                 if (col) *col=0;
  182.                 if (strncmp(buf,"wait",4)==0 || strncmp(buf,"busy",4)==0 || strncmp(buf,"127.0.0.1",9)==0) {
  183.                     /* Sometimes the server returns "127.0.0.1:1111" - quite odd. */
  184.                     gui_stat((char *)MSG_INFO_SERVERBUSY);
  185.                     nap_logout();
  186.                     return;
  187.                 }
  188.                 gui_debugf((char *)MSG_INFO_OPTIMAL, buf);
  189.                 gui_stat((char *)MSG_STATUS2_CONNECTINGTO, buf);
  190.                 if (!nap_connect(buf)) {
  191.                     nap_logout();
  192.                     return;
  193.                 }
  194.                 nap_state = NAPS_CON;
  195.             }
  196.             break;
  197.  
  198.         case NAPS_CON:
  199.             if (WaitSelect(nap_sock+1,NULL,&fds,NULL,&tv,0) != 1) break;
  200.             {
  201.                 long tmp=0;
  202.                 int r = 0;
  203.  
  204.                 IoctlSocket(nap_sock, FIONBIO, (char *)&tmp);
  205.  
  206.                 nap_state = NAPS_ON;
  207.                 gui_stat((char *)MSG_STATUS2_LOGGINGINTO, nap_server);
  208.                 gui_state(1);
  209.  
  210.                 DoMethod(gui->WI_Navigator, NAVI_MARKSERVER, nap_server);
  211.  
  212.                 if (prf->regflag > 0) r = nap_sendbuf(NAPC_CREATEUSER, prf->user);
  213.                 /* Create user before we attempt to log in */
  214.  
  215.                 if (r == -1) {
  216.                     prf->regflag = 1;
  217.                     if (Errno() == EPIPE) {
  218.                         gui_stat((char *)MSG_STATUS2_CONNECTFAILED_TMP, nap_server, getneterror(Errno()));
  219.                         nap_logout();
  220.                         return;
  221.                     }
  222.                 }
  223.                 if (prf->regflag == 1) {
  224.                     sprintf(nap_buf, "%s %s %d \"%s\" %d", prf->user, prf->pass, 0 /*prf->port */, prf->napvers, prf->link);
  225.                 }
  226.                 else {
  227.                     sprintf(nap_buf, "%s %s %d \"%s\" %d", gui->ConnectUser, gui->ConnectPw, 0 /*prf->port */, prf->napvers, prf->link);
  228.                 }
  229.                 prf->regflag = 1;
  230.                 nap_send(NAPC_LOGIN);
  231.             }
  232.             break;
  233.  
  234.         case NAPS_ON:
  235.             while (1) {
  236.                 int r;
  237.                 FD_ZERO(&fds);
  238.                 FD_SET(nap_sock,&fds);
  239.                 tv.tv_sec=0;
  240.                 tv.tv_usec=0;
  241.                 if (WaitSelect(nap_sock+1,&fds,NULL,NULL,&tv,0) < 1) {
  242.                     return;
  243.                 }
  244.  
  245.                 r = nap_recv(buf);
  246.                 if (r == -1) {
  247.                     gui_stat((char *)MSG_ERR_NETWORKERROR);
  248.                     nap_logout();
  249.                     return;
  250.                 }
  251.                 if (r == 1) nap_interpret(buf[2]+(buf[3]<<8), buf+4);
  252.             }
  253.     }
  254. }
  255.  
  256.  
  257. void nap_send(u_int com)
  258. {
  259.     int len;
  260.  
  261.     if (!gui_napon) return;
  262.     /* Avoid crash - but calling functions should check this first! */
  263.  
  264.     len = strlen(nap_buf);
  265.     nap_buffer[0] = len&0xFF;
  266.     nap_buffer[1] = (len&0xFF00)>>8;
  267.     nap_buffer[2] = com&0xFF;
  268.     nap_buffer[3] = (com&0xFF00)>>8;
  269.     send(nap_sock,nap_buffer,4+len,0);
  270. }
  271.  
  272.  
  273. int nap_sendbuf(u_int com, char *buf)
  274. {
  275.     static u_char hdr[4];
  276.     int len, r;
  277.  
  278.     if (!gui_napon) return -1;
  279.     /* Avoid crash - but calling functions should check this first! */
  280.  
  281.     len = strlen(buf);
  282.     hdr[0] = len&0xFF;
  283.     hdr[1] = (len&0xFF00)>>8;
  284.     hdr[2] = com&0xFF;
  285.     hdr[3] = (com&0xFF00)>>8;
  286.  
  287.     r = send(nap_sock, hdr, 4, 0);
  288.     if (r <= 0) return r;
  289.  
  290.     r = send(nap_sock, buf, len, 0);
  291.     if (r <= 0) return r;
  292.     else return 0;
  293. }
  294.  
  295.  
  296. void nap_songfree(song s)
  297. {
  298.     if (!s) return;
  299.     if (s->title) free(s->title);
  300.     if (s->md5) free(s->md5);
  301.     if (s->user) free(s->user);
  302.     free(s);
  303. }
  304.  
  305.  
  306. song nap_songdup(song s)
  307. {
  308.     song sn;
  309.  
  310.     sn = malloc(sizeof(_song));
  311.     if (!sn) return(NULL);
  312.     memset(sn,0,sizeof(_song));
  313.  
  314.     sn->title = strdup(s->title);
  315.     sn->md5 = strdup(s->md5);
  316.     sn->size = s->size;
  317.     sn->bit = s->bit;
  318.     sn->freq = s->freq;
  319.     sn->time = s->time;
  320.     sn->user = strdup(s->user);
  321.     sn->ip = s->ip;
  322.     sn->link = s->link;
  323.  
  324.     if (!sn->title || !sn->md5 || !sn->user) {
  325.         nap_songfree(sn);
  326.         return(NULL);
  327.     }
  328.  
  329.     return(sn);
  330. }
  331.  
  332.  
  333. char *nap_strippath(char *name)
  334. {
  335.     int i;
  336.     char tmp;
  337.  
  338.     for (i=strlen(name)-1; i>=0; i--) {
  339.         tmp = name[i];
  340.         if (tmp==':' || tmp=='/' || tmp=='\\') return(name+i+1);
  341.     }
  342.     return(name);
  343. }
  344.  
  345.  
  346. /* private functions */
  347.  
  348. int nap_connect(char *server)
  349. {
  350.     struct hostent *host;
  351.     struct sockaddr_in sin;
  352.     long tmp=1;
  353.     char *addr, *col;
  354.     int port = 0;
  355.  
  356.     addr = strdup(server);
  357.     col = strtok(addr, ":");
  358.     if (col) col = strtok(NULL,"");
  359.     if (col) port = atoi(col);
  360.  
  361.     if (!port) {
  362.         gui_stat((char *)MSG_INFO_PORTNEEDED);
  363.         free(addr);
  364.         return(0);
  365.     }
  366.  
  367.     host = gethostbyname(addr);
  368.     free(addr);
  369.     if (!host) {
  370.         gui_stat((char *)MSG_ERR_LOOKUPFAILED);
  371.         return(0);
  372.     }
  373.     memcpy(&sin.sin_addr, host->h_addr, host->h_length);
  374.     sin.sin_family = host->h_addrtype;
  375.     sin.sin_port = port;
  376.     sin.sin_len = sizeof(sin);
  377.  
  378.     strcpy(nap_host, host->h_name);
  379.     strcpy(nap_server, server);
  380.  
  381.     nap_sock = socket(AF_INET,SOCK_STREAM,0);
  382.     if (nap_sock<0) {
  383.         gui_debug((char *)MSG_ERR_SOCKETERROR);
  384.         return(0);
  385.     }
  386.  
  387.     IoctlSocket(nap_sock,FIOASYNC,(char *)&tmp);
  388.     IoctlSocket(nap_sock,FIONBIO,(char *)&tmp);
  389.  
  390.     tmp = connect(nap_sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
  391.     if(tmp != -1) return(1);
  392.     if(Errno()==EINPROGRESS) return(1);
  393.  
  394.     gui_stat((char *)MSG_STATUS2_CONNECTFAILED_TMP, nap_host, getneterror(Errno()));
  395.     return(0);
  396. }
  397.  
  398.  
  399. int nap_recv(u_char *buf)
  400. {
  401.     static u_int blen=0;
  402.     u_int len=4;
  403.     int ret;
  404.  
  405.     if (blen>=4) {
  406.         len = buf[0] + (buf[1]<<8) + 4;
  407.         if (len==0) {
  408.             buf[blen] = 0;
  409.             blen = 0;
  410.             return(1);
  411.         }
  412.     }
  413.  
  414.  
  415.     ret = recv(nap_sock, buf+blen, len-blen,0);
  416.     if (ret<=0) {
  417.         blen = 0;
  418.         return(-1);
  419.     }
  420.     blen += ret;
  421.  
  422.  
  423.     if (blen==4) {
  424.         len = buf[0] + (buf[1]<<8) + 4;
  425.         if (len==0) {
  426.             buf[blen] = 0;
  427.             blen = 0;
  428.             return(1);
  429.         }
  430.     }
  431.  
  432.     if(blen==len) {
  433.         buf[blen] = 0;
  434.         blen = 0;
  435.         return(1);
  436.     }
  437.  
  438.     return(0);
  439. }
  440.  
  441.  
  442. void nap_interpret(u_int com, char *data)
  443. {
  444.     switch(com) {
  445.         case NAPC_PUBLICMSGRECV:
  446.         case NAPC_JOINACK:
  447.         case NAPC_JOINMSG:
  448.         case NAPC_USERPART:
  449.         case NAPC_USERLIST:
  450.         case NAPC_USERLISTEND:
  451.         case NAPC_CHANNELTOPIC:
  452.             chat_interpret(com, data);
  453.             break;
  454.         case NAPC_LOGINERROR:
  455.             gui_debugf("\33bLogin error:\33n %s\n", data);
  456.             break;
  457.         case NAPC_REGSUCCESS:
  458.             gui_debug((char *)MSG_INFO_REGSUCCESS);
  459.             break;
  460.         case NAPC_REGUSED:
  461.             gui_debug((char *)MSG_INFO_REGUSED);
  462.             break;
  463.         case NAPC_SEARCHRESULT:
  464.             nap_parseresult(0,data);
  465.             break;
  466.         case NAPC_SEARCHCOMPLETE:
  467.             gui_found(NULL);
  468.             break;
  469.         case NAPC_BROWSERESULT:
  470.             nap_parseresult(1,data);
  471.             break;
  472.         case NAPC_BROWSECOMPLETE:
  473.             gui_found(NULL);
  474.             break;
  475.         case NAPC_FILECOUNT:
  476.             {
  477.             int a,b,c;
  478.             a = nap_itoken(&data);
  479.             b = nap_itoken(&data);
  480.             c = nap_itoken(&data);
  481.             gui_srvstat(a,b,c);
  482.             break;
  483.             }
  484.         case NAPC_PRIVATEMSG:
  485.             {
  486.             char *nick, *msg;
  487.  
  488.             nick = nap_token(&data);
  489.             msg = nick + strlen(nick) + 1;
  490.             msg_got(nick, msg);
  491.             break;
  492.             }
  493.         case NAPC_FILEINFO:
  494.             {
  495.             char *title,*user;
  496.             u_long ip;
  497.             int port;
  498.             user = nap_token(&data);
  499.             ip = nap_ltoken(&data);
  500.             port = nap_itoken(&data);
  501.             title = nap_token(&data);
  502.             dl_startq(title, user, NAP_SWAPIP(ip), port);
  503.             }
  504.             break;
  505.         case NAPC_LOGINRESP:
  506.             DoMethod(gui->shwin, SHARE_NOTIFYALL);
  507.             gui_state(2);
  508.             break;
  509.         case NAPC_WHOISRESP:
  510.             msg_gotwhois(data);
  511.             break;
  512.         case NAPC_WHOWASRESP:
  513.             {
  514.             char *user, *level;
  515.             u_long lastseen;
  516.  
  517.             user = nap_token(&data);
  518.             level = nap_token(&data);
  519.             lastseen = nap_ltoken(&data);
  520.  
  521.             DoMethod(gui->mwin, MSG_WHOWAS, user, level, lastseen);
  522.             }
  523.             break;
  524.         case NAPC_UPLOADREQ:
  525.             {
  526.             char *user,*fname;
  527.             user = nap_token(&data);
  528.             fname = nap_token(&data);
  529.             upload_req(user,fname);
  530.             }
  531.             break;
  532.         case NAPC_ALTDLACK:
  533.             {
  534.             char *nick, *fname, *md5;
  535.             u_long ip;
  536.             int port, speed;
  537.  
  538.             nick = nap_token(&data);
  539.             ip = nap_ltoken(&data);
  540.             port = nap_itoken(&data);
  541.             fname = nap_token(&data);
  542.             md5 = nap_token(&data);
  543.             speed = nap_itoken(&data);
  544.  
  545.             ul_startq(fname, nick, NAP_SWAPIP(ip), port, speed);
  546.             }
  547.             break;
  548.         case NAPC_REMOTEQUEUEFULL:
  549.             {
  550.             char *nick, *fname;
  551.             int size, limit;
  552.             nick = nap_token(&data);
  553.             fname = nap_token(&data);
  554.             size = nap_itoken(&data);
  555.             limit = nap_itoken(&data);
  556.  
  557.             DoMethod(gui->dwin, DL_RETRY, fname, nick, limit);
  558.             }
  559.             break;
  560.         case NAPC_GETERROR:
  561.             {
  562.             char *user,*fname;
  563.             user = nap_token(&data);
  564.             fname = nap_token(&data);
  565.             DoMethod(gui->dwin, DL_SETERROR, fname, user, ERROR_LOGGEDOUT);
  566.             }
  567.             break;
  568.         case NAPC_GENERALERROR:
  569.             gui_debugf("\33bError:\33n %s\n", data);
  570.             break;
  571.         case NAPC_SYSMSG:
  572.             gui_debug(data);
  573.             break;
  574.         case NAPC_GLOBALMSG:
  575.             {
  576.             char *nick, *text, buf[50];
  577.  
  578.             nick = nap_token(&data);
  579.             text = nick+strlen(nick)+1;
  580.             sprintf(buf, MSG_GLOBALMESSAGE_TITLE, nick);
  581.  
  582.             MUI_Request(gui->app, gui->win, 0L,
  583.                 buf,
  584.                 (char *)MSG_OK_GAD,
  585.                 text);
  586.             }
  587.             break;
  588.         default:
  589.             gui_debugf("<<%d:%s>>",com,data);
  590.     }
  591. }
  592.  
  593.  
  594. void nap_parseresult(int type, char *data)
  595. {
  596.     song s;
  597.     char *tmp=data;
  598.  
  599.     s = malloc(sizeof(_song));
  600.     if(!s) return;
  601.     memset(s,0,sizeof(_song));
  602.  
  603.     switch(type) {
  604.         case 0:
  605.             s->title = strdup(nap_token(&tmp));
  606.             s->md5 = strdup(nap_token(&tmp));
  607.             strtok(s->md5,"-");
  608.             s->size = nap_ltoken(&tmp);
  609.             s->bit = nap_itoken(&tmp);
  610.             s->freq = nap_itoken(&tmp);
  611.             s->time = nap_itoken(&tmp);
  612.             s->user = strdup(nap_token(&tmp));
  613.             s->ip = nap_ltoken(&tmp);
  614.             s->ip = NAP_SWAPIP(s->ip);
  615.             s->link = nap_itoken(&tmp);
  616.             break;
  617.  
  618.         case 1:
  619.             s->user = strdup(nap_token(&tmp));
  620.             s->title = strdup(nap_token(&tmp));
  621.             s->md5 = strdup(nap_token(&tmp));
  622.             strtok(s->md5,"-");
  623.             s->size = nap_ltoken(&tmp);
  624.             s->bit = nap_itoken(&tmp);
  625.             s->freq = nap_itoken(&tmp);
  626.             s->time = nap_itoken(&tmp);
  627.             break;
  628.     }
  629.  
  630.     if (!s->user || !s->title || !s->md5) {
  631.         nap_songfree(s);
  632.         return;
  633.     }
  634.  
  635.     gui_found(s);
  636. }
  637.  
  638.  
  639. u_long nap_ltoken(char **str)
  640. {
  641.     char *t;
  642.  
  643.     t = nap_token(str);
  644.     if(!t) return(0);
  645.     return((u_long)atol(t));
  646. }
  647.  
  648.  
  649. int nap_itoken(char **str)
  650. {
  651.     char *t;
  652.  
  653.     t = nap_token(str);
  654.     if(!t) return(0);
  655.     return(atoi(t));
  656. }
  657.  
  658.  
  659. char *nap_token(char **str)
  660. {
  661.     int sf=0,i,len=0;
  662.     char c, *t=NULL;
  663.  
  664.     if (!*str) return(NULL);
  665.  
  666.     for (i=0; ; i++) {
  667.         c = *(*str + i);
  668.         switch (c) {
  669.             case '\0':
  670.                 *str = NULL;
  671.                 if (len==0 || sf) return(NULL);
  672.                 return(t);
  673.             case '"':
  674.                 if (sf) {
  675.                     *(*str+i) = 0;
  676.                     *str = *str+i+1;
  677.                     return(t);
  678.                 }
  679.                 sf=1;
  680.                 break;
  681.             case ' ':
  682.                 if (len==0) break;
  683.                 if (!sf) {
  684.                     *(*str+i) = 0;
  685.                     *str = *str+i+1;
  686.                     return(t);
  687.                 }
  688.                 break;
  689.             default:
  690.                 if (!t) t=*str+i;
  691.                 len++;
  692.         }
  693.     }
  694. }
  695.  
  696.  
  697. /*
  698. int createlistener(void)
  699. {
  700.     struct sockaddr_in server;
  701.  
  702.     if (loc_sock != -1) return 1;
  703.  
  704.     loc_sock = socket(AF_INET, SOCK_STREAM, 0);
  705.     if (loc_sock < 0) {
  706.         gui_debug("Error creating listener socket!\n");
  707.         return 0;
  708.     }
  709.  
  710.     server.sin_family = AF_INET;
  711.     server.sin_addr.s_addr = htonl(INADDR_ANY);
  712.     server.sin_port = htons(prf->port);
  713.  
  714.     if (bind(loc_sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
  715.         gui_debug("Error binding listening socket\n");
  716.         CloseSocket(loc_sock);
  717.         loc_sock = -1;
  718.         return 0;
  719.     }
  720.  
  721.     listen(loc_sock, 0);    / Number of allowed connections /
  722.  
  723.     return 1;
  724. }
  725. */
  726.  
  727.  
  728. char *getneterror(int type)
  729. {
  730.     char *message, buf[512];
  731.  
  732.     switch (type) {
  733.         case ENETDOWN:
  734.             message = (char *)MSG_NETERROR_ENETDOWN;
  735.             break;
  736.         case ENETUNREACH:
  737.             message = (char *)MSG_NETERROR_ENETUNREACH;
  738.             break;
  739.         case ECONNRESET:
  740.             message = (char *)MSG_NETERROR_ECONNRESET;
  741.             break;
  742.         case ETIMEDOUT:
  743.             message = (char *)MSG_NETERROR_ETIMEDOUT;
  744.             break;
  745.         case ECONNREFUSED:
  746.             message = (char *)MSG_NETERROR_ECONNREFUSED;
  747.             break;
  748.         case EPIPE:
  749.             message = (char *)MSG_NETERROR_EPIPE;
  750.             break;
  751.         default:
  752.             sprintf(buf, MSG_NETERROR_UNKNOWN, type);
  753.             message = buf;
  754.     }
  755.  
  756.     return message;
  757. }
  758.