c:调用socket()时的Segfault

时间:2017-05-17 08:36:07

标签: c sockets segmentation-fault

我正在努力完成学校的任务。目标是创建一个连接管理器来创建和管理套接字。我们从教授那里收到了一个文件(tcpsock.c),但在调用其中一个函数时我一直遇到段错误。 这是所有使用的包含/变量。

#define _GNU_SOURCE 

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> 
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <assert.h>

#include "tcpsock.h"

//#define DEBUG

#ifdef DEBUG
#define TCP_DEBUG_PRINTF(condition,...)                                 \
    do {                                                \
       if((condition))                                      \
       {                                                \
        fprintf(stderr,"\nIn %s - function %s at line %d: ", __FILE__,     __func__, __LINE__); \
        fprintf(stderr,__VA_ARGS__);                                \
       }                                                \
    } while(0)                      
#else
#define TCP_DEBUG_PRINTF(...) (void)0
#endif


#define TCP_ERR_HANDLER(condition,...)  \
do {                        \
    if ((condition))            \
    {                   \
      TCP_DEBUG_PRINTF(1,"error condition \"" #condition "\" is true\n");   \
      __VA_ARGS__;              \
    }                   \
} while(0)


#define MAGIC_COOKIE    (long)(0xA2E1CF37D35)   // used to check if a socket is bounded

#define CHAR_IP_ADDR_LENGTH 16      // 4 numbers of 3 digits, 3 dots and \0
#define PROTOCOLFAMILY  AF_INET     // internet protocol suite
#define TYPE        SOCK_STREAM // streaming protool type
#define PROTOCOL    IPPROTO_TCP     // TCP protocol 

struct tcpsock {
  long cookie;      // if the socket is bound, cookie should be equal to MAGIC_COOKIE
        // remark: the use of magic cookies doesn't guarantee a 'bullet proof' test
  int sd;       // socket descriptor
  char * ip_addr;   // socket IP address
  int port;         // socket port number
  } ;   

typedef struct tcpsock tcpsock_t;

static tcpsock_t * tcp_sock_create();

每当调用函数tcp_passive_open时,我总是会遇到段错误。

int tcp_passive_open(tcpsock_t ** sock, int port)
{
  int result;
  struct sockaddr_in addr;
  TCP_ERR_HANDLER(((port<MIN_PORT)||(port>MAX_PORT)), return TCP_ADDRESS_ERROR);  
  tcpsock_t * s = tcp_sock_create();
  TCP_ERR_HANDLER(s==NULL,return TCP_MEMORY_ERROR); 
  //printf("%p\n",s); printf("%d\n",s->sd); 
  s->sd = socket(PROTOCOLFAMILY, TYPE, PROTOCOL);
  TCP_DEBUG_PRINTF(s->sd<0,"Socket() failed with errno = %d [%s]", errno, strerror(errno));
  TCP_ERR_HANDLER(s->sd<0,free(s);return TCP_SOCKOP_ERROR); 
  // Construct the server address structure 
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = PROTOCOLFAMILY;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  result = bind(s->sd,(struct sockaddr *)&addr,sizeof(addr));
  TCP_DEBUG_PRINTF(result==-1,"Bind() failed with errno = %d [%s]", errno, strerror(errno));
  TCP_ERR_HANDLER(result!=0,free(s);return TCP_SOCKOP_ERROR);   
  result = listen(s->sd,MAX_PENDING);
  TCP_DEBUG_PRINTF(result==-1,"Listen() failed with errno = %d [%s]", errno, strerror(errno));
  TCP_ERR_HANDLER(result!=0,free(s);return TCP_SOCKOP_ERROR);  
  s->ip_addr = NULL; // address set to INADDR_ANY - not a specific IP address
  s->port = port;
  s->cookie = MAGIC_COOKIE; 
  *sock = s;
  return TCP_NO_ERROR;  
}

运行调试器时,会出现段错误发生在以下行:

s->sd = socket(PROTOCOLFAMILY, TYPE, PROTOCOL);

生成段错误的是套接字函数(我通过打印来检查s-&gt; sd,并且它没有任何问题。) 这个代码适用于所有人,除了我。有没有人知道它为什么会出现分段错误?

使用以下函数调用该函数:

tcpsock_t * server;
int main( void )
{
    conmgr_listen(1234);
    return 0;
}

void conmgr_listen(int port)
{
    int e = tcp_passive_open(&server,port);
}

函数&#t; tcp_sock_crreate()&#39;:

static tcpsock_t * tcp_sock_create()
{
  tcpsock_t * s = (tcpsock_t *) malloc(sizeof(tcpsock_t));
  if (s) // init the socket to default values
  {
    s->cookie = 0;  // socket is not yet bound!
    s->port = -1;
    s->ip_addr = NULL; 
    s->sd = -1;
  }
  return s;
}

0 个答案:

没有答案