comparison src/core/nginx.c @ 1:d220029ac7f3

nginx-0.0.1-2002-08-15-21:20:26 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 15 Aug 2002 17:20:26 +0000
parents 4eff17414a43
children ffffe1499bce
comparison
equal deleted inserted replaced
0:4eff17414a43 1:d220029ac7f3
23 int ngx_http_init_connection(void *data); 23 int ngx_http_init_connection(void *data);
24 24
25 25
26 int ngx_max_conn = 512; 26 int ngx_max_conn = 512;
27 struct sockaddr_in ngx_addr = {0, AF_INET, 0, 0, 0}; 27 struct sockaddr_in ngx_addr = {0, AF_INET, 0, 0, 0};
28 28 int ngx_backlog = 0;
29 29
30 ngx_pool_t ngx_pool; 30 ngx_pool_t ngx_pool;
31 ngx_log_t ngx_log; 31 ngx_log_t ngx_log;
32 ngx_server_t ngx_server; 32 ngx_server_t ngx_server;
33 33
34 34
35 int main(int argc, char *const *argv) 35 int main(int argc, char *const *argv)
36 { 36 {
37 char addr_text[22]; 37 char addr_text[22];
38 ngx_socket_t fd; 38 ngx_socket_t s;
39 ngx_listen_t ls; 39 ngx_listen_t ls;
40 int reuseaddr = 1;
40 #if (WIN32) 41 #if (WIN32)
41 WSADATA wsd; 42 WSADATA wsd;
43 unsigned long nb = 1;
42 #endif 44 #endif
43 45
44 46
45 ngx_log.log_level = NGX_LOG_DEBUG; 47 ngx_log.log_level = NGX_LOG_DEBUG;
46 ngx_pool.log = &ngx_log; 48 ngx_pool.log = &ngx_log;
59 if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) 61 if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
60 ngx_log_error(NGX_LOG_EMERG, (&ngx_log), ngx_socket_errno, 62 ngx_log_error(NGX_LOG_EMERG, (&ngx_log), ngx_socket_errno,
61 "WSAStartup failed"); 63 "WSAStartup failed");
62 #endif 64 #endif
63 65
66 /* for each listening socket */
67 s = socket(AF_INET, SOCK_STREAM, 0);
68 if (s == -1)
69 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
70 "socket failed");
71
72 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
73 (const void *) &reuseaddr, sizeof(int)) == -1)
74 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
75 "setsockopt (SO_REUSEADDR) failed");
76
77 #if (WIN32)
78 if (ioctlsocket(s, FIONBIO, &nb) == -1)
79 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
80 "ioctlsocket (FIONBIO) failed");
81 #else
82 if (fcntl(s, F_SETFL, O_NONBLOCK) == -1)
83 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
84 "fcntl (O_NONBLOCK) failed");
85 #endif
86
64 ngx_snprintf(ngx_cpystrn(addr_text, inet_ntoa(ngx_addr.sin_addr), 16), 87 ngx_snprintf(ngx_cpystrn(addr_text, inet_ntoa(ngx_addr.sin_addr), 16),
65 7, ":%d", ntohs(ngx_addr.sin_port)); 88 7, ":%d", ntohs(ngx_addr.sin_port));
66 fd = ngx_listen((struct sockaddr *) &ngx_addr, -1, &ngx_log, addr_text); 89
90 if (bind(s, (struct sockaddr *) &ngx_addr,
91 sizeof(struct sockaddr_in)) == -1)
92 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
93 "bind to %s failed", addr_text);
94
95 if (listen(s, ngx_backlog) == -1)
96 ngx_log_error(NGX_LOG_EMERG, &(ngx_log), ngx_socket_errno,
97 "listen to %s failed", addr_text);
67 98
68 ngx_server.buff_size = 1024; 99 ngx_server.buff_size = 1024;
69 ngx_server.handler = ngx_http_init_connection; 100 ngx_server.handler = ngx_http_init_connection;
70 101
71 /* daemon */ 102 /* daemon */
72 103
73 ls.fd = fd; 104 ls.fd = s;
74 ls.server = &ngx_server; 105 ls.server = &ngx_server;
75 ls.log = &ngx_log; 106 ls.log = &ngx_log;
76 107
77 /* fork */ 108 /* fork */
78 109