socket.cc revision 2665
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 462SN/A//////////////////////////////////////////////////////////////////////// 472SN/A// 482SN/A// 492SN/A 502SN/AListenSocket::ListenSocket() 512SN/A : listening(false), fd(-1) 522SN/A{} 532SN/A 542SN/AListenSocket::~ListenSocket() 552SN/A{ 562SN/A if (fd != -1) 572SN/A close(fd); 582SN/A} 592SN/A 602SN/A// Create a socket and configure it for listening 612SN/Abool 622SN/AListenSocket::listen(int port, bool reuse) 632SN/A{ 642SN/A if (listening) 652SN/A panic("Socket already listening!"); 662SN/A 672SN/A fd = ::socket(PF_INET, SOCK_STREAM, 0); 682SN/A if (fd < 0) 691290SN/A panic("Can't create socket:%s !", strerror(errno)); 702SN/A 712SN/A if (reuse) { 722SN/A int i = 1; 732SN/A if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i, 742SN/A sizeof(i)) < 0) 752SN/A panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!"); 762SN/A } 772SN/A 782SN/A struct sockaddr_in sockaddr; 792SN/A sockaddr.sin_family = PF_INET; 802SN/A sockaddr.sin_addr.s_addr = INADDR_ANY; 812SN/A 822SN/A sockaddr.sin_port = htons(port); 832SN/A int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr)); 842SN/A if (ret != 0) { 852SN/A if (ret == -1 && errno != EADDRINUSE) 862SN/A panic("ListenSocket(listen): bind() failed!"); 872SN/A return false; 882SN/A } 892SN/A 902SN/A if (::listen(fd, 1) == -1) 912SN/A panic("ListenSocket(listen): listen() failed!"); 922SN/A 932SN/A listening = true; 942SN/A 952SN/A return true; 962SN/A} 972SN/A 982SN/A 992SN/A// Open a connection. Accept will block, so if you don't want it to, 1002SN/A// make sure a connection is ready before you call accept. 1012SN/Aint 1022SN/AListenSocket::accept(bool nodelay) 1032SN/A{ 1042SN/A struct sockaddr_in sockaddr; 1052SN/A socklen_t slen = sizeof (sockaddr); 1062SN/A int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen); 1072SN/A if (sfd != -1 && nodelay) { 1082SN/A int i = 1; 1092SN/A ::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i, sizeof(i)); 1102SN/A } 1112SN/A 1122SN/A return sfd; 1132SN/A} 114