12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
3111793Sbrandon.potter@amd.com#include "base/socket.hh"
3211793Sbrandon.potter@amd.com
332SN/A#include <netinet/in.h>
342SN/A#include <netinet/tcp.h>
358229Snate@binkert.org#include <sys/socket.h>
368229Snate@binkert.org#include <sys/types.h>
372SN/A#include <unistd.h>
382SN/A
398229Snate@binkert.org#include <cerrno>
408229Snate@binkert.org
4112334Sgabeblack@google.com#include "base/logging.hh"
428229Snate@binkert.org#include "base/types.hh"
4312010Sgabeblack@google.com#include "sim/byteswap.hh"
442SN/A
452SN/Ausing namespace std;
462SN/A
475523Snate@binkert.orgbool ListenSocket::listeningDisabled = false;
485523Snate@binkert.orgbool ListenSocket::anyListening = false;
495523Snate@binkert.org
5012010Sgabeblack@google.combool ListenSocket::bindToLoopback = false;
5112010Sgabeblack@google.com
525523Snate@binkert.orgvoid
535523Snate@binkert.orgListenSocket::disableAll()
545523Snate@binkert.org{
555523Snate@binkert.org    if (anyListening)
565523Snate@binkert.org        panic("Too late to disable all listeners, already have a listener");
575523Snate@binkert.org    listeningDisabled = true;
585523Snate@binkert.org}
595523Snate@binkert.org
605523Snate@binkert.orgbool
615523Snate@binkert.orgListenSocket::allDisabled()
625523Snate@binkert.org{
635523Snate@binkert.org    return listeningDisabled;
645523Snate@binkert.org}
655523Snate@binkert.org
6612010Sgabeblack@google.comvoid
6712010Sgabeblack@google.comListenSocket::loopbackOnly()
6812010Sgabeblack@google.com{
6912010Sgabeblack@google.com    if (anyListening)
7012010Sgabeblack@google.com        panic("Too late to bind to loopback, already have a listener");
7112010Sgabeblack@google.com    bindToLoopback = true;
7212010Sgabeblack@google.com}
7312010Sgabeblack@google.com
742SN/A////////////////////////////////////////////////////////////////////////
752SN/A//
762SN/A//
772SN/A
782SN/AListenSocket::ListenSocket()
792SN/A    : listening(false), fd(-1)
802SN/A{}
812SN/A
822SN/AListenSocket::~ListenSocket()
832SN/A{
842SN/A    if (fd != -1)
852SN/A        close(fd);
862SN/A}
872SN/A
882SN/A// Create a socket and configure it for listening
892SN/Abool
902SN/AListenSocket::listen(int port, bool reuse)
912SN/A{
922SN/A    if (listening)
932SN/A        panic("Socket already listening!");
942SN/A
952SN/A    fd = ::socket(PF_INET, SOCK_STREAM, 0);
962SN/A    if (fd < 0)
971290SN/A        panic("Can't create socket:%s !", strerror(errno));
982SN/A
992SN/A    if (reuse) {
1002SN/A        int i = 1;
1012SN/A        if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
1022SN/A                         sizeof(i)) < 0)
1032SN/A            panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
1042SN/A    }
1052SN/A
1062SN/A    struct sockaddr_in sockaddr;
1072SN/A    sockaddr.sin_family = PF_INET;
10812010Sgabeblack@google.com    sockaddr.sin_addr.s_addr =
10912228Sgabeblack@google.com        htobe<in_addr_t>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
1102SN/A    sockaddr.sin_port = htons(port);
11110412Sandreas.hansson@arm.com    // finally clear sin_zero
11210412Sandreas.hansson@arm.com    memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
1132SN/A    int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
1142SN/A    if (ret != 0) {
1152SN/A        if (ret == -1 && errno != EADDRINUSE)
1162SN/A            panic("ListenSocket(listen): bind() failed!");
1172SN/A        return false;
1182SN/A    }
1192SN/A
12010049Smitch.hayenga+gem5@gmail.com    if (::listen(fd, 1) == -1) {
12110049Smitch.hayenga+gem5@gmail.com        if (errno != EADDRINUSE)
12210049Smitch.hayenga+gem5@gmail.com            panic("ListenSocket(listen): listen() failed!");
12310049Smitch.hayenga+gem5@gmail.com
12410049Smitch.hayenga+gem5@gmail.com        return false;
12510049Smitch.hayenga+gem5@gmail.com    }
1262SN/A
1272SN/A    listening = true;
1285523Snate@binkert.org    anyListening = true;
1292SN/A    return true;
1302SN/A}
1312SN/A
1322SN/A
1332SN/A// Open a connection.  Accept will block, so if you don't want it to,
1342SN/A// make sure a connection is ready before you call accept.
1352SN/Aint
1362SN/AListenSocket::accept(bool nodelay)
1372SN/A{
1382SN/A    struct sockaddr_in sockaddr;
1392SN/A    socklen_t slen = sizeof (sockaddr);
1402SN/A    int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen);
1412SN/A    if (sfd != -1 && nodelay) {
1422SN/A        int i = 1;
14310412Sandreas.hansson@arm.com        if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i,
14410412Sandreas.hansson@arm.com                         sizeof(i)) < 0)
14510412Sandreas.hansson@arm.com            warn("ListenSocket(accept): setsockopt() TCP_NODELAY failed!");
1462SN/A    }
1472SN/A
1482SN/A    return sfd;
1492SN/A}
150