static void inclinenumber (LexState *LS) { static const char *pragmas [] = { "debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL }; next(LS); // skip '\n' ++LS->linenumber; if (LS->current == '$') { // is a pragma? char buff[PRAGMASIZE + 1]; int32 ifnot = 0; int32 skip = LS->ifstate[LS->iflevel].skip; next(LS); // skip $ readname(LS, buff); switch (luaO_findstring(buff, pragmas)) { case 0: // debug if (!skip) lua_debug = 1; break; case 1: // nodebug if (!skip) lua_debug = 0; break; case 2: // endinput if (!skip) { LS->current = EOZ; LS->iflevel = 0; // to allow $endinput inside a $if } break; case 3: // end if (LS->iflevel-- == 0) luaY_syntaxerror("unmatched $end", "$end"); break; case 4: // ifnot ifnot = 1; // go through case 5: // if if (LS->iflevel == MAX_IFS - 1) luaY_syntaxerror("too many nested $ifs", "$if"); readname(LS, buff); LS->iflevel++; LS->ifstate[LS->iflevel].elsepart = 0; LS->ifstate[LS->iflevel].condition = checkcond(buff) ? !ifnot : ifnot; LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition; break; case 6: // else if (LS->ifstate[LS->iflevel].elsepart) luaY_syntaxerror("unmatched $else", "$else"); LS->ifstate[LS->iflevel].elsepart = 1; LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel - 1].skip || LS->ifstate[LS->iflevel].condition; break; default: luaY_syntaxerror("unknown pragma", buff); } skipspace(LS); if (LS->current == '\n') // pragma must end with a '\n' ... inclinenumber(LS); else if (LS->current != EOZ) // or eof luaY_syntaxerror("invalid pragma format", buff); ifskip(LS); } }
static void inclinenumber (LexState *LS) { static char *pragmas [] = {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; next(LS); /* skip '\n' */ ++LS->linenumber; if (LS->current == '$') { /* is a pragma? */ char buff[PRAGMASIZE+1]; int ifnot = 0; int skip = LS->ifstate[LS->iflevel].skip; next(LS); /* skip $ */ readname(LS, buff); switch (luaL_findstring(buff, pragmas)) { case 0: /* debug */ if (!skip) L->debug = 1; break; case 1: /* nodebug */ if (!skip) L->debug = 0; break; case 2: /* endinput */ if (!skip) { LS->current = EOZ; LS->iflevel = 0; /* to allow $endinput inside a $if */ } break; case 3: /* end */ if (LS->iflevel-- == 0) luaX_syntaxerror(LS, "unmatched $end", "$end"); break; case 4: /* ifnot */ ifnot = 1; /* go through */ case 5: /* if */ if (LS->iflevel == MAX_IFS-1) luaX_syntaxerror(LS, "too many nested $ifs", "$if"); readname(LS, buff); LS->iflevel++; LS->ifstate[LS->iflevel].elsepart = 0; LS->ifstate[LS->iflevel].condition = checkcond(LS, buff) ? !ifnot : ifnot; LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition; break; case 6: /* else */ if (LS->ifstate[LS->iflevel].elsepart) luaX_syntaxerror(LS, "unmatched $else", "$else"); LS->ifstate[LS->iflevel].elsepart = 1; LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip || LS->ifstate[LS->iflevel].condition; break; default: luaX_syntaxerror(LS, "unknown pragma", buff); } skipspace(LS); if (LS->current == '\n') /* pragma must end with a '\n' ... */ inclinenumber(LS); else if (LS->current != EOZ) /* or eof */ luaX_syntaxerror(LS, "invalid pragma format", buff); ifskip(LS); } }
static unsigned int protocol_child(struct service_backend *backend, struct service_state *state, char *path, unsigned int length) { struct cpio_header header; struct cpio_header eheader; unsigned char name[1024]; unsigned int id = 0; if (!length) return 1; if (!readheader(backend, &header, state->id)) return 0; if (!readname(backend, &header, state->id, name, 1024)) return 0; if (path[length - 1] == '/') length--; do { unsigned char cname[1024]; if (id == state->id) break; if (!readheader(backend, &eheader, id)) break; if (eheader.namesize - header.namesize != length + 1) continue; if (!readname(backend, &eheader, id, cname, 1024)) break; if (memory_match(cname + header.namesize, path, length)) { state->id = id; return 1; } } while ((id = cpio_next(&eheader, id))); return 0; }
char * getusershell (void) { if (default_index > 0) { if (default_shells[default_index]) /* Not at the end of the list yet. */ return xstrdup (default_shells[default_index++]); return NULL; } if (shellstream == NULL) { shellstream = fopen (SHELLS_FILE, "r"); if (shellstream == NULL) { /* No shells file. Use the default list. */ default_index = 1; return xstrdup (default_shells[0]); } } while (readname (&line, &line_size, shellstream)) { if (*line != '#') return line; } return NULL; /* End of file. */ }
END_TEST START_TEST(test_read_name_badjump_start) { unsigned char badjump[] = { 'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xcc, 0x00, 0x01, 0x00, 0x01 }; unsigned char *jumper; char buf[1024]; char *data; int rv; /* This test uses malloc to cause segfault if jump is executed */ memset(buf, 0, sizeof(buf)); jumper = malloc(sizeof(badjump)); if (jumper) { memcpy(jumper, badjump, sizeof(badjump)); data = (char*) jumper + sizeof(HEADER); rv = readname((char*) jumper, sizeof(badjump), &data, buf, 256); fail_unless(rv == 0); fail_unless(buf[0] == 0); } free(jumper); }
static unsigned int readdirectory(struct service_backend *backend, void *buffer, unsigned int count, unsigned int current, struct cpio_header *header) { struct record *record = buffer; struct cpio_header eheader; unsigned char name[1024]; if (!current) return 0; if (!readheader(backend, &eheader, current)) return 0; if (!readname(backend, &eheader, current, name, 1024)) return 0; record->id = current; record->size = cpio_filesize(&eheader); record->length = memory_read(record->name, RECORD_NAMESIZE, name, eheader.namesize - 1, header->namesize); switch (eheader.mode & 0xF000) { case 0x4000: record->length += memory_write(record->name, RECORD_NAMESIZE, "/", 1, record->length); break; } return sizeof (struct record); }
END_TEST START_TEST(test_read_name_badjump_second) { unsigned char badjump2[] = { 'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 'B', 'A', 0xfe, 0xcc, 0x00, 0x01, 0x00, 0x01 }; unsigned char *jumper; char buf[1024]; char *data; int rv; /* This test uses malloc to cause segfault if jump is executed */ memset(buf, 0, sizeof(buf)); jumper = malloc(sizeof(badjump2)); if (jumper) { memcpy(jumper, badjump2, sizeof(badjump2)); data = (char*) jumper + sizeof(HEADER); rv = readname((char*) jumper, sizeof(badjump2), &data, buf, 256); fail_unless(rv == 4); fail_unless(strcmp("BA.", buf) == 0, "buf is not BA: %s", buf); } free(jumper); }
static unsigned int parent(struct service_backend *backend, struct cpio_header *header, unsigned int id) { struct cpio_header eheader; unsigned char name[1024]; unsigned int length; if (!readname(backend, header, id, name, 1024)) return 0; for (length = header->namesize - 1; length && name[length] != '/'; length--); do { if (!readheader(backend, &eheader, id)) break; if ((eheader.mode & 0xF000) != 0x4000) continue; if (eheader.namesize == length + 1) return id; } while ((id = cpio_next(&eheader, id))); return 0; }
// Function main - execution starts here int main(void) { Node *pRoot = NULL; char answer = 'n'; do { if(!pRoot) pRoot = createnode(readname()); else addnode(readname(), pRoot); printf_s("\nDo you want to enter another (y or n)? "); scanf_s(" %c", &answer, sizeof(answer)); fflush(stdin); // Get rid of the newline } while(tolower(answer) == 'y'); printf_s("The names in ascending sequence are:\n"); listnodes(pRoot); // Output the contents of the tree freenodes(pRoot); // Release the heap memory return 0; }
void *chld(void *threadarg){ struct thread_data *data; struct sockaddr_in6 clnt_addr; data = (struct thread_data *) threadarg; char *buff = data->buf; clnt_addr = data->addr; int of=0; int loc; of = of + 13; loc = readname(buff, of); buff[loc+1] = 1; int x=send_to_DNS(buff, clnt_addr); fflush(stdout); pthread_exit(NULL); }
END_TEST START_TEST(test_read_name_onejump) { unsigned char onejump[] = "AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00" "\x02hh\xc0\x15\x00\x01\x00\x01\x05zBCDE\x00"; char buf[1024]; char *data; int rv; memset(buf, 0, sizeof(buf)); data = (char*) onejump + sizeof(HEADER); rv = readname((char*) onejump, sizeof(onejump), &data, buf, 256); fail_unless(rv == 9); }
int check(char* buffer) { int of = 0; int loc; int rt=0; of = of + 13; loc = readname(buffer, of); if(buffer[loc+1] == 28) { rt=1; } else { rt=0; } return rt; }
END_TEST START_TEST(test_read_name_inf_loop) { unsigned char infloop[] = { 'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 'A', 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 }; char buf[1024]; char *data; int rv; memset(buf, 0, sizeof(buf)); data = (char*) infloop + sizeof(HEADER); buf[4] = '\a'; rv = readname((char*) infloop, sizeof(infloop), &data, buf, 4); fail_unless(rv == 3); fail_unless(buf[4] == '\a'); }
END_TEST START_TEST(test_read_name_empty_loop) { unsigned char emptyloop[] = { 'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 }; char buf[1024]; char *data; int rv; memset(buf, 0, sizeof(buf)); data = (char*) emptyloop + sizeof(HEADER); buf[1023] = 'A'; rv = readname((char *) emptyloop, sizeof(emptyloop), &data, buf, 1023); fail_unless(rv == 0); fail_unless(buf[1023] == 'A'); }
static unsigned int protocol_parent(struct service_backend *backend, struct service_state *state) { struct cpio_header header; struct cpio_header eheader; unsigned char name[1024]; unsigned int id = state->id; if (!readheader(backend, &header, state->id)) return 0; if (!readname(backend, &header, state->id, name, 1024)) return 0; while (name[header.namesize] != '/') header.namesize--; do { if (!readheader(backend, &eheader, id)) break; if ((eheader.mode & 0xF000) != 0x4000) continue; if (eheader.namesize == header.namesize + 1) { state->id = id; return 1; } } while ((id = cpio_next(&eheader, id))); return 0; }
/* Function main - execution starts here */ int main(void) { Name *pNewName = NULL; Node *pRoot = NULL; char answer = 'n'; do { pNewName = readname(); if(!pRoot) pRoot = createnode(pNewName); else addnode(pNewName, pRoot); printf("\nDo you want to enter another (y or n)? "); scanf(" %c", &answer); fflush(stdin); /* Get rid of the newline */ } while(tolower(answer) == 'y'); printf("The names in ascending sequence are:\n"); listnodes(pRoot); /* Output the contents of the tree */ freenodes(pRoot); /* Release the heap memory */ return 0; }
END_TEST START_TEST(test_read_name_longname) { unsigned char longname[] = "AA\x81\x80\x00\x01\x00\x00\x00\x00\x00\x00" "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" "\x3FzBCDEFGHIJKLMNOPQURSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzAA" "\x00\x00\x01\x00\x01"; char buf[1024]; char *data; int rv; memset(buf, 0, sizeof(buf)); data = (char*) longname + sizeof(HEADER); buf[256] = '\a'; rv = readname((char*) longname, sizeof(longname), &data, buf, 256); fail_unless(rv == 256); fail_unless(buf[256] == '\a'); }
int yylex(void) { int c, r; char c1; Sym *s; genblock: if(block != nblock){ if(block > nblock){ if(debug) fprint(2, "%s %d -> %d\n", ty(Tend), block, nblock); block--; return Tend; }else{ if(debug) fprint(2, "%s %d -> %d\n", ty(Tbegin), block, nblock); block++; return Tbegin; } } c = GETC(); if(c == Beof){ nblock = 0; if(block != nblock) goto genblock; return 0; } UNGETC(c); switch(state){ case Head0: /* [%type] string */ c = GETC(); if(c != '%'){ UNGETC(c); setstate(Body0); goto genblock; } r = readname(" \t"); if(r == Terror) return fail(Head2); s = lookup(s_to_c(sbuf)); yylval.sym = s; if(debug) fprint(2, "Sym[%s] %s\n", ty(s->type), s->name); setstate(Head1); return s->type; case Head1: /* %type [string] */ r = readstr("\n", " \t", 0); if(r == Terror) return fail(Head2); yylval.s = estrdup(s_to_c(sbuf)); if(debug) fprint(2, "%s %q\n", ty(Tstring), yylval.s); setstate(Head2); return Tstring; case Head2: skip(" \t"); c = GETC(); assert(c == '\n' || c == Beof); setstate(Head0); return ';'; case Body0: /* [indent] cmd inline */ c = GETC(); if(c == '%'){ UNGETC(c); setstate(Head0); goto genblock; } UNGETC(c); r = skip(" \t"); c = GETC(); if(c == '\n'){ if(debug) fprint(2, "%s\n", ty(Tbreak)); return Tbreak; } UNGETC(c); nblock = r; setstate(Body1); goto genblock; case Body1: /* indent [cmd] inline */ switch(c = GETC()){ case Beof: yyerror("eof in body"); if(debug) fprint(2, "%s\n", ty(Terror)); return fail(Body3); case '=': case '\\': case '+': case '*': case ':': case '-': case '>': if(debug) fprint(2, "%c\n", c); setstate(Body2); return c; case '!': if(debug) fprint(2, "%c\n", c); setstate(Code); return c; case '{': case '}': if(debug) fprint(2, "%c\n", c); setstate(Body3); return c; case '#': if(debug) fprint(2, "%c\n", c); setstate(ID); return c; case '.': if(debug) fprint(2, "%c\n", c); setstate(Class); return c; case '|': if(debug) fprint(2, "%c\n", c); setstate(Table); return c; default: UNGETC(c); if(debug) fprint(2, "\\\n"); setstate(Body2); return '\\'; } case Body2: /* indent cmd [inline] */ case Table: r = skip(" \t"); if(state == Table && r > 0) return ','; switch(c = getcc(&c1, "*[]|<>")){ case Beof: yyerror("eof in body"); if(debug) fprint(2, "%s\n", ty(Terror)); return fail(Body3); case '\n': UNGETC(c); setstate(Body3); break; case '*': case '[': case ']': case '|': case '<': case '>': if(c == '[' || c == '|' || c == '<') skip(" \t\n"); if(debug) fprint(2, "%c\n", c); return c; default: s_reset(sbuf); s_putc(sbuf, c1); while((c=getcc(&c1, "*[]|<>")) == 0){ if(state == Table && c1 == '\t'){ UNGETC(c); break; } s_putc(sbuf, c1); } s_terminate(sbuf); if(c > 0) UNGETC(c); yylval.s = estrdup(s_to_c(sbuf)); if(debug) fprint(2, "%s %q\n", ty(Tstring), yylval.s); return Tstring; } case Body3: skip(" \t"); c = GETC(); if(c != '\n') return fail(Body3); setstate(Body0); return ';'; case Code: r = readstr("\n", "", 1); if(r == Terror){ if(debug) fprint(2, "%s\n", ty(Terror)); return fail(Body3); } setstate(Body3); yylval.s = estrdup(s_to_c(sbuf)); if(debug) fprint(2, "%s %q\n", ty(Tstring), yylval.s); return Tstring; case ID: case Class: r = readname(" \t"); if(r == Terror){ if(debug) fprint(2, "%s\n", ty(Terror)); return fail(Body3); } setstate(Body3); yylval.s = estrdup(s_to_c(sbuf)); if(debug) fprint(2, "%s %q\n", ty(Tstring), yylval.s); return Tstring; default: assert(0); return Terror; } }
/** * check wether there is a same running process * exit if there is a running process or error * Checking have 3 steps: * 1) lock executable file * 2) check pidfile (if you run a copy?) * 3) check /proc for executables with the same name (no/wrong pidfile) * @param argv - argument of main() or NULL for non-locking, call this function before getopt() * @param pidfilename - name of pidfile or NULL if none * @param iffound - action to run if file found or NULL for exit(0) */ void check4running(char **argv, char *pidfilename, void (*iffound)(pid_t pid)){ DIR *dir; FILE *pidfile, *fself; struct dirent *de; struct stat s_buf; pid_t pid = 0, self; struct flock fl; char *name, *myname; if(!iffound) iffound = iffound_default; if(argv){ // block self fself = fopen(argv[0], "r"); // open self binary to lock memset(&fl, 0, sizeof(struct flock)); fl.l_type = F_WRLCK; if(fcntl(fileno(fself), F_GETLK, &fl) == -1){ // check locking perror("fcntl"); exit(1); } if(fl.l_type != F_UNLCK){ // file is locking - exit printf("Found locker, PID = %d!\n", fl.l_pid); exit(1); } fl.l_type = F_RDLCK; if(fcntl(fileno(fself), F_SETLKW, &fl) == -1){ perror("fcntl"); exit(1); } } self = getpid(); // get self PID if(!(dir = opendir(PROC_BASE))){ // open /proc directory perror(PROC_BASE); exit(1); } if(!(name = readname(self))){ // error reading self name perror("Can't read self name"); exit(1); } myname = strdup(name); if(pidfilename && stat(pidfilename, &s_buf) == 0){ // pidfile exists pidfile = fopen(pidfilename, "r"); if(pidfile){ if(fscanf(pidfile, "%d", &pid) > 0){ // read PID of (possibly) running process if((name = readname(pid)) && strncmp(name, myname, 255) == 0) iffound(pid); } fclose(pidfile); } } // There is no pidfile or it consists a wrong record while((de = readdir(dir))){ // scan /proc if(!(pid = (pid_t)atoi(de->d_name)) || pid == self) // pass non-PID files and self continue; if((name = readname(pid)) && strncmp(name, myname, 255) == 0) iffound(pid); } closedir(dir); if(pidfilename){ pidfile = fopen(pidfilename, "w"); fprintf(pidfile, "%d\n", self); // write self PID to pidfile fclose(pidfile); } free(myname); }
int luaX_lex (LexState *LS, SemInfo *seminfo) { for (;;) { switch (LS->current) { case '\n': { inclinenumber(LS); continue; } case '-': { next(LS); if (LS->current != '-') return '-'; /* else is a comment */ next(LS); if (LS->current == '[' && (next(LS), LS->current == '[')) read_long_string(LS, NULL); /* long comment */ else /* short comment */ while (LS->current != '\n' && LS->current != EOZ) next(LS); continue; } case '[': { next(LS); if (LS->current != '[') return '['; else { read_long_string(LS, seminfo); return TK_STRING; } } case '=': { next(LS); if (LS->current != '=') return '='; else { next(LS); return TK_EQ; } } case '<': { next(LS); if (LS->current == '<') { next(LS); return TK_SHL; } else if (LS->current != '=') return '<'; else { next(LS); return TK_LE; } } case '>': { next(LS); if (LS->current == '>') { next(LS); return TK_SHR; } else if (LS->current != '=') return '>'; else { next(LS); return TK_GE; } } case '~': { next(LS); if (LS->current != '=') return '~'; else { next(LS); return TK_NE; } } case '"': case '\'': { read_string(LS, LS->current, seminfo); return TK_STRING; } case '.': { next(LS); if (LS->current == '.') { next(LS); if (LS->current == '.') { next(LS); return TK_DOTS; /* ... */ } else return TK_CONCAT; /* .. */ } else if (!lex_isdigit(LS->current)) return '.'; else { return read_numeral(LS, 1, seminfo); } } case EOZ: { return TK_EOS; } default: { if (isspace(LS->current)) { next(LS); continue; } else if (lex_isdigit(LS->current)) { return (read_numeral(LS, 0, seminfo)); } else if (lex_isalpha(LS->current) || LS->current == '_') { char saveCh = 0; size_t l; TString *ts; if (LS->current == 'L') { next(LS); if (LS->current == '"') { read_wstring(LS, LS->current, seminfo); return TK_WSTRING; } saveCh = 'L'; } /* identifier or reserved word */ l = readname(LS, saveCh); ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff), l); if (ts->tsv.reserved > 0) /* reserved word? */ return ts->tsv.reserved - 1 + FIRST_RESERVED; seminfo->ts = ts; return TK_NAME; } else { int c = LS->current; if (iscntrl(c)) luaX_error(LS, "invalid control char", luaO_pushfstring(LS->L, "char(%d)", c)); next(LS); return c; /* single-char tokens (+ - / ...) */ } } } } }
int send_to_DNS(char *buff, struct sockaddr_in6 clnt_addr){ unsigned char buf_server[1000]; int sock_server; struct DNS_HEADER *dns1; struct sockaddr_in6 dest; struct in6_addr to_addr; int server_sock,bytes_sent; if ((sock_server = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) { printf("error"); } if(inet_pton(AF_INET6,"2001:df0:92:0:21e:8cff:fec7:437f",&to_addr)<=0){ } dest.sin6_family=AF_INET6; dest.sin6_port=htons(53); //server.sin6_addr = in6addr_loopback; dest.sin6_addr=to_addr; dest.sin6_scope_id=1; int test = sendto(sock_server,(char*)buff,1000 ,0,(struct sockaddr*)&dest, addr_len); recvfrom (sock_server,(char*)buf_server,1000,0,(struct sockaddr*)&dest, &adr_len); dns1=(struct DNS_HEADER*) buf_server; int of = 13; int loc,k; loc = readname(buf_server, of); buf_server[loc + 1] = 28; loc = loc + 4; of = loc; int i; for(i=0;i<ntohs(dns1->ans_count);i++) { loc = readname(buf_server, loc); if (buf_server[loc + 1] == 1) { buf_server[loc + 1] = 28; buf_server[loc + 9] = 16; for(k=500;k>=10;k--) { buf_server[loc+k+12]=buf_server[loc+k]; } buf_server[loc+10]=0x40; buf_server[loc+11]=0x01; buf_server[loc+12]=0x44; buf_server[loc+13]=0x90; buf_server[loc+14]=0x00; buf_server[loc+15]=0x00; buf_server[loc+16]=0x00; buf_server[loc+17]=0x00; buf_server[loc+18]=0x00; buf_server[loc+19]=0x00; buf_server[loc+20]=0x00; buf_server[loc+21]=0x00; } loc = loc + 9 + buf_server[loc + 9] + 1; } int l=0; sendto(sock_client,(char*)buf_server,500 ,0,(struct sockaddr*)&clnt_addr, addr_len); }
int dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, size_t packetlen) { char name[QUERY_NAME_SIZE]; char rdata[4*1024]; HEADER *header; short qdcount; short ancount; uint32_t ttl; unsigned short class; unsigned short type; char *data; unsigned short rlen; uint16_t id; int rv; rv = 0; header = (HEADER*)packet; /* Reject short packets */ if (packetlen < sizeof(HEADER)) return 0; if (header->qr != qr) { warnx("header->qr does not match the requested qr"); return -1; } data = packet + sizeof(HEADER); qdcount = ntohs(header->qdcount); ancount = ntohs(header->ancount); id = ntohs(header->id); rlen = 0; if (q != NULL) q->rcode = header->rcode; switch (qr) { case QR_ANSWER: if(qdcount < 1) { /* We need a question */ return -1; } if (q != NULL) q->id = id; /* Read name even if no answer, to give better error message */ readname(packet, packetlen, &data, name, sizeof(name)); CHECKLEN(4); readshort(packet, &data, &type); readshort(packet, &data, &class); /* if CHECKLEN okay, then we're sure to have a proper name */ if (q != NULL) { /* We only need the first char to check it */ q->name[0] = name[0]; q->name[1] = '\0'; } if (ancount < 1) { /* DNS errors like NXDOMAIN have ancount=0 and stop here. CNAME may also have A; MX/SRV may have multiple results. */ return -1; } /* Here type is still the question type */ if (type == T_NULL || type == T_PRIVATE) { /* Assume that first answer is what we wanted */ readname(packet, packetlen, &data, name, sizeof(name)); CHECKLEN(10); readshort(packet, &data, &type); readshort(packet, &data, &class); readlong(packet, &data, &ttl); readshort(packet, &data, &rlen); rv = MIN(rlen, sizeof(rdata)); rv = readdata(packet, &data, rdata, rv); if (rv >= 2 && buf) { rv = MIN(rv, buflen); memcpy(buf, rdata, rv); } else { rv = 0; } } else if ((type == T_A || type == T_CNAME ||
int luaX_lex (LexState *LS, SemInfo *seminfo) { for (;;) { switch (LS->current) { case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */ next(LS); continue; case '\n': inclinenumber(LS); continue; case '$': luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$'); break; case '-': next(LS); if (LS->current != '-') return '-'; do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); continue; case '[': next(LS); if (LS->current != '[') return '['; else { read_long_string(LS, seminfo); return TK_STRING; } case '=': next(LS); if (LS->current != '=') return '='; else { next(LS); return TK_EQ; } case '<': next(LS); if (LS->current != '=') return '<'; else { next(LS); return TK_LE; } case '>': next(LS); if (LS->current != '=') return '>'; else { next(LS); return TK_GE; } case '~': next(LS); if (LS->current != '=') return '~'; else { next(LS); return TK_NE; } case '"': case '\'': read_string(LS, LS->current, seminfo); return TK_STRING; case '.': next(LS); if (LS->current == '.') { next(LS); if (LS->current == '.') { next(LS); return TK_DOTS; /* ... */ } else return TK_CONCAT; /* .. */ } else if (!isdigit(LS->current)) return '.'; else { read_number(LS, 1, seminfo); return TK_NUMBER; } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': read_number(LS, 0, seminfo); return TK_NUMBER; case EOZ: return TK_EOS; case '_': goto tname; default: if (!isalpha(LS->current)) { int c = LS->current; if (iscntrl(c)) luaX_invalidchar(LS, c); next(LS); return c; } tname: { /* identifier or reserved word */ TString *ts = luaS_new(LS->L, readname(LS)); if (ts->marked >= RESERVEDMARK) /* reserved word? */ return ts->marked-RESERVEDMARK+FIRST_RESERVED; seminfo->ts = ts; return TK_NAME; } } } }