1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 26 unchanged lines hidden (view full) ---

35#include <sys/socket.h>
36#include <sys/types.h>
37#include <unistd.h>
38
39#include <cerrno>
40
41#include "base/misc.hh"
42#include "base/types.hh"
43#include "sim/byteswap.hh"
44
45using namespace std;
46
47bool ListenSocket::listeningDisabled = false;
48bool ListenSocket::anyListening = false;
49
50bool ListenSocket::bindToLoopback = false;
51
52void
53ListenSocket::disableAll()
54{
55 if (anyListening)
56 panic("Too late to disable all listeners, already have a listener");
57 listeningDisabled = true;
58}
59
60bool
61ListenSocket::allDisabled()
62{
63 return listeningDisabled;
64}
65
66void
67ListenSocket::loopbackOnly()
68{
69 if (anyListening)
70 panic("Too late to bind to loopback, already have a listener");
71 bindToLoopback = true;
72}
73
74////////////////////////////////////////////////////////////////////////
75//
76//
77
78ListenSocket::ListenSocket()
79 : listening(false), fd(-1)
80{}
81

--- 18 unchanged lines hidden (view full) ---

100 int i = 1;
101 if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
102 sizeof(i)) < 0)
103 panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
104 }
105
106 struct sockaddr_in sockaddr;
107 sockaddr.sin_family = PF_INET;
97 sockaddr.sin_addr.s_addr = INADDR_ANY;
108 sockaddr.sin_addr.s_addr =
109 htobe<unsigned long>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
110 sockaddr.sin_port = htons(port);
111 // finally clear sin_zero
112 memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
113 int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
114 if (ret != 0) {
115 if (ret == -1 && errno != EADDRINUSE)
116 panic("ListenSocket(listen): bind() failed!");
117 return false;

--- 32 unchanged lines hidden ---