static int news_open(News * news) { int sock, status, fd; sock = openSocket(news->host, "nntp", news->port); if (sock < 0) goto open_err; news->rf = newInputStream(sock); if ((fd = dup(sock)) < 0) goto open_err; news->wf = fdopen(fd, "wb"); if (!news->rf || !news->wf) goto open_err; IStype(news->rf) |= IST_UNCLOSE; news_command(news, NULL, NULL, &status); if (status != 200 && status != 201) goto open_err; if (news->mode) { news_command(news, "MODE", news->mode, &status); if (status != 200 && status != 201) goto open_err; } return TRUE; open_err: news_close(news); return FALSE; }
static void news_close(News * news) { if (!news->host) return; if (news->rf) { IStype(news->rf) &= ~IST_UNCLOSE; ISclose(news->rf); news->rf = NULL; } if (news->wf) { fclose(news->wf); news->wf = NULL; } news->host = NULL; }
int ISfileno(InputStream stream) { if (stream == NULL) return -1; switch (IStype(stream) & ~IST_UNCLOSE) { case IST_BASIC: return *(int *)stream->base.handle; case IST_FILE: return fileno(stream->file.handle->f); #ifdef USE_SSL case IST_SSL: return stream->ssl.handle->sock; #endif case IST_ENCODED: return ISfileno(stream->ens.handle->is); default: return -1; } }
static void ftp_close(FTP ftp) { if (!ftp->host) return; if (ftp->rf) { IStype(ftp->rf) &= ~IST_UNCLOSE; ISclose(ftp->rf); ftp->rf = NULL; } if (ftp->wf) { fclose(ftp->wf); ftp->wf = NULL; } if (ftp->data) { fclose(ftp->data); ftp->data = NULL; } ftp->host = NULL; return; }
static int ftp_login(FTP ftp) { int sock, status; sock = openSocket(ftp->host, "ftp", 21); if (sock < 0) goto open_err; if (ftppass_hostnamegen && !strcmp(ftp->user, "anonymous")) { size_t n = strlen(ftp->pass); if (n > 0 && ftp->pass[n - 1] == '@') { #ifdef INET6 struct sockaddr_storage sockname; #else struct sockaddr_in sockname; #endif socklen_t socknamelen = sizeof(sockname); if (!getsockname(sock, (struct sockaddr *)&sockname, &socknamelen)) { struct hostent *sockent; Str tmp = Strnew_charp(ftp->pass); #ifdef INET6 char hostbuf[NI_MAXHOST]; if (getnameinfo((struct sockaddr *)&sockname, socknamelen, hostbuf, sizeof hostbuf, NULL, 0, NI_NAMEREQD) == 0) Strcat_charp(tmp, hostbuf); else if (getnameinfo((struct sockaddr *)&sockname, socknamelen, hostbuf, sizeof hostbuf, NULL, 0, NI_NUMERICHOST) == 0) Strcat_m_charp(tmp, "[", hostbuf, "]", NULL); else Strcat_charp(tmp, "unknown"); #else if ((sockent = gethostbyaddr((char *)&sockname.sin_addr, sizeof(sockname.sin_addr), sockname.sin_family))) Strcat_charp(tmp, sockent->h_name); else Strcat_m_charp(tmp, "[", inet_ntoa(sockname.sin_addr), "]", NULL); #endif ftp->pass = tmp->ptr; } } } ftp->rf = newInputStream(sock); ftp->wf = fdopen(dup(sock), "wb"); if (!ftp->rf || !ftp->wf) goto open_err; IStype(ftp->rf) |= IST_UNCLOSE; ftp_command(ftp, NULL, NULL, &status); if (status != 220) goto open_err; if (fmInitialized) { message(Sprintf("Sending FTP username (%s) to remote server.", ftp->user)->ptr, 0, 0); refresh(); } ftp_command(ftp, "USER", ftp->user, &status); /* * Some ftp daemons(e.g. publicfile) return code 230 for user command. */ if (status == 230) goto succeed; if (status != 331) goto open_err; if (fmInitialized) { message("Sending FTP password to remote server.", 0, 0); refresh(); } ftp_command(ftp, "PASS", ftp->pass, &status); if (status != 230) goto open_err; succeed: return TRUE; open_err: ftp_close(ftp); return FALSE; }