Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SSDP UDP broadcast ...

Posted on 2008-11-11
4
Medium Priority
?
8,192 Views
Last Modified: 2013-12-19
I got the this simple SSDP search example working. I was not able to get all root UPnP devices that I got with the UPnP Device spy?

Is this kind of timing problem?
Is there any simple ansi-c BSD based example that works properlly?
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
 
#define RESPONSE_BUFFER_LEN 1024
#define SSDP_MULTICAST      "239.255.255.250"
#define SSDP_PORT           1900
 
int main (int argc, const char * argv[])
{
	int sock;
	size_t ret;
	unsigned int socklen;
	struct sockaddr_in sockname;
	struct sockaddr clientsock;
	struct hostent *hostname;
	char data[] = 
    "M-SEARCH * HTTP/1.1\r\n"
    "Host: 239.255.255.250:1900\r\n"
    "Man: \"ssdp:discover\"\r\n"
    "ST:upnp:rootdevice\r\n"
    "MX:3\r\n"
    "\r\n";
	char buffer[RESPONSE_BUFFER_LEN];
	unsigned int len = RESPONSE_BUFFER_LEN;
	fd_set fds;
	struct timeval timeout;
	
	hostname = gethostbyname(SSDP_MULTICAST);
	hostname->h_addrtype = AF_INET;
	
	
	if((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1){
		printf("err: socket() failed");
		return -1;
	}
	
	memset((char*)&sockname, 0, sizeof(struct sockaddr_in));
	sockname.sin_family=AF_INET;
	sockname.sin_port=htons(SSDP_PORT);
	sockname.sin_addr.s_addr=*((unsigned long*)(hostname->h_addr_list[0]));
	
	ret=sendto(sock, data, strlen(data), 0, (struct sockaddr*) &sockname, 
			   sizeof(struct sockaddr_in));
	if(ret != strlen(data)){
		printf("err:sendto");
		return -1;
	}
	
	/* Get response */
	FD_ZERO(&fds);
	FD_SET(sock, &fds);
	timeout.tv_sec=10;
	timeout.tv_usec=10;
	
	if(select(sock+1, &fds, NULL, NULL, &timeout) < 0){
		printf("eer:select");
		return -1;
	}
	if(FD_ISSET(sock, &fds)){
		socklen=sizeof(clientsock);
		if((len = recvfrom(sock, buffer, len, MSG_PEEK, 
						   &clientsock, &socklen)) == (size_t)-1){
			printf("err: recvfrom");
			return -1;
		}
		buffer[len]='\0';
		close(sock);
		
		/* Check the HTTP response code */
		if(strncmp(buffer, "HTTP/1.1 200 OK", 12) != 0){
			printf("err: ssdp parsing ");
			return -1;
		}
		
		printf(buffer);
		return 0;
		
	}else{
		
		printf("err: no ssdp answer");
		return -1;
	}
}

Open in new window

0
Comment
Question by:koleto115
  • 2
  • 2
4 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 22941704
Examples here:
http://www.gupnp.org/

Suggest one has to reissue multicast request until device list stops growing.
0
 

Author Comment

by:koleto115
ID: 22943321
1. I will check the link, sound promising.
2. You mean I should run the function on a loop ... I tryied already but somehow i still get my rooter and no one other device and i have another one for sure.
It looks like a timing problem.


0
 
LVL 62

Accepted Solution

by:
gheist earned 1500 total points
ID: 22944768
I am not the exper on the matter. I just looked how Gnome does things you asked for.
0
 

Author Comment

by:koleto115
ID: 22945150
Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains the fundamentals of industrial networking which ultimately is the backbone network which is providing communications for process devices like robots and other not so interesting stuff.
LinkedIn blogging is great for networking, building up an audience, and expanding your influence as well. However, if you want to achieve these results, you need to work really hard to make your post worth liking and sharing. Here are 4 tips that ca…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question