{"id":85776,"date":"2020-05-15T23:04:29","date_gmt":"2020-05-15T15:04:29","guid":{"rendered":"http:\/\/4563.org\/?p=85776"},"modified":"2020-05-15T23:04:29","modified_gmt":"2020-05-15T15:04:29","slug":"c-%e8%af%ad%e8%a8%80_reentrant-%e8%bf%99%e4%b8%aa%e5%ae%8f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=85776","title":{"rendered":"C \u8bed\u8a00_REENTRANT \u8fd9\u4e2a\u5b8f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C \u8bed\u8a00_REENTRANT \u8fd9\u4e2a\u5b8f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : salamanderMH <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u95ee\u9898<\/h2>\n<p>\u770b\u4e86\u4e0b\u8fd9\u4e2a\u5e93\u7684\u6e90\u7801\uff0c\u4e00\u4e2a FTP \u7684\u5e93\uff0c\u7136\u540e\u6211\u770b\u4e86\u4e0b<code>FtpConnect<\/code>\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u770b\u5230\u7528\u5230\u4e86<code>_REENTRANT<\/code>\uff0c\u4e0d\u592a\u6e05\u695a\u4e3a\u4ec0\u4e48\u8981\u7528\u8fd9\u4e2a\u5b8f\uff0c\u4f3c\u4e4e\u662f\u591a\u7ebf\u7a0b\u6709\u7528<\/p>\n<pre><code>GLOBALDEF int FtpConnect(const char *host, netbuf **nControl) {     int sControl;     struct sockaddr_in sin;     int on=1;     netbuf *ctrl;     char *lhost;     char *pnum;      memset(&amp;sin,0,sizeof(sin));     sin.sin_family = AF_INET;     lhost = strdup(host);     pnum = strchr(lhost,':');     if (pnum == NULL)  pnum = \"ftp\";     else  *pnum++ = '\u0000';     if (isdigit(*pnum))  sin.sin_port = htons(atoi(pnum));     else     {  struct servent *pse; #if _REENTRANT  struct servent se;  char tmpbuf[TMP_BUFSIZ];  int i;  if ( ( i = getservbyname_r(pnum,\"tcp\",&amp;se,tmpbuf,TMP_BUFSIZ,&amp;pse) ) != 0 )  {      errno = i;      if ( ftplib_debug )   perror(\"getservbyname_r\");      free(lhost);      return 0;  } #else  if ((pse = getservbyname(pnum,\"tcp\") ) == NULL )  {      if ( ftplib_debug )   perror(\"getservbyname\");      free(lhost);      return 0;  } #endif  sin.sin_port = pse-&gt;s_port;     }     if ((sin.sin_addr.s_addr = inet_addr(lhost)) == INADDR_NONE)     {  struct hostent *phe; #ifdef _REENTRANT  struct hostent he;  char tmpbuf[TMP_BUFSIZ];  int i, herr;  if ( ( ( i = gethostbyname_r( lhost, &amp;he, tmpbuf, TMP_BUFSIZ, &amp;phe, &amp;herr ) ) != 0 ) ||       ( phe == NULL ) )  {      if ( ftplib_debug )   fprintf(stderr, \"gethostbyname: %sn\", hstrerror(herr));      free(lhost);      return 0;  } #else      if ((phe = gethostbyname(lhost)) == NULL)      {      if (ftplib_debug)   fprintf(stderr, \"gethostbyname: %sn\", hstrerror(h_errno));      free(lhost);      return 0;      } #endif      memcpy((char *)&amp;sin.sin_addr, phe-&gt;h_addr, phe-&gt;h_length);     }     free(lhost);     sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);     if (sControl == -1)     {  if (ftplib_debug)      perror(\"socket\");  return 0;     }     if (setsockopt(sControl,SOL_SOCKET,SO_REUSEADDR,      SETSOCKOPT_OPTVAL_TYPE &amp;on, sizeof(on)) == -1)     {  if (ftplib_debug)      perror(\"setsockopt\");  net_close(sControl);  return 0;     }     if (connect(sControl, (struct sockaddr *)&amp;sin, sizeof(sin)) == -1)     {  if (ftplib_debug)      perror(\"connect\");  net_close(sControl);  return 0;     }     ctrl = calloc(1,sizeof(netbuf));     if (ctrl == NULL)     {  if (ftplib_debug)      perror(\"calloc\");  net_close(sControl);  return 0;     }     ctrl-&gt;buf = malloc(FTPLIB_BUFSIZ);     if (ctrl-&gt;buf == NULL)     {  if (ftplib_debug)      perror(\"calloc\");  net_close(sControl);  free(ctrl);  return 0;     }     ctrl-&gt;handle = sControl;     ctrl-&gt;dir = FTPLIB_CONTROL;     ctrl-&gt;ctrl = NULL;     ctrl-&gt;data = NULL;     ctrl-&gt;cmode = FTPLIB_DEFMODE;     ctrl-&gt;idlecb = NULL;     ctrl-&gt;idletime.tv_sec = ctrl-&gt;idletime.tv_usec = 0;     ctrl-&gt;idlearg = NULL;     ctrl-&gt;xfered = 0;     ctrl-&gt;xfered1 = 0;     ctrl-&gt;cbbytes = 0;     if (readresp('2', ctrl) == 0)     {  net_close(sControl);  free(ctrl-&gt;buf);  free(ctrl);  return 0;     }     *nControl = ctrl;     return 1; } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1517022\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ea\u662f\u7528\u4f5c\u7279\u6027\u5f00\u5173\u5427\uff0c\u67d0\u4e2a\u5730\u65b9\u5b9a\u4e49\u4e4b\u540e\u5c31\u7528\u53ef\u91cd\u5165\u7684\u90a3\u5757\uff0c\u4e0d\u7136\u5c31\u666e\u901a\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1517023\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shiny2017 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u8981\u662f\u533a\u5206\u6bd4\u5982: getservbyname_r \u4e0e gethostbyname \u4e4b\u7c7b\u7684\u63a5\u53e3, \u4e0d\u5e26_r \u540e\u7f00\u7684\u5c31\u662f\u4e0d\u53ef\u91cd\u5165\u7684, \u6bd4\u5982 gethostbyname \u4f7f\u7528\u4e86\u5168\u5c40\u53d8\u91cf\u6216\u8005\u9759\u6001\u53d8\u91cf, \u5982\u679c\u8fdb\u7a0b\u7684\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u53c8\u4e0d\u52a0\u9501\u7684\u60c5\u51b5\u4e0b, \u90fd\u66f4\u65b0\u76f8\u540c\u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u4ea7\u751f\u95ee\u9898\u7684.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1517024\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : salamanderMH <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @shiny2017 \u8c22\u8c22\uff0c\u90a3\u770b\u8d77\u6765`gethostbyname`\u548c`getservbyname`\u6709\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1517025\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : salamanderMH <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             SO \u4e0a\u641c\u4e86\u4e0b\uff0c\u786e\u5b9e\u6709 thread safety \u7684\u95ee\u9898\uff1a<br \/>https:\/\/stackoverflow.com\/questions\/25951925\/gethostbyname-processes-resolving-consistently-even-from-different-threads                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C \u8bed\u8a00_REENTRANT \u8fd9\u4e2a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85776"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85776"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85776\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85776"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}