Annotation of embedaddon/miniupnpd/minissdpd/testminissdpd.c, revision 1.1
1.1 ! misho 1: /* $Id: testminissdpd.c,v 1.14 2016/03/01 17:49:51 nanard Exp $ */
! 2: /* Project : miniupnp
! 3: * website : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
! 4: * Author : Thomas BERNARD
! 5: * copyright (c) 2005-2018 Thomas Bernard
! 6: * This software is subjet to the conditions detailed in the
! 7: * provided LICENCE file. */
! 8:
! 9: #include <unistd.h>
! 10: #include <stdio.h>
! 11: #include <stdlib.h>
! 12: #include <string.h>
! 13: #include <sys/types.h>
! 14: #include <sys/socket.h>
! 15: #include <sys/un.h>
! 16:
! 17: #include "codelength.h"
! 18: #include "printresponse.h"
! 19:
! 20: void printversion(const unsigned char * resp, int n)
! 21: {
! 22: int l;
! 23: const unsigned char * p;
! 24:
! 25: p = resp;
! 26: DECODELENGTH(l, p);
! 27: if(resp + n < p + l) {
! 28: printf("get version error\n");
! 29: }
! 30: printf("MiniSSDPd version : %.*s\n", l, p);
! 31: }
! 32:
! 33: #define SENDCOMMAND(command, size) write(s, command, size); \
! 34: printf("Command written type=%u\n", (unsigned char)command[0]);
! 35:
! 36: int connect_unix_socket(const char * sockpath)
! 37: {
! 38: int s;
! 39: struct sockaddr_un addr;
! 40:
! 41: s = socket(AF_UNIX, SOCK_STREAM, 0);
! 42: addr.sun_family = AF_UNIX;
! 43: strncpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
! 44: if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
! 45: fprintf(stderr, "connecting to %s : ", addr.sun_path);
! 46: perror("connect");
! 47: exit(1);
! 48: }
! 49: printf("Connected to %s\n", addr.sun_path);
! 50: return s;
! 51: }
! 52:
! 53: /* test program for minissdpd */
! 54: int
! 55: main(int argc, char * * argv)
! 56: {
! 57: const unsigned char command0[] = { 0x00, 0x00 };
! 58: char command1[] = "\x01\x00urn:schemas-upnp-org:device:InternetGatewayDevice";
! 59: char command2[] = "\x02\x00uuid:fc4ec57e-b051-11db-88f8-0060085db3f6::upnp:rootdevice";
! 60: const unsigned char command3[] = { 0x03, 0x00 };
! 61: /* old versions of minissdpd would reject a command with
! 62: * a zero length string argument */
! 63: char command3compat[] = "\x03\x00ssdp:all";
! 64: char command4[] = "\x04\x00test:test:test";
! 65: const unsigned char bad_command[] = { 0xff, 0xff };
! 66: const unsigned char overflow[] = { 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
! 67: const unsigned char command5[] = { 0x05, 0x00 };
! 68: const unsigned char bad_command4[] = { 0x04, 0x01, 0x60, 0x8f, 0xff, 0xff, 0xff, 0x7f};
! 69: int s;
! 70: int i;
! 71: void * tmp;
! 72: unsigned char * resp = NULL;
! 73: size_t respsize = 0;
! 74: unsigned char buf[4096];
! 75: ssize_t n;
! 76: int total = 0;
! 77: const char * sockpath = "/var/run/minissdpd.sock";
! 78:
! 79: for(i=0; i<argc-1; i++) {
! 80: if(0==strcmp(argv[i], "-s"))
! 81: sockpath = argv[++i];
! 82: }
! 83: command1[1] = sizeof(command1) - 3;
! 84: command2[1] = sizeof(command2) - 3;
! 85: command3compat[1] = sizeof(command3compat) - 3;
! 86: command4[1] = sizeof(command4) - 3;
! 87: s = connect_unix_socket(sockpath);
! 88:
! 89: n = SENDCOMMAND(command0, sizeof(command0));
! 90: n = read(s, buf, sizeof(buf));
! 91: printf("Response received %d bytes\n", (int)n);
! 92: if(n > 0) {
! 93: printversion(buf, n);
! 94: } else {
! 95: printf("Command 0 (get version) not supported\n");
! 96: close(s);
! 97: s = connect_unix_socket(sockpath);
! 98: }
! 99:
! 100: n = SENDCOMMAND(command1, sizeof(command1) - 1);
! 101: n = read(s, buf, sizeof(buf));
! 102: printf("Response received %d bytes\n", (int)n);
! 103: printresponse(buf, n);
! 104: if(n == 0) {
! 105: close(s);
! 106: s = connect_unix_socket(sockpath);
! 107: }
! 108:
! 109: n = SENDCOMMAND(command2, sizeof(command2) - 1);
! 110: n = read(s, buf, sizeof(buf));
! 111: printf("Response received %d bytes\n", (int)n);
! 112: printresponse(buf, n);
! 113: if(n == 0) {
! 114: close(s);
! 115: s = connect_unix_socket(sockpath);
! 116: }
! 117:
! 118: buf[0] = 0; /* Slight hack for printing num devices when 0 */
! 119: n = SENDCOMMAND(command3, sizeof(command3));
! 120: n = read(s, buf, sizeof(buf));
! 121: if(n == 0) {
! 122: printf("command3 failed, testing compatible one\n");
! 123: close(s);
! 124: s = connect_unix_socket(sockpath);
! 125: n = SENDCOMMAND(command3compat, sizeof(command3compat) - 1);
! 126: n = read(s, buf, sizeof(buf));
! 127: }
! 128: printf("Response received %d bytes\n", (int)n);
! 129: printf("Number of devices %d\n", (int)buf[0]);
! 130: while(n > 0) {
! 131: tmp = realloc(resp, respsize + n);
! 132: if(tmp == NULL) {
! 133: fprintf(stderr, "memory allocation error\n");
! 134: break;
! 135: }
! 136: resp = tmp;
! 137: respsize += n;
! 138: if (n > 0) {
! 139: memcpy(resp + total, buf, n);
! 140: total += n;
! 141: }
! 142: if (n < (ssize_t)sizeof(buf)) {
! 143: break;
! 144: }
! 145:
! 146: n = read(s, buf, sizeof(buf));
! 147: printf("response received %d bytes\n", (int)n);
! 148: }
! 149: if(resp != NULL) {
! 150: printresponse(resp, total);
! 151: free(resp);
! 152: resp = NULL;
! 153: }
! 154: if(n == 0) {
! 155: close(s);
! 156: s = connect_unix_socket(sockpath);
! 157: }
! 158:
! 159: n = SENDCOMMAND(command4, sizeof(command4));
! 160: /* no response for request type 4 */
! 161:
! 162: n = SENDCOMMAND(bad_command, sizeof(bad_command));
! 163: n = read(s, buf, sizeof(buf));
! 164: printf("Response received %d bytes\n", (int)n);
! 165: printresponse(buf, n);
! 166: if(n == 0) {
! 167: close(s);
! 168: s = connect_unix_socket(sockpath);
! 169: }
! 170:
! 171: n = SENDCOMMAND(overflow, sizeof(overflow));
! 172: n = read(s, buf, sizeof(buf));
! 173: printf("Response received %d bytes\n", (int)n);
! 174: printresponse(buf, n);
! 175: if(n == 0) {
! 176: close(s);
! 177: s = connect_unix_socket(sockpath);
! 178: }
! 179:
! 180: n = SENDCOMMAND(command5, sizeof(command5));
! 181: n = read(s, buf, sizeof(buf));
! 182: printf("Response received %d bytes\n", (int)n);
! 183: printresponse(buf, n);
! 184: if(n == 0) {
! 185: close(s);
! 186: s = connect_unix_socket(sockpath);
! 187: }
! 188:
! 189: n = SENDCOMMAND(bad_command4, sizeof(bad_command4));
! 190: n = read(s, buf, sizeof(buf));
! 191: printf("Response received %d bytes\n", (int)n);
! 192: printresponse(buf, n);
! 193:
! 194: close(s);
! 195: return 0;
! 196: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>