Пример #1
0
/*
 * FtpAccess - return a handle for a data stream
 *
 * return 1 if successful, 0 otherwise
 */
GLOBALDEF int FtpAccess (const char *path, int typ, int mode, netbuf *nControl,
    netbuf **nData) {
    char buf[256];
    int dir;
    if ((path == NULL) &&
        ((typ == FTPLIB_FILE_WRITE) || (typ == FTPLIB_FILE_READ))) {
        sprintf(nControl->response,
            "Missing path argument for file transfer\n");
        return 0;
    }
    sprintf(buf, "TYPE %c", mode);
    if (!FtpSendCmd(buf, '2', nControl))
        return 0;
    switch (typ) {
        case FTPLIB_DIR:
            strcpy(buf, "NLST");
            dir = FTPLIB_READ;
            break;
        case FTPLIB_DIR_VERBOSE:
            strcpy(buf, "LIST");
            dir = FTPLIB_READ;
            break;
        case FTPLIB_FILE_READ:
            strcpy(buf, "RETR");
            dir = FTPLIB_READ;
            break;
        case FTPLIB_FILE_WRITE:
            strcpy(buf, "STOR");
            dir = FTPLIB_WRITE;
            break;
        default:
            sprintf(nControl->response, "Invalid open type %d\n", typ);
            return 0;
    }
    if (path != NULL) {
        int i = strlen(buf);
        buf[i++] = ' ';
        if ((strlen(path) + i) >= sizeof (buf))
            return 0;
        strcpy(&buf[i], path);
    }
    if (FtpOpenPort(nControl, nData, mode, dir) == -1)
        return 0;
    if (!FtpSendCmd(buf, '1', nControl)) {
        FtpClose(*nData);
        *nData = NULL;
        return 0;
    }
    (*nData)->ctrl = nControl;
    nControl->data = *nData;
    if (nControl->cmode == FTPLIB_PORT) {
        if (!FtpAcceptConnection(*nData, nControl)) {
            FtpClose(*nData);
            *nData = NULL;
            nControl->data = NULL;
            return 0;
        }
    }
    return 1;
}
Пример #2
0
static int FtpOpenData(stream_t* s,size_t newpos) {
  struct stream_priv_s* p = s->priv;
  int resp;
  char str[256],rsp_txt[256];

  // Open a new connection
  s->fd = FtpOpenPort(p);

  if(s->fd < 0) return 0;

  if(newpos > 0) {
    snprintf(str,255,"REST %"PRId64, (int64_t)newpos);

    resp = FtpSendCmd(str,p,rsp_txt);
    if(resp != 3) {
      mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
      newpos = 0;
    }
  }

  // Get the file
  snprintf(str,255,"RETR %s",p->filename);
  resp = FtpSendCmd(str,p,rsp_txt);

  if(resp != 1) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
    return 0;
  }

  s->pos = newpos;
  return 1;
}