void printresponse(const unsigned char * resp, int n) { int i, l; unsigned int nresp; const unsigned char * p; for(i=0; i<n; i++) printf("%02x ", resp[i]); printf("\n"); nresp = resp[0]; p = resp + 1; for(i = 0; i < (int)nresp; i++) { /*l = *(p++);*/ DECODELENGTH(l, p); printf("%d - %.*s\n", i, l, p); p += l; /*l = *(p++);*/ DECODELENGTH(l, p); printf(" %.*s\n", l, p); p += l; /*l = *(p++);*/ DECODELENGTH(l, p); printf(" %.*s\n", l, p); p += l; } }
void printresponse(const unsigned char * resp, int n) { int i, l; unsigned int nresp; const unsigned char * p; if(n == 0) return; /* first, hexdump the response : */ for(i = 0; i < n; i += 16) { printf("%06x | ", i); for(l = i; l < n && l < (i + 16); l++) printf("%02x ", resp[l]); while(l < (i + 16)) { printf(" "); l++; } printf("| "); for(l = i; l < n && l < (i + 16); l++) putchar((resp[l] >= ' ' && resp[l] < 128) ? resp[l] : '.'); putchar('\n'); } /* now parse and display all devices of response */ nresp = resp[0]; /* 1st byte : number of devices in response */ p = resp + 1; for(i = 0; i < (int)nresp; i++) { if(p >= resp + n) goto error; /*l = *(p++);*/ DECODELENGTH(l, p); if(p + l > resp + n) goto error; printf("%d - %.*s\n", i, l, p); /* URL */ p += l; if(p >= resp + n) goto error; /*l = *(p++);*/ DECODELENGTH(l, p); if(p + l > resp + n) goto error; printf(" %.*s\n", l, p); /* ST */ p += l; if(p >= resp + n) goto error; /*l = *(p++);*/ DECODELENGTH(l, p); if(p + l > resp + n) goto error; printf(" %.*s\n", l, p); /* USN */ p += l; } return; error: printf("*** WARNING : TRUNCATED RESPONSE ***\n"); }
void printversion(const unsigned char * resp, int n) { int l; const unsigned char * p; p = resp; DECODELENGTH(l, p); if(resp + n < p + l) { printf("get version error\n"); } printf("MiniSSDPd version : %.*s\n", l, p); }
int main(int argc, char * * argv) { unsigned char buf[256]; unsigned char * p; long i, j; (void)argc; (void)argv; for(i = 1; i < 1000000000; i *= 2) { /* encode i, decode to j */ printf("%ld ", i); p = buf; CODELENGTH(i, p); p = buf; DECODELENGTH(j, p); if(i != j) { fprintf(stderr, "Error ! encoded %ld, decoded %ld.\n", i, j); return 1; } } printf("Test succesful\n"); return 0; }
struct UPNPDev * getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath) { struct UPNPDev * tmp; struct UPNPDev * devlist = NULL; unsigned char buffer[2048]; ssize_t n; unsigned char * p; unsigned char * url; unsigned int i; unsigned int urlsize, stsize, usnsize, l; int s; struct sockaddr_un addr; s = socket(AF_UNIX, SOCK_STREAM, 0); if(s < 0) { /*syslog(LOG_ERR, "socket(unix): %m");*/ perror("socket(unix)"); return NULL; } addr.sun_family = AF_UNIX; strncpy(addr.sun_path, socketpath, sizeof(addr.sun_path)); /* TODO : check if we need to handle the EINTR */ if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) { /*syslog(LOG_WARNING, "connect(\"%s\"): %m", socketpath);*/ close(s); return NULL; } stsize = strlen(devtype); buffer[0] = 1; /* request type 1 : request devices/services by type */ p = buffer + 1; l = stsize; CODELENGTH(l, p); if(p + stsize > buffer + sizeof(buffer)) { /* devtype is too long ! */ close(s); return NULL; } memcpy(p, devtype, stsize); p += stsize; if(write(s, buffer, p - buffer) < 0) { /*syslog(LOG_ERR, "write(): %m");*/ perror("minissdpc.c: write()"); close(s); return NULL; } n = read(s, buffer, sizeof(buffer)); if(n<=0) { perror("minissdpc.c: read()"); close(s); return NULL; } p = buffer + 1; for(i = 0; i < buffer[0]; i++) { if(p+2>=buffer+sizeof(buffer)) break; DECODELENGTH(urlsize, p); if(p+urlsize+2>=buffer+sizeof(buffer)) break; url = p; p += urlsize; DECODELENGTH(stsize, p); if(p+stsize+2>=buffer+sizeof(buffer)) break; tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize); tmp->pNext = devlist; tmp->descURL = tmp->buffer; tmp->st = tmp->buffer + 1 + urlsize; memcpy(tmp->buffer, url, urlsize); tmp->buffer[urlsize] = '\0'; memcpy(tmp->buffer + urlsize + 1, p, stsize); p += stsize; tmp->buffer[urlsize+1+stsize] = '\0'; devlist = tmp; /* added for compatibility with recent versions of MiniSSDPd * >= 2007/12/19 */ DECODELENGTH(usnsize, p); p += usnsize; if(p>buffer + sizeof(buffer)) break; } close(s); return devlist; }