/* slimrio - SLIMP3 emulation for the Rio Receiver.
 *
 * ssdp.c: Listen for port 21075 broadcast UDP and reply with
 * address of NFS server.
 *
 * Copyright (C) 2007 Robin O'Leary <slimrio@ro.nu>
 *
 * See also http://empeg.org.uk/slimrio/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>	/* for inet_ntoa() */


int main(int argc, char *argv[])
{
	int s;
	struct sockaddr_in a={ AF_INET, htons(21075), {INADDR_ANY}};
	int n;
	char r[100];
	struct sockaddr_in b;
	size_t bl=sizeof b;
	char *server="194.168.151.150";

	if(argv[1]) server=argv[1];
 
/*
	a.sin_family=AF_INET;
	a.sin_port=htons(21075);
	a.sin_addr.s_addr=INADDR_ANY;
*/
	if(bind(s=socket(PF_INET, SOCK_DGRAM, 0), (struct sockaddr *)&a, sizeof a)) {
		perror("bind");
		return 1;
	}

	while((n=recvfrom(s, r,sizeof r, 0, (struct sockaddr*)&b,&bl))>0) {
		r[n]=0;
		printf("%s: %s", inet_ntoa(b.sin_addr), r);
		fflush(stdout);
		snprintf(r, sizeof r, "http://%s/descriptor.xml\n",server);         
		if(sendto(s, &r, strlen(r), 0, (struct sockaddr*)&b,bl) < 0) {
			perror("sendto");
		}
	}
	printf("got %d", n);
	return 0;
}
