| 95 | } |
| 96 | |
| 97 | struct conn * |
| 98 | conn_init(struct conn_params *p) |
| 99 | { |
| 100 | int fd_server, fd_client_group, rc; |
| 101 | struct sockaddr_in server_address; |
| 102 | struct conn *conn = NULL; |
| 103 | int reuse = 1; |
| 104 | |
| 105 | memset(&server_address, 0, sizeof(server_address)); |
| 106 | |
| 107 | /* Check input arguments */ |
| 108 | if ((p == NULL) || (p->welcome == NULL) || (p->prompt == NULL) || (p->addr == NULL) || |
| 109 | (p->buf_size == 0) || (p->msg_in_len_max == 0) || (p->msg_out_len_max == 0) || |
| 110 | (p->msg_handle == NULL)) |
| 111 | goto exit; |
| 112 | |
| 113 | rc = inet_aton(p->addr, &server_address.sin_addr); |
| 114 | if (rc == 0) |
| 115 | goto exit; |
| 116 | |
| 117 | /* Memory allocation */ |
| 118 | conn = calloc(1, sizeof(struct conn)); |
| 119 | if (conn == NULL) |
| 120 | goto exit; |
| 121 | |
| 122 | conn->welcome = calloc(1, CONN_WELCOME_LEN_MAX + 1); |
| 123 | conn->prompt = calloc(1, CONN_PROMPT_LEN_MAX + 1); |
| 124 | conn->buf = calloc(1, p->buf_size); |
| 125 | conn->msg_in = calloc(1, p->msg_in_len_max + 1); |
| 126 | conn->msg_out = calloc(1, p->msg_out_len_max + 1); |
| 127 | |
| 128 | if ((conn->welcome == NULL) || (conn->prompt == NULL) || (conn->buf == NULL) || |
| 129 | (conn->msg_in == NULL) || (conn->msg_out == NULL)) { |
| 130 | conn_free(conn); |
| 131 | conn = NULL; |
| 132 | goto exit; |
| 133 | } |
| 134 | |
| 135 | /* Server socket */ |
| 136 | server_address.sin_family = AF_INET; |
| 137 | server_address.sin_port = htons(p->port); |
| 138 | |
| 139 | fd_server = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); |
| 140 | if (fd_server == -1) { |
| 141 | conn_free(conn); |
| 142 | conn = NULL; |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | if (setsockopt(fd_server, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, |
| 147 | sizeof(reuse)) < 0) |
| 148 | goto free; |
| 149 | |
| 150 | rc = bind(fd_server, (struct sockaddr *)&server_address, sizeof(server_address)); |
| 151 | if (rc == -1) |
| 152 | goto free; |
| 153 | |
| 154 | rc = listen(fd_server, 16); |
no test coverage detected