static int set_list(void) { int olderrors; char *token = currtok(); errlog(BEGIN, "set_list() {"); errlog(VERBOSE, "token = '%s'", (token) ? token : "<NULL>"); if (set() == FALSE) { errlog(END, "} /* set_list */"); return (FALSE); } olderrors = Errors; while (set()) { continue; } if (olderrors != Errors) { errlog(END, "} /* set_list */"); return (FALSE); } errlog(END, "} /* set_list */"); return (TRUE); }
void perform_defile(struct room_data *room, int *state, char **olddesc, char **oldtitle) { struct obj_data *fount = NULL; if (*state != STATE_HOLY) { errlog("invalid state in perform_defile from unholy_square."); return; } *state = STATE_UNHOLY; for (fount = room->contents; fount; fount = fount->next_content) if (GET_OBJ_VNUM(fount) == FOUNT_HOLY) { extract_obj(fount); break; } if (!(fount = read_object(FOUNT_UNHOLY))) errlog("unable to load unholy fount in unholy_square."); else obj_to_room(fount, room); *olddesc = room->description; *oldtitle = room->name; room->name = strdup(TITLE_UNHOLY); room->description = strdup(DESC_UNHOLY); SET_BIT(room->zone->flags, ZONE_LOCKED); REMOVE_BIT(room->room_flags, ROOM_PEACEFUL); }
/* * generate_linkage -- make code for the linkage part of an individual * interface. Assumes Bodyfp. */ void generate_linkage(ENTRY *function) { char *library_name = db_get_current_library(), *function_name; char composite_name[MAXLINE]; errlog(BEGIN, "generate_linkage() {"); function_name = name_of(function); (void) snprintf(composite_name, sizeof (composite_name), "%s_%s", library_name, function_name); /* Print the predeclaration of the interceptor. */ generate_interface_predeclaration(composite_name, function); /* Collect things we'll use more than once. */ /* Next the struct used to pass parameters. */ (void) fprintf(Bodyfp, "static abisym_t __abi_%s_%s_sym;\n", library_name, function_name); /* The linkage function, */ generate_linkage_function(library_name, function_name); (void) fputs("\n\n", Bodyfp); errlog(END, "}"); }
/* conf was used on the command line * mysql and dbconf modules must be loaded * the command line will be processed with dbconf_cmd_line() */ int cmd_conf(int argc, char** argv) { /* int* (*dbconf_cmd_line)(int argc, char **argv); */ int* (*dbconf_cmd_line)(int, char **); if(module_load("mysql", 1) <0 ) { errlog("Error loading mysql module !"); return -1; } if(module_load("dbconf", 1) < 0) { errlog("Error loading dbconf module !"); return -2; } dbconf_cmd_line = attach_to_function("dbconf_cmd_line", NULL); if(dbconf_cmd_line == NULL) { errlog("Missing dbconf_cmd_line() function !"); return -2; } return (int)dbconf_cmd_line(argc, argv); }
/* Wrapper for clutter_init() clutter_init()のラッパー関数 log result of clutter_init() clutter_init()の結果を出力する。 */ static ClutterInitError my_clutter_init( int argc, char *argv[] ) { ClutterInitError ret = clutter_init( &argc, &argv ); switch( ret ) { case CLUTTER_INIT_SUCCESS: dbglog( "Initialisation successful\n" ); break; case CLUTTER_INIT_ERROR_UNKNOWN: errlog( "Unknown error\n" ); break; case CLUTTER_INIT_ERROR_THREADS: errlog( "Thread initialisation failed\n" ); break; case CLUTTER_INIT_ERROR_BACKEND: errlog( "Backend initialisation failed\n" ); break; case CLUTTER_INIT_ERROR_INTERNAL: errlog( "Internal error\n" ); break; default: errlog( "Other error\n" ); break; } return ret; }
/* Create a pseudo terminal for other process to use (as this program is using up the actual TTY) */ int create_pseudo_tty() { int amaster, aslave; int flags; if (openpty(&amaster, &aslave, NULL, NULL, NULL) == -1) { errlog("Error: Openpty failed - %m\n"); return -1; } /* Set to non blocking mode */ flags = fcntl(amaster, F_GETFL); flags |= O_NONBLOCK; fcntl(amaster, F_SETFL, flags); FILE *pseudo_save_file = fopen(pseudo_tty_save_file, "w+"); if (!pseudo_save_file) { errlog("Error: Unable to open the pseudo info file - %m\n"); return -1; } /* Save the name of the created pseudo tty in a text file for other processes to use */ if (fprintf(pseudo_save_file, "%s\n", ttyname(aslave)) == -1) { errlog("Error writing to the pseudo info file\n"); fclose(pseudo_save_file); return -1; } fclose(pseudo_save_file); if (set_tty(aslave) == -1) { errlog("Error: Slave TTY not set properly\n"); return -1; } return amaster; }
/** internal functions implementation starts here **/ int ev_irc_connect(void* dummy1, void* dummy2) { int cr; stdlog(L_INFO, "Connecting to IRC server %s:%i", RemoteServer, RemotePort); cr = irc_FullConnect(RemoteServer, RemotePort, ServerPass, 0); if(cr < 0) { errlog("Could not connect to IRC server: %s", irc_GetLastMsg()); exit(1); } stdlog(L_INFO, "Netjoin complete, %.1d Kbs received", irc_InByteCount()/1024); /* not sure if this fork should be on the irc module for now lets leave it like this to make sure we only fork after the connection is fully established */ if( nofork == 0 ) { fork_process(); write_pidfile(); } irc_LoopWhileConnected(); errlog("Disconnected:%s\n", irc_GetLastMsg()); /* stdlog(L_INFO, "PTlink IRC Services Terminated"); */ return 0; }
/* * Receives message string from the network and * returns the appropriate numeric token. If error * occurs -1 is returned. */ int recvmstr(int sock,char **s) { char buf[MAX_MSGSTR + 1]; unsigned long ultmp; int i; size_t len; DLOG_MSG(("recvmstr() : recvul(%d)",sock)); if (!recvul(sock,&ultmp)) return -1; len = (size_t)ultmp; if (len == 0 || len > MAX_MSGSTR) { errlog(LOG_ERR,"recvmstr() : len=%lu",(unsigned long)len); return -1; } DLOG_MSG(("recvmstr() : x_recv(%d)",sock)); if (!x_recv(sock,buf,len)) { DLOG_ERROR(("recvstr()")); return -1; } buf[len] = '\0'; DLOG_MSG(("recvstr() : buf=%s",buf)); for (i = 0; s[i]; i++) if (streql(s[i],buf)) return i; errlog(LOG_ERR,"recvmstr() : unknown message (%s)",buf); return -1; }
vm read_vm(int task_id) { unsigned i, top, func_id, max; int vector; char c; vm the_vm; if (dbio_scanf("%u %d %u%c", &top, &vector, &func_id, &c) != 4 || (c == ' ' ? dbio_scanf("%u%c", &max, &c) != 2 || c != '\n' : (max = DEFAULT_MAX_STACK_DEPTH, c != '\n'))) { errlog("READ_VM: Bad vm header\n"); return 0; } the_vm = new_vm(task_id, top + 1); the_vm->max_stack_size = max; the_vm->top_activ_stack = top; the_vm->root_activ_vector = vector; the_vm->func_id = func_id; for (i = 0; i <= top; i++) if (!read_activ(&the_vm->activ_stack[i], i == 0 ? vector : MAIN_VECTOR)) { errlog("READ_VM: Bad activ number %d\n", i); return 0; } return the_vm; }
/* Get the address info of netcons server */ struct addrinfo *get_addr_info(int ip_version) { int ip_family = (ip_version == IPV4) ? AF_INET : AF_INET6; struct addrinfo hints; struct addrinfo *result; result = malloc(sizeof(*result)); if (!result) { errlog("Error: Unable to allocate memory - %m\n"); return NULL; } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = ip_family; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ hints.ai_protocol = 0; /* Any protocol */ hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; if (getaddrinfo(hostname, NULL, &hints, &result)) { errlog("Error: getaddrinfo failed - %m\n"); return NULL; } return result; }
/* process a #pragma directive */ static void tokpragma(tokcxdef *ctx, char *p, int len) { /* ignore empty pragmas */ if (len == 0) { errlog(ctx->tokcxerr, ERR_PRAGMA); return; } /* see what we have */ if (len > 1 && (*p == 'c' || *p == 'C') && (*(p+1) == '+' || *(p+1) == '-') || t_isspace(*(p+1))) { /* skip spaces after the 'C', if any */ for (++p, --len ; len && t_isspace(*p) ; ++p, --len) ; /* look for the + or - flag */ if (len && *p == '+') ctx->tokcxflg |= TOKCXFCMODE; else if (len && *p == '-') ctx->tokcxflg &= ~TOKCXFCMODE; else { errlog(ctx->tokcxerr, ERR_PRAGMA); return; } } else { errlog(ctx->tokcxerr, ERR_PRAGMA); } }
int get_perms(struct identity_downcall_data *data) { FILE *fp; char line[PATH_MAX]; fp = fopen(PERM_PATHNAME, "r"); if (fp == NULL) { if (errno == ENOENT) { return 0; } else { errlog("open %s failed: %s\n", PERM_PATHNAME, strerror(errno)); data->idd_err = errno; return -1; } } while (fgets(line, sizeof(line), fp)) { if (comment_line(line)) continue; if (parse_perm_line(data, line, sizeof(line))) { errlog("parse line %s failed!\n", line); data->idd_err = EINVAL; fclose(fp); return -1; } } fclose(fp); return 0; }
void init_pcap(const char *dev, const char *bpf) { char errbuf[PCAP_ERRBUF_SIZE]; struct bpf_program bp; memset(errbuf, 0, PCAP_ERRBUF_SIZE); /* 获取pcap设备句柄 */ pcap_handle = pcap_open_live(dev, 5000, 1, 512, errbuf); if( pcap_handle == NULL) { errlog("Couldn't open device %s:%s", dev, errbuf); exit(-1); } /* 设置过滤规则 */ if(pcap_compile(pcap_handle, &bp, bpf, 1, 0) == -1) { errlog("Couldn't parse filter %s:%s", bpf, pcap_geterr(pcap_handle)); exit(-1); } if(pcap_setfilter(pcap_handle, &bp) == -1) { errlog("Couldn't install filter %s:%s", bpf, pcap_geterr(pcap_handle)); exit(-1); } }
int exec_cmd() { pid_t pid; int pstat; switch((pid = vfork())) { case -1: errlog(__FILE__, __LINE__, errno); return -11; break; case 0: if(execve(args[0], args, NULL) == -1) { errlog(__FILE__, __LINE__, errno); exit(-11); } break; default: if (wait(&pstat) == -1) { errlog(__FILE__, __LINE__, errno); return -11; } if (WIFEXITED(pstat)) return WEXITSTATUS(pstat); break; } return -11; /* Normally, program never come here */ }
/* * parse_versions -- parse the file whose name is passed, return * the number of (fatal) errors encountered. Currently only * knows about reading set files and writing vers files. */ int parse_versions(const char *fileName) { /* Prime the set-file parser dfa: */ assert(fileName != NULL, "passed null filename to parse_versions"); errlog(BEGIN, "parse_versions(%s) {", fileName); if ((Fp = fopen(fileName, "r")) == NULL) { (void) fprintf(stderr, "Cannot open version file \"%s\"\n", fileName); errlog(END, "} /* parse_versions */"); return (1); } Filename = fileName; Line = 0; errlog(VERBOSE, "reading set file %s looking for architecture %s", Filename, TargetArchStr); /* Run the dfa. */ while (arch()) continue; (void) fclose(Fp); /* print_all_buckets(); */ errlog(END, "} /* parse_versions */"); return (Errors); }
static int parse_args(int argc, char* argv[]) { int opt, ret = 0; while ((opt = getopt(argc, argv, "ni:h:d:s:g:")) != -1) { switch (opt) { case 'i': _ifname = optarg; break; case 'n': _is_pdump_file = 0; /* output to stderr */ break; case 'h': _remote_host = optarg; break; case 'd': _eoj_code.cgc = parse_uchar(optarg); _eoj_code.clc = parse_uchar(optarg + 2); _eoj_code.inc = parse_uchar(optarg + 4); break; case 'g': _get_prop[_get_prop_len].epc = (unsigned char)strtoul(optarg, NULL, 16); _get_prop_len++; break; case 's': _set_prop[_set_prop_len].epc = (unsigned char)strtoul(optarg, NULL, 16); _set_prop[_set_prop_len].data = argv[optind++]; _set_prop_len++; break; default: /* '?' */ usage(argv[0]); break; } } if (!_remote_host || (0 == _get_prop_len && 0 == _set_prop_len)) { usage(argv[0]); } _esv_code = _select_esv(); #if 0 { int i; errlog("%s,%s,%x,%x,%x\n", _ifname, _remote_host, _eoj_code.cgc, _eoj_code.clc, _eoj_code.inc); for (i = 0; i < _set_prop_len; i++) { errlog("SET:%d: %x=%s\n", i, _set_prop[i].epc, _set_prop[i].data); } for (i = 0; i < _get_prop_len; i++) { errlog("GET:%d: %x\n", i, _get_prop[i].epc); } exit(0); } #endif return ret; }
static int x_recv(int sock,char *buf,size_t len) { size_t n; int nn, ret; /* now, this sure is a stupid kludge, but we just can't afford having the recv() block for ages when something goes wrong... */ if (!alarmset) { struct sigaction sact; sact.sa_handler = xrecv_sigalarm; sigemptyset(&sact.sa_mask); sact.sa_flags = 0; if (sigaction(SIGALRM,&sact,(struct sigaction *)0) < 0) errlog(LOG_ERR,"x_recv() : cannot set SIGALRM"); alarmset = 1; } if (setjmp(xrecv_jmp)) { errlog(LOG_ERR,"x_recv() : timeout (%d seconds)",RECV_ALARMTIMEOUT); ret = 0; goto end; } alarm(RECV_ALARMTIMEOUT); #ifdef lint nn = 0; #endif ret = 1; for (n = 0; n < len; n += (size_t)nn) { #if 0 int m = 0; #endif DLOG_MSG(("x_recv() : len=%u, n=%u",len,n)); errno = 0; while ((nn = recv(sock,&buf[n],len - n,0)) <= 0) { if (nn == 0) goto end; #if 0 /* tolerate EOF for a while */ if (nn == 0 && m++ < MAX_SYSATTEMPTS) { errno = 0; continue; } #endif errlog(LOG_INFO,"x_recv() : recv returns %d",nn); if (errno == EINTR) continue; errno = 0; ret = 0; goto end; } DLOG_MSG(("x_recv() : nn=%d",nn)); } end:; alarm(0); return ret; }
/* * add_valid_version and _arch -- add a name to the table. */ static void add_valid_version(char *vers_name) { errlog(BEGIN, "add_valid_version(\"%s\") {", vers_name); if (Vers == NULL) { init_tables(); } Vers = add_to_stringtable(Vers, vers_name); errlog(END, "}"); }
static void add_valid_arch(char *arch_name) { errlog(BEGIN, "add_valid_arch(\"%s\") {", arch_name); if (Vers == NULL) { init_tables(); } Varch = add_to_stringtable(Varch, arch_name); errlog(END, "}"); }
static int create_dyntext_backup(dynamic_text_file * dyntext) { DIR *dir; struct dirent *dirp; int len = strlen(dyntext->filename); int num = 0, maxnum = 0; char filename[1024]; FILE *fl = NULL; // open backup dir if (!(dir = opendir(DYN_TEXT_BACKUP_DIR))) { errlog("Cannot open dynamic text backup dir."); return 1; } // scan files in backup dir while ((dirp = readdir(dir))) { // looks like the right filename if (!strncasecmp(dirp->d_name, dyntext->filename, len) && *(dirp->d_name + len) && *(dirp->d_name + len) == '.' && *(dirp->d_name + len + 1)) { num = atoi(dirp->d_name + len + 1); if (num > maxnum) maxnum = num; } } closedir(dir); snprintf(filename, sizeof(filename), "%s/%s.%02d", DYN_TEXT_BACKUP_DIR, dyntext->filename, maxnum + 1); if (!(fl = fopen(filename, "w"))) { errlog("Dyntext backup unable to open '%s'.", filename); return 1; } if (dyntext->buffer) { char *ptr; for (ptr = dyntext->buffer; *ptr; ptr++) { if (*ptr == '\r') continue; fputc(*ptr, fl); } } fclose(fl); return 0; }
int get_groups_local(struct identity_downcall_data *data, unsigned int maxgroups) { gid_t *groups, *groups_tmp = NULL; unsigned int ngroups = 0; int ngroups_tmp; struct passwd *pw; int i; pw = getpwuid(data->idd_uid); if (!pw) { errlog("no such user %u\n", data->idd_uid); data->idd_err = errno ? errno : EIDRM; return -1; } data->idd_gid = pw->pw_gid; groups = data->idd_groups; /* * Allocate array of size maxgroups instead of handling two * consecutive and potentially racy getgrouplist() calls. */ groups_tmp = malloc(maxgroups * sizeof(gid_t)); if (!groups_tmp) { data->idd_err = errno ? errno : ENOMEM; errlog("malloc error=%u\n", data->idd_err); return -1; } ngroups_tmp = maxgroups; if (getgrouplist(pw->pw_name, pw->pw_gid, groups_tmp, &ngroups_tmp) < 0) { free(groups_tmp); data->idd_err = errno ? errno : EIDRM; errlog("getgrouplist() error for uid %u: error=%u\n", data->idd_uid, data->idd_err); return -1; } /* Do not place user's group ID in to the resulting groups list */ for (i = 0; i < ngroups_tmp; i++) if (pw->pw_gid != groups_tmp[i]) groups[ngroups++] = groups_tmp[i]; if (ngroups > 0) qsort(groups, ngroups, sizeof(*groups), compare_u32); data->idd_ngroups = ngroups; free(groups_tmp); return 0; }
void check_object_killer(struct obj_data *obj, struct creature *vict) { struct creature *killer = NULL; bool loaded_killer = false; int obj_id; if (ROOM_FLAGGED(vict->in_room, ROOM_PEACEFUL)) { return; } if (IS_NPC(vict)) return; slog("Checking object killer %s -> %s. ", obj->name, GET_NAME(vict)); if (IS_BOMB(obj)) obj_id = BOMB_IDNUM(obj); else if (GET_OBJ_SIGIL_IDNUM(obj)) obj_id = GET_OBJ_SIGIL_IDNUM(obj); else { errlog("unknown damager in check_object_killer."); return; } if (!obj_id) return; killer = get_char_in_world_by_idnum(obj_id); // load the bastich from file. if (!killer) { killer = load_player_from_xml(obj_id); if (killer) { killer->account = account_by_idnum(player_account_by_idnum(obj_id)); loaded_killer = true; } } // the piece o shit has a bogus killer idnum on it! if (!killer) { errlog("bogus idnum %d on object %s damaging %s.", obj_id, obj->name, GET_NAME(vict)); return; } count_pkill(killer, vict); // save the sonuvabitch to file crashsave(killer); if (loaded_killer) free_creature(killer); }
/* * Read the SMTP authentication config file * * file format is: * user|host:password * * A line starting with # is treated as comment and ignored. */ void parse_authfile(const char *path) { char line[2048]; struct authuser *au; FILE *a; char *data; int lineno = 0; a = fopen(path, "r"); if (a == NULL) { errlog(1, "can not open auth file `%s'", path); /* NOTREACHED */ } while (!feof(a)) { if (fgets(line, sizeof(line), a) == NULL) break; lineno++; chomp(line); /* We hit a comment */ if (*line == '#') continue; /* Ignore empty lines */ if (*line == 0) continue; au = calloc(1, sizeof(*au)); if (au == NULL) errlog(1, "calloc failed"); data = strdup(line); au->login = strsep(&data, "|"); au->host = strsep(&data, DP); au->password = data; if (au->login == NULL || au->host == NULL || au->password == NULL) { errlogx(1, "syntax error in authfile %s:%d", path, lineno); /* NOTREACHED */ } SLIST_INSERT_HEAD(&authusers, au, next); } fclose(a); }
static void mysqlchild_readable(int fd, void *data) { struct sql_request req; mysql_task **tptr = data; mysql_task *t = *tptr; /* mysql_pipe *p = t->pipe; */ static char *buffer = 0; static int buflen = 0; static char *serial; Var r; if (from_mysql_fd != fd) panic("MYSQLCHILD_READABLE: fd doesn't match from_mysql_fd!"); if (robust_read(fd, &req, sizeof(req)) != sizeof(req)) { errlog("MYSQLCHILD_READABLE: req read failed\n"); req.kind = SQLREQ_ERROR; } else { if (req.length != 0) { ensure_buffer(&buffer, &buflen, req.length + 1); if (robust_read(fd, buffer, req.length) != sizeof(req.length)) { errlog("MYSQLCHILD_READABLE: str read failed\n"); req.kind = SQLREQ_ERROR; req.length = 0; } buffer[req.length] = '\0'; } else { req.kind = SQLREQ_ERROR; } } switch (req.kind) { case SQLREQ_ERROR: r.type = TYPE_ERR; r.v.err = E_INVARG; break; case SQLREQ_CLOSE_HANDLE: case SQLREQ_NEW_HANDLE: case SQLREQ_DO_QUERY: case SQLREQ_GET_ROW: r = deserialize(buffer); } enqueue_from_enumerator(t->the_vm, r); myfree(t, M_TASK); }
/* * create_lists -- initialize the bucket list and hash map. */ void create_lists(void) { errlog(BEGIN, "create_lists() {"); new_hashmap(); if ((Buckethead = calloc(sizeof (bucket_t *), NLISTS)) == NULL) { errlog(FATAL, "out of memory creating initial " "list of versions"); } N_lists = NLISTS; errlog(END, "}"); }
/* dbconf_get * For each item of the dbitems array * Get the value from the dbconf table * If the item is missing, report error and return -1 * If the value is null and the item is mandatory, report error * and return -2 * return 0 on success */ int dbconf_get(dbConfGet *dbitems) { dbConfGet* item = dbitems; char *stype; while(item && item->name) { if(sql_singlequery("SELECT value, stype, optional FROM dbconf WHERE module=%s AND name=%s" " ORDER BY module, name", sql_str(item->module), sql_str(item->name)) > 0) { stype = sql_field(1); /* read the value */ if(!strcmp(stype, "str") || !strcmp(stype, "word")) { FREE(*(char **)item->vptr); *(char **)item->vptr = sql_field(0) ? strdup(sql_field(0)) : NULL; } else if(!strcmp(stype,"int") && sql_field_i(0)) *(int*) item->vptr = sql_field_i(0); else if(!strcmp(stype,"time") && sql_field(0)) { if(ftime_str(sql_field(0)) == -1) { errlog("Invalid time value on %s.%s", item->module, item->name); return -1; } *(int*) item->vptr = ftime_str(sql_field(0)); } if(!strcmp(stype,"switch") && sql_field(0)) *(int*) item->vptr = (!strcasecmp(sql_field(0),"on")); } else { errlog("Unable to find configuratiom item %s.%s", item->module, item->name); return -1; } if((*(sql_field(2)) == 'n') && (item->vptr == NULL)) { errlog("Mandatory item %s.%s is not set!", item->module, item->name); return -2; } ++item; } return 0; }
static void usage(char *name) { errlog("Usage: %s [-i interface-name] -h remote-host -d destination-eoj \n", name); errlog(" [-s epc value] ... [-g epc] ...\n"); errlog(" interface-name: specified interface-name, default eth0.\n"); errlog(" remote-host: ip-address:port\n"); errlog(" destination-eoj: cgc clc inc, Ex.) 0EF001 \n"); errlog("Result: \n"); errlog(" {S|G},epc,pdc[,edt] ....\n"); errlog(" Ex.)\n"); errlog(" S,80,0,\n"); errlog(" G,80,1,31\n"); exit(-1); }
/* * poll for accept(), return file descriptor for accepted connection, * or -1 for error. */ int conn_poll_for_accept(int sock, struct pollfd *pfd) { int rd; /* new descriptor on accept */ int timeo_polla; /* for accept() */ struct sockaddr cliaddr; socklen_t len; /* listen socket info */ rd = 0; timeo_polla = net_timeout; /* timeout value for accept() */ len = sizeof(struct sockaddr); /* * first we're going to poll for accept() */ pfd[0].fd = sock; pfd[0].events = POLLIN; for (;;) { switch(poll(pfd, 1, timeo_polla)) { case 0: errlog(LOG_INFO, "timeout polling to accept read connection"); return -1; case -1: errlog(LOG_ERR, "error polling to accept read connection: %s", strerror(errno)); return -1; default: break; } if (pfd[0].revents & POLLIN) { rd = accept(sock, &cliaddr, &len); if (rd == -1) { errlog(LOG_ERR, "error accepting read connection: %s", strerror(errno)); return -1; } break; } break; } return rd; }
int send_data(int fd, void *buffer,int length) { int bytes_left = 0; int written_bytes = 0; char *ptr=NULL; int retry_times = 0; int buf_size = 8192; ptr=buffer; bytes_left = length; if (length == 0) return 0; while (bytes_left > 0) { if(bytes_left < 8192){ buf_size = bytes_left; } written_bytes = write(fd, ptr, buf_size); if (written_bytes == 0) return length - bytes_left; if(written_bytes < 0) { if((errno == EINTR) || (errno == EAGAIN)) { usleep(500000); retry_times++; //timeout 5s if(retry_times > 100){ errlog("retry 100 ,write timout !! %d ",length); return -1; } } else { errlog("write to %d error %d,%s\n",fd,errno,strerror(errno)); return -1; } } else{ bytes_left -= written_bytes; ptr += written_bytes; } } // do_log(2,"send to %d success len = %d!",fd,length); return length; }
int chain_rand(REMAILER *remailer, int badchains[MAXREM][MAXREM], int maxrem, int thischain[], int chainlen, int t, int ignore_constraints_if_necessary) /* set random chain. returns 0 if not random, 1 if random, -1 on error */ /* t... 0 for mixmaster Type II * 1 for cypherpunk Type I */ { int hop; int err = 0; int constraints_ignored = 0; assert(t == 0 || t == 1); start: for (hop = 0; hop < chainlen; hop++) if (thischain[hop] == 0) { int select[MAXREM]; int randavail = 0; int i; err = 1; if (hop > 0) assert(thischain[hop-1]); /* we already should have chosen a remailer after this one */ for (i = 1; i < maxrem; i++) { select[i] = ((remailer[i].flags.mix && t == 0) || /* remailer supports type */ (remailer[i].flags.pgp && remailer[i].flags.ek && t == 1)) && (remailer[i].info[t].reliability >= 100 * MINREL || constraints_ignored ) && /* remailer has sufficient reliability */ (remailer[i].info[t].latency <= MAXLAT || constraints_ignored ) && /* remailer has low enough latency */ (remailer[i].info[t].latency >= MINLAT || constraints_ignored ) && /* remailer has high enough latency */ !remailer[i].flags.star_ex && /* remailer is not excluded from random selection */ !badchains[i][0] && !badchains[i][thischain[hop-1]] && /* remailer can send to the next one */ (hop == chainlen-1 || !badchains[thischain[hop+1]][i]); /* we are at the first hop or the previous one can send to this (may be random) */ randavail += select[i]; } for (i = hop - DISTANCE; i <= hop + DISTANCE; i++) if (i >= 0 && i < chainlen && select[thischain[i]] && thischain[i]) { select[thischain[i]] = 0; randavail--; } assert(randavail >= 0); if (randavail < 1) { if (ignore_constraints_if_necessary && !constraints_ignored) { errlog(WARNING, "No reliable remailers. Ignoring for randhop\n"); constraints_ignored = 1; goto start; }; err = -1; goto end; } do thischain[hop] = rnd_number(maxrem - 1) + 1; while (!select[thischain[hop]]); } end: return (err); }