socket.cc revision 5523
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
312SN/A#include <sys/types.h>
322SN/A#include <sys/socket.h>
332SN/A
342SN/A#include <netinet/in.h>
352SN/A#include <netinet/tcp.h>
362SN/A
372SN/A#include <errno.h>
382SN/A#include <unistd.h>
392SN/A
4056SN/A#include "sim/host.hh"
4156SN/A#include "base/misc.hh"
4256SN/A#include "base/socket.hh"
432SN/A
442SN/Ausing namespace std;
452SN/A
465523Snate@binkert.orgbool ListenSocket::listeningDisabled = false;
475523Snate@binkert.orgbool ListenSocket::anyListening = false;
485523Snate@binkert.org
495523Snate@binkert.orgvoid
505523Snate@binkert.orgListenSocket::disableAll()
515523Snate@binkert.org{
525523Snate@binkert.org    if (anyListening)
535523Snate@binkert.org        panic("Too late to disable all listeners, already have a listener");
545523Snate@binkert.org    listeningDisabled = true;
555523Snate@binkert.org}
565523Snate@binkert.org
575523Snate@binkert.orgbool
585523Snate@binkert.orgListenSocket::allDisabled()
595523Snate@binkert.org{
605523Snate@binkert.org    return listeningDisabled;
615523Snate@binkert.org}
625523Snate@binkert.org
632SN/A////////////////////////////////////////////////////////////////////////
642SN/A//
652SN/A//
662SN/A
672SN/AListenSocket::ListenSocket()
682SN/A    : listening(false), fd(-1)
692SN/A{}
702SN/A
712SN/AListenSocket::~ListenSocket()
722SN/A{
732SN/A    if (fd != -1)
742SN/A        close(fd);
752SN/A}
762SN/A
772SN/A// Create a socket and configure it for listening
782SN/Abool
792SN/AListenSocket::listen(int port, bool reuse)
802SN/A{
812SN/A    if (listening)
822SN/A        panic("Socket already listening!");
832SN/A
842SN/A    fd = ::socket(PF_INET, SOCK_STREAM, 0);
852SN/A    if (fd < 0)
861290SN/A        panic("Can't create socket:%s !", strerror(errno));
872SN/A
882SN/A    if (reuse) {
892SN/A        int i = 1;
902SN/A        if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
912SN/A                         sizeof(i)) < 0)
922SN/A            panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
932SN/A    }
942SN/A
952SN/A    struct sockaddr_in sockaddr;
962SN/A    sockaddr.sin_family = PF_INET;
972SN/A    sockaddr.sin_addr.s_addr = INADDR_ANY;
982SN/A
992SN/A    sockaddr.sin_port = htons(port);
1002SN/A    int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
1012SN/A    if (ret != 0) {
1022SN/A        if (ret == -1 && errno != EADDRINUSE)
1032SN/A            panic("ListenSocket(listen): bind() failed!");
1042SN/A        return false;
1052SN/A    }
1062SN/A
1072SN/A    if (::listen(fd, 1) == -1)
1082SN/A        panic("ListenSocket(listen): listen() failed!");
1092SN/A
1102SN/A    listening = true;
1112SN/A
1125523Snate@binkert.org    anyListening = true;
1132SN/A    return true;
1142SN/A}
1152SN/A
1162SN/A
1172SN/A// Open a connection.  Accept will block, so if you don't want it to,
1182SN/A// make sure a connection is ready before you call accept.
1192SN/Aint
1202SN/AListenSocket::accept(bool nodelay)
1212SN/A{
1222SN/A    struct sockaddr_in sockaddr;
1232SN/A    socklen_t slen = sizeof (sockaddr);
1242SN/A    int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen);
1252SN/A    if (sfd != -1 && nodelay) {
1262SN/A        int i = 1;
1272SN/A        ::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, sizeof(i));
1282SN/A    }
1292SN/A
1302SN/A    return sfd;
1312SN/A}
132