Changeset 048cd69 in mainline for uspace/app
- Timestamp:
- 2015-06-07T15:41:04Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 204ba47
- Parents:
- 4d11204 (diff), c3f7d37 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace/app
- Files:
-
- 19 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/download/main.c
r4d11204 r048cd69 36 36 */ 37 37 38 #include <errno.h> 38 39 #include <stdio.h> 39 40 #include <stdlib.h> … … 42 43 #include <task.h> 43 44 #include <macros.h> 44 45 #include <net/in.h>46 #include <net/inet.h>47 #include <net/socket.h>48 45 49 46 #include <http/http.h> -
uspace/app/nterm/conn.c
r4d11204 r048cd69 38 38 #include <fibril.h> 39 39 #include <inet/dnsr.h> 40 #include < net/inet.h>41 #include < net/socket.h>40 #include <inet/endpoint.h> 41 #include <inet/tcp.h> 42 42 #include <stdio.h> 43 43 #include <stdlib.h> … … 48 48 #include "nterm.h" 49 49 50 static int conn_fd;51 static fid_t rcv_fid;50 static tcp_t *tcp; 51 static tcp_conn_t *conn; 52 52 53 53 #define RECV_BUF_SIZE 1024 54 54 static uint8_t recv_buf[RECV_BUF_SIZE]; 55 55 56 static int rcv_fibril(void *arg) 56 static void conn_conn_reset(tcp_conn_t *); 57 static void conn_data_avail(tcp_conn_t *); 58 59 static tcp_cb_t conn_cb = { 60 .conn_reset = conn_conn_reset, 61 .data_avail = conn_data_avail 62 }; 63 64 static void conn_conn_reset(tcp_conn_t *conn) 57 65 { 58 ssize_t nr; 66 printf("\n[Connection reset]\n"); 67 } 68 69 static void conn_data_avail(tcp_conn_t *conn) 70 { 71 int rc; 72 size_t nrecv; 59 73 60 74 while (true) { 61 nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0); 62 if (nr < 0) 75 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv); 76 if (rc != EOK) { 77 printf("\n[Receive error]\n"); 63 78 break; 79 } 64 80 65 nterm_received(recv_buf, nr); 81 nterm_received(recv_buf, nrecv); 82 83 if (nrecv != RECV_BUF_SIZE) 84 break; 66 85 } 67 68 if (nr == ENOTCONN)69 printf("\n[Other side has closed the connection]\n");70 else71 printf("'\n[Receive errror (%s)]\n", str_error(nr));72 73 exit(0);74 return 0;75 86 } 76 87 77 88 int conn_open(const char *host, const char *port_s) 78 89 { 79 int conn_fd = -1; 80 struct sockaddr *saddr = NULL; 81 socklen_t saddrlen; 82 90 inet_ep2_t epp; 91 83 92 /* Interpret as address */ 84 93 inet_addr_t iaddr; 85 94 int rc = inet_addr_parse(host, &iaddr); 86 95 87 96 if (rc != EOK) { 88 97 /* Interpret as a host name */ 89 98 dnsr_hostinfo_t *hinfo = NULL; 90 99 rc = dnsr_name2host(host, &hinfo, ip_any); 91 100 92 101 if (rc != EOK) { 93 102 printf("Error resolving host '%s'.\n", host); 94 103 goto error; 95 104 } 96 105 97 106 iaddr = hinfo->addr; 98 107 } 99 108 100 109 char *endptr; 101 110 uint16_t port = strtol(port_s, &endptr, 10); … … 104 113 goto error; 105 114 } 106 107 rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen); 108 if (rc != EOK) { 109 assert(rc == ENOMEM); 110 printf("Out of memory.\n"); 111 return ENOMEM; 112 } 113 115 116 inet_ep2_init(&epp); 117 epp.remote.addr = iaddr; 118 epp.remote.port = port; 119 114 120 printf("Connecting to host %s port %u\n", host, port); 115 116 conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0); 117 if (conn_fd < 0) 118 goto error; 119 120 rc = connect(conn_fd, saddr, saddrlen); 121 122 rc = tcp_create(&tcp); 121 123 if (rc != EOK) 122 124 goto error; 123 124 rc v_fid = fibril_create(rcv_fibril, NULL);125 if (rc v_fid == 0)125 126 rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn); 127 if (rc != EOK) 126 128 goto error; 127 128 fibril_add_ready(rcv_fid); 129 130 free(saddr); 129 130 rc = tcp_conn_wait_connected(conn); 131 if (rc != EOK) 132 goto error; 133 131 134 return EOK; 132 135 error: 133 if (conn_fd >= 0) { 134 closesocket(conn_fd); 135 conn_fd = -1; 136 } 137 free(saddr); 138 136 tcp_conn_destroy(conn); 137 tcp_destroy(tcp); 138 139 139 return EIO; 140 140 } … … 142 142 int conn_send(void *data, size_t size) 143 143 { 144 int rc = send(conn_fd, data, size, 0);144 int rc = tcp_conn_send(conn, data, size); 145 145 if (rc != EOK) 146 146 return EIO; 147 147 148 148 return EOK; 149 149 } -
uspace/app/websrv/websrv.c
r4d11204 r048cd69 44 44 #include <task.h> 45 45 46 #include < net/in.h>47 #include < net/inet.h>48 #include < net/socket.h>46 #include <inet/addr.h> 47 #include <inet/endpoint.h> 48 #include <inet/tcp.h> 49 49 50 50 #include <arg_parse.h> … … 56 56 57 57 #define DEFAULT_PORT 8080 58 #define BACKLOG_SIZE 359 58 60 59 #define WEB_ROOT "/data/web" … … 62 61 /** Buffer for receiving the request. */ 63 62 #define BUFFER_SIZE 1024 63 64 static void websrv_new_conn(tcp_listener_t *, tcp_conn_t *); 65 66 static tcp_listen_cb_t listen_cb = { 67 .new_conn = websrv_new_conn 68 }; 69 70 static tcp_cb_t conn_cb = { 71 .connected = NULL 72 }; 64 73 65 74 static uint16_t port = DEFAULT_PORT; … … 122 131 123 132 /** Receive one character (with buffering) */ 124 static int recv_char(int fd, char *c) 125 { 133 static int recv_char(tcp_conn_t *conn, char *c) 134 { 135 size_t nrecv; 136 int rc; 137 126 138 if (rbuf_out == rbuf_in) { 127 139 rbuf_out = 0; 128 140 rbuf_in = 0; 129 141 130 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);131 if (rc <= 0) {132 fprintf(stderr, " recv() failed (%zd)\n", rc);142 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv); 143 if (rc != EOK) { 144 fprintf(stderr, "tcp_conn_recv() failed (%d)\n", rc); 133 145 return rc; 134 146 } 135 147 136 rbuf_in = rc;148 rbuf_in = nrecv; 137 149 } 138 150 … … 142 154 143 155 /** Receive one line with length limit */ 144 static int recv_line( int fd)156 static int recv_line(tcp_conn_t *conn) 145 157 { 146 158 char *bp = lbuf; … … 149 161 while (bp < lbuf + BUFFER_SIZE) { 150 162 char prev = c; 151 int rc = recv_char( fd, &c);163 int rc = recv_char(conn, &c); 152 164 153 165 if (rc != EOK) … … 187 199 } 188 200 189 static int send_response( int conn_sd, const char *msg)201 static int send_response(tcp_conn_t *conn, const char *msg) 190 202 { 191 203 size_t response_size = str_size(msg); … … 194 206 fprintf(stderr, "Sending response\n"); 195 207 196 ssize_t rc = send(conn_sd, (void *) msg, response_size, 0);197 if (rc < 0) {198 fprintf(stderr, " send() failed\n");208 int rc = tcp_conn_send(conn, (void *) msg, response_size); 209 if (rc != EOK) { 210 fprintf(stderr, "tcp_conn_send() failed\n"); 199 211 return rc; 200 212 } … … 203 215 } 204 216 205 static int uri_get(const char *uri, int conn_sd)217 static int uri_get(const char *uri, tcp_conn_t *conn) 206 218 { 207 219 if (str_cmp(uri, "/") == 0) … … 215 227 int fd = open(fname, O_RDONLY); 216 228 if (fd < 0) { 217 rc = send_response(conn _sd, msg_not_found);229 rc = send_response(conn, msg_not_found); 218 230 free(fname); 219 231 return rc; … … 222 234 free(fname); 223 235 224 rc = send_response(conn _sd, msg_ok);236 rc = send_response(conn, msg_ok); 225 237 if (rc != EOK) 226 238 return rc; … … 236 248 } 237 249 238 rc = send(conn_sd, fbuf, nr, 0);239 if (rc < 0) {240 fprintf(stderr, " send() failed\n");250 rc = tcp_conn_send(conn, fbuf, nr); 251 if (rc != EOK) { 252 fprintf(stderr, "tcp_conn_send() failed\n"); 241 253 close(fd); 242 254 return rc; … … 249 261 } 250 262 251 static int req_process( int conn_sd)252 { 253 int rc = recv_line(conn _sd);263 static int req_process(tcp_conn_t *conn) 264 { 265 int rc = recv_line(conn); 254 266 if (rc != EOK) { 255 267 fprintf(stderr, "recv_line() failed\n"); … … 261 273 262 274 if (str_lcmp(lbuf, "GET ", 4) != 0) { 263 rc = send_response(conn _sd, msg_not_implemented);275 rc = send_response(conn, msg_not_implemented); 264 276 return rc; 265 277 } … … 277 289 278 290 if (!uri_is_valid(uri)) { 279 rc = send_response(conn _sd, msg_bad_request);280 return rc; 281 } 282 283 return uri_get(uri, conn _sd);291 rc = send_response(conn, msg_bad_request); 292 return rc; 293 } 294 295 return uri_get(uri, conn); 284 296 } 285 297 … … 346 358 } 347 359 360 static void websrv_new_conn(tcp_listener_t *lst, tcp_conn_t *conn) 361 { 362 int rc; 363 364 if (verbose) 365 fprintf(stderr, "New connection, waiting for request\n"); 366 367 rbuf_out = 0; 368 rbuf_in = 0; 369 370 rc = req_process(conn); 371 if (rc != EOK) { 372 fprintf(stderr, "Error processing request (%s)\n", 373 str_error(rc)); 374 return; 375 } 376 377 rc = tcp_conn_send_fin(conn); 378 if (rc != EOK) { 379 fprintf(stderr, "Error sending FIN.\n"); 380 return; 381 } 382 } 383 348 384 int main(int argc, char *argv[]) 349 385 { 386 inet_ep_t ep; 387 tcp_listener_t *lst; 388 tcp_t *tcp; 389 int rc; 390 350 391 /* Parse command line arguments */ 351 392 for (int i = 1; i < argc; i++) { 352 393 if (argv[i][0] == '-') { 353 intrc = parse_option(argc, argv, &i);394 rc = parse_option(argc, argv, &i); 354 395 if (rc != EOK) 355 396 return rc; … … 360 401 } 361 402 362 struct sockaddr_in addr; 363 364 addr.sin_family = AF_INET; 365 addr.sin_port = htons(port); 366 367 int rc = inet_pton(AF_INET, "127.0.0.1", (void *) 368 &addr.sin_addr.s_addr); 369 if (rc != EOK) { 370 fprintf(stderr, "Error parsing network address (%s)\n", 371 str_error(rc)); 403 printf("%s: HelenOS web server\n", NAME); 404 405 if (verbose) 406 fprintf(stderr, "Creating listener\n"); 407 408 inet_ep_init(&ep); 409 ep.port = port; 410 411 rc = tcp_create(&tcp); 412 if (rc != EOK) { 413 fprintf(stderr, "Error initializing TCP.\n"); 372 414 return 1; 373 415 } 374 375 printf("%s: HelenOS web server\n", NAME); 376 377 if (verbose) 378 fprintf(stderr, "Creating socket\n"); 379 380 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 381 if (listen_sd < 0) { 382 fprintf(stderr, "Error creating listening socket (%s)\n", 383 str_error(listen_sd)); 416 417 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL, 418 &lst); 419 if (rc != EOK) { 420 fprintf(stderr, "Error creating listener.\n"); 384 421 return 2; 385 }386 387 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));388 if (rc != EOK) {389 fprintf(stderr, "Error binding socket (%s)\n",390 str_error(rc));391 return 3;392 }393 394 rc = listen(listen_sd, BACKLOG_SIZE);395 if (rc != EOK) {396 fprintf(stderr, "listen() failed (%s)\n", str_error(rc));397 return 4;398 422 } 399 423 … … 402 426 403 427 task_retval(0); 404 405 while (true) { 406 struct sockaddr_in raddr; 407 socklen_t raddr_len = sizeof(raddr); 408 int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr, 409 &raddr_len); 410 411 if (conn_sd < 0) { 412 fprintf(stderr, "accept() failed (%s)\n", str_error(rc)); 413 continue; 414 } 415 416 if (verbose) { 417 fprintf(stderr, "Connection accepted (sd=%d), " 418 "waiting for request\n", conn_sd); 419 } 420 421 rbuf_out = 0; 422 rbuf_in = 0; 423 424 rc = req_process(conn_sd); 425 if (rc != EOK) 426 fprintf(stderr, "Error processing request (%s)\n", 427 str_error(rc)); 428 429 rc = closesocket(conn_sd); 430 if (rc != EOK) { 431 fprintf(stderr, "Error closing connection socket (%s)\n", 432 str_error(rc)); 433 closesocket(listen_sd); 434 return 5; 435 } 436 437 if (verbose) 438 fprintf(stderr, "Connection closed\n"); 439 } 428 async_manager(); 440 429 441 430 /* Not reached */
Note:
See TracChangeset
for help on using the changeset viewer.