static int msm_v4l2_vidioc_querymenu(char * args) { int min, max; char * pstr_id, * pstr_min, * pstr_max; struct v4l2_querymenu querymenu; char *prev_value; memset( &querymenu, 0, sizeof(querymenu)); if (args != NULL) { pstr_id = strtok_r(args, ", ", &prev_value); if (!pstr_id) { printf("msm_v4l2_vidioc_querymenu: Invalid Args\n"); return -1; } pstr_min = strtok_r(NULL, ", ", &prev_value); pstr_max = strtok_r(NULL, ", ", &prev_value); querymenu.id = string_to_id(pstr_id); if (msm_v4l2_vidioc_queryctrl(args) == -1) { printf("%s not supported\n", args); return -1; } if (pstr_min == NULL) { min = queryctrl.minimum; max = queryctrl.maximum; } else { min = atoi(pstr_min); if (!pstr_max) { printf("msm_v4l2_vidioc_querymenu: Invalid Args\n"); return -1; } max = atoi(pstr_max); } } else { // in this case queryctrl id has been set; querymenu.id = queryctrl.id; min = queryctrl.minimum; max = queryctrl.maximum; } for(querymenu.index = min; querymenu.index < (unsigned int) max; querymenu.index ++) { if ( ioctl(fd, VIDIOC_QUERYMENU, &querymenu)) { printf("%s: %s\n", __FUNCTION__, querymenu.name); } else { printf("%s failed\n", __FUNCTION__); return -1; } } return 0; }
static int8_t do_query(char *query, char *address) { uint32_t ip; if(!inet_pton(AF_INET, address, &ip)) { return ERROR_INTERNAL; } char *name = strstr(query, "name="), *id = strstr(query, "id="), *c; uint32_t len; uint8_t name_length; if(name && id) { name += 5; id += 3; c = name; while(*c != '&'&& *c) { if(*c >= 'A' && *c <= 'Z') { *c = *c - 'A' + 'a'; c++; continue; } if(!((*c >= 'a' && *c <= 'z') || (*c >= '0' && *c <= '9'))) { return INVALID_CHARS_NAME; } c++; } len = c - name; if(len >= MAX_NAME_LENGTH) { len = MAX_NAME_LENGTH; } name_length = len; c = id; while(*c != '&'&& *c){c++;} len = c - id; if(len != TOX_ID_SIZE * 2) { return INVALID_LENGTH; } uint8_t _id[TOX_ID_SIZE]; if(!string_to_id(_id, (uint8_t*)id)) { return INVALID_CHARS; } if(!validate_id(_id)) { return INVALID_CHECKSUM; } return database_write(_id, (uint8_t*)name, name_length, ip); } else { return 1; } }
void friend_add(char_t *name, STRING_IDX length, char_t *msg, STRING_IDX msg_length) { if(!length) { addfriend_status = ADDF_NONAME; return; } uint8_t id[TOX_FRIEND_ADDRESS_SIZE]; if(length == TOX_FRIEND_ADDRESS_SIZE * 2 && string_to_id(id, name)) { friend_addid(id, msg, msg_length); } else { /* not a regular id, try DNS discovery */ addfriend_status = ADDF_DISCOVER; dns_request(name, length); } }
static int msm_v4l2_vidioc_g_ctrl(char * args) { int id; struct v4l2_control control; char * pstr_of_id = args; memset( &control, 0, sizeof(control)); control.id = id = string_to_id(pstr_of_id); if (id < 0) { printf("%s: invalid ID\n", __FUNCTION__); return -1; } if ( ioctl(fd, VIDIOC_G_CTRL, & control) == -1) { printf("%s failed\n", __FUNCTION__); return -1; } else { printf("%s successed\n", __FUNCTION__); } return 0; }
static int msm_v4l2_vidioc_s_ctrl(char * args) { int id; struct v4l2_control control; char * pstr_of_id; char * pstr_of_value; char *prev_value; pstr_of_id = strtok_r(args, ", ", &prev_value); if (!pstr_of_id) { printf("%s: invalid args\n", __FUNCTION__); return -1; } pstr_of_value = strtok_r(NULL, ", ", &prev_value); memset( &control, 0, sizeof(control)); control.id = id = string_to_id(pstr_of_id); if (id < 0) { printf("%s: invalid ID\n", __FUNCTION__); return -1; } if (pstr_of_value == NULL) { msm_v4l2_vidioc_queryctrl(args); control.value = queryctrl.default_value; } else { control.value = atoi(pstr_of_value); } if ( ioctl(fd, VIDIOC_S_CTRL, & control) == -1) { printf("%s failed\n", __FUNCTION__); return -1; } else { printf("%s successed\n", __FUNCTION__); } return 0; }
void friend_add(char_t *name, STRING_IDX length, char_t *msg, STRING_IDX msg_length) { if(!length) { addfriend_status = ADDF_NONAME; return; } #ifdef EMOJI_IDS uint8_t emo_id[TOX_FRIEND_ADDRESS_SIZE]; if (emoji_string_to_bytes(emo_id, TOX_FRIEND_ADDRESS_SIZE, name, length) == TOX_FRIEND_ADDRESS_SIZE) { friend_addid(emo_id, msg, msg_length); } #endif uint8_t name_cleaned[length]; uint16_t length_cleaned = 0; unsigned int i; for (i = 0; i < length; ++i) { if (name[i] != ' ') { name_cleaned[length_cleaned] = name[i]; ++length_cleaned; } } if(!length_cleaned) { addfriend_status = ADDF_NONAME; return; } uint8_t id[TOX_FRIEND_ADDRESS_SIZE]; if(length_cleaned == TOX_FRIEND_ADDRESS_SIZE * 2 && string_to_id(id, name_cleaned)) { friend_addid(id, msg, msg_length); } else { /* not a regular id, try DNS discovery */ addfriend_status = ADDF_DISCOVER; dns_request(name_cleaned, length_cleaned); } }
static int msm_v4l2_vidioc_queryctrl(char * args) { int result = 0; int id_start, id_end; char * pstr_id; char *prev_value; memset(&queryctrl, 0, sizeof (queryctrl)); if ( args == NULL ) { id_start = V4L2_CID_BASE; id_end = V4L2_CID_LASTP1 - 1; } else { pstr_id = strtok_r(args, ", ", &prev_value); id_start = string_to_id(args); id_end = id_start; } for (queryctrl.id = id_start; queryctrl.id <= (unsigned int) id_end; queryctrl.id++) { if (ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) { printf ("%s: Control 0x%X %s", __FUNCTION__, queryctrl.id, queryctrl.name); if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) { printf (" disabled\n"); continue; } printf (" OK\n"); if (queryctrl.type == V4L2_CTRL_TYPE_MENU) { msm_v4l2_vidioc_querymenu(NULL); } } else { printf ("%s failed id 0x%X \n", __FUNCTION__, queryctrl.id); result = -1; } } return result; }
int main(int argc, char *argv[]) { unsigned char tox_id[TOX_ADDRESS_SIZE]; unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1]; TOX_ERR_NEW tox_new_err; int oc; size_t save_size = 0; uint8_t *save_data = NULL; allowed_toxid *allowed_toxid_obj = NULL; log_init(); while ((oc = getopt(argc, argv, "L:pi:C:s:P:dqhSF:DU:")) != -1) { switch(oc) { case 'L': /* Local port forwarding */ client_mode = 1; client_local_port_mode = 1; if(parse_local_port_forward(optarg, &local_port, &remote_host, &remote_port) < 0) { log_printf(L_ERROR, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n"); exit(1); } if(min_log_level == L_UNSET) { min_log_level = L_INFO; } log_printf(L_DEBUG, "Forwarding remote port %d to local port %d\n", remote_port, local_port); break; case 'P': /* Pipe forwarding */ client_mode = 1; client_pipe_mode = 1; if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0) { log_printf(L_ERROR, "Invalid value for -P option - use something like -P 127.0.0.1:22\n"); exit(1); } if(min_log_level == L_UNSET) { min_log_level = L_ERROR; } log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", remote_port); break; case 'p': /* Ping */ client_mode = 1; ping_mode = 1; if(min_log_level == L_UNSET) { min_log_level = L_INFO; } break; case 'i': /* Tox ID */ server_whitelist_mode = 1; log_printf(L_DEBUG, "Server whitelist mode enabled"); allowed_toxid_obj = (allowed_toxid *)calloc(sizeof(allowed_toxid), 1); if(!allowed_toxid_obj) { log_printf(L_ERROR, "Could not allocate memory for allowed_toxid"); exit(1); } remote_tox_id = optarg; if(!string_to_id(allowed_toxid_obj->toxid, optarg)) { log_printf(L_ERROR, "Invalid Tox ID"); exit(1); } LL_APPEND(allowed_toxids, allowed_toxid_obj); break; case 'C': /* Config directory */ strncpy(config_path, optarg, sizeof(config_path) - 1); if(optarg[strlen(optarg) - 1] != '/') { int optarg_len = strlen(optarg); config_path[optarg_len] = '/'; config_path[optarg_len + 1] = '\0'; } load_saved_toxid_in_client_mode = 1; break; case 's': /* Shared secret */ use_shared_secret = 1; memset(shared_secret, 0, TOX_MAX_FRIEND_REQUEST_LENGTH); strncpy(shared_secret, optarg, TOX_MAX_FRIEND_REQUEST_LENGTH-1); break; case 'd': min_log_level = L_DEBUG; break; case 'q': min_log_level = L_ERROR; break; case 'S': use_syslog = 1; break; case 'D': daemonize = 1; use_syslog = 1; break; case 'F': pidfile = optarg; break; case 'U': daemon_username = optarg; break; case '?': case 'h': default: print_version(); help(); exit(1); } } if(!client_mode && min_log_level == L_UNSET) { min_log_level = L_INFO; } if(!client_mode && server_whitelist_mode) { log_printf(L_INFO, "Server in ToxID whitelisting mode - only clients listed with -i can connect"); } if(daemonize) { do_daemonize(); } atexit(cleanup); print_version(); /* Bootstrap tox */ tox_options_default(&tox_options); if((!client_mode) || load_saved_toxid_in_client_mode) { uint8_t *save_data = NULL; save_size = load_save(&save_data); if(save_data && save_size) { tox_options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; tox_options.savedata_data = save_data; tox_options.savedata_length = save_size; } } tox = tox_new(&tox_options, &tox_new_err); if(tox == NULL) { log_printf(L_DEBUG, "tox_new() failed (%u) - trying without proxy\n", tox_new_err); if((tox_options.proxy_type != TOX_PROXY_TYPE_NONE) || (tox_options.proxy_type = TOX_PROXY_TYPE_NONE, (tox = tox_new(&tox_options, &tox_new_err)) == NULL)) { log_printf(L_DEBUG, "tox_new() failed (%u) - trying without IPv6\n", tox_new_err); if(!tox_options.ipv6_enabled || (tox_options.ipv6_enabled = 0, (tox = tox_new(&tox_options, &tox_new_err)) == NULL)) { log_printf(L_DEBUG, "tox_new() failed (%u) - trying with Tor\n", tox_new_err); if((tox_options.proxy_type = TOX_PROXY_TYPE_SOCKS5, tox_options.proxy_host="127.0.0.1", tox_options.proxy_port=9050, (tox = tox_new(&tox_options, &tox_new_err)) == NULL)) { log_printf(L_ERROR, "tox_new() failed (%u) - exiting\n", tox_new_err); exit(1); } } } } if(save_size && save_data) { free(save_data); } set_tox_username(tox); tox_callback_self_connection_status(tox, handle_connection_status_change, NULL); do_bootstrap(tox); if(client_mode) { tox_self_get_address(tox, tox_id); id_to_string(tox_printable_id, tox_id); tox_printable_id[TOX_ADDRESS_SIZE * 2] = '\0'; log_printf(L_DEBUG, "Generated Tox ID: %s\n", tox_printable_id); if(!remote_tox_id) { log_printf(L_ERROR, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n"); exit(1); } do_client_loop(remote_tox_id); } else { write_save(tox); if(!use_shared_secret) { log_printf(L_WARNING, "Shared secret authentication is not used - skilled attackers may connect to your tuntox server"); } tox_self_get_address(tox, tox_id); memset(tox_printable_id, '\0', sizeof(tox_printable_id)); id_to_string(tox_printable_id, tox_id); tox_printable_id[TOX_ADDRESS_SIZE * 2] = '\0'; log_printf(L_INFO, "Using Tox ID: %s\n", tox_printable_id); tox_callback_friend_request(tox, accept_friend_request, NULL); do_server_loop(); } return 0; }