static void url_http_close(URL url) { URL_http *urlp = (URL_http *)url; int save_errno = errno; if(urlp->fp != NULL) socket_fclose(urlp->fp); free(url); errno = save_errno; }
static void close_news_server(NewsConnection *news) { if(news->fd != (SOCKET)-1) { socket_write(news->fd, "QUIT\r\n", 6); closesocket(news->fd); } if(news->fp != NULL) socket_fclose(news->fp); free(news->host); news->status = -1; }
static NewsConnection *open_news_server(char *host, unsigned short port) { NewsConnection *p; char buff[512]; for(p = connection_cache; p != NULL; p = p->next) { if(p->status == 0 && strcmp(p->host, host) == 0 && p->port == port) { p->status = 1; return p; } } for(p = connection_cache; p != NULL; p = p->next) if(p->status == -1) break; if(p == NULL) { if((p = (NewsConnection *)safe_malloc(sizeof(NewsConnection))) == NULL) return NULL; p->next = connection_cache; connection_cache = p; p->status = -1; } if((p->host = safe_strdup(host)) == NULL) return NULL; p->port = port; #ifdef __W32__ timeout_flag = 0; p->fd = open_socket(host, port); #else timeout_flag = 0; signal(SIGALRM, timeout); alarm(ALARM_TIMEOUT); p->fd = open_socket(host, port); alarm(0); signal(SIGALRM, SIG_DFL); #endif /* __W32__ */ if(p->fd == (SOCKET)-1) { int save_errno; VOLATILE_TOUCH(timeout_flag); #ifdef ETIMEDOUT if(timeout_flag) errno = ETIMEDOUT; #endif /* ETIMEDOUT */ if(errno) url_errno = errno; else { url_errno = URLERR_CANTOPEN; errno = ENOENT; } #ifdef DEBUG perror(host); #endif /* DEBUG */ save_errno = errno; free(p->host); errno = save_errno; return NULL; } if((p->fp = socket_fdopen(p->fd, "rb")) == NULL) { url_errno = errno; closesocket(p->fd); free(p->host); errno = url_errno; return NULL; } buff[0] = '\0'; if(socket_fgets(buff, sizeof(buff), p->fp) == NULL) { url_errno = errno; closesocket(p->fd); socket_fclose(p->fp); free(p->host); errno = url_errno; return NULL; } #ifdef DEBUG fprintf(stderr, "Connect status: %s", buff); #endif /* DEBUG */ if(buff[0] != NNTP_OK_ID) { closesocket(p->fd); socket_fclose(p->fp); free(p->host); url_errno = URLERR_CANTOPEN; errno = ENOENT; return NULL; } p->status = 1; return p; }