147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* -*- Mode: C; tab-width: 4 -*-
247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * you may not use this file except in compliance with the License.
747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * You may obtain a copy of the License at
847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     http://www.apache.org/licenses/LICENSE-2.0
1047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
1147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
1247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
1347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * See the License for the specific language governing permissions and
1547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * limitations under the License.
1647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt */
1747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
1847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <stdio.h>			// For printf()
1947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <stdlib.h>			// For exit() etc.
2047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <string.h>			// For strlen() etc.
2147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <unistd.h>			// For select()
2247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <errno.h>			// For errno, EINTR
2347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <netinet/in.h>		// For INADDR_NONE
2447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <arpa/inet.h>		// For inet_addr()
2547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <netdb.h>			// For gethostbyname()
2647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <signal.h>			// For SIGINT, etc.
2747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "mDNSEmbeddedAPI.h"  // Defines the interface to the client layer above
2947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "mDNSPosix.h"      // Defines the specific types needed to run mDNS on this platform
3047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//*******************************************************************************************
3247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Main
3347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstatic volatile mDNSBool StopNow;
3547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3647e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSlocal void HandleSIG(int signal)
3747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
3847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(void)signal;	// Unused
3947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	debugf("%s","");
4047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	debugf("HandleSIG");
4147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	StopNow = mDNStrue;
4247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
4347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
4447e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSexport void ExampleClientEventLoop(mDNS *const m)
4547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
4647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	signal(SIGINT, HandleSIG);	// SIGINT is what you get for a Ctrl-C
4747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	signal(SIGTERM, HandleSIG);
4847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
4947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	while (!StopNow)
5047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		{
5147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		int nfds = 0;
5247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		fd_set readfds;
5347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		struct timeval timeout;
5447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		int result;
5547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// 1. Set up the fd_set as usual here.
5747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// This example client has no file descriptors of its own,
5847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// but a real application would call FD_SET to add them to the set here
5947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		FD_ZERO(&readfds);
6047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
6147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// 2. Set up the timeout.
6247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// This example client has no other work it needs to be doing,
6347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// so we set an effectively infinite timeout
6447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		timeout.tv_sec = 0x3FFFFFFF;
6547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		timeout.tv_usec = 0;
6647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
6747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// 3. Give the mDNSPosix layer a chance to add its information to the fd_set and timeout
6847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		mDNSPosixGetFDSet(m, &nfds, &readfds, &timeout);
6947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		// 4. Call select as normal
7147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		verbosedebugf("select(%d, %d.%06d)", nfds, timeout.tv_sec, timeout.tv_usec);
7247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		result = select(nfds, &readfds, NULL, NULL, &timeout);
7347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		if (result < 0)
7547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			{
7647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			verbosedebugf("select() returned %d errno %d", result, errno);
7747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			if (errno != EINTR) StopNow = mDNStrue;
7847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			}
7947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		else
8047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			{
8147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			// 5. Call mDNSPosixProcessFDSet to let the mDNSPosix layer do its work
8247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			mDNSPosixProcessFDSet(m, &readfds);
8347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
8447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			// 6. This example client has no other work it needs to be doing,
8547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			// but a real client would do its work here
8647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			// ... (do work) ...
8747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			}
8847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		}
8947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
9047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	debugf("Exiting");
9147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
92