static void remote_set_service_url(MHEGBackend *t) { char cmd[32]; FILE *sock; char url[1024]; size_t len; /* send backend a "service" command, response is carousel service in dvb:// format */ snprintf(cmd, sizeof(cmd), "service\n"); if((sock = remote_command(t, true, cmd)) == NULL || remote_response(sock) != BACKEND_RESPONSE_OK || fgets(url, sizeof(url), sock) == NULL) { /* this should never happen, and I don't want a NULL rec_svc_def */ fatal("Unable to determine current service"); } /* chop any trailing \n off the URL */ len = strlen(url); while(len > 0 && url[len-1] == '\n') len --; /* overwrite any existing value */ t->rec_svc_def.size = len; t->rec_svc_def.data = safe_realloc(t->rec_svc_def.data, len); memcpy(t->rec_svc_def.data, url, len); return; }
error_code_t cd_remote(receiver_param *rp, const char *command) { if (command[2] != ' ' && command[2] != '\0') return INV_CMD; return remote_command(rp, 'C', (char*)&command[3], strlen(&command[3]), NULL); }
void remote_retune(MHEGBackend *t, OctetString *service) { char cmd[128]; FILE *sock; /* assert */ if(service->size < 6 || strncmp((char *) service->data, "dvb://", 6) != 0) fatal("remote_retune: unable to tune to '%.*s'", service->size, service->data); snprintf(cmd, sizeof(cmd), "retune %u\n", si_get_service_id(service)); if((sock = remote_command(t, true, cmd)) == NULL || remote_response(sock) != BACKEND_RESPONSE_OK) { error("Unable to retune to '%.*s' (service_id %u)", service->size, service->data, si_get_service_id(service)); } /* a "retune" command closes the connection to the backend, so close our end */ if(t->be_sock != NULL) { fclose(t->be_sock); t->be_sock = NULL; } /* update rec://svc/def */ remote_set_service_url(t); return; }
error_code_t get(receiver_param *rp, const char *command) { char *file_name; unsigned char name_size; long file_size, available; error_code_t e; FILE *stream; char *cwd; struct statfs fs; if(command[3] != ' ' && command[3] != '\0') return INV_CMD; file_name = (char*)&command[4]; name_size = (unsigned char) strlen(file_name); if(name_size > DATA_MAX_SIZE) return BIG_NAME; e = remote_command(rp, 'G', file_name, name_size, &file_size); if(e != OK) return e; /* obtem espaco livre */ cwd = getcwd(NULL, 0); statfs(cwd, &fs); free(cwd); available = fs.f_bavail * fs.f_bsize; if( file_size >= available) { e = NO_SPACE; send_error(rp->source_sd, e,0); return e; } stream = fopen(file_name, "w"); if(!stream) { if (errno == EACCES) e = NO_PERMISSION; else if (errno == EISDIR) e = IS_DIR; send_error(rp->source_sd, e, 0); return e; } send_ack(rp->source_sd, 0); receiving_file(rp, fileno(stream), 1, file_size); fclose(stream); return OK; }
error_code_t put(receiver_param *rp, const char *command) // ou sd ao invez do rp { char *file_name; unsigned char name_size; long file_size; error_code_t e; FILE *stream; if (command[3] != ' ' && command[3] != '\0') return INV_CMD; file_name = (char*)&command[4]; name_size = (unsigned char)strlen(file_name); if (name_size > DATA_MAX_SIZE) return BIG_NAME; stream = fopen(file_name, "r"); if (!stream) { switch (errno) { case EACCES: return NO_PERMISSION; case ENOENT: return NO_FILE; } } e = remote_command(rp, 'P', file_name, name_size, NULL); if (e != OK) { fclose(stream); return e; } file_size = get_file_size(stream); // tamanho do arquivo e = send_d(rp, (char*)&file_size, sizeof(long), 1); // talvez colocar mais coisas que o tamanho do arquivo if (e != OK) { fclose(stream); return e; } file_transfer(rp, stream, 2, file_size); fclose(stream); return OK; }
void MHEGBackend_fini(MHEGBackend *b) { /* send quit command */ if(b->be_sock != NULL && remote_command(b, true, "quit\n") != NULL) fclose(b->be_sock); safe_free(b->base_dir); safe_free(b->rec_svc_def.data); return; }
bool remote_checkContentRef(MHEGBackend *t, ContentReference *name) { char cmd[PATH_MAX]; FILE *sock; bool exists; snprintf(cmd, sizeof(cmd), "check %s\n", MHEGEngine_absoluteFilename(name)); if((sock = remote_command(t, true, cmd)) == NULL) return false; exists = (remote_response(sock) == BACKEND_RESPONSE_OK); return exists; }
FILE * remote_openFile(MHEGBackend *t, OctetString *name) { char cmd[PATH_MAX]; FILE *sock; unsigned int size; char buf[8 * 1024]; size_t nread; FILE *out; snprintf(cmd, sizeof(cmd), "file %s\n", MHEGEngine_absoluteFilename(name)); if((sock = remote_command(t, true, cmd)) == NULL) return NULL; /* if it exists, read the file size */ if(remote_response(sock) != BACKEND_RESPONSE_OK || fgets(cmd, sizeof(cmd), sock) == NULL || sscanf(cmd, "Length %u", &size) != 1) { return NULL; } /* tmpfile() will delete the file when we fclose() it */ out = tmpfile(); while(out != NULL && size > 0) { nread = (size < sizeof(buf)) ? size : sizeof(buf); nread = fread(buf, 1, nread, sock); if(fwrite(buf, 1, nread, out) != nread) { error("Unable to write to local file"); fclose(out); out = NULL; } size -= nread; } /* rewind the file */ if(out != NULL) rewind(out); return out; }
int main (void){ serial_init_b(9600); remote_init(0x00); uint8_t command; //Main program loop while (1){ command = remote_command(); if (command == REMOTE_MENU) UDR0 = 'M'; else if (command == REMOTE_RIGHT) UDR0 = 'R'; else if (command == REMOTE_LEFT) UDR0 = 'L'; else if (command == REMOTE_UP) UDR0 = 'U'; else if (command == REMOTE_DOWN) UDR0 = 'D'; else if (command == REMOTE_CENTER) UDR0 = 'C'; else if (command == REMOTE_A_CENTER) UDR0 = 'C'; else if (command == REMOTE_A_PLAY) UDR0 = 'P'; } }
bool remote_loadFile(MHEGBackend *t, OctetString *name, OctetString *out) { char cmd[PATH_MAX]; FILE *sock; unsigned int size; size_t nread; snprintf(cmd, sizeof(cmd), "file %s\n", MHEGEngine_absoluteFilename(name)); if((sock = remote_command(t, true, cmd)) == NULL) return false; /* if it exists, read the file size */ if(remote_response(sock) != BACKEND_RESPONSE_OK || fgets(cmd, sizeof(cmd), sock) == NULL || sscanf(cmd, "Length %u", &size) != 1) { error("Unable to load '%.*s'", name->size, name->data); return false; } verbose("Loading '%.*s'", name->size, name->data); out->size = size; out->data = safe_malloc(size); nread = 0; while(!feof(sock) && nread < size) nread += fread(out->data + nread, 1, size - nread, sock); /* did we read it all */ if(nread < size) { error("Unable to load '%.*s'", name->size, name->data); safe_free(out->data); out->data = NULL; out->size = 0; return false; } return true; }
static void prompt(void *udata) { cli_socket_t *cli_socket = udata; char *line; if (prompt_wait) return; line = linenoise(COMMAND_PROMPT); if (line != NULL && line[0] != '\0') { linenoiseHistoryAdd(line); if (!local_command(line) && remote_command(line, cli_socket)) { jlog(L_ERROR, "unable to send command to remote target"); free(line); return; } } free(line); }
bool remote_isServiceAvailable(MHEGBackend *t, OctetString *service) { char cmd[128]; FILE *sock; bool available = true; /* assert */ if(service->size < 6 || strncmp((char *) service->data, "dvb://", 6) != 0) fatal("remote_isServiceAvailable: invalid service '%.*s'", service->size, service->data); snprintf(cmd, sizeof(cmd), "available %u\n", si_get_service_id(service)); if((sock = remote_command(t, true, cmd)) == NULL || remote_response(sock) != BACKEND_RESPONSE_OK) { available = false; } return available; }
static MHEGStream * open_stream(MHEGBackend *t, int service_id, bool have_audio, int *audio_tag, int *audio_type, bool have_video, int *video_tag, int *video_type) { MHEGStream *stream; bool loopback; char *avcmd; char cmd[PATH_MAX]; FILE *be; char pids[PATH_MAX]; unsigned int audio_pid = 0; unsigned int video_pid = 0; bool err; char *ts_dev; int tail; /* are the backend and frontend on the same host */ loopback = (t->addr.sin_addr.s_addr == htonl(INADDR_LOOPBACK)); if(loopback) avcmd = "demux"; else avcmd = "stream"; /* no PIDs required */ if(!have_audio && !have_video) return NULL; /* video and audio */ else if(have_audio && have_video) snprintf(cmd, sizeof(cmd), "av%s %d %d %d\n", avcmd, service_id, *audio_tag, *video_tag); /* audio only */ else if(have_audio) snprintf(cmd, sizeof(cmd), "a%s %d %d\n", avcmd, service_id, *audio_tag); /* video only */ else snprintf(cmd, sizeof(cmd), "v%s %d %d\n", avcmd, service_id, *video_tag); /* false => create a new connection to the backend */ if((be = remote_command(t, false, cmd)) == NULL) return NULL; /* did it work */ if(remote_response(be) != BACKEND_RESPONSE_OK || fgets(pids, sizeof(pids), be) == NULL) { fclose(be); return NULL; } /* update the PID variables */ if(have_audio && have_video) err = (sscanf(pids, "AudioPID %u AudioType %u VideoPID %u VideoType %u", &audio_pid, audio_type, &video_pid, video_type) != 4); else if(have_audio) err = (sscanf(pids, "AudioPID %u AudioType %u", &audio_pid, audio_type) != 2); else err = (sscanf(pids, "VideoPID %u VideoType %u", &video_pid, video_type) != 2); if(err) { fclose(be); return NULL; } /* set up the MHEGStream */ stream = safe_malloc(sizeof(MHEGStream)); /* * if we sent a "demux" command, open the DVR device * if we sent a "stream" command, the TS is streamed from the backend */ if(loopback) { /* backend tells us where the DVR device is */ if(fgets(pids, sizeof(pids), be) == NULL || strncmp(pids, "Device ", 7) != 0) { fclose(be); safe_free(stream); return NULL; } ts_dev = pids + 7; /* chop off any trailing \n */ tail = strlen(ts_dev) - 1; while(tail > 0 && ts_dev[tail] == '\n') ts_dev[tail--] = '\0'; if((stream->ts = fopen(ts_dev, "r")) == NULL) { fclose(be); safe_free(stream); return NULL; } /* backend keeps the PID filters in place until we close this connection */ stream->demux = be; } else { stream->ts = be; stream->demux = NULL; } /* now we are sure it all worked, set up the return values */ if(have_audio) *audio_tag = audio_pid; if(have_video) *video_tag = video_pid; return stream; }
/*ARGSUSED*/ int nis_put_printer(const ns_printer_t *printer) { static char *domain = NULL; char *map = "printers.conf.byname"; char *tmp = NULL; char *host = NULL; char lfile[BUFSIZ]; char rfile[BUFSIZ]; char cmd[BUFSIZ]; if (domain == NULL) (void) yp_get_default_domain(&domain); if ((yp_master(domain, (char *)map, &host) != 0) && (yp_master(domain, "passwd.byname", &host) != 0)) return (-1); if (snprintf(lfile, sizeof (lfile), "/tmp/%s", map) >= sizeof (lfile)) { syslog(LOG_ERR, "nis_put_printer:lfile buffer overflow"); return (-1); } if (snprintf(rfile, sizeof (rfile), "root@%s:/etc/%s", host, map) >= sizeof (rfile)) { syslog(LOG_ERR, "nis_put_printer:rfile buffer overflow"); return (-1); } if (((tmp = strrchr(rfile, '.')) != NULL) && (strcmp(tmp, ".byname") == 0)) *tmp = NULL; /* strip the .byname */ /* copy it local */ if (snprintf(cmd, sizeof (cmd), "rcp %s %s >/dev/null 2>&1", rfile, lfile) >= sizeof (cmd)) { syslog(LOG_ERR, "nis_put_printer:buffer overflow building cmd"); return (-1); } (void) system(cmd); /* could fail because it doesn't exist */ /* update it */ if (_file_put_printer(lfile, printer) != 0) return (-1); /* copy it back */ if (snprintf(cmd, sizeof (cmd), "rcp %s %s >/dev/null 2>&1", lfile, rfile) >= sizeof (cmd)) { syslog(LOG_ERR, "nis_put_printer:buffer overflow building cmd"); return (-1); } if (system(cmd) != 0) return (-1); /* copy the Makefile excerpt */ if (snprintf(cmd, sizeof (cmd), "rcp %s root@%s:%s.print >/dev/null 2>&1", MAKE_EXCERPT, host, NIS_MAKEFILE) >= sizeof (cmd)) { syslog(LOG_ERR, "nis_put_printer:buffer overflow building cmd"); return (-1); } if (system(cmd) != 0) return (-1); /* run the make */ if (snprintf(cmd, sizeof (cmd), "/bin/sh -c 'PATH=/usr/ccs/bin:/bin:/usr/bin:$PATH " "make -f %s -f %s.print printers.conf >/dev/null 2>&1'", NIS_MAKEFILE, NIS_MAKEFILE) >= sizeof (cmd)) { syslog(LOG_ERR, "nis_put_printer:buffer overflow on make"); return (-1); } return (remote_command(cmd, host)); }