-
Notifications
You must be signed in to change notification settings - Fork 0
Add listening server #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
enerdhil
wants to merge
15
commits into
master
Choose a base branch
from
add_listening_server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cfc908d
Fix(Default port)
enerdhil d04f994
Add(server)
enerdhil 3a23666
Fix(Compatibility)
enerdhil 9e72367
Update(submodule)
enerdhil bd1f9d2
Add(server): Now effectively listening and doing nothing
enerdhil ff1f711
Fix(server): Requested changes for PR
enerdhil 275edd3
Fix(arg.c): Typo
enerdhil c70b2f6
Fix(test_args.c): Format
enerdhil 9d5eabf
Fix(test/Makefile): Now deleting coverage files
enerdhil 049f89c
Update(Makefile): Now maek coverage on all sources
enerdhil be942ea
Fix(launch_server): Changed a type
enerdhil d47c747
Update(Submodule): Update lib/libmorphux
Ne02ptzero c664630
Fix(Makefile): Fix coverage instructions
Ne02ptzero 8f7f98d
Fix(Tests): Fix linker file at the end of line
Ne02ptzero 9343c61
Saving my work, did not test it
enerdhil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /*********************************** LICENSE **********************************\ | ||
| * Copyright 2017 Morphux * | ||
| * * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| * you may not use this file except in compliance with the License. * | ||
| * You may obtain a copy of the License at * | ||
| * * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| * * | ||
| * Unless required by applicable law or agreed to in writing, software * | ||
| * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| * See the License for the specific language governing permissions and * | ||
| * limitations under the License. * | ||
| \******************************************************************************/ | ||
|
|
||
| #ifndef LAUNCH_SERVER_H | ||
| # define LAUNCH_SERVER_H | ||
|
|
||
| void launch_server(void); | ||
|
|
||
| #endif |
Submodule lib
updated
19 files
| +3 −0 | .gitmodules | |
| +5 −5 | Makefile | |
| +3 −3 | docs/doxyfile | |
| +1 −0 | docs/doxygen-theme | |
| +8 −0 | inc/fail_test.h | |
| +67 −0 | inc/m_file.h | |
| +26 −2 | inc/m_print.h | |
| +24 −2 | inc/m_test.h | |
| +1 −0 | inc/morphux.h | |
| +85 −0 | src/m_file.c | |
| +53 −5 | src/m_print.c | |
| +13 −8 | src/m_test.c | |
| +39 −0 | src/test.c | |
| +3 −3 | tests/Makefile | |
| +1 −0 | tests/main.c | |
| +1 −0 | tests/test.h | |
| +114 −0 | tests/test_files.c | |
| +28 −12 | tests/test_print.c | |
| +33 −0 | tests/test_tests.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /*********************************** LICENSE **********************************\ | ||
| * Copyright 2017 Morphux * | ||
| * * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| * you may not use this file except in compliance with the License. * | ||
| * You may obtain a copy of the License at * | ||
| * * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| * * | ||
| * Unless required by applicable law or agreed to in writing, software * | ||
| * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| * See the License for the specific language governing permissions and * | ||
| * limitations under the License. * | ||
| \******************************************************************************/ | ||
|
|
||
| #include <builder.h> | ||
| #include <stdio.h> | ||
| #include <unistd.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/wait.h> | ||
| #include <sys/types.h> | ||
| #include <sys/socket.h> | ||
| #include <arpa/inet.h> | ||
| #include <netdb.h> | ||
| #include <args.h> | ||
|
|
||
| #define BACKLOG 10 /* How many pending connection we will keep in queue */ | ||
|
|
||
| // get sockaddr, IPv4 or IPv6: | ||
| static void *get_in_addr(struct sockaddr *sa) | ||
| { | ||
| if (!sa) | ||
| return NULL; | ||
| if (sa->sa_family == AF_INET) | ||
| { | ||
| return &(((struct sockaddr_in*)sa)->sin_addr); | ||
| } | ||
|
|
||
| return &(((struct sockaddr_in6*)sa)->sin6_addr); | ||
| } | ||
|
|
||
| void launch_server(void) { | ||
| int sockfd = 0, /* Read on this one */ | ||
| rem_fd = 0, /* Fd that will be attributed to the client */ | ||
| rval; /* To stock getaddrinfo() return */ | ||
| bool enable = 1; /* Int to enable the option in setsockopt */ | ||
| struct addrinfo hints, /* Flags of getaddrinfo() */ | ||
| *servinfo, /* Result of getaddrinfo() */ | ||
| *ptr; /* Variable used to loop into the linked lst*/ | ||
| char port_str[6]; /* Buffer to hold the port number */ | ||
|
|
||
| struct sockaddr_storage rem_addr; /* Hold client infos */ | ||
| socklen_t sin_size = 0; /* Size of rem_addr */ | ||
|
|
||
| char str[INET6_ADDRSTRLEN]; | ||
|
|
||
| memset(&hints, 0, sizeof(hints)); | ||
| hints.ai_family = AF_UNSPEC; /* Don't care IPv4 and IPv6 */ | ||
| hints.ai_socktype = SOCK_STREAM; /* Use TCP stream socket */ | ||
|
|
||
| /* Get port number as a string */ | ||
| snprintf(port_str, 6, "%d", flags_get_port()); | ||
|
|
||
| if ((rval = getaddrinfo(NULL, port_str, &hints, &servinfo)) != 0) | ||
| { | ||
| /* Exit if getaddrinfo fails */ | ||
| m_panic("getaddrinfo failed : %s\n", gai_strerror(rval)); | ||
| } | ||
|
|
||
| /* Now we will loop into the linked list */ | ||
| for (ptr = servinfo; ptr != NULL; ptr = ptr->ai_next) | ||
| { | ||
| /* Try to get a fd from node of linked list */ | ||
| if ((sockfd = socket(ptr->ai_family, | ||
| ptr->ai_socktype, ptr->ai_protocol)) == -1) | ||
| { | ||
| /* If it fails, display the error and try with the next struct */ | ||
| m_error("Server: socket %s" , strerror(errno)); | ||
| continue ; | ||
| } | ||
| /* If socket succeded, call setsockopt */ | ||
| if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, | ||
| sizeof(int)) == -1) | ||
| { | ||
| /* If it fails, cleanup, display the error and exit */ | ||
| freeaddrinfo(servinfo); | ||
| m_panic("Server: setsockopt %s" , strerror(errno)); | ||
| } | ||
| /* If we got here, try to bind to the socket */ | ||
| if (bind(sockfd, ptr->ai_addr, ptr->ai_addrlen) == -1) | ||
| { | ||
| /* If it fails, display the error and try with the next struct */ | ||
| close(sockfd); | ||
| m_error("Server: bind %s" , strerror(errno)); | ||
| continue ; | ||
| } | ||
| /* If all that succeded, get out of the loop */ | ||
| break ; | ||
| } | ||
| /* Don't need this struct anymore */ | ||
| freeaddrinfo(servinfo); | ||
|
|
||
| if (ptr == NULL) | ||
| { | ||
| /* Exit if we looked at all the stucts and bound on none */ | ||
| m_panic("Server: failed to bind"); | ||
| } | ||
|
|
||
| if (listen(sockfd, BACKLOG) == -1) | ||
| { | ||
| /* Exit if listen() fails */ | ||
| m_panic("Server: listen %s" , strerror(errno)); | ||
| } | ||
|
|
||
| m_info("Waiting for connections ...\n"); | ||
|
|
||
| /* This is the main accept() loop */ | ||
| while (1) | ||
| { | ||
| sin_size = sizeof(rem_addr); | ||
| /* Try to get a fd for the client */ | ||
| if ((rem_fd = accept(sockfd, (struct sockaddr *)&rem_addr, | ||
| &sin_size)) == -1) | ||
| { | ||
| /* If it fails, try again */ | ||
| m_error("Server: accept %s", strerror); | ||
| continue ; | ||
| } | ||
| inet_ntop(rem_addr.ss_family, get_in_addr((struct sockaddr *)&rem_addr), | ||
| str, sizeof(str)); | ||
| m_info("Server: Got connection from %s\n", str); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /*********************************** LICENSE **********************************\ | ||
| * Copyright 2017 Morphux * | ||
| * * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); * | ||
| * you may not use this file except in compliance with the License. * | ||
| * You may obtain a copy of the License at * | ||
| * * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 * | ||
| * * | ||
| * Unless required by applicable law or agreed to in writing, software * | ||
| * distributed under the License is distributed on an "AS IS" BASIS, * | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | ||
| * See the License for the specific language governing permissions and * | ||
| * limitations under the License. * | ||
| \******************************************************************************/ | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question:
snprintfmet un\0à la fin de la string ?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Man snprintf sous debian :
The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.Sous OSX :
The snprintf() and vsnprintf() functions will write at most size-1 of the characters printed into the output string (the size'th character then gets the terminating\0'); if the return value is greater than or equal to the size argument, the string was too short and some of the printed characters were discarded. The output is always null-terminated, unless size is 0.`