int DOopen() { char *ent; int s; if(linkopen) { printf("Use \"CLOSE\" to close the connection first.\n"); return(0); } if(cmdargc < 2) readline("Host: ", host, sizeof(host)); else strncpy(host, cmdargv[1], sizeof(host)); ent = malloc(strlen(host)+13); strcpy(ent, "/dev/tcp/"); strcat(ent, host); strcat(ent, ":21"); s = open(ent, O_RDWR); if(s < 0) { perror("ftp: connect"); return(s); } ftpcomm_fd = s; fpcommin = fdopen(ftpcomm_fd, "r"); fpcommout = fdopen(ftpcomm_fd, "w"); s = DOgetreply(); if(s < 0) { fclose(fpcommin); fclose(fpcommout); close(ftpcomm_fd); return(s); } if(s != 220) { fclose(fpcommin); fclose(fpcommout); close(ftpcomm_fd); return(0); } linkopen = 1; return(s); }
int DOopen() { nwio_tcpconf_t tcpconf; nwio_tcpcl_t tcpcopt; char *tcp_device; tcpport_t port; int s; struct hostent *hp; struct servent *servent; if(linkopen) { printf("Use \"CLOSE\" to close the connection first.\n"); return(0); } if(cmdargc < 2) { if(readline("Host: ", host, sizeof(host)) < 0) return(-1); } else strncpy(host, cmdargv[1], sizeof(host)); if((servent = getservbyname("ftp", "tcp")) == (struct servent *)NULL) { fprintf(stderr, "ftp: Could not find ftp tcp service\n"); port = htons(21); } else port = (tcpport_t)servent->s_port; hp = gethostbyname(host); if(hp == (struct hostent *)NULL) { hostip = (ipaddr_t)0; printf("Unresolved host %s\n", host); return(0); } else memcpy((char *) &hostip, (char *) hp->h_addr, hp->h_length); /* This HACK allows the server to establish data connections correctly */ /* when using the loopback device to talk to ourselves */ #ifdef __NBSD_LIBC if((hostip & ntohl(0xFF000000)) == inet_addr("127.0.0.0")) #else if((hostip & NTOHL(0xFF000000)) == inet_addr("127.0.0.0")) #endif hostip = myip; if((tcp_device = getenv("TCP_DEVICE")) == NULL) tcp_device = "/dev/tcp"; if((ftpcomm_fd = open(tcp_device, O_RDWR)) < 0) { perror("ftp: open error on tcp device"); return(-1); } tcpconf.nwtc_flags = NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP; tcpconf.nwtc_remaddr = hostip; tcpconf.nwtc_remport = port; s = ioctl(ftpcomm_fd, NWIOSTCPCONF, &tcpconf); if(s < 0) { perror("ftp: ioctl error on NWIOSTCPCONF"); close(ftpcomm_fd); return(-1); } tcpcopt.nwtcl_flags = 0; s = ioctl(ftpcomm_fd, NWIOTCPCONN, &tcpcopt); if(s < 0) { perror("ftp: ioctl error on NWIOTCPCONN"); close(ftpcomm_fd); return(-1); } s = ioctl(ftpcomm_fd, NWIOGTCPCONF, &tcpconf); if(s < 0) { perror("ftp: ioctl error on NWIOGTCPCONF"); close(ftpcomm_fd); return(-1); } s = DOgetreply(); if(s < 0) { close(ftpcomm_fd); return(s); } if(s != 220) { close(ftpcomm_fd); return(0); } linkopen = 1; return(s); }