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