socket.cc revision 10412
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 sockaddr.sin_port = htons(port); 9810412Sandreas.hansson@arm.com // finally clear sin_zero 9910412Sandreas.hansson@arm.com memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero)); 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 10710049Smitch.hayenga+gem5@gmail.com if (::listen(fd, 1) == -1) { 10810049Smitch.hayenga+gem5@gmail.com if (errno != EADDRINUSE) 10910049Smitch.hayenga+gem5@gmail.com panic("ListenSocket(listen): listen() failed!"); 11010049Smitch.hayenga+gem5@gmail.com 11110049Smitch.hayenga+gem5@gmail.com return false; 11210049Smitch.hayenga+gem5@gmail.com } 1132SN/A 1142SN/A listening = true; 1155523Snate@binkert.org anyListening = true; 1162SN/A return true; 1172SN/A} 1182SN/A 1192SN/A 1202SN/A// Open a connection. Accept will block, so if you don't want it to, 1212SN/A// make sure a connection is ready before you call accept. 1222SN/Aint 1232SN/AListenSocket::accept(bool nodelay) 1242SN/A{ 1252SN/A struct sockaddr_in sockaddr; 1262SN/A socklen_t slen = sizeof (sockaddr); 1272SN/A int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen); 1282SN/A if (sfd != -1 && nodelay) { 1292SN/A int i = 1; 13010412Sandreas.hansson@arm.com if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, 13110412Sandreas.hansson@arm.com sizeof(i)) < 0) 13210412Sandreas.hansson@arm.com warn("ListenSocket(accept): setsockopt() TCP_NODELAY failed!"); 1332SN/A } 1342SN/A 1352SN/A return sfd; 1362SN/A} 137